mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-24 12:25:59 +08:00
Change Android Permission Module to use promises
Summary: This is a follow-up to #9292 satya164 Closes https://github.com/facebook/react-native/pull/9325 Differential Revision: D3723568 fbshipit-source-id: d553a70bde53ed5d7c1f5f544c85c5c5935e3ca4
This commit is contained in:
committed by
Facebook Github Bot 2
parent
7655e537ed
commit
6df41d5184
@@ -12,11 +12,11 @@
|
||||
'use strict';
|
||||
|
||||
const DialogManagerAndroid = require('NativeModules').DialogManagerAndroid;
|
||||
const AndroidPermissions = require('NativeModules').AndroidPermissions;
|
||||
const Permissions = require('NativeModules').PermissionsAndroid;
|
||||
|
||||
type Rationale = {
|
||||
title: string,
|
||||
message: string,
|
||||
title: string;
|
||||
message: string;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -99,13 +99,7 @@ class PermissionsAndroid {
|
||||
* permissions has been granted
|
||||
*/
|
||||
checkPermission(permission: string) : Promise<boolean> {
|
||||
return new Promise((resolve, reject) => {
|
||||
AndroidPermissions.checkPermission(
|
||||
permission,
|
||||
function(perm: string, result: boolean) { resolve(result); },
|
||||
function(error: string) { reject(error); },
|
||||
);
|
||||
});
|
||||
return Permissions.checkPermission(permission);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -118,41 +112,21 @@ class PermissionsAndroid {
|
||||
* (https://developer.android.com/training/permissions/requesting.html#explain)
|
||||
* and then shows the system permission dialog
|
||||
*/
|
||||
requestPermission(permission: string, rationale?: Rationale) : Promise<boolean> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const requestPermission = () => {
|
||||
AndroidPermissions.requestPermission(
|
||||
permission,
|
||||
function(perm: string, result: boolean) { resolve(result); },
|
||||
function(error: string) { reject(error); },
|
||||
);
|
||||
};
|
||||
async requestPermission(permission: string, rationale?: Rationale) : Promise<boolean> {
|
||||
if (rationale) {
|
||||
const shouldShowRationale = await Permissions.shouldShowRequestPermissionRationale(permission);
|
||||
|
||||
if (rationale) {
|
||||
AndroidPermissions.shouldShowRequestPermissionRationale(
|
||||
permission,
|
||||
function(perm: string, shouldShowRationale: boolean) {
|
||||
if (shouldShowRationale) {
|
||||
DialogManagerAndroid.showAlert(
|
||||
rationale,
|
||||
() => {
|
||||
DialogManagerAndroid.showAlert({
|
||||
message: 'Error Requesting Permissions'
|
||||
}, {}, {});
|
||||
reject();
|
||||
},
|
||||
requestPermission
|
||||
);
|
||||
} else {
|
||||
requestPermission();
|
||||
}
|
||||
},
|
||||
function(error: string) { reject(error); },
|
||||
);
|
||||
} else {
|
||||
requestPermission();
|
||||
if (shouldShowRationale) {
|
||||
return new Promise((resolve, reject) => {
|
||||
DialogManagerAndroid.showAlert(
|
||||
rationale,
|
||||
() => reject(new Error('Error showing rationale')),
|
||||
() => resolve(Permissions.requestPermission(permission))
|
||||
);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
return Permissions.requestPermission(permission);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user