@nativescript-community/perms
Advanced tools
Comparing version 2.2.13 to 2.2.14
@@ -6,2 +6,10 @@ # Change Log | ||
## [2.2.14](https://github.com/nativescript-community/perms/compare/v2.2.13...v2.2.14) (2022-11-25) | ||
**Note:** Version bump only for package @nativescript-community/perms | ||
## [2.2.13](https://github.com/nativescript-community/perms/compare/v2.2.12...v2.2.13) (2022-09-27) | ||
@@ -8,0 +16,0 @@ |
{ | ||
"name": "@nativescript-community/perms", | ||
"version": "2.2.13", | ||
"version": "2.2.14", | ||
"description": "An unified permissions API for NativeScript on iOS and Android.", | ||
@@ -35,3 +35,3 @@ "main": "./permissions", | ||
"readmeFilename": "README.md", | ||
"gitHead": "a846bfb535fdf23e9d2d271316eea89714882b79" | ||
"gitHead": "0a73a1ac6a11cfae6b0994155029daf906591772" | ||
} |
@@ -1,2 +0,2 @@ | ||
import { CheckOptions, Permissions as PermissionsType, RequestOptions, Status } from './permissions'; | ||
import { CheckOptions, ObjectPermissions, Permissions as PermissionsType, RequestOptions, Status } from './permissions'; | ||
export * from './permissions.common'; | ||
@@ -7,5 +7,5 @@ export declare function canOpenSettings(): Promise<boolean>; | ||
export declare function check(permission: PermissionsType | string, options?: CheckOptions): Promise<[Status, boolean]>; | ||
export declare function request(permission: PermissionsType | PermissionsType[] | string[], options?: RequestOptions): Promise<[Status, boolean] | { | ||
export declare function request(permission: PermissionsType | string | ObjectPermissions, options?: RequestOptions): Promise<[Status, boolean] | { | ||
[permission: string]: Status; | ||
}>; | ||
export declare function checkMultiple(permissions: PermissionsType[]): Promise<any>; | ||
export declare function checkMultiple(permissions: ObjectPermissions): Promise<{}>; |
@@ -13,3 +13,2 @@ import { Trace, Utils } from '@nativescript/core'; | ||
} | ||
const JELLY_BEAN = 18; | ||
const MARSHMALLOW = 23; | ||
@@ -21,7 +20,12 @@ const ANDROIDQ = 29; | ||
function getNativePermissions(permission, options) { | ||
var _a, _b, _c, _d; | ||
if (Trace.isEnabled()) { | ||
CLog(CLogTypes.info, 'getNativePermissions', permission, options); | ||
} | ||
switch (permission) { | ||
case 'location': { | ||
const result = []; | ||
const coarse = options && options.coarse; | ||
const precise = options && options.precise; | ||
const coarse = (_a = options === null || options === void 0 ? void 0 : options.coarse) !== null && _a !== void 0 ? _a : true; | ||
const precise = (_b = options === null || options === void 0 ? void 0 : options.precise) !== null && _b !== void 0 ? _b : true; | ||
console.log('coarse', coarse, options); | ||
if (coarse !== false) { | ||
@@ -34,3 +38,3 @@ result.push(android.Manifest.permission.ACCESS_COARSE_LOCATION); | ||
if (getAndroidSDK() >= ANDROIDQ) { | ||
const type = typeof options === 'string' ? options : options && options.type; | ||
const type = typeof options === 'string' ? options : options === null || options === void 0 ? void 0 : options.type; | ||
if (type === 'always') { | ||
@@ -62,4 +66,4 @@ result.push(android.Manifest.permission.ACCESS_BACKGROUND_LOCATION); | ||
const result = []; | ||
const read = options && options.read; | ||
const write = options && options.write; | ||
const read = (_c = options === null || options === void 0 ? void 0 : options.read) !== null && _c !== void 0 ? _c : true; | ||
const write = (_d = options === null || options === void 0 ? void 0 : options.write) !== null && _d !== void 0 ? _d : true; | ||
if (read !== false) { | ||
@@ -117,9 +121,11 @@ result.push(android.Manifest.permission.READ_EXTERNAL_STORAGE); | ||
const getDidAskOnce = (permission) => Promise.resolve(!!getBoolean(STORAGE_KEY + permission)); | ||
const GRANT_RESULTS = { | ||
UNDETERMINED: 'undetermined', | ||
GRANTED: 'authorized', | ||
DENIED: 'denied', | ||
NEVER_ASK_AGAIN: 'never_ask_again' | ||
}; | ||
var PermissionsAndroid; | ||
(function (PermissionsAndroid) { | ||
PermissionsAndroid.RESULTS = { | ||
GRANTED: 'authorized', | ||
DENIED: 'denied', | ||
NEVER_ASK_AGAIN: 'never_ask_again' | ||
}; | ||
PermissionsAndroid.RESULTS = GRANT_RESULTS; | ||
async function check(permission) { | ||
@@ -141,3 +147,3 @@ const context = Utils.android.getApplicationContext(); | ||
PermissionsAndroid.check = check; | ||
async function request(permission, rationale) { | ||
async function request(permission) { | ||
return (await requestMultiplePermissions([permission]))[permission]; | ||
@@ -166,7 +172,7 @@ } | ||
? 'authorized' | ||
: 'denied'; | ||
: GRANT_RESULTS.DENIED; | ||
checkedPermissionsCount++; | ||
} | ||
else if (context.checkSelfPermission(perm) === android.content.pm.PackageManager.PERMISSION_GRANTED) { | ||
grantedPermissions[perm] = 'authorized'; | ||
grantedPermissions[perm] = GRANT_RESULTS.GRANTED; | ||
checkedPermissionsCount++; | ||
@@ -185,2 +191,5 @@ } | ||
const requestCode = mRequestCode++; | ||
if (Trace.isEnabled()) { | ||
CLog(CLogTypes.info, 'requestPermissions', permissionsToCheck); | ||
} | ||
activity.requestPermissions(permissionsToCheck, requestCode); | ||
@@ -190,13 +199,16 @@ androidApp.on(AndroidApplication.activityRequestPermissionsEvent, (args) => { | ||
const results = args.grantResults; | ||
if (Trace.isEnabled()) { | ||
CLog(CLogTypes.info, 'requestPermissions results', results); | ||
} | ||
for (let j = 0; j < permissionsToCheck.length; j++) { | ||
const permission = permissionsToCheck[j]; | ||
if (results.length > j && results[j] === android.content.pm.PackageManager.PERMISSION_GRANTED) { | ||
grantedPermissions[permission] = 'authorized'; | ||
grantedPermissions[permission] = GRANT_RESULTS.GRANTED; | ||
} | ||
else { | ||
if (activity.shouldShowRequestPermissionRationale(permission)) { | ||
grantedPermissions[permission] = 'denied'; | ||
grantedPermissions[permission] = GRANT_RESULTS.DENIED; | ||
} | ||
else { | ||
grantedPermissions[permission] = 'never_ask_again'; | ||
grantedPermissions[permission] = GRANT_RESULTS.NEVER_ASK_AGAIN; | ||
} | ||
@@ -256,3 +268,3 @@ } | ||
if (!perms || perms.length === 0) { | ||
return ['authorized', true]; | ||
return [GRANT_RESULTS.GRANTED, true]; | ||
} | ||
@@ -265,12 +277,12 @@ const isAuthorized = await PermissionsAndroid.check(perms); | ||
const backAuthorized = await PermissionsAndroid.check(android.Manifest.permission.ACCESS_BACKGROUND_LOCATION); | ||
return (['authorized', backAuthorized]); | ||
return ([GRANT_RESULTS.GRANTED, backAuthorized]); | ||
} | ||
} | ||
return (['authorized', true]); | ||
return ([GRANT_RESULTS.GRANTED, true]); | ||
} | ||
return getDidAskOnce(permission).then(didAsk => { | ||
if (didAsk) { | ||
return shouldShowRequestPermissionRationale(perms).then(shouldShow => [shouldShow ? 'denied' : 'restricted', true]); | ||
return shouldShowRequestPermissionRationale(perms).then(shouldShow => [shouldShow ? GRANT_RESULTS.DENIED : 'restricted', true]); | ||
} | ||
return (['undetermined', true]); | ||
return ([GRANT_RESULTS.UNDETERMINED, true]); | ||
}); | ||
@@ -283,35 +295,28 @@ } | ||
let types = []; | ||
if (Array.isArray(permission)) { | ||
permission.forEach(s => { | ||
if (s.startsWith('android.permission.')) { | ||
types.push(s); | ||
} | ||
else { | ||
types.push(...getNativePermissions(s, options)); | ||
} | ||
let permissions = []; | ||
if (typeof permission === 'object') { | ||
permissions = Object.keys(permission); | ||
permissions.forEach(s => { | ||
types.push(...getNativePermissions(s, permission[s])); | ||
}); | ||
} | ||
else { | ||
if (permission.startsWith('android.permission.')) { | ||
types.push(permission); | ||
} | ||
else { | ||
types = getNativePermissions(permission, options); | ||
} | ||
permissions = [permission]; | ||
types = getNativePermissions(permission, options); | ||
} | ||
console.log('types', types); | ||
if (types.length === 0) { | ||
return Promise.resolve(['authorized', true]); | ||
return Promise.resolve([GRANT_RESULTS.GRANTED, true]); | ||
} | ||
const rationale = typeof options === 'string' ? undefined : options && options.rationale; | ||
if (types.length > 1) { | ||
return requestMultiplePermissions(types); | ||
} | ||
return PermissionsAndroid.request(types[0], rationale).then(result => { | ||
return PermissionsAndroid.request(types[0]).then(result => { | ||
if (typeof result === 'boolean') { | ||
return [result ? 'authorized' : 'denied', true]; | ||
return [result ? GRANT_RESULTS.GRANTED : GRANT_RESULTS.DENIED, true]; | ||
} | ||
if (Array.isArray(permission)) { | ||
return Promise.all(permission.map(setDidAskOnce)).then(() => [result, true]); | ||
if (permissions.length > 1) { | ||
return Promise.all(permissions.map(setDidAskOnce)).then(() => [result, true]); | ||
} | ||
return setDidAskOnce(permission).then(() => [result, true]); | ||
return setDidAskOnce(permissions[0]).then(() => [result, true]); | ||
}); | ||
@@ -323,8 +328,10 @@ } | ||
} | ||
return Promise.all(permissions.map(permission => this.check(permission))).then(result => result.reduce((acc, value, index) => { | ||
const name = permissions[index]; | ||
acc[name] = value; | ||
return acc; | ||
}, {})); | ||
return Promise.all(Object.keys(permissions).map(permission => check(permission, permissions[permission]).then(r => [permission, r]))).then(result => { | ||
console.log('test', result); | ||
return result.reduce((acc, value, index) => { | ||
acc[value[0]] = value[1]; | ||
return acc; | ||
}, {}); | ||
}); | ||
} | ||
//# sourceMappingURL=permissions.android.js.map |
@@ -0,4 +1,12 @@ | ||
/* eslint-disable @typescript-eslint/unified-signatures */ | ||
/* eslint-disable no-redeclare */ | ||
export type Status = 'authorized' | 'denied' | 'limited' | 'restricted' | 'undetermined' | 'never_ask_again'; | ||
export const PermsTraceCategory = 'NativescriptPerms'; | ||
export type AndroidPermissions<T = Exclude<keyof typeof android.Manifest.permission, keyof typeof java.lang.Object>> = { | ||
[K in keyof T]: T[K] extends string ? T : never; | ||
}; | ||
export type AndroidPermissionsString = `android.permission.${AndroidPermissions}`/* | `android.Manifest.permission.${AndroidPermissions}` */; | ||
export type Permissions = | ||
@@ -24,26 +32,31 @@ | 'location' | ||
| 'readSms' | ||
| 'receiveSms'; | ||
export interface Rationale { | ||
title: string; | ||
message: string; | ||
buttonPositive?: string; | ||
buttonNegative?: string; | ||
buttonNeutral?: string; | ||
} | ||
| 'receiveSms' | ||
| AndroidPermissionsString; | ||
export interface LocationOptions { | ||
[k: string]: any; | ||
type: string; | ||
coarce?: boolean; | ||
coarse?: boolean; | ||
precise?: boolean; | ||
} | ||
export interface StorageOptions { | ||
[k: string]: any; | ||
read?: boolean; | ||
write?: boolean; | ||
// manage?: boolean; | ||
} | ||
export type CheckOptions = string | LocationOptions | StorageOptions; | ||
export type RequestOptions = string | LocationOptions | StorageOptions; | ||
export type PermissionOptions = Record<string, any>; | ||
export interface ObjectPermissions { | ||
'location': LocationOptions; | ||
'storage': StorageOptions; | ||
} | ||
export type ObjectPermissionsRest = { | ||
[key in Permissions]: PermissionOptions; | ||
}; | ||
export type CheckOptions <T extends Permissions = Permissions>= T extends keyof ObjectPermissions ? ObjectPermissions[T] : any; | ||
export function check<T extends Permissions>(permission: T, options?: CheckOptions<T>): Promise<Result>; | ||
export function check<T extends string>(permission: T): Promise<Result>; | ||
export function canOpenSettings(): Promise<boolean>; | ||
@@ -55,8 +68,10 @@ | ||
export type Result<T> = T extends any[] ? { [k: string]: Status } : [Status, boolean]; | ||
export type Result = [Status, boolean]; | ||
export interface MultiResult { [k: Permissions | string]: Status } | ||
export function check<T = Permissions | string>(permission: T, options?: CheckOptions): Promise<Result<T>>; | ||
export type RequestOptions <T extends Permissions = Permissions>= T extends keyof ObjectPermissions ? ObjectPermissions[T] : any; | ||
export function request<T extends Permissions>(permission: T, options?: RequestOptions<T>): Promise<Result>; | ||
export function request<T extends Partial<ObjectPermissions | ObjectPermissionsRest>>(permission: T): Promise<MultiResult>; | ||
export function request<T extends string>(permission: T): Promise<Result>; | ||
export function request<T = Permissions | Permissions[] | string[]>(permission: T, options?: RequestOptions): Promise<Result<T>>; | ||
export function checkMultiple<T = Permissions[]>(permissions: T): Promise<Result<T>>; | ||
export function checkMultiple<T extends Partial<ObjectPermissions | ObjectPermissionsRest>>(permissions: T): Promise<MultiResult>; |
@@ -41,3 +41,3 @@ import { CheckOptions, Permissions as PermissionsType, RequestOptions } from './permissions'; | ||
export declare function check(permission: IOSPermissionTypes, options?: CheckOptions): Promise<SingleResult>; | ||
export declare function request<T extends IOSPermissionTypes | IOSPermissionTypes[]>(permission: T, options?: RequestOptions): Promise<Result<T>>; | ||
export declare function request<T extends IOSPermissionTypes | Record<IOSPermissionTypes, any>>(permission: T, options?: RequestOptions): Promise<Result<T>>; | ||
export declare function checkMultiple(permissions: PermissionsType[]): Promise<any>; |
@@ -48,6 +48,3 @@ import { Device, Trace, Utils } from '@nativescript/core'; | ||
let locationManagerDelegate; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.CLLocationManagerDelegateImpl = void 0; | ||
var CLLocationManagerDelegateImpl = /** @class */ (function (_super) { | ||
var CLLocationManagerDelegateImpl = /** @class */ (function (_super) { | ||
__extends(CLLocationManagerDelegateImpl, _super); | ||
@@ -90,3 +87,3 @@ function CLLocationManagerDelegateImpl() { | ||
}(NSObject)); | ||
exports.CLLocationManagerDelegateImpl = CLLocationManagerDelegateImpl; | ||
export { CLLocationManagerDelegateImpl }; | ||
function request(type) { | ||
@@ -177,6 +174,3 @@ const status = getStatusForType(type); | ||
NSPBluetooth.getStatus = getStatus; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.CBPeripheralManagerDelegateImpl = void 0; | ||
var CBPeripheralManagerDelegateImpl = /** @class */ (function (_super) { | ||
var CBPeripheralManagerDelegateImpl = /** @class */ (function (_super) { | ||
__extends(CBPeripheralManagerDelegateImpl, _super); | ||
@@ -215,3 +209,3 @@ function CBPeripheralManagerDelegateImpl() { | ||
}(NSObject)); | ||
exports.CBPeripheralManagerDelegateImpl = CBPeripheralManagerDelegateImpl; | ||
export { CBPeripheralManagerDelegateImpl }; | ||
let peripheralManager; | ||
@@ -769,6 +763,7 @@ function request() { | ||
} | ||
if (Array.isArray(permission)) { | ||
if (typeof permission === 'object') { | ||
const grantedPermissions = {}; | ||
for (let index = 0; index < permission.length; index++) { | ||
const res = await request(permission[index], options); | ||
const keys = Object.keys(permission); | ||
for (let index = 0; index < keys.length; index++) { | ||
const res = await request(keys[index], options[keys[index]]); | ||
grantedPermissions[permission[index]] = res[0]; | ||
@@ -775,0 +770,0 @@ } |
@@ -65,6 +65,6 @@ [](https://www.npmjs.com/package/@nativescript-community/perms) | ||
| `request()` | `type` | - Accepts any permission type except `backgroundRefresh`. If the current status is `undetermined`, shows the permission dialog and returns a promise with the resulting status. Otherwise, immediately return a promise with the current status. See iOS Notes for special cases | | ||
| `checkMultiple()` | `[types]` | - Accepts an array of permission types and returns a promise with an object mapping permission types to statuses | | ||
| `checkMultiple()` | `{[key:string]:Record<string, any>}` | - Accepts an array of permission types and returns a promise with an object mapping permission types to statuses | | ||
| `getTypes()` | _none_ | - Returns an array of valid permission types | | ||
| `openSettings()` | _none_ | - _(iOS only - 8.0 and later)_ Switches the user to the settings page of your app | | ||
| `canOpenSettings()` | _none_ | - _(iOS only)_ Returns a boolean indicating if the device supports switching to the settings page | | ||
| `openSettings()` | _none_ | - Switches the user to the settings page of your app | | ||
| `canOpenSettings()` | _none_ | - Returns a boolean indicating if the device supports switching to the settings page | | ||
@@ -71,0 +71,0 @@ ### iOS Notes |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
116998
1256