Example usage for org.apache.cordova PermissionHelper requestPermissions

List of usage examples for org.apache.cordova PermissionHelper requestPermissions

Introduction

In this page you can find the example usage for org.apache.cordova PermissionHelper requestPermissions.

Prototype

public static void requestPermissions(CordovaPlugin plugin, int requestCode, String[] permissions) 

Source Link

Document

Requests "dangerous" permissions for the application at runtime.

Usage

From source file:de.martinreinhardt.cordova.plugins.hotspot.HotSpotPlugin.java

License:Open Source License

/**
 * Executes the request.//  w w w  .j  ava2 s  . com
 * <p/>
 * This method is called from the WebView thread.
 * To do a non-trivial amount of work, use:
 * cordova.getThreadPool().execute(runnable);
 * <p/>
 * To run on the UI thread, use:
 * cordova.getThreadPool().execute(runnable);
 *
 * @param action   The action to execute.
 * @param rawArgs  The exec() arguments in String form.
 * @param callback The callback context used when calling
 *                 back into JavaScript.
 * @return Whether the action was valid.
 */
@Override
public boolean execute(String action, String rawArgs, CallbackContext callback) throws JSONException {
    this.callback = callback;
    this.action = action;
    this.rawArgs = rawArgs;
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP + 1) {
        Class systemClass = Settings.System.class;
        try {
            Method canWriteMethod = systemClass.getDeclaredMethod("canWrite", Context.class);
            boolean retVal = (Boolean) canWriteMethod.invoke(null, this.cordova.getActivity());
            Log.d(LOG_TAG, "Can Write Settings: " + retVal);
            if (!retVal) {
                Intent intent = new Intent("android.settings.action.MANAGE_WRITE_SETTINGS");
                intent.setData(Uri.parse("package:" + cordova.getActivity().getPackageName()));
                intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                // intent.
                try {
                    cordova.getActivity().startActivity(intent);
                } catch (Exception e) {
                    Log.e(LOG_TAG, "error starting permission intent", e);
                    return false;
                }
            }
        } catch (Exception ignored) {
            Log.e(LOG_TAG, "Could not perform permission check");
            this.callback.sendPluginResult(new PluginResult(PluginResult.Status.ILLEGAL_ACCESS_EXCEPTION));
        }
    }
    if (!this.hasPermissions()) {
        PermissionHelper.requestPermissions(this, 0, HotSpotPlugin.permissions);
        return true;
    } else {
        // pre Android 6 behaviour
        return executeInternal(action, rawArgs, callback);
    }
    // Returning false results in a "MethodNotFound" error.
}

From source file:org.cloudsky.cordovaPlugins.BarcodeminCDV.java

License:BSD License

/**
 * We override this so that we can access the permissions variable, which no longer exists in
 * the parent class, since we can't initialize it reliably in the constructor!
 *
 * @param requestCode The code to get request action
 *///from  w  w  w  . j av  a  2 s. c o  m
public void requestPermissions(int requestCode) {
    PermissionHelper.requestPermissions(this, requestCode, permissions);
}