Commit eacd23d778dedf9e693fb2f5ca0770ec79fcf076

Authored by cuong
1 parent a092fc595b

add checkOverlayPermission

Showing 6 changed files with 54 additions and 66 deletions Side-by-side Diff

android/src/main/java/com/reactnativecommunity/rnpermissions/RNPermissionsModule.java
... ... @@ -37,40 +37,40 @@ public class RNPermissionsModule extends ReactContextBaseJavaModule {
37 37 private static final String SETTING_NAME = "@RNPermissions:NonRequestables";
38 38 private static final int OVERLAY_PERMISSION_CODE = 1009;
39 39  
40   - private Promise mPickerPromise;
  40 + private Promise mPickerPromise;
41 41  
42   - private final ActivityEventListener mActivityEventListener = new BaseActivityEventListener() {
43   - @Override
44   - public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent intent) {
45   - if (requestCode == OVERLAY_PERMISSION_CODE) {
  42 + private final ActivityEventListener mActivityEventListener = new BaseActivityEventListener() {
  43 + @Override
  44 + public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent intent) {
  45 + if (requestCode == OVERLAY_PERMISSION_CODE) {
46 46 // Log.e("TEAGGGG", "OVERLAY_PERMISSION_CODE: "+ requestCode);
47   - if (mPickerPromise != null) {
48   - final WritableMap output = Arguments.createMap();
49   - final WritableMap settings = Arguments.createMap();
50   - output.putMap("settings", settings);
51   - if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
52   - final ReactApplicationContext reactContext = getReactApplicationContext();
53   - if (!Settings.canDrawOverlays(reactContext)) {
54   - output.putString("status", "blocked");
55   - } else {
56   - output.putString("status", "granted");
57   - }
  47 + if (mPickerPromise != null) {
  48 + final WritableMap output = Arguments.createMap();
  49 + final WritableMap settings = Arguments.createMap();
  50 + output.putMap("settings", settings);
  51 + if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
  52 + final ReactApplicationContext reactContext = getReactApplicationContext();
  53 + if (!Settings.canDrawOverlays(reactContext)) {
  54 + output.putString("status", "blocked");
58 55 } else {
59   - if (resultCode == Activity.RESULT_CANCELED) {
60   - output.putString("status", "blocked");
61   - } else if (resultCode == Activity.RESULT_OK) {
62   - output.putString("status", "granted");
63   - }
  56 + output.putString("status", "granted");
64 57 }
  58 + } else {
  59 + if (resultCode == Activity.RESULT_CANCELED) {
  60 + output.putString("status", "blocked");
  61 + } else if (resultCode == Activity.RESULT_OK) {
  62 + output.putString("status", "granted");
  63 + }
  64 + }
65 65  
66 66 // Log.e("TEAGGGG", "output: "+ output.toString());
67   - mPickerPromise.resolve(output);
68   - }
  67 + mPickerPromise.resolve(output);
69 68 }
70 69 }
71   - };
  70 + }
  71 + };
72 72  
73   - private static final String[][] PERMISSIONS = new String[][] {
  73 + private static final String[][] PERMISSIONS = new String[][] {
74 74 { "ACCEPT_HANDOVER", "android.permission.ACCEPT_HANDOVER" },
75 75 { "ACCESS_BACKGROUND_LOCATION", "android.permission.ACCESS_BACKGROUND_LOCATION" },
76 76 { "ACCESS_COARSE_LOCATION", "android.permission.ACCESS_COARSE_LOCATION" },
... ... @@ -209,7 +209,7 @@ public class RNPermissionsModule extends ReactContextBaseJavaModule {
209 209 //
210 210 //https://developer.android.com/preview/behavior-changes-all#manage_overlay
211 211 @ReactMethod
212   - public void requestOverlayPermission(final Promise promise) {
  212 + public void checkOrRequestOverlayPermission(final Promise promise) {
213 213 // Check if Android M or higher
214 214 final ReactApplicationContext reactContext = getReactApplicationContext();
215 215 if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M && !Settings.canDrawOverlays(reactContext)) {
... ... @@ -13,8 +13,6 @@ export interface Contract {
13 13  
14 14 checkOverlayPermission(): Promise<boolean>;
15 15  
16   - requestOverlayPermission(): Promise<PermissionStatus>;
17   -
18 16 request(
19 17 permission: Permission,
20 18 rationale?: Rationale,
... ... @@ -15,7 +15,6 @@ export * from &#39;./types&#39;;
15 15 export const openSettings = module.openSettings;
16 16 export const check = module.check;
17 17 export const checkOverlayPermission = module.checkOverlayPermission;
18   -export const requestOverlayPermission = module.requestOverlayPermission;
19 18 export const request = module.request;
20 19 export const checkNotifications = module.checkNotifications;
21 20 export const requestNotifications = module.requestNotifications;
... ... @@ -28,7 +27,6 @@ export default {
28 27 openSettings,
29 28 check,
30 29 checkOverlayPermission,
31   - requestOverlayPermission,
32 30 request,
33 31 checkNotifications,
34 32 requestNotifications,
src/module.android.ts
... ... @@ -15,8 +15,8 @@ const RNP: {
15 15  
16 16 checkNotifications: () => Promise<NotificationsResponse>;
17 17 openSettings: () => Promise<true>;
  18 + checkOrRequestOverlayPermission: () => Promise<true>;
18 19 checkOverlayPermission: () => Promise<boolean>;
19   - requestOverlayPermission: () => Promise<PermissionStatus>;
20 20 getNonRequestables: () => Promise<Permission[]>;
21 21 isNonRequestable: (permission: Permission) => Promise<boolean>;
22 22 setNonRequestable: (permission: Permission) => Promise<true>;
... ... @@ -41,26 +41,9 @@ async function openSettings(): Promise&lt;void&gt; {
41 41 }
42 42  
43 43 async function check(permission: Permission): Promise<PermissionStatus> {
44   - if (!RNP.available.includes(permission)) {
45   - return RESULTS.UNAVAILABLE;
46   - }
47   -
48   - if (await Core.check(permission as CorePermission)) {
49   - return RESULTS.GRANTED;
50   - }
51   -
52   - return (await RNP.isNonRequestable(permission))
53   - ? RESULTS.BLOCKED
54   - : RESULTS.DENIED;
55   -}
56   -
57   -async function checkOverlayPermission(): Promise<boolean> {
58   - return await RNP.checkOverlayPermission();
59   -}
60   -
61   -async function requestOverlayPermission(): Promise<PermissionStatus> {
62   - console.warn("requestOverlayPermission")
63   - const output = await RNP.requestOverlayPermission()
  44 + if (permission == PERMISSIONS.ANDROID.MANAGE_OVERLAY_PERMISSION) {
  45 + console.warn("check MANAGE_OVERLAY_PERMISSION ts")
  46 + const output = await RNP.checkOrRequestOverlayPermission()
64 47 if (output.status == RESULTS.GRANTED) {
65 48 return RESULTS.GRANTED
66 49 } else if (output.status == RESULTS.BLOCKED) {
... ... @@ -68,6 +51,26 @@ async function requestOverlayPermission(): Promise&lt;PermissionStatus&gt; {
68 51 } else {
69 52 return RESULTS.UNAVAILABLE
70 53 }
  54 + // return (await RNP.checkOrRequestOverlayPermission())
  55 + // ? RESULTS.BLOCKED
  56 + // : RESULTS.DENIED;
  57 + } else {
  58 + if (!RNP.available.includes(permission)) {
  59 + return RESULTS.UNAVAILABLE;
  60 + }
  61 +
  62 + if (await Core.check(permission as CorePermission)) {
  63 + return RESULTS.GRANTED;
  64 + }
  65 +
  66 + return (await RNP.isNonRequestable(permission))
  67 + ? RESULTS.BLOCKED
  68 + : RESULTS.DENIED;
  69 + }
  70 +}
  71 +
  72 +async function checkOverlayPermission(): Promise<boolean> {
  73 + return await RNP.checkOverlayPermission();
71 74 }
72 75  
73 76 async function request(
... ... @@ -166,7 +169,6 @@ export const module: Contract = {
166 169 openSettings,
167 170 check,
168 171 checkOverlayPermission,
169   - requestOverlayPermission,
170 172 request,
171 173 checkNotifications,
172 174 requestNotifications: checkNotifications,
... ... @@ -32,14 +32,6 @@ async function check(permission: Permission): Promise&lt;PermissionStatus&gt; {
32 32 : RESULTS.UNAVAILABLE;
33 33 }
34 34  
35   -async function checkOverlayPermission(): Promise<boolean> {
36   - return true;
37   -}
38   -
39   -async function requestOverlayPermission(): Promise<PermissionStatus> {
40   - return RESULTS.UNAVAILABLE;
41   -}
42   -
43 35 async function request(permission: Permission): Promise<PermissionStatus> {
44 36 return RNP.available.includes(permission)
45 37 ? RNP.request(permission)
... ... @@ -50,6 +42,10 @@ export function checkNotifications(): Promise&lt;NotificationsResponse&gt; {
50 42 return RNP.checkNotifications();
51 43 }
52 44  
  45 +async function checkOverlayPermission(): Promise<boolean> {
  46 + return true;
  47 +}
  48 +
53 49 export function requestNotifications(
54 50 options: NotificationOption[],
55 51 ): Promise<NotificationsResponse> {
... ... @@ -93,7 +89,6 @@ export const module: Contract = {
93 89 openSettings,
94 90 check,
95 91 checkOverlayPermission,
96   - requestOverlayPermission,
97 92 request,
98 93 checkNotifications,
99 94 requestNotifications,
... ... @@ -10,10 +10,6 @@ async function checkOverlayPermission(): Promise&lt;boolean&gt; {
10 10 return true;
11 11 }
12 12  
13   -async function requestOverlayPermission(): Promise<PermissionStatus> {
14   - return RESULTS.UNAVAILABLE;
15   -}
16   -
17 13 async function checkNotifications(): Promise<NotificationsResponse> {
18 14 return {status: RESULTS.UNAVAILABLE, settings: {}};
19 15 }
... ... @@ -31,7 +27,6 @@ export const module: Contract = {
31 27 openSettings: Promise.reject,
32 28 check,
33 29 checkOverlayPermission,
34   - requestOverlayPermission,
35 30 request: check,
36 31 checkNotifications,
37 32 requestNotifications: checkNotifications,