PermissionHelper.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 java.util.Arrays;
  19. import org.json.JSONException;
  20. import android.content.pm.PackageManager;
  21. /**
  22. * This class provides reflective methods for permission requesting and checking so that plugins
  23. * written for cordova-android 5.0.0+ can still compile with earlier cordova-android versions.
  24. */
  25. public class PermissionHelper {
  26. private static final String LOG_TAG = "CordovaPermissionHelper";
  27. /**
  28. * Requests a "dangerous" permission for the application at runtime. This is a helper method
  29. * alternative to cordovaInterface.requestPermission() that does not require the project to be
  30. * built with cordova-android 5.0.0+
  31. *
  32. * @param plugin The plugin the permission is being requested for
  33. * @param requestCode A requestCode to be passed to the plugin's onRequestPermissionResult()
  34. * along with the result of the permission request
  35. * @param permission The permission to be requested
  36. */
  37. public static void requestPermission(CordovaPlugin plugin, int requestCode, String permission) {
  38. PermissionHelper.requestPermissions(plugin, requestCode, new String[] {permission});
  39. }
  40. /**
  41. * Requests "dangerous" permissions for the application at runtime. This is a helper method
  42. * alternative to cordovaInterface.requestPermissions() that does not require the project to be
  43. * built with cordova-android 5.0.0+
  44. *
  45. * @param plugin The plugin the permissions are being requested for
  46. * @param requestCode A requestCode to be passed to the plugin's onRequestPermissionResult()
  47. * along with the result of the permissions request
  48. * @param permissions The permissions to be requested
  49. */
  50. public static void requestPermissions(CordovaPlugin plugin, int requestCode, String[] permissions) {
  51. plugin.cordova.requestPermissions(plugin, requestCode, permissions);
  52. }
  53. /**
  54. * Checks at runtime to see if the application has been granted a permission. This is a helper
  55. * method alternative to cordovaInterface.hasPermission() that does not require the project to
  56. * be built with cordova-android 5.0.0+
  57. *
  58. * @param plugin The plugin the permission is being checked against
  59. * @param permission The permission to be checked
  60. *
  61. * @return True if the permission has already been granted and false otherwise
  62. */
  63. public static boolean hasPermission(CordovaPlugin plugin, String permission) {
  64. return plugin.cordova.hasPermission(permission);
  65. }
  66. private static void deliverPermissionResult(CordovaPlugin plugin, int requestCode, String[] permissions) {
  67. // Generate the request results
  68. int[] requestResults = new int[permissions.length];
  69. Arrays.fill(requestResults, PackageManager.PERMISSION_GRANTED);
  70. try {
  71. plugin.onRequestPermissionResult(requestCode, permissions, requestResults);
  72. } catch (JSONException e) {
  73. LOG.e(LOG_TAG, "JSONException when delivering permissions results", e);
  74. }
  75. }
  76. }