Browse Source

v2 proper permission handling (closes #3376) (#3385)

serjek 6 years ago
parent
commit
fdeb4d2c0d

+ 25
- 1
lib/android/app/src/main/java/com/reactnativenavigation/NavigationActivity.java View File

1
 package com.reactnativenavigation;
1
 package com.reactnativenavigation;
2
 
2
 
3
+import android.annotation.TargetApi;
4
+import android.support.annotation.NonNull;
5
+
3
 import android.content.Intent;
6
 import android.content.Intent;
4
 import android.os.Bundle;
7
 import android.os.Bundle;
8
+import android.os.Build;
5
 import android.support.annotation.Nullable;
9
 import android.support.annotation.Nullable;
6
 import android.support.v7.app.AppCompatActivity;
10
 import android.support.v7.app.AppCompatActivity;
7
 import android.view.KeyEvent;
11
 import android.view.KeyEvent;
14
 import com.reactnativenavigation.viewcontrollers.ChildControllersRegistry;
18
 import com.reactnativenavigation.viewcontrollers.ChildControllersRegistry;
15
 import com.reactnativenavigation.viewcontrollers.Navigator;
19
 import com.reactnativenavigation.viewcontrollers.Navigator;
16
 
20
 
17
-public class NavigationActivity extends AppCompatActivity implements DefaultHardwareBackBtnHandler {
21
+import com.facebook.react.modules.core.PermissionAwareActivity;
22
+import com.facebook.react.modules.core.PermissionListener;
23
+
24
+public class NavigationActivity extends AppCompatActivity implements DefaultHardwareBackBtnHandler, PermissionAwareActivity {
25
+    @Nullable
26
+    private PermissionListener mPermissionListener;
27
+    
18
     protected Navigator navigator;
28
     protected Navigator navigator;
19
 
29
 
20
     @Override
30
     @Override
81
     public Navigator getNavigator() {
91
     public Navigator getNavigator() {
82
         return navigator;
92
         return navigator;
83
     }
93
     }
94
+
95
+    @TargetApi(Build.VERSION_CODES.M)
96
+    public void requestPermissions(String[] permissions, int requestCode, PermissionListener listener) {
97
+        mPermissionListener = listener;
98
+        requestPermissions(permissions, requestCode);
99
+    }
100
+
101
+    @Override
102
+    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
103
+        NavigationApplication.instance.onRequestPermissionsResult(requestCode, permissions, grantResults);
104
+        if (mPermissionListener != null && mPermissionListener.onRequestPermissionsResult(requestCode, permissions, grantResults)) {
105
+            mPermissionListener = null;
106
+        }
107
+    }
84
 }
108
 }

+ 8
- 0
lib/android/app/src/main/java/com/reactnativenavigation/NavigationApplication.java View File

2
 
2
 
3
 import android.app.Application;
3
 import android.app.Application;
4
 import android.support.annotation.Nullable;
4
 import android.support.annotation.Nullable;
5
+import android.support.annotation.NonNull;
5
 
6
 
6
 import com.facebook.react.ReactApplication;
7
 import com.facebook.react.ReactApplication;
7
 import com.facebook.react.ReactNativeHost;
8
 import com.facebook.react.ReactNativeHost;
53
 		return getReactGateway().getReactNativeHost();
54
 		return getReactGateway().getReactNativeHost();
54
 	}
55
 	}
55
 
56
 
57
+    /**
58
+     * Generally no need to override this; override for custom permission handling.
59
+     */
60
+    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
61
+
62
+    }
63
+
56
 	public abstract boolean isDebug();
64
 	public abstract boolean isDebug();
57
 
65
 
58
     /**
66
     /**