2016-08-15 12:52:04 +00:00
|
|
|
/**
|
2018-09-11 22:27:47 +00:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2016-08-15 12:52:04 +00:00
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-08-15 12:52:04 +00:00
|
|
|
*
|
2018-05-11 02:06:46 +00:00
|
|
|
* @format
|
2016-08-15 12:52:04 +00:00
|
|
|
* @flow
|
|
|
|
*/
|
2018-05-11 02:06:46 +00:00
|
|
|
|
2016-08-15 12:52:04 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-02-18 00:39:50 +00:00
|
|
|
const NativeModules = require('NativeModules');
|
2016-08-15 12:52:04 +00:00
|
|
|
|
|
|
|
type Rationale = {
|
2017-02-18 00:39:50 +00:00
|
|
|
title: string,
|
|
|
|
message: string,
|
2018-05-11 02:06:46 +00:00
|
|
|
};
|
2016-08-15 12:52:04 +00:00
|
|
|
|
2016-11-25 06:36:23 +00:00
|
|
|
type PermissionStatus = 'granted' | 'denied' | 'never_ask_again';
|
2016-08-15 12:52:04 +00:00
|
|
|
/**
|
|
|
|
* `PermissionsAndroid` provides access to Android M's new permissions model.
|
2018-02-22 15:04:35 +00:00
|
|
|
*
|
2018-01-30 00:10:49 +00:00
|
|
|
* See https://facebook.github.io/react-native/docs/permissionsandroid.html
|
2016-08-15 12:52:04 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
class PermissionsAndroid {
|
|
|
|
PERMISSIONS: Object;
|
2016-11-25 06:36:23 +00:00
|
|
|
RESULTS: Object;
|
2016-08-15 12:52:04 +00:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
/**
|
|
|
|
* A list of specified "dangerous" permissions that require prompting the user
|
|
|
|
*/
|
|
|
|
this.PERMISSIONS = {
|
|
|
|
READ_CALENDAR: 'android.permission.READ_CALENDAR',
|
|
|
|
WRITE_CALENDAR: 'android.permission.WRITE_CALENDAR',
|
|
|
|
CAMERA: 'android.permission.CAMERA',
|
|
|
|
READ_CONTACTS: 'android.permission.READ_CONTACTS',
|
|
|
|
WRITE_CONTACTS: 'android.permission.WRITE_CONTACTS',
|
2018-05-11 02:06:46 +00:00
|
|
|
GET_ACCOUNTS: 'android.permission.GET_ACCOUNTS',
|
2016-08-15 12:52:04 +00:00
|
|
|
ACCESS_FINE_LOCATION: 'android.permission.ACCESS_FINE_LOCATION',
|
|
|
|
ACCESS_COARSE_LOCATION: 'android.permission.ACCESS_COARSE_LOCATION',
|
|
|
|
RECORD_AUDIO: 'android.permission.RECORD_AUDIO',
|
|
|
|
READ_PHONE_STATE: 'android.permission.READ_PHONE_STATE',
|
|
|
|
CALL_PHONE: 'android.permission.CALL_PHONE',
|
|
|
|
READ_CALL_LOG: 'android.permission.READ_CALL_LOG',
|
|
|
|
WRITE_CALL_LOG: 'android.permission.WRITE_CALL_LOG',
|
|
|
|
ADD_VOICEMAIL: 'com.android.voicemail.permission.ADD_VOICEMAIL',
|
|
|
|
USE_SIP: 'android.permission.USE_SIP',
|
|
|
|
PROCESS_OUTGOING_CALLS: 'android.permission.PROCESS_OUTGOING_CALLS',
|
2018-05-11 02:06:46 +00:00
|
|
|
BODY_SENSORS: 'android.permission.BODY_SENSORS',
|
2016-08-15 12:52:04 +00:00
|
|
|
SEND_SMS: 'android.permission.SEND_SMS',
|
|
|
|
RECEIVE_SMS: 'android.permission.RECEIVE_SMS',
|
|
|
|
READ_SMS: 'android.permission.READ_SMS',
|
|
|
|
RECEIVE_WAP_PUSH: 'android.permission.RECEIVE_WAP_PUSH',
|
|
|
|
RECEIVE_MMS: 'android.permission.RECEIVE_MMS',
|
|
|
|
READ_EXTERNAL_STORAGE: 'android.permission.READ_EXTERNAL_STORAGE',
|
|
|
|
WRITE_EXTERNAL_STORAGE: 'android.permission.WRITE_EXTERNAL_STORAGE',
|
|
|
|
};
|
2016-11-25 06:36:23 +00:00
|
|
|
|
|
|
|
this.RESULTS = {
|
|
|
|
GRANTED: 'granted',
|
|
|
|
DENIED: 'denied',
|
|
|
|
NEVER_ASK_AGAIN: 'never_ask_again',
|
|
|
|
};
|
2016-08-15 12:52:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-25 06:36:23 +00:00
|
|
|
* DEPRECATED - use check
|
|
|
|
*
|
2016-08-15 12:52:04 +00:00
|
|
|
* Returns a promise resolving to a boolean value as to whether the specified
|
|
|
|
* permissions has been granted
|
2016-11-25 06:36:23 +00:00
|
|
|
*
|
|
|
|
* @deprecated
|
2016-08-15 12:52:04 +00:00
|
|
|
*/
|
2018-05-11 02:06:46 +00:00
|
|
|
checkPermission(permission: string): Promise<boolean> {
|
|
|
|
console.warn(
|
|
|
|
'"PermissionsAndroid.checkPermission" is deprecated. Use "PermissionsAndroid.check" instead',
|
|
|
|
);
|
2017-02-18 00:39:50 +00:00
|
|
|
return NativeModules.PermissionsAndroid.checkPermission(permission);
|
2016-08-15 12:52:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-25 06:36:23 +00:00
|
|
|
* Returns a promise resolving to a boolean value as to whether the specified
|
|
|
|
* permissions has been granted
|
2018-02-22 15:04:35 +00:00
|
|
|
*
|
2018-01-30 00:10:49 +00:00
|
|
|
* See https://facebook.github.io/react-native/docs/permissionsandroid.html#check
|
2016-11-25 06:36:23 +00:00
|
|
|
*/
|
2018-05-11 02:06:46 +00:00
|
|
|
check(permission: string): Promise<boolean> {
|
2017-02-18 00:39:50 +00:00
|
|
|
return NativeModules.PermissionsAndroid.checkPermission(permission);
|
2016-11-25 06:36:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DEPRECATED - use request
|
|
|
|
*
|
2016-08-15 12:52:04 +00:00
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* 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
|
2016-11-25 06:36:23 +00:00
|
|
|
*
|
|
|
|
* @deprecated
|
2016-08-15 12:52:04 +00:00
|
|
|
*/
|
2018-05-11 02:06:46 +00:00
|
|
|
async requestPermission(
|
|
|
|
permission: string,
|
|
|
|
rationale?: Rationale,
|
|
|
|
): Promise<boolean> {
|
|
|
|
console.warn(
|
|
|
|
'"PermissionsAndroid.requestPermission" is deprecated. Use "PermissionsAndroid.request" instead',
|
|
|
|
);
|
2016-11-25 06:36:23 +00:00
|
|
|
const response = await this.request(permission, rationale);
|
2018-05-11 02:06:46 +00:00
|
|
|
return response === this.RESULTS.GRANTED;
|
2016-11-25 06:36:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
2018-01-30 00:10:49 +00:00
|
|
|
* See https://facebook.github.io/react-native/docs/permissionsandroid.html#request
|
2016-11-25 06:36:23 +00:00
|
|
|
*/
|
2018-05-11 02:06:46 +00:00
|
|
|
async request(
|
|
|
|
permission: string,
|
|
|
|
rationale?: Rationale,
|
|
|
|
): Promise<PermissionStatus> {
|
2016-08-16 19:51:54 +00:00
|
|
|
if (rationale) {
|
2018-05-11 02:06:46 +00:00
|
|
|
const shouldShowRationale = await NativeModules.PermissionsAndroid.shouldShowRequestPermissionRationale(
|
|
|
|
permission,
|
|
|
|
);
|
2016-08-15 12:52:04 +00:00
|
|
|
|
2016-08-16 19:51:54 +00:00
|
|
|
if (shouldShowRationale) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2017-02-18 00:39:50 +00:00
|
|
|
NativeModules.DialogManagerAndroid.showAlert(
|
2016-08-16 19:51:54 +00:00
|
|
|
rationale,
|
|
|
|
() => reject(new Error('Error showing rationale')),
|
2018-05-11 02:06:46 +00:00
|
|
|
() =>
|
|
|
|
resolve(
|
|
|
|
NativeModules.PermissionsAndroid.requestPermission(permission),
|
|
|
|
),
|
2016-08-16 19:51:54 +00:00
|
|
|
);
|
|
|
|
});
|
2016-08-15 12:52:04 +00:00
|
|
|
}
|
2016-08-16 19:51:54 +00:00
|
|
|
}
|
2017-02-18 00:39:50 +00:00
|
|
|
return NativeModules.PermissionsAndroid.requestPermission(permission);
|
2016-08-15 12:52:04 +00:00
|
|
|
}
|
2016-11-25 06:36:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2018-02-22 15:04:35 +00:00
|
|
|
*
|
2018-01-30 00:10:49 +00:00
|
|
|
* See https://facebook.github.io/react-native/docs/permissionsandroid.html#requestmultiple
|
2016-11-25 06:36:23 +00:00
|
|
|
*/
|
2018-05-11 02:06:46 +00:00
|
|
|
requestMultiple(
|
|
|
|
permissions: Array<string>,
|
|
|
|
): Promise<{[permission: string]: PermissionStatus}> {
|
|
|
|
return NativeModules.PermissionsAndroid.requestMultiplePermissions(
|
|
|
|
permissions,
|
|
|
|
);
|
2016-11-25 06:36:23 +00:00
|
|
|
}
|
2016-08-15 12:52:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PermissionsAndroid = new PermissionsAndroid();
|
|
|
|
|
|
|
|
module.exports = PermissionsAndroid;
|