Commit 6dd8fc203ee879ec065ffaded5aea22a59ec54d4
Exists in
master
Merge branch 'dev_checkOverlayAndroid' into 'master'
Dev check overlay android
Showing 6 changed files Side-by-side Diff
android/src/main/java/com/reactnativecommunity/rnpermissions/RNPermissionsModule.java
... | ... | @@ -253,4 +253,37 @@ public class RNPermissionsModule extends ReactContextBaseJavaModule { |
253 | 253 | } |
254 | 254 | } |
255 | 255 | |
256 | + @ReactMethod | |
257 | + public void requestOverlayAndroid(final Promise promise) { | |
258 | +// Check if Android M or higher | |
259 | + final ReactApplicationContext reactContext = getReactApplicationContext(); | |
260 | + if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M && !Settings.canDrawOverlays(reactContext)) { | |
261 | + // Store the promise to resolve/reject when picker returns data | |
262 | + try { | |
263 | + Activity currentActivity = getCurrentActivity(); | |
264 | + if (currentActivity == null) { | |
265 | + promise.reject("E_ACTIVITY_DOES_NOT_EXIST", "Activity doesn't exist"); | |
266 | + return; | |
267 | + } | |
268 | + mPickerPromise = promise; | |
269 | + final String packageName = reactContext.getPackageName(); | |
270 | + // Show alert dialog to the user saying a separate permission is needed | |
271 | + // Launch the settings activity if the user prefers | |
272 | + Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, | |
273 | + Uri.parse("package:" + reactContext.getPackageName())); | |
274 | + currentActivity.startActivityForResult(intent, OVERLAY_PERMISSION_CODE); | |
275 | + } catch(Exception e) { | |
276 | + mPickerPromise.reject("ACTION_MANAGE_OVERLAY_PERMISSION_ERROR", e); | |
277 | + mPickerPromise = null; | |
278 | + } | |
279 | + } else { | |
280 | + //default is granted :D | |
281 | + final WritableMap output = Arguments.createMap(); | |
282 | + final WritableMap settings = Arguments.createMap(); | |
283 | + output.putString("status", "granted"); | |
284 | + output.putMap("settings", settings); | |
285 | + promise.resolve(output); | |
286 | + } | |
287 | + } | |
288 | + | |
256 | 289 | } |
src/contract.ts
src/index.ts
... | ... | @@ -15,6 +15,7 @@ export * from './types'; |
15 | 15 | export const openSettings = module.openSettings; |
16 | 16 | export const check = module.check; |
17 | 17 | export const checkOverlayAndroid = module.checkOverlayAndroid; |
18 | +export const requestOverlayAndroid = module.requestOverlayAndroid; | |
18 | 19 | export const request = module.request; |
19 | 20 | export const checkNotifications = module.checkNotifications; |
20 | 21 | export const requestNotifications = module.requestNotifications; |
... | ... | @@ -27,6 +28,7 @@ export default { |
27 | 28 | openSettings, |
28 | 29 | check, |
29 | 30 | checkOverlayAndroid, |
31 | + requestOverlayAndroid, | |
30 | 32 | request, |
31 | 33 | checkNotifications, |
32 | 34 | requestNotifications, |
src/module.android.ts
... | ... | @@ -17,6 +17,7 @@ const RNP: { |
17 | 17 | openSettings: () => Promise<true>; |
18 | 18 | checkOrRequestOverlayPermission: () => Promise<true>; |
19 | 19 | checkOverlayAndroid: () => Promise<boolean>; |
20 | + requestOverlayAndroid: () => Promise<PermissionStatus>; | |
20 | 21 | getNonRequestables: () => Promise<Permission[]>; |
21 | 22 | isNonRequestable: (permission: Permission) => Promise<boolean>; |
22 | 23 | setNonRequestable: (permission: Permission) => Promise<true>; |
... | ... | @@ -73,6 +74,18 @@ async function checkOverlayAndroid(): Promise<boolean> { |
73 | 74 | return await RNP.checkOverlayAndroid(); |
74 | 75 | } |
75 | 76 | |
77 | +async function requestOverlayAndroid(): Promise<PermissionStatus> { | |
78 | + console.warn("check requestOverlayAndroid ts") | |
79 | + const output = await RNP.requestOverlayAndroid() | |
80 | + if (output.status == RESULTS.GRANTED) { | |
81 | + return RESULTS.GRANTED | |
82 | + } else if (output.status == RESULTS.BLOCKED) { | |
83 | + return RESULTS.BLOCKED | |
84 | + } else { | |
85 | + return RESULTS.UNAVAILABLE | |
86 | + } | |
87 | +} | |
88 | + | |
76 | 89 | async function request( |
77 | 90 | permission: Permission, |
78 | 91 | rationale?: Rationale, |
... | ... | @@ -169,6 +182,7 @@ export const module: Contract = { |
169 | 182 | openSettings, |
170 | 183 | check, |
171 | 184 | checkOverlayAndroid, |
185 | + requestOverlayAndroid, | |
172 | 186 | request, |
173 | 187 | checkNotifications, |
174 | 188 | requestNotifications: checkNotifications, |
src/module.ios.ts
... | ... | @@ -46,6 +46,11 @@ async function checkOverlayAndroid(): Promise<boolean> { |
46 | 46 | return true; |
47 | 47 | } |
48 | 48 | |
49 | +async function requestOverlayAndroid(): Promise<PermissionStatus> { | |
50 | + return RESULTS.UNAVAILABLE; | |
51 | +} | |
52 | + | |
53 | + | |
49 | 54 | export function requestNotifications( |
50 | 55 | options: NotificationOption[], |
51 | 56 | ): Promise<NotificationsResponse> { |
... | ... | @@ -89,6 +94,7 @@ export const module: Contract = { |
89 | 94 | openSettings, |
90 | 95 | check, |
91 | 96 | checkOverlayAndroid, |
97 | + requestOverlayAndroid, | |
92 | 98 | request, |
93 | 99 | checkNotifications, |
94 | 100 | requestNotifications, |
src/module.ts
... | ... | @@ -10,6 +10,11 @@ async function checkOverlayAndroid(): Promise<boolean> { |
10 | 10 | return true; |
11 | 11 | } |
12 | 12 | |
13 | +async function requestOverlayAndroid(): Promise<PermissionStatus> { | |
14 | + return RESULTS.UNAVAILABLE; | |
15 | +} | |
16 | + | |
17 | + | |
13 | 18 | async function checkNotifications(): Promise<NotificationsResponse> { |
14 | 19 | return {status: RESULTS.UNAVAILABLE, settings: {}}; |
15 | 20 | } |
... | ... | @@ -27,6 +32,7 @@ export const module: Contract = { |
27 | 32 | openSettings: Promise.reject, |
28 | 33 | check, |
29 | 34 | checkOverlayAndroid, |
35 | + requestOverlayAndroid, | |
30 | 36 | request: check, |
31 | 37 | checkNotifications, |
32 | 38 | requestNotifications: checkNotifications, |