2023-06-10 18:02:58 +02:00
|
|
|
import Logger from '@joplin/lib/Logger';
|
|
|
|
|
2020-06-08 10:01:11 +02:00
|
|
|
const { Platform, PermissionsAndroid } = require('react-native');
|
2023-06-10 18:02:58 +02:00
|
|
|
const logger = Logger.create('checkPermissions');
|
2020-06-08 10:01:11 +02:00
|
|
|
|
|
|
|
type rationale = {
|
2020-11-12 21:29:22 +02:00
|
|
|
title: string;
|
|
|
|
message: string;
|
|
|
|
buttonPositive?: string;
|
|
|
|
buttonNegative?: string;
|
|
|
|
buttonNeutral?: string;
|
|
|
|
};
|
2020-06-08 10:01:11 +02:00
|
|
|
|
|
|
|
export default async (permissions: string, rationale?: rationale) => {
|
2023-06-10 18:02:58 +02:00
|
|
|
// On iOS, permissions are prompted for by the system, so here we assume it's granted.
|
|
|
|
if (Platform.OS !== 'android') return PermissionsAndroid.RESULTS.GRANTED;
|
2020-06-04 19:40:44 +02:00
|
|
|
|
|
|
|
let result = await PermissionsAndroid.check(permissions);
|
2023-06-10 18:02:58 +02:00
|
|
|
logger.info('Checked permission:', result);
|
2020-06-04 19:40:44 +02:00
|
|
|
if (result !== PermissionsAndroid.RESULTS.GRANTED) {
|
2020-06-08 10:01:11 +02:00
|
|
|
result = await PermissionsAndroid.request(permissions, rationale);
|
2023-06-10 18:02:58 +02:00
|
|
|
logger.info('Requested permission:', result);
|
2020-06-04 19:40:44 +02:00
|
|
|
}
|
2020-10-16 17:26:19 +02:00
|
|
|
return result;
|
2020-06-04 19:40:44 +02:00
|
|
|
};
|