CordovaInterfaceImpl.java 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 android.annotation.SuppressLint;
  19. import android.app.Activity;
  20. import android.content.Context;
  21. import android.content.Intent;
  22. import android.content.pm.PackageManager;
  23. import android.os.Build;
  24. import android.os.Bundle;
  25. import android.util.Pair;
  26. import org.json.JSONException;
  27. import org.json.JSONObject;
  28. import java.util.concurrent.ExecutorService;
  29. import java.util.concurrent.Executors;
  30. /**
  31. * Default implementation of CordovaInterface.
  32. */
  33. public class CordovaInterfaceImpl implements CordovaInterface {
  34. private static final String TAG = "CordovaInterfaceImpl";
  35. protected Activity activity;
  36. protected ExecutorService threadPool;
  37. protected PluginManager pluginManager;
  38. protected ActivityResultHolder savedResult;
  39. protected CallbackMap permissionResultCallbacks;
  40. protected CordovaPlugin activityResultCallback;
  41. protected String initCallbackService;
  42. protected int activityResultRequestCode;
  43. protected boolean activityWasDestroyed = false;
  44. protected Bundle savedPluginState;
  45. public CordovaInterfaceImpl(Activity activity) {
  46. this(activity, Executors.newCachedThreadPool());
  47. }
  48. public CordovaInterfaceImpl(Activity activity, ExecutorService threadPool) {
  49. this.activity = activity;
  50. this.threadPool = threadPool;
  51. this.permissionResultCallbacks = new CallbackMap();
  52. }
  53. @Override
  54. public void startActivityForResult(CordovaPlugin command, Intent intent, int requestCode) {
  55. setActivityResultCallback(command);
  56. try {
  57. activity.startActivityForResult(intent, requestCode);
  58. } catch (RuntimeException e) { // E.g.: ActivityNotFoundException
  59. activityResultCallback = null;
  60. throw e;
  61. }
  62. }
  63. @Override
  64. public void setActivityResultCallback(CordovaPlugin plugin) {
  65. // Cancel any previously pending activity.
  66. if (activityResultCallback != null) {
  67. activityResultCallback.onActivityResult(activityResultRequestCode, Activity.RESULT_CANCELED, null);
  68. }
  69. activityResultCallback = plugin;
  70. }
  71. @Override
  72. public Activity getActivity() {
  73. return activity;
  74. }
  75. @Override
  76. public Context getContext() {
  77. return activity;
  78. }
  79. @Override
  80. public Object onMessage(String id, Object data) {
  81. if ("exit".equals(id)) {
  82. activity.finish();
  83. }
  84. return null;
  85. }
  86. @Override
  87. public ExecutorService getThreadPool() {
  88. return threadPool;
  89. }
  90. /**
  91. * Dispatches any pending onActivityResult callbacks and sends the resume event if the
  92. * Activity was destroyed by the OS.
  93. */
  94. public void onCordovaInit(PluginManager pluginManager) {
  95. this.pluginManager = pluginManager;
  96. if (savedResult != null) {
  97. onActivityResult(savedResult.requestCode, savedResult.resultCode, savedResult.intent);
  98. } else if(activityWasDestroyed) {
  99. // If there was no Activity result, we still need to send out the resume event if the
  100. // Activity was destroyed by the OS
  101. activityWasDestroyed = false;
  102. if(pluginManager != null)
  103. {
  104. CoreAndroid appPlugin = (CoreAndroid) pluginManager.getPlugin(CoreAndroid.PLUGIN_NAME);
  105. if(appPlugin != null) {
  106. JSONObject obj = new JSONObject();
  107. try {
  108. obj.put("action", "resume");
  109. } catch (JSONException e) {
  110. LOG.e(TAG, "Failed to create event message", e);
  111. }
  112. appPlugin.sendResumeEvent(new PluginResult(PluginResult.Status.OK, obj));
  113. }
  114. }
  115. }
  116. }
  117. /**
  118. * Routes the result to the awaiting plugin. Returns false if no plugin was waiting.
  119. */
  120. public boolean onActivityResult(int requestCode, int resultCode, Intent intent) {
  121. CordovaPlugin callback = activityResultCallback;
  122. if(callback == null && initCallbackService != null) {
  123. // The application was restarted, but had defined an initial callback
  124. // before being shut down.
  125. savedResult = new ActivityResultHolder(requestCode, resultCode, intent);
  126. if (pluginManager != null) {
  127. callback = pluginManager.getPlugin(initCallbackService);
  128. if(callback != null) {
  129. callback.onRestoreStateForActivityResult(savedPluginState.getBundle(callback.getServiceName()),
  130. new ResumeCallback(callback.getServiceName(), pluginManager));
  131. }
  132. }
  133. }
  134. activityResultCallback = null;
  135. if (callback != null) {
  136. LOG.d(TAG, "Sending activity result to plugin");
  137. initCallbackService = null;
  138. savedResult = null;
  139. callback.onActivityResult(requestCode, resultCode, intent);
  140. return true;
  141. }
  142. LOG.w(TAG, "Got an activity result, but no plugin was registered to receive it" + (savedResult != null ? " yet!" : "."));
  143. return false;
  144. }
  145. /**
  146. * Call this from your startActivityForResult() overload. This is required to catch the case
  147. * where plugins use Activity.startActivityForResult() + CordovaInterface.setActivityResultCallback()
  148. * rather than CordovaInterface.startActivityForResult().
  149. */
  150. public void setActivityResultRequestCode(int requestCode) {
  151. activityResultRequestCode = requestCode;
  152. }
  153. /**
  154. * Saves parameters for startActivityForResult().
  155. */
  156. public void onSaveInstanceState(Bundle outState) {
  157. if (activityResultCallback != null) {
  158. String serviceName = activityResultCallback.getServiceName();
  159. outState.putString("callbackService", serviceName);
  160. }
  161. if(pluginManager != null){
  162. outState.putBundle("plugin", pluginManager.onSaveInstanceState());
  163. }
  164. }
  165. /**
  166. * Call this from onCreate() so that any saved startActivityForResult parameters will be restored.
  167. */
  168. public void restoreInstanceState(Bundle savedInstanceState) {
  169. initCallbackService = savedInstanceState.getString("callbackService");
  170. savedPluginState = savedInstanceState.getBundle("plugin");
  171. activityWasDestroyed = true;
  172. }
  173. private static class ActivityResultHolder {
  174. private int requestCode;
  175. private int resultCode;
  176. private Intent intent;
  177. public ActivityResultHolder(int requestCode, int resultCode, Intent intent) {
  178. this.requestCode = requestCode;
  179. this.resultCode = resultCode;
  180. this.intent = intent;
  181. }
  182. }
  183. /**
  184. * Called by the system when the user grants permissions
  185. *
  186. * @param requestCode
  187. * @param permissions
  188. * @param grantResults
  189. */
  190. public void onRequestPermissionResult(int requestCode, String[] permissions,
  191. int[] grantResults) throws JSONException {
  192. Pair<CordovaPlugin, Integer> callback = permissionResultCallbacks.getAndRemoveCallback(requestCode);
  193. if(callback != null) {
  194. callback.first.onRequestPermissionResult(callback.second, permissions, grantResults);
  195. }
  196. }
  197. public void requestPermission(CordovaPlugin plugin, int requestCode, String permission) {
  198. String[] permissions = new String [1];
  199. permissions[0] = permission;
  200. requestPermissions(plugin, requestCode, permissions);
  201. }
  202. @SuppressLint("NewApi")
  203. public void requestPermissions(CordovaPlugin plugin, int requestCode, String [] permissions) {
  204. int mappedRequestCode = permissionResultCallbacks.registerCallback(plugin, requestCode);
  205. getActivity().requestPermissions(permissions, mappedRequestCode);
  206. }
  207. public boolean hasPermission(String permission)
  208. {
  209. if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
  210. {
  211. int result = activity.checkSelfPermission(permission);
  212. return PackageManager.PERMISSION_GRANTED == result;
  213. }
  214. else
  215. {
  216. return true;
  217. }
  218. }
  219. }