-
Notifications
You must be signed in to change notification settings - Fork 24.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[PermissionsAndroid] Handle "Never Ask Again" in permissions and add requestMultiplePermissions #10221
[PermissionsAndroid] Handle "Never Ask Again" in permissions and add requestMultiplePermissions #10221
Changes from 8 commits
4e8fcb9
5b0c28c
1e7e69f
053d2b8
0b83011
8d2c4b3
e6a6378
dfb1125
bec452a
27cf4e8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,7 +47,7 @@ type Rationale = { | |
* 'so you can take awesome pictures.' | ||
* } | ||
* ) | ||
* if (granted) { | ||
* if (granted === PermissionsAndroid.RESULTS.GRANTED) { | ||
* console.log("You can use the camera") | ||
* } else { | ||
* console.log("Camera permission denied") | ||
|
@@ -61,6 +61,7 @@ type Rationale = { | |
|
||
class PermissionsAndroid { | ||
PERMISSIONS: Object; | ||
RESULTS: Object; | ||
|
||
constructor() { | ||
/** | ||
|
@@ -92,17 +93,38 @@ class PermissionsAndroid { | |
READ_EXTERNAL_STORAGE: 'android.permission.READ_EXTERNAL_STORAGE', | ||
WRITE_EXTERNAL_STORAGE: 'android.permission.WRITE_EXTERNAL_STORAGE', | ||
}; | ||
|
||
this.RESULTS = { | ||
GRANTED: 'granted', | ||
DENIED: 'denied', | ||
NEVER_ASK_AGAIN: 'never_ask_again', | ||
}; | ||
} | ||
|
||
/** | ||
* DEPRECATED - use query | ||
* | ||
* Returns a promise resolving to a boolean value as to whether the specified | ||
* permissions has been granted | ||
* | ||
* @deprecated | ||
*/ | ||
checkPermission(permission: string) : Promise<boolean> { | ||
console.warn('"PermissionsAndroid.checkPermission" is deprecated. Use "PermissionsAndroid.query" instead'); | ||
return Permissions.checkPermission(permission); | ||
} | ||
|
||
/** | ||
* Returns a promise resolving to a boolean value as to whether the specified | ||
* permissions has been granted | ||
*/ | ||
query(permission: string) : Promise<boolean> { | ||
return Permissions.checkPermission(permission); | ||
} | ||
|
||
/** | ||
* DEPRECATED - use request | ||
* | ||
* Prompts the user to enable a permission and returns a promise resolving to a | ||
* boolean value indicating whether the user allowed or denied the request | ||
* | ||
|
@@ -111,8 +133,26 @@ class PermissionsAndroid { | |
* necessary to show a dialog explaining why the permission is needed | ||
* (https://developer.android.com/training/permissions/requesting.html#explain) | ||
* and then shows the system permission dialog | ||
* | ||
* @deprecated | ||
*/ | ||
async requestPermission(permission: string, rationale?: Rationale) : Promise<boolean> { | ||
console.warn('"PermissionsAndroid.requestPermission" is deprecated. Use "PermissionsAndroid.request" instead'); | ||
const response = await this.request(permission, rationale); | ||
return (response === this.RESULTS.GRANTED); | ||
} | ||
|
||
/** | ||
* Prompts the user to enable a permission and returns a promise resolving to a | ||
* string value indicating whether the user allowed or denied the request | ||
* | ||
* If the optional rationale argument is included (which is an object with a | ||
* `title` and `message`), this function checks with the OS whether it is | ||
* necessary to show a dialog explaining why the permission is needed | ||
* (https://developer.android.com/training/permissions/requesting.html#explain) | ||
* and then shows the system permission dialog | ||
*/ | ||
async request(permission: string, rationale?: Rationale) : Promise<string> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add |
||
if (rationale) { | ||
const shouldShowRationale = await Permissions.shouldShowRequestPermissionRationale(permission); | ||
|
||
|
@@ -128,6 +168,15 @@ class PermissionsAndroid { | |
} | ||
return Permissions.requestPermission(permission); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change flow return type from Will at least warn people using flow that this api has changed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Although because this is javascript, |
||
} | ||
|
||
/** | ||
* Prompts the user to enable multiple permissions in the same dialog and | ||
* returns an object with the permissions as keys and strings as values | ||
* indicating whether the user allowed or denied the request | ||
*/ | ||
requestMultiple(permissions: Array<string>) : Promise<{[permission: string]: string}> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change |
||
return Permissions.requestMultiplePermissions(permissions); | ||
} | ||
} | ||
|
||
PermissionsAndroid = new PermissionsAndroid(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's better to change this to
check
since it doesn't actually return the permission status (which is what I thought at first)