@@ -517,37 +517,37 @@ public boolean hasPermission(String permission) {
517517
518518
519519 public void requestPermissions (String [] permissions ) {
520- int comp = component .getKind ();
521- if (comp == AppComponent .FRAGMENT ) {
522- // Requesting permissions from user when the app resumes.
523- // Nice example on how to handle user response
524- // http://stackoverflow.com/a/35495855
525- // More on permission in Android 23:
526- // https://inthecheesefactory.com/blog/things-you-need-to-know-about-android-m-permission-developer-edition/en
527- ActivityCompat .requestPermissions (activity , permissions , REQUEST_PERMISSIONS );
528- } else if (comp == AppComponent .WALLPAPER || comp == AppComponent .WATCHFACE ) {
520+ if (component .isService ()) {
529521 // https://developer.android.com/training/articles/wear-permissions.html
530522 // Inspired by PermissionHelper.java from Michael von Glasow:
531523 // https://github.com/mvglasow/satstat/blob/master/src/com/vonglasow/michael/satstat/utils/PermissionHelper.java
532524 // Example of use:
533525 // https://github.com/mvglasow/satstat/blob/master/src/com/vonglasow/michael/satstat/PasvLocListenerService.java
534526 final ServiceEngine eng = getEngine ();
535- ResultReceiver resultReceiver = new ResultReceiver (new Handler (Looper .getMainLooper ())) {
536- @ Override
537- protected void onReceiveResult (int resultCode , Bundle resultData ) {
538- String [] outPermissions = resultData .getStringArray (PermissionRequestor .KEY_PERMISSIONS );
539- int [] grantResults = resultData .getIntArray (PermissionRequestor .KEY_GRANT_RESULTS );
540- eng .onRequestPermissionsResult (resultCode , outPermissions , grantResults );
541- }
542- };
543- final Intent permIntent = new Intent (getContext (), PermissionRequestor .class );
544- permIntent .putExtra (PermissionRequestor .KEY_RESULT_RECEIVER , resultReceiver );
545- permIntent .putExtra (PermissionRequestor .KEY_PERMISSIONS , permissions );
546- permIntent .putExtra (PermissionRequestor .KEY_REQUEST_CODE , REQUEST_PERMISSIONS );
547- // Show the dialog requesting the permissions
548- permIntent .addFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
549-
550- startActivity (permIntent );
527+ if (eng != null ) { // A valid service should have a non-null engine at this point, but just in case
528+ ResultReceiver resultReceiver = new ResultReceiver (new Handler (Looper .getMainLooper ())) {
529+ @ Override
530+ protected void onReceiveResult (int resultCode , Bundle resultData ) {
531+ String [] outPermissions = resultData .getStringArray (PermissionRequestor .KEY_PERMISSIONS );
532+ int [] grantResults = resultData .getIntArray (PermissionRequestor .KEY_GRANT_RESULTS );
533+ eng .onRequestPermissionsResult (resultCode , outPermissions , grantResults );
534+ }
535+ };
536+ final Intent permIntent = new Intent (getContext (), PermissionRequestor .class );
537+ permIntent .putExtra (PermissionRequestor .KEY_RESULT_RECEIVER , resultReceiver );
538+ permIntent .putExtra (PermissionRequestor .KEY_PERMISSIONS , permissions );
539+ permIntent .putExtra (PermissionRequestor .KEY_REQUEST_CODE , REQUEST_PERMISSIONS );
540+ // Show the dialog requesting the permissions
541+ permIntent .addFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
542+ startActivity (permIntent );
543+ }
544+ } else if (activity != null ) {
545+ // Requesting permissions from user when the app resumes.
546+ // Nice example on how to handle user response
547+ // http://stackoverflow.com/a/35495855
548+ // More on permission in Android 23:
549+ // https://inthecheesefactory.com/blog/things-you-need-to-know-about-android-m-permission-developer-edition/en
550+ ActivityCompat .requestPermissions (activity , permissions , REQUEST_PERMISSIONS );
551551 }
552552 }
553553}
0 commit comments