Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

CordovaPlugin.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /*
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. package org.apache.cordova;
  18. import org.apache.cordova.CordovaArgs;
  19. import org.apache.cordova.CordovaWebView;
  20. import org.apache.cordova.CordovaInterface;
  21. import org.apache.cordova.CallbackContext;
  22. import org.json.JSONArray;
  23. import org.json.JSONException;
  24. import android.content.Intent;
  25. import android.content.pm.PackageManager;
  26. import android.content.res.Configuration;
  27. import android.net.Uri;
  28. import android.os.Build;
  29. import android.os.Bundle;
  30. import java.io.FileNotFoundException;
  31. import java.io.IOException;
  32. /**
  33. * Plugins must extend this class and override one of the execute methods.
  34. */
  35. public class CordovaPlugin {
  36. public CordovaWebView webView;
  37. public CordovaInterface cordova;
  38. protected CordovaPreferences preferences;
  39. private String serviceName;
  40. /**
  41. * Call this after constructing to initialize the plugin.
  42. * Final because we want to be able to change args without breaking plugins.
  43. */
  44. public final void privateInitialize(String serviceName, CordovaInterface cordova, CordovaWebView webView, CordovaPreferences preferences) {
  45. assert this.cordova == null;
  46. this.serviceName = serviceName;
  47. this.cordova = cordova;
  48. this.webView = webView;
  49. this.preferences = preferences;
  50. initialize(cordova, webView);
  51. pluginInitialize();
  52. }
  53. /**
  54. * Called after plugin construction and fields have been initialized.
  55. * Prefer to use pluginInitialize instead since there is no value in
  56. * having parameters on the initialize() function.
  57. */
  58. public void initialize(CordovaInterface cordova, CordovaWebView webView) {
  59. }
  60. /**
  61. * Called after plugin construction and fields have been initialized.
  62. */
  63. protected void pluginInitialize() {
  64. }
  65. /**
  66. * Returns the plugin's service name (what you'd use when calling pluginManger.getPlugin())
  67. */
  68. public String getServiceName() {
  69. return serviceName;
  70. }
  71. /**
  72. * Executes the request.
  73. *
  74. * This method is called from the WebView thread. To do a non-trivial amount of work, use:
  75. * cordova.getThreadPool().execute(runnable);
  76. *
  77. * To run on the UI thread, use:
  78. * cordova.getActivity().runOnUiThread(runnable);
  79. *
  80. * @param action The action to execute.
  81. * @param rawArgs The exec() arguments in JSON form.
  82. * @param callbackContext The callback context used when calling back into JavaScript.
  83. * @return Whether the action was valid.
  84. */
  85. public boolean execute(String action, String rawArgs, CallbackContext callbackContext) throws JSONException {
  86. JSONArray args = new JSONArray(rawArgs);
  87. return execute(action, args, callbackContext);
  88. }
  89. /**
  90. * Executes the request.
  91. *
  92. * This method is called from the WebView thread. To do a non-trivial amount of work, use:
  93. * cordova.getThreadPool().execute(runnable);
  94. *
  95. * To run on the UI thread, use:
  96. * cordova.getActivity().runOnUiThread(runnable);
  97. *
  98. * @param action The action to execute.
  99. * @param args The exec() arguments.
  100. * @param callbackContext The callback context used when calling back into JavaScript.
  101. * @return Whether the action was valid.
  102. */
  103. public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
  104. CordovaArgs cordovaArgs = new CordovaArgs(args);
  105. return execute(action, cordovaArgs, callbackContext);
  106. }
  107. /**
  108. * Executes the request.
  109. *
  110. * This method is called from the WebView thread. To do a non-trivial amount of work, use:
  111. * cordova.getThreadPool().execute(runnable);
  112. *
  113. * To run on the UI thread, use:
  114. * cordova.getActivity().runOnUiThread(runnable);
  115. *
  116. * @param action The action to execute.
  117. * @param args The exec() arguments, wrapped with some Cordova helpers.
  118. * @param callbackContext The callback context used when calling back into JavaScript.
  119. * @return Whether the action was valid.
  120. */
  121. public boolean execute(String action, CordovaArgs args, CallbackContext callbackContext) throws JSONException {
  122. return false;
  123. }
  124. /**
  125. * Called when the system is about to start resuming a previous activity.
  126. *
  127. * @param multitasking Flag indicating if multitasking is turned on for app
  128. */
  129. public void onPause(boolean multitasking) {
  130. }
  131. /**
  132. * Called when the activity will start interacting with the user.
  133. *
  134. * @param multitasking Flag indicating if multitasking is turned on for app
  135. */
  136. public void onResume(boolean multitasking) {
  137. }
  138. /**
  139. * Called when the activity is becoming visible to the user.
  140. */
  141. public void onStart() {
  142. }
  143. /**
  144. * Called when the activity is no longer visible to the user.
  145. */
  146. public void onStop() {
  147. }
  148. /**
  149. * Called when the activity receives a new intent.
  150. */
  151. public void onNewIntent(Intent intent) {
  152. }
  153. /**
  154. * The final call you receive before your activity is destroyed.
  155. */
  156. public void onDestroy() {
  157. }
  158. /**
  159. * Called when the Activity is being destroyed (e.g. if a plugin calls out to an external
  160. * Activity and the OS kills the CordovaActivity in the background). The plugin should save its
  161. * state in this method only if it is awaiting the result of an external Activity and needs
  162. * to preserve some information so as to handle that result; onRestoreStateForActivityResult()
  163. * will only be called if the plugin is the recipient of an Activity result
  164. *
  165. * @return Bundle containing the state of the plugin or null if state does not need to be saved
  166. */
  167. public Bundle onSaveInstanceState() {
  168. return null;
  169. }
  170. /**
  171. * Called when a plugin is the recipient of an Activity result after the CordovaActivity has
  172. * been destroyed. The Bundle will be the same as the one the plugin returned in
  173. * onSaveInstanceState()
  174. *
  175. * @param state Bundle containing the state of the plugin
  176. * @param callbackContext Replacement Context to return the plugin result to
  177. */
  178. public void onRestoreStateForActivityResult(Bundle state, CallbackContext callbackContext) {}
  179. /**
  180. * Called when a message is sent to plugin.
  181. *
  182. * @param id The message id
  183. * @param data The message data
  184. * @return Object to stop propagation or null
  185. */
  186. public Object onMessage(String id, Object data) {
  187. return null;
  188. }
  189. /**
  190. * Called when an activity you launched exits, giving you the requestCode you started it with,
  191. * the resultCode it returned, and any additional data from it.
  192. *
  193. * @param requestCode The request code originally supplied to startActivityForResult(),
  194. * allowing you to identify who this result came from.
  195. * @param resultCode The integer result code returned by the child activity through its setResult().
  196. * @param intent An Intent, which can return result data to the caller (various data can be
  197. * attached to Intent "extras").
  198. */
  199. public void onActivityResult(int requestCode, int resultCode, Intent intent) {
  200. }
  201. /**
  202. * Hook for blocking the loading of external resources.
  203. *
  204. * This will be called when the WebView's shouldInterceptRequest wants to
  205. * know whether to open a connection to an external resource. Return false
  206. * to block the request: if any plugin returns false, Cordova will block
  207. * the request. If all plugins return null, the default policy will be
  208. * enforced. If at least one plugin returns true, and no plugins return
  209. * false, then the request will proceed.
  210. *
  211. * Note that this only affects resource requests which are routed through
  212. * WebViewClient.shouldInterceptRequest, such as XMLHttpRequest requests and
  213. * img tag loads. WebSockets and media requests (such as <video> and <audio>
  214. * tags) are not affected by this method. Use CSP headers to control access
  215. * to such resources.
  216. */
  217. public Boolean shouldAllowRequest(String url) {
  218. return null;
  219. }
  220. /**
  221. * Hook for blocking navigation by the Cordova WebView. This applies both to top-level and
  222. * iframe navigations.
  223. *
  224. * This will be called when the WebView's needs to know whether to navigate
  225. * to a new page. Return false to block the navigation: if any plugin
  226. * returns false, Cordova will block the navigation. If all plugins return
  227. * null, the default policy will be enforced. It at least one plugin returns
  228. * true, and no plugins return false, then the navigation will proceed.
  229. */
  230. public Boolean shouldAllowNavigation(String url) {
  231. return null;
  232. }
  233. /**
  234. * Hook for allowing page to call exec(). By default, this returns the result of
  235. * shouldAllowNavigation(). It's generally unsafe to allow untrusted content to be loaded
  236. * into a CordovaWebView, even within an iframe, so it's best not to touch this.
  237. */
  238. public Boolean shouldAllowBridgeAccess(String url) {
  239. return shouldAllowNavigation(url);
  240. }
  241. /**
  242. * Hook for blocking the launching of Intents by the Cordova application.
  243. *
  244. * This will be called when the WebView will not navigate to a page, but
  245. * could launch an intent to handle the URL. Return false to block this: if
  246. * any plugin returns false, Cordova will block the navigation. If all
  247. * plugins return null, the default policy will be enforced. If at least one
  248. * plugin returns true, and no plugins return false, then the URL will be
  249. * opened.
  250. */
  251. public Boolean shouldOpenExternalUrl(String url) {
  252. return null;
  253. }
  254. /**
  255. * Allows plugins to handle a link being clicked. Return true here to cancel the navigation.
  256. *
  257. * @param url The URL that is trying to be loaded in the Cordova webview.
  258. * @return Return true to prevent the URL from loading. Default is false.
  259. */
  260. public boolean onOverrideUrlLoading(String url) {
  261. return false;
  262. }
  263. /**
  264. * Hook for redirecting requests. Applies to WebView requests as well as requests made by plugins.
  265. * To handle the request directly, return a URI in the form:
  266. *
  267. * cdvplugin://pluginId/...
  268. *
  269. * And implement handleOpenForRead().
  270. * To make this easier, use the toPluginUri() and fromPluginUri() helpers:
  271. *
  272. * public Uri remapUri(Uri uri) { return toPluginUri(uri); }
  273. *
  274. * public CordovaResourceApi.OpenForReadResult handleOpenForRead(Uri uri) throws IOException {
  275. * Uri origUri = fromPluginUri(uri);
  276. * ...
  277. * }
  278. */
  279. public Uri remapUri(Uri uri) {
  280. return null;
  281. }
  282. /**
  283. * Called to handle CordovaResourceApi.openForRead() calls for a cdvplugin://pluginId/ URL.
  284. * Should never return null.
  285. * Added in cordova-android@4.0.0
  286. */
  287. public CordovaResourceApi.OpenForReadResult handleOpenForRead(Uri uri) throws IOException {
  288. throw new FileNotFoundException("Plugin can't handle uri: " + uri);
  289. }
  290. /**
  291. * Refer to remapUri()
  292. * Added in cordova-android@4.0.0
  293. */
  294. protected Uri toPluginUri(Uri origUri) {
  295. return new Uri.Builder()
  296. .scheme(CordovaResourceApi.PLUGIN_URI_SCHEME)
  297. .authority(serviceName)
  298. .appendQueryParameter("origUri", origUri.toString())
  299. .build();
  300. }
  301. /**
  302. * Refer to remapUri()
  303. * Added in cordova-android@4.0.0
  304. */
  305. protected Uri fromPluginUri(Uri pluginUri) {
  306. return Uri.parse(pluginUri.getQueryParameter("origUri"));
  307. }
  308. /**
  309. * Called when the WebView does a top-level navigation or refreshes.
  310. *
  311. * Plugins should stop any long-running processes and clean up internal state.
  312. *
  313. * Does nothing by default.
  314. */
  315. public void onReset() {
  316. }
  317. /**
  318. * Called when the system received an HTTP authentication request. Plugin can use
  319. * the supplied HttpAuthHandler to process this auth challenge.
  320. *
  321. * @param view The WebView that is initiating the callback
  322. * @param handler The HttpAuthHandler used to set the WebView's response
  323. * @param host The host requiring authentication
  324. * @param realm The realm for which authentication is required
  325. *
  326. * @return Returns True if plugin will resolve this auth challenge, otherwise False
  327. *
  328. */
  329. public boolean onReceivedHttpAuthRequest(CordovaWebView view, ICordovaHttpAuthHandler handler, String host, String realm) {
  330. return false;
  331. }
  332. /**
  333. * Called when he system received an SSL client certificate request. Plugin can use
  334. * the supplied ClientCertRequest to process this certificate challenge.
  335. *
  336. * @param view The WebView that is initiating the callback
  337. * @param request The client certificate request
  338. *
  339. * @return Returns True if plugin will resolve this auth challenge, otherwise False
  340. *
  341. */
  342. public boolean onReceivedClientCertRequest(CordovaWebView view, ICordovaClientCertRequest request) {
  343. return false;
  344. }
  345. /**
  346. * Called by the system when the device configuration changes while your activity is running.
  347. *
  348. * @param newConfig The new device configuration
  349. */
  350. public void onConfigurationChanged(Configuration newConfig) {
  351. }
  352. /**
  353. * Called by the Plugin Manager when we need to actually request permissions
  354. *
  355. * @param requestCode Passed to the activity to track the request
  356. *
  357. * @return Returns the permission that was stored in the plugin
  358. */
  359. public void requestPermissions(int requestCode) {
  360. }
  361. /*
  362. * Called by the WebView implementation to check for geolocation permissions, can be used
  363. * by other Java methods in the event that a plugin is using this as a dependency.
  364. *
  365. * @return Returns true if the plugin has all the permissions it needs to operate.
  366. */
  367. public boolean hasPermisssion() {
  368. return true;
  369. }
  370. /**
  371. * Called by the system when the user grants permissions
  372. *
  373. * @param requestCode
  374. * @param permissions
  375. * @param grantResults
  376. */
  377. public void onRequestPermissionResult(int requestCode, String[] permissions,
  378. int[] grantResults) throws JSONException {
  379. }
  380. }