NeoLSN / cordova-plugin-android-permissions

This plugin is designed for supporting Android new permissions checking mechanism.
Apache License 2.0
273 stars 177 forks source link

requestPermission(..) never returns if request has been denied by the user #16

Open headcr4sh opened 8 years ago

headcr4sh commented 8 years ago

I expect that a call of cordova.plugins.permissions.requestPermission will always trigger the success callback, even if a user decides that a permission shall not be granted.

Example:

cordova.plugins.permissions.requestPermission(
  cordova.plugins.permissions.CAMERA,
  // I expect this callback to be called, even if the user clicks "DENY".
  // Unfortunately this is not the case.
  p => console.log(`Permission granted: ${p.hasPermission}`),
  e => console.log("An error occured.", e)
);
hoongoon86 commented 8 years ago

I'm not here for solution but, isn't it natural to have success callback if requestPermission has been called successfully? If you want to check whether the permission had been granted, we have hasPermission (as you already know).

NeoLSN commented 8 years ago

Is the value of p.hasPermission false if a user decided not to grant the permission?

kristopolous commented 8 years ago

Quoting: https://developer.android.com/training/permissions/requesting.html

When the system asks the user to grant a permission, the user has the option of telling the system not to ask for that permission again. In that case, any time an app uses requestPermissions() to ask for that permission again, the system immediately denies the request. The system calls your onRequestPermissionsResult() callback method and passes PERMISSION_DENIED, the same way it would if the user had explicitly rejected your request again. This means that when you call requestPermissions(), you cannot assume that any direct interaction with the user has taken place.

What this means is that this use-case could be valid under those conditions. I however, believe it's happening in more than just this instance ... let me get back with a confirmation soon.

kristopolous commented 8 years ago

Also, this doesn't preclude including things in the manifest in addition to asking explicitly. Quoting the same link above:

On all versions of Android, your app needs to declare both the normal and the dangerous permissions it needs in its app manifest, as described in Declaring Permissions

kristopolous commented 8 years ago

So for me at least, after manually adding this line to my AndroidManifest.xml (for in this case camera, since this seems to be the big pain point)

 <uses-permission android:name="android.permission.CAMERA" />

And then rebuilding, I can

This appears to be the right result. This is with

<plugin name="cordova-plugin-camera" spec="~2.3.0" />

There is a PR that is outstanding that will do this: https://github.com/apache/cordova-plugin-camera/pull/228 ... once that closes this probably doesn't need to be done any more.

Until then it looks like this is needed. Bummer...

kristopolous commented 8 years ago

As far as detecting if the user had done the "never ask again" option probably the only feasible way to do it (since android doesn't tell you) would be something heuristic like this:

var beforeRequest = new Date();
window.cordova.plugins.permissions.requestPermission(
    window.cordova.plugins.permissions.CAMERA, function(m) { 
    if(!m.hasPermission && (new Date() - beforeRequest) < 350) {
      // 350 ms is pretty fast for a user to tap on a deny ... and even if they did
      // this is probably a user error and we should pretend like they didn't see it.
      // either way this should be handling as if the dialog never appeared.
      // In *my* user testing on a moto 3g 2015, I couldn't tap this dialog with a value 
      // less than 554 -and that was with a sincere effort to dismiss it as soon as possible
      // as if it was a game. This implicates there's likely a significant overhead baked in
      // before the dialog even appears
     ...
    }
 }, null);
jacquesdev commented 7 years ago

@NeoLSN - "Is the value of p.hasPermission false if a user decided not to grant the permission?"

Yes it is false even when the dialog is ignored.

santhu180485 commented 7 years ago

@kristopolous , I am using this plugin and when I select the "Deny" option, the app just gets closed/crashed. Am I missing anything?

artuska commented 5 years ago

When user clicks Deny my app just crashes.

Truelion commented 5 years ago

cordova plugin does not fire a failure or success callback when requestPermission() is called on the very first install of the apk via USB cable or on emulator or from installing an APK build from disk. It does pop the dialog to request geo permissions fine, I grant it, but it never fires a callback. (?)

When i close the app and reopen, geolocation works, because i previously granted. But on very first install, callbacks for requestPermission() does nothing, so app is at standstill. Does anyone know what it could be? Tried on several phones, all identical behavior.

USING window.cordova.plugins:


requestCordovaPermissions() {
    var self = this;
    window.cordova.plugins.permissions.requestPermission(
        window.cordova.plugins.permissions.ACCESS_FINE_LOCATION,
        function(status) {// <-------never fired
            if (status.hasPermission) {
                alert("permission granted") _ //never seen, even if i granted_
            } else {
                alert("unable to get geo location permissions")
            }
        },
        function(e) {//<------ never fired
            alert("Error : " + e)
        }
    )
}

Snippet from AndroidManifest.xml:
`
<supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
    <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="25" />
    <application android:hardwareAccelerated="true" android:icon="@mipmap/icon" android:label="@string/app_name" android:supportsRtl="true">
        <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:screenOrientation="portrait" android:theme="@android:style/Theme.DeviceDefault.NoActionBar" android:windowSoftInputMode="adjustResize">
            <intent-filter android:label="@string/launcher_name">
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
`

config.xml:

`
<feature name="Geolocation">
        <param name="android-package" value="org.apache.cordova.GeoBroker" />
    </feature>
    <engine name="android" spec="^6.3.0" />
    <plugin name="cordova-plugin-android-permissions" spec="^1.0.0" />
    <plugin name="cordova-plugin-exclude-files" spec="^0.4.2" />
    <plugin name="cordova-plugin-fullscreen" spec="^1.2.0" />
    <plugin name="cordova-plugin-geolocation" spec="^4.0.1" />
    <plugin name="cordova-plugin-whitelist" spec="^1.3.3" />
`