@@ -66,6 +66,7 @@ public class RNCallKeepModule extends ReactContextBaseJavaModule {
6666
6767 private static final String E_ACTIVITY_DOES_NOT_EXIST = "E_ACTIVITY_DOES_NOT_EXIST" ;
6868 private static final String REACT_NATIVE_MODULE_NAME = "RNCallKeep" ;
69+ private static final String [] permissions = { Manifest .permission .READ_PHONE_STATE , Manifest .permission .CALL_PHONE };
6970
7071 private static TelecomManager telecomManager ;
7172 private static Promise hasPhoneAccountPromise ;
@@ -120,7 +121,7 @@ public void displayIncomingCall(String number, String callerName) {
120121
121122 @ ReactMethod
122123 public void startCall (String number , String callerName ) {
123- if (!isConnectionServiceAvailable () || !hasPhoneAccount ()) {
124+ if (!isConnectionServiceAvailable () || !hasPhoneAccount () || ! hasPermissions () ) {
124125 return ;
125126 }
126127
@@ -154,18 +155,21 @@ public void endCall() {
154155
155156 @ ReactMethod
156157 public void checkPhoneAccountPermission (Promise promise ) {
158+ Activity currentActivity = this .getCurrentActivity ();
159+
157160 if (!isConnectionServiceAvailable ()) {
158161 promise .reject (E_ACTIVITY_DOES_NOT_EXIST , "ConnectionService not available for this version of Android." );
159162 return ;
160163 }
161- if (this . getCurrentActivity () == null ) {
164+ if (currentActivity == null ) {
162165 promise .reject (E_ACTIVITY_DOES_NOT_EXIST , "Activity doesn't exist" );
163166 return ;
164167 }
165168
166169 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 );
169173 return ;
170174 }
171175
@@ -177,6 +181,11 @@ public void hasPhoneAccount(Promise promise) {
177181 promise .resolve (hasPhoneAccount ());
178182 }
179183
184+ @ ReactMethod
185+ public void hasPermissions (Promise promise ) {
186+ promise .resolve (this .hasPermissions ());
187+ }
188+
180189 @ ReactMethod
181190 public void setAvailable (Boolean active ) {
182191 VoiceConnectionService .setAvailable (active );
@@ -248,7 +257,7 @@ private String getApplicationName(Context appContext) {
248257 return stringId == 0 ? applicationInfo .nonLocalizedLabel .toString () : appContext .getString (stringId );
249258 }
250259
251- private Boolean checkPermissions ( String [] permissions , int id ) {
260+ private Boolean hasPermissions ( ) {
252261 Activity currentActivity = this .getCurrentActivity ();
253262
254263 boolean hasPermissions = true ;
@@ -259,19 +268,11 @@ private Boolean checkPermissions(String[] permissions, int id) {
259268 }
260269 }
261270
262- if (!hasPermissions ) {
263- ActivityCompat .requestPermissions (currentActivity , permissions , id );
264- }
265-
266271 return hasPermissions ;
267272 }
268273
269274 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 ();
275276 }
276277
277278 private void registerReceiver () {
0 commit comments