@@ -66,6 +66,7 @@ public class RNCallKeepModule extends ReactContextBaseJavaModule {
66
66
67
67
private static final String E_ACTIVITY_DOES_NOT_EXIST = "E_ACTIVITY_DOES_NOT_EXIST" ;
68
68
private static final String REACT_NATIVE_MODULE_NAME = "RNCallKeep" ;
69
+ private static final String [] permissions = { Manifest .permission .READ_PHONE_STATE , Manifest .permission .CALL_PHONE };
69
70
70
71
private static TelecomManager telecomManager ;
71
72
private static Promise hasPhoneAccountPromise ;
@@ -120,7 +121,7 @@ public void displayIncomingCall(String number, String callerName) {
120
121
121
122
@ ReactMethod
122
123
public void startCall (String number , String callerName ) {
123
- if (!isConnectionServiceAvailable () || !hasPhoneAccount ()) {
124
+ if (!isConnectionServiceAvailable () || !hasPhoneAccount () || ! hasPermissions () ) {
124
125
return ;
125
126
}
126
127
@@ -154,18 +155,21 @@ public void endCall() {
154
155
155
156
@ ReactMethod
156
157
public void checkPhoneAccountPermission (Promise promise ) {
158
+ Activity currentActivity = this .getCurrentActivity ();
159
+
157
160
if (!isConnectionServiceAvailable ()) {
158
161
promise .reject (E_ACTIVITY_DOES_NOT_EXIST , "ConnectionService not available for this version of Android." );
159
162
return ;
160
163
}
161
- if (this . getCurrentActivity () == null ) {
164
+ if (currentActivity == null ) {
162
165
promise .reject (E_ACTIVITY_DOES_NOT_EXIST , "Activity doesn't exist" );
163
166
return ;
164
167
}
165
168
166
169
hasPhoneAccountPromise = promise ;
167
- String [] permissions = { Manifest .permission .READ_PHONE_STATE , Manifest .permission .CALL_PHONE };
168
- if (!this .checkPermissions (permissions , REQUEST_READ_PHONE_STATE )) {
170
+
171
+ if (!this .hasPermissions ()) {
172
+ ActivityCompat .requestPermissions (currentActivity , permissions , REQUEST_READ_PHONE_STATE );
169
173
return ;
170
174
}
171
175
@@ -177,6 +181,11 @@ public void hasPhoneAccount(Promise promise) {
177
181
promise .resolve (hasPhoneAccount ());
178
182
}
179
183
184
+ @ ReactMethod
185
+ public void hasPermissions (Promise promise ) {
186
+ promise .resolve (this .hasPermissions ());
187
+ }
188
+
180
189
@ ReactMethod
181
190
public void setAvailable (Boolean active ) {
182
191
VoiceConnectionService .setAvailable (active );
@@ -248,7 +257,7 @@ private String getApplicationName(Context appContext) {
248
257
return stringId == 0 ? applicationInfo .nonLocalizedLabel .toString () : appContext .getString (stringId );
249
258
}
250
259
251
- private Boolean checkPermissions ( String [] permissions , int id ) {
260
+ private Boolean hasPermissions ( ) {
252
261
Activity currentActivity = this .getCurrentActivity ();
253
262
254
263
boolean hasPermissions = true ;
@@ -259,19 +268,11 @@ private Boolean checkPermissions(String[] permissions, int id) {
259
268
}
260
269
}
261
270
262
- if (!hasPermissions ) {
263
- ActivityCompat .requestPermissions (currentActivity , permissions , id );
264
- }
265
-
266
271
return hasPermissions ;
267
272
}
268
273
269
274
private static boolean hasPhoneAccount () {
270
- if (!isConnectionServiceAvailable ()) {
271
- return false ;
272
- }
273
-
274
- return telecomManager .getPhoneAccount (handle ).isEnabled ();
275
+ return !isConnectionServiceAvailable () ? false : telecomManager .getPhoneAccount (handle ).isEnabled ();
275
276
}
276
277
277
278
private void registerReceiver () {
0 commit comments