You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

568 lines
19 KiB

  1. #if UNITY_ANDROID && !UNITY_EDITOR
  2. using UnityEngine;
  3. class UniWebViewMethodChannel: AndroidJavaProxy
  4. {
  5. public UniWebViewMethodChannel() : base("com.onevcat.uniwebview.UniWebViewNativeChannel") { }
  6. string invokeChannelMethod(string name, string method, string parameters) {
  7. UniWebViewLogger.Instance.Verbose("invokeChannelMethod invoked by native side. Name: " + name + " Method: "
  8. + method + " Params: " + parameters);
  9. return UniWebViewChannelMethodManager.Instance.InvokeMethod(name, method, parameters);
  10. }
  11. }
  12. public class UniWebViewInterface {
  13. private static readonly AndroidJavaClass plugin;
  14. private static bool correctPlatform = Application.platform == RuntimePlatform.Android;
  15. static UniWebViewInterface() {
  16. var go = new GameObject("UniWebViewAndroidStaticListener");
  17. go.AddComponent<UniWebViewAndroidStaticListener>();
  18. plugin = new AndroidJavaClass("com.onevcat.uniwebview.UniWebViewInterface");
  19. CheckPlatform();
  20. plugin.CallStatic("prepare");
  21. UniWebViewLogger.Instance.Info("Connecting to native side method channel.");
  22. plugin.CallStatic("registerChannel", new UniWebViewMethodChannel());
  23. }
  24. public static void SetLogLevel(int level) {
  25. CheckPlatform();
  26. plugin.CallStatic("setLogLevel", level);
  27. }
  28. public static bool IsWebViewSupported() {
  29. CheckPlatform();
  30. return plugin.CallStatic<bool>("isWebViewSupported");
  31. }
  32. public static void Init(string name, int x, int y, int width, int height) {
  33. CheckPlatform();
  34. plugin.CallStatic("init", name, x, y, width, height);
  35. }
  36. public static void Destroy(string name) {
  37. CheckPlatform();
  38. plugin.CallStatic("destroy", name);
  39. }
  40. public static void Load(string name, string url, bool skipEncoding, string readAccessURL) {
  41. CheckPlatform();
  42. plugin.CallStatic("load", name, url);
  43. }
  44. public static void LoadHTMLString(string name, string html, string baseUrl, bool skipEncoding) {
  45. CheckPlatform();
  46. plugin.CallStatic("loadHTMLString", name, html, baseUrl);
  47. }
  48. public static void Reload(string name) {
  49. CheckPlatform();
  50. plugin.CallStatic("reload", name);
  51. }
  52. public static void Stop(string name) {
  53. CheckPlatform();
  54. plugin.CallStatic("stop", name);
  55. }
  56. public static string GetUrl(string name) {
  57. CheckPlatform();
  58. return plugin.CallStatic<string>("getUrl", name);
  59. }
  60. public static void SetFrame(string name, int x, int y, int width, int height) {
  61. CheckPlatform();
  62. plugin.CallStatic("setFrame", name, x, y, width, height);
  63. }
  64. public static void SetPosition(string name, int x, int y) {
  65. CheckPlatform();
  66. plugin.CallStatic("setPosition", name, x, y);
  67. }
  68. public static void SetSize(string name, int width, int height) {
  69. CheckPlatform();
  70. plugin.CallStatic("setSize", name, width, height);
  71. }
  72. public static bool Show(string name, bool fade, int edge, float duration, bool useAsync, string identifier) {
  73. CheckPlatform();
  74. if (useAsync) {
  75. plugin.CallStatic("showAsync", name, fade, edge, duration, identifier);
  76. return true;
  77. } else {
  78. return plugin.CallStatic<bool>("show", name, fade, edge, duration, identifier);
  79. }
  80. }
  81. public static bool Hide(string name, bool fade, int edge, float duration, bool useAsync, string identifier) {
  82. CheckPlatform();
  83. if (useAsync) {
  84. plugin.CallStatic("hideAsync", name, fade, edge, duration, identifier);
  85. return true;
  86. } else {
  87. return plugin.CallStatic<bool>("hide", name, fade, edge, duration, identifier);
  88. }
  89. }
  90. public static bool AnimateTo(string name, int x, int y, int width, int height, float duration, float delay, string identifier) {
  91. CheckPlatform();
  92. return plugin.CallStatic<bool>("animateTo", name, x, y, width, height, duration, delay, identifier);
  93. }
  94. public static void AddJavaScript(string name, string jsString, string identifier) {
  95. CheckPlatform();
  96. plugin.CallStatic("addJavaScript", name, jsString, identifier);
  97. }
  98. public static void EvaluateJavaScript(string name, string jsString, string identifier) {
  99. CheckPlatform();
  100. plugin.CallStatic("evaluateJavaScript", name, jsString, identifier);
  101. }
  102. public static void AddUrlScheme(string name, string scheme) {
  103. CheckPlatform();
  104. plugin.CallStatic("addUrlScheme", name, scheme);
  105. }
  106. public static void RemoveUrlScheme(string name, string scheme) {
  107. CheckPlatform();
  108. plugin.CallStatic("removeUrlScheme", name, scheme);
  109. }
  110. public static void AddSslExceptionDomain(string name, string domain) {
  111. CheckPlatform();
  112. plugin.CallStatic("addSslExceptionDomain", name, domain);
  113. }
  114. public static void RemoveSslExceptionDomain(string name, string domain) {
  115. CheckPlatform();
  116. plugin.CallStatic("removeSslExceptionDomain", name, domain);
  117. }
  118. public static void AddPermissionTrustDomain(string name, string domain) {
  119. CheckPlatform();
  120. plugin.CallStatic("addPermissionTrustDomain", name, domain);
  121. }
  122. public static void RemovePermissionTrustDomain(string name, string domain) {
  123. CheckPlatform();
  124. plugin.CallStatic("removePermissionTrustDomain", name, domain);
  125. }
  126. public static void SetHeaderField(string name, string key, string value) {
  127. CheckPlatform();
  128. plugin.CallStatic("setHeaderField", name, key, value);
  129. }
  130. public static void SetUserAgent(string name, string userAgent) {
  131. CheckPlatform();
  132. plugin.CallStatic("setUserAgent", name, userAgent);
  133. }
  134. public static string GetUserAgent(string name) {
  135. CheckPlatform();
  136. return plugin.CallStatic<string>("getUserAgent", name);
  137. }
  138. public static void SetAllowAutoPlay(bool flag) {
  139. CheckPlatform();
  140. plugin.CallStatic("setAllowAutoPlay", flag);
  141. }
  142. public static void SetAllowJavaScriptOpenWindow(bool flag) {
  143. CheckPlatform();
  144. plugin.CallStatic("setAllowJavaScriptOpenWindow", flag);
  145. }
  146. public static void SetAllowFileAccess(string name, bool flag) {
  147. CheckPlatform();
  148. plugin.CallStatic("setAllowFileAccess", name, flag);
  149. }
  150. public static void SetAcceptThirdPartyCookies(string name, bool flag) {
  151. CheckPlatform();
  152. plugin.CallStatic("setAcceptThirdPartyCookies", name, flag);
  153. }
  154. public static void SetAllowFileAccessFromFileURLs(string name, bool flag) {
  155. CheckPlatform();
  156. plugin.CallStatic("setAllowFileAccessFromFileURLs", name, flag);
  157. }
  158. public static void SetAllowUniversalAccessFromFileURLs(bool flag) {
  159. CheckPlatform();
  160. plugin.CallStatic("setAllowUniversalAccessFromFileURLs", flag);
  161. }
  162. public static void BringContentToFront(string name) {
  163. CheckPlatform();
  164. plugin.CallStatic("bringContentToFront", name);
  165. }
  166. public static void SetForwardWebConsoleToNativeOutput(bool flag) {
  167. CheckPlatform();
  168. plugin.CallStatic("setForwardWebConsoleToNativeOutput", flag);
  169. }
  170. public static void SetEnableKeyboardAvoidance(bool flag) {
  171. CheckPlatform();
  172. plugin.CallStatic("setEnableKeyboardAvoidance", flag);
  173. }
  174. public static void SetJavaScriptEnabled(bool enabled) {
  175. CheckPlatform();
  176. plugin.CallStatic("setJavaScriptEnabled", enabled);
  177. }
  178. public static void CleanCache(string name) {
  179. CheckPlatform();
  180. plugin.CallStatic("cleanCache", name);
  181. }
  182. public static void SetCacheMode(string name, int mode) {
  183. CheckPlatform();
  184. plugin.CallStatic("setCacheMode", name, mode);
  185. }
  186. public static void ClearCookies() {
  187. CheckPlatform();
  188. plugin.CallStatic("clearCookies");
  189. }
  190. public static void SetCookie(string url, string cookie, bool skipEncoding) {
  191. CheckPlatform();
  192. plugin.CallStatic("setCookie", url, cookie);
  193. }
  194. public static string GetCookie(string url, string key, bool skipEncoding) {
  195. CheckPlatform();
  196. return plugin.CallStatic<string>("getCookie", url, key);
  197. }
  198. public static void RemoveCookies(string url, bool skipEncoding) {
  199. CheckPlatform();
  200. plugin.CallStatic("removeCookies", url);
  201. }
  202. public static void RemoveCookie(string url, string key, bool skipEncoding) {
  203. CheckPlatform();
  204. plugin.CallStatic("removeCookie", url, key);
  205. }
  206. public static void ClearHttpAuthUsernamePassword(string host, string realm) {
  207. CheckPlatform();
  208. plugin.CallStatic("clearHttpAuthUsernamePassword", host, realm);
  209. }
  210. public static void SetBackgroundColor(string name, float r, float g, float b, float a) {
  211. CheckPlatform();
  212. plugin.CallStatic("setBackgroundColor", name, r, g, b, a);
  213. }
  214. public static void SetWebViewAlpha(string name, float alpha) {
  215. CheckPlatform();
  216. plugin.CallStatic("setWebViewAlpha", name, alpha);
  217. }
  218. public static float GetWebViewAlpha(string name) {
  219. CheckPlatform();
  220. return plugin.CallStatic<float>("getWebViewAlpha", name);
  221. }
  222. public static void SetShowSpinnerWhileLoading(string name, bool show) {
  223. CheckPlatform();
  224. plugin.CallStatic("setShowSpinnerWhileLoading", name, show);
  225. }
  226. public static void SetSpinnerText(string name, string text) {
  227. CheckPlatform();
  228. plugin.CallStatic("setSpinnerText", name, text);
  229. }
  230. public static void SetAllowUserDismissSpinnerByGesture(string name, bool flag) {
  231. CheckPlatform();
  232. plugin.CallStatic("setAllowUserDismissSpinnerByGesture", name, flag);
  233. }
  234. public static void ShowSpinner(string name) {
  235. CheckPlatform();
  236. plugin.CallStatic("showSpinner", name);
  237. }
  238. public static void HideSpinner(string name) {
  239. CheckPlatform();
  240. plugin.CallStatic("hideSpinner", name);
  241. }
  242. public static bool CanGoBack(string name) {
  243. CheckPlatform();
  244. return plugin.CallStatic<bool>("canGoBack", name);
  245. }
  246. public static bool CanGoForward(string name) {
  247. CheckPlatform();
  248. return plugin.CallStatic<bool>("canGoForward", name);
  249. }
  250. public static void GoBack(string name) {
  251. CheckPlatform();
  252. plugin.CallStatic("goBack", name);
  253. }
  254. public static void GoForward(string name) {
  255. CheckPlatform();
  256. plugin.CallStatic("goForward", name);
  257. }
  258. public static void SetOpenLinksInExternalBrowser(string name, bool flag) {
  259. CheckPlatform();
  260. plugin.CallStatic("setOpenLinksInExternalBrowser", name, flag);
  261. }
  262. public static void SetHorizontalScrollBarEnabled(string name, bool enabled) {
  263. CheckPlatform();
  264. plugin.CallStatic("setHorizontalScrollBarEnabled", name, enabled);
  265. }
  266. public static void SetVerticalScrollBarEnabled(string name, bool enabled) {
  267. CheckPlatform();
  268. plugin.CallStatic("setVerticalScrollBarEnabled", name, enabled);
  269. }
  270. public static void SetBouncesEnabled(string name, bool enabled) {
  271. CheckPlatform();
  272. plugin.CallStatic("setBouncesEnabled", name, enabled);
  273. }
  274. public static void SetZoomEnabled(string name, bool enabled) {
  275. CheckPlatform();
  276. plugin.CallStatic("setZoomEnabled", name, enabled);
  277. }
  278. public static void SetUseWideViewPort(string name, bool use) {
  279. CheckPlatform();
  280. plugin.CallStatic("setUseWideViewPort", name, use);
  281. }
  282. public static void SetLoadWithOverviewMode(string name, bool overview) {
  283. CheckPlatform();
  284. plugin.CallStatic("setLoadWithOverviewMode", name, overview);
  285. }
  286. public static void SetImmersiveModeEnabled(string name, bool enabled) {
  287. CheckPlatform();
  288. plugin.CallStatic("setImmersiveModeEnabled", name, enabled);
  289. }
  290. public static void SetUserInteractionEnabled(string name, bool enabled) {
  291. CheckPlatform();
  292. plugin.CallStatic("setUserInteractionEnabled", name, enabled);
  293. }
  294. public static void SetTransparencyClickingThroughEnabled(string name, bool enabled) {
  295. CheckPlatform();
  296. plugin.CallStatic("setTransparencyClickingThroughEnabled", name, enabled);
  297. }
  298. public static void SetWebContentsDebuggingEnabled(bool enabled) {
  299. CheckPlatform();
  300. plugin.CallStatic("setWebContentsDebuggingEnabled", enabled);
  301. }
  302. public static void SetAllowHTTPAuthPopUpWindow(string name, bool flag) {
  303. CheckPlatform();
  304. plugin.CallStatic("setAllowHTTPAuthPopUpWindow", name, flag);
  305. }
  306. public static void Print(string name) {
  307. CheckPlatform();
  308. plugin.CallStatic("print", name);
  309. }
  310. public static void CaptureSnapshot(string name, string filename) {
  311. CheckPlatform();
  312. plugin.CallStatic("captureSnapshot", name, filename);
  313. }
  314. public static void ScrollTo(string name, int x, int y, bool animated) {
  315. CheckPlatform();
  316. plugin.CallStatic("scrollTo", name, x, y, animated);
  317. }
  318. public static void SetCalloutEnabled(string name, bool flag) {
  319. CheckPlatform();
  320. plugin.CallStatic("setCalloutEnabled", name, flag);
  321. }
  322. public static void SetSupportMultipleWindows(string name, bool enabled, bool allowJavaScriptOpening) {
  323. CheckPlatform();
  324. plugin.CallStatic("setSupportMultipleWindows", name, enabled, allowJavaScriptOpening);
  325. }
  326. public static void SetDragInteractionEnabled(string name, bool flag) {
  327. CheckPlatform();
  328. plugin.CallStatic("setDragInteractionEnabled", name, flag);
  329. }
  330. public static void SetDefaultFontSize(string name, int size) {
  331. CheckPlatform();
  332. plugin.CallStatic("setDefaultFontSize", name, size);
  333. }
  334. public static void SetTextZoom(string name, int textZoom) {
  335. CheckPlatform();
  336. plugin.CallStatic("setTextZoom", name, textZoom);
  337. }
  338. public static float NativeScreenWidth() {
  339. CheckPlatform();
  340. return plugin.CallStatic<float>("screenWidth");
  341. }
  342. public static float NativeScreenHeight() {
  343. CheckPlatform();
  344. return plugin.CallStatic<float>("screenHeight");
  345. }
  346. public static void SetDownloadEventForContextMenuEnabled(string name, bool enabled) {
  347. CheckPlatform();
  348. plugin.CallStatic("setDownloadEventForContextMenuEnabled", name, enabled);
  349. }
  350. public static void SetAllowUserEditFileNameBeforeDownloading(string name, bool allowed) {
  351. CheckPlatform();
  352. plugin.CallStatic("setAllowUserEditFileNameBeforeDownloading", name, allowed);
  353. }
  354. // Safe Browsing
  355. public static bool IsSafeBrowsingSupported() {
  356. CheckPlatform();
  357. return plugin.CallStatic<bool>("isSafeBrowsingSupported");
  358. }
  359. public static void SafeBrowsingInit(string name, string url) {
  360. CheckPlatform();
  361. plugin.CallStatic("safeBrowsingInit", name, url);
  362. }
  363. public static void SafeBrowsingSetToolbarColor(string name, float r, float g, float b) {
  364. CheckPlatform();
  365. plugin.CallStatic("safeBrowsingSetToolbarColor", name, r, g, b);
  366. }
  367. public static void SafeBrowsingShow(string name) {
  368. CheckPlatform();
  369. plugin.CallStatic("safeBrowsingShow", name);
  370. }
  371. // Authentication
  372. public static bool IsAuthenticationIsSupported() {
  373. CheckPlatform();
  374. return plugin.CallStatic<bool>("isAuthenticationIsSupported");
  375. }
  376. public static void AuthenticationInit(string name, string url, string scheme) {
  377. CheckPlatform();
  378. plugin.CallStatic("authenticationInit", name, url, scheme);
  379. }
  380. public static void AuthenticationStart(string name) {
  381. CheckPlatform();
  382. plugin.CallStatic("authenticationStart", name);
  383. }
  384. public static void AuthenticationSetPrivateMode(string name, bool enabled) {
  385. CheckPlatform();
  386. plugin.CallStatic("authenticationSetPrivateMode", name, enabled);
  387. }
  388. public static void SetShowEmbeddedToolbar(string name, bool show) {
  389. CheckPlatform();
  390. plugin.CallStatic("setShowEmbeddedToolbar", name, show);
  391. }
  392. public static void SetEmbeddedToolbarOnTop(string name, bool top) {
  393. CheckPlatform();
  394. plugin.CallStatic("setEmbeddedToolbarOnTop", name, top);
  395. }
  396. public static void SetEmbeddedToolbarDoneButtonText(string name, string text) {
  397. CheckPlatform();
  398. plugin.CallStatic("setEmbeddedToolbarDoneButtonText", name, text);
  399. }
  400. public static void SetEmbeddedToolbarGoBackButtonText(string name, string text) {
  401. CheckPlatform();
  402. plugin.CallStatic("setEmbeddedToolbarGoBackButtonText", name, text);
  403. }
  404. public static void SetEmbeddedToolbarGoForwardButtonText(string name, string text) {
  405. CheckPlatform();
  406. plugin.CallStatic("setEmbeddedToolbarGoForwardButtonText", name, text);
  407. }
  408. public static void SetEmbeddedToolbarTitleText(string name, string text) {
  409. CheckPlatform();
  410. plugin.CallStatic("setEmbeddedToolbarTitleText", name, text);
  411. }
  412. public static void SetEmbeddedToolbarBackgroundColor(string name, Color color) {
  413. CheckPlatform();
  414. plugin.CallStatic("setEmbeddedToolbarBackgroundColor", name, color.r, color.g, color.b, color.a);
  415. }
  416. public static void SetEmbeddedToolbarButtonTextColor(string name, Color color) {
  417. CheckPlatform();
  418. plugin.CallStatic("setEmbeddedToolbarButtonTextColor", name, color.r, color.g, color.b, color.a);
  419. }
  420. public static void SetEmbeddedToolbarTitleTextColor(string name, Color color) {
  421. CheckPlatform();
  422. plugin.CallStatic("setEmbeddedToolbarTitleTextColor", name, color.r, color.g, color.b, color.a);
  423. }
  424. public static void SetEmeddedToolbarNavigationButtonsShow(string name, bool show) {
  425. CheckPlatform();
  426. plugin.CallStatic("setEmbeddedToolbarNavigationButtonsShow", name, show);
  427. }
  428. public static void StartSnapshotForRendering(string name, string identifier) {
  429. CheckPlatform();
  430. plugin.CallStatic("startSnapshotForRendering", name, identifier);
  431. }
  432. public static void StopSnapshotForRendering(string name) {
  433. CheckPlatform();
  434. plugin.CallStatic("stopSnapshotForRendering", name);
  435. }
  436. public static byte[] GetRenderedData(string name, int x, int y, int width, int height) {
  437. CheckPlatform();
  438. var sbyteArray = plugin.CallStatic<sbyte[]>("getRenderedData", name, x, y, width, height);
  439. if (sbyteArray == null) {
  440. return null;
  441. }
  442. int length = sbyteArray.Length;
  443. byte[] byteArray = new byte[length];
  444. for (int i = 0; i < length; i++) {
  445. byteArray[i] = (byte)sbyteArray[i];
  446. }
  447. return byteArray;
  448. }
  449. // Platform
  450. public static void CheckPlatform() {
  451. if (!correctPlatform) {
  452. throw new System.InvalidOperationException("Method can only be performed on Android.");
  453. }
  454. }
  455. }
  456. #endif