Skip to content

Commit

Permalink
Fix permission denied error when starting collector on Android 13.
Browse files Browse the repository at this point in the history
Closes #242
  • Loading branch information
zamojski committed Dec 2, 2024
1 parent 0795220 commit 8f05c31
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -881,14 +881,24 @@ private void startCollectorServiceWithCheck() {
return;
}
if (PermissionUtils.isNotificationPermissionRequired()) {
MainActivityPermissionsDispatcher.startCollectorServiceApi33WithPermissionCheck(MainActivity.this);
if (PermissionUtils.isForegroundServicePermissionAware()) {
MainActivityPermissionsDispatcher.startCollectorServiceApi34WithPermissionCheck(MainActivity.this);
} else {
MainActivityPermissionsDispatcher.startCollectorServiceApi33WithPermissionCheck(MainActivity.this);
}
} else {
MainActivityPermissionsDispatcher.startCollectorServiceWithPermissionCheck(MainActivity.this);
}
}

@TargetApi(Build.VERSION_CODES.TIRAMISU)
@TargetApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
@NeedsPermission({Manifest.permission.POST_NOTIFICATIONS, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.FOREGROUND_SERVICE_LOCATION, Manifest.permission.READ_PHONE_STATE})
void startCollectorServiceApi34() {
startCollectorServiceInternal();
}

@TargetApi(Build.VERSION_CODES.TIRAMISU)
@NeedsPermission({Manifest.permission.POST_NOTIFICATIONS, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.READ_PHONE_STATE})
void startCollectorServiceApi33() {
startCollectorServiceInternal();
}
Expand Down Expand Up @@ -920,8 +930,14 @@ private void startCollectorServiceInternal() {
}
}

@TargetApi(Build.VERSION_CODES.TIRAMISU)
@TargetApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
@OnShowRationale({Manifest.permission.POST_NOTIFICATIONS, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.FOREGROUND_SERVICE_LOCATION, Manifest.permission.READ_PHONE_STATE})
void onStartCollectorShowRationaleApi34(PermissionRequest request) {
onStartCollectorShowRationaleInternal(request);
}

@TargetApi(Build.VERSION_CODES.TIRAMISU)
@OnShowRationale({Manifest.permission.POST_NOTIFICATIONS, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.READ_PHONE_STATE})
void onStartCollectorShowRationaleApi33(PermissionRequest request) {
onStartCollectorShowRationaleInternal(request);
}
Expand All @@ -935,8 +951,14 @@ private void onStartCollectorShowRationaleInternal(PermissionRequest request) {
onShowRationale(request, GpsUtils.isPreciseLocationAware() ? R.string.permission_collector_rationale_api31_message : R.string.permission_collector_rationale_message);
}

@TargetApi(Build.VERSION_CODES.TIRAMISU)
@TargetApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
@OnPermissionDenied({Manifest.permission.POST_NOTIFICATIONS, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.FOREGROUND_SERVICE_LOCATION, Manifest.permission.READ_PHONE_STATE})
void onStartCollectorPermissionDeniedApi34() {
onStartCollectorPermissionDeniedInternal();
}

@TargetApi(Build.VERSION_CODES.TIRAMISU)
@OnPermissionDenied({Manifest.permission.POST_NOTIFICATIONS, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.READ_PHONE_STATE})
void onStartCollectorPermissionDeniedApi33() {
onStartCollectorPermissionDeniedInternal();
}
Expand All @@ -950,8 +972,14 @@ private void onStartCollectorPermissionDeniedInternal() {
onPermissionDenied(R.string.permission_collector_denied_message);
}

@TargetApi(Build.VERSION_CODES.TIRAMISU)
@TargetApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
@OnNeverAskAgain({Manifest.permission.POST_NOTIFICATIONS, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.FOREGROUND_SERVICE_LOCATION, Manifest.permission.READ_PHONE_STATE})
void onStartCollectorNeverAskAgainApi34() {
onStartCollectorNeverAskAgainInternal();
}

@TargetApi(Build.VERSION_CODES.TIRAMISU)
@OnNeverAskAgain({Manifest.permission.POST_NOTIFICATIONS, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.READ_PHONE_STATE})
void onStartCollectorNeverAskAgainApi33() {
onStartCollectorNeverAskAgainInternal();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static boolean isNotificationPermissionRequired() {
}

public static boolean isForegroundServicePermissionAware() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q; // TODO: should be Build.VERSION_CODES.UPSIDE_DOWN_CAKE but I recall there were issues on Android 12 as well
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE;
}

public static String getAppPermissions() {
Expand Down

0 comments on commit 8f05c31

Please sign in to comment.