Skip to content

Commit

Permalink
(android): For new runtime permissions added in API 33, consider them…
Browse files Browse the repository at this point in the history
… implicitly granted if the build SDK >= 33 and device runtime is < 33.

Resolves #493
  • Loading branch information
dpa99c committed Sep 29, 2023
1 parent f7d7e05 commit ec97085
Showing 1 changed file with 51 additions and 11 deletions.
62 changes: 51 additions & 11 deletions src/android/Diagnostic.java
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ protected JSONObject _getPermissionsAuthorizationStatus(String[] permissions) th
String androidPermission = permissionsMap.get(permission);
Log.v(TAG, "Get authorisation status for "+androidPermission);
boolean granted = hasRuntimePermission(androidPermission);
if(granted){
if(granted || isPermissionImplicitlyGranted(permission)){
statuses.put(permission, Diagnostic.STATUS_GRANTED);
}else{
boolean showRationale = shouldShowRequestPermissionRationale(this.cordova.getActivity(), androidPermission);
Expand All @@ -588,7 +588,7 @@ protected void _requestRuntimePermissions(JSONArray permissions, int requestId)
for(int i = 0; i<currentPermissionsStatuses.names().length(); i++){
String permission = currentPermissionsStatuses.names().getString(i);
boolean granted = currentPermissionsStatuses.getString(permission) == Diagnostic.STATUS_GRANTED;
if(granted){
if(granted || isPermissionImplicitlyGranted(permission)){
Log.d(TAG, "Permission already granted for "+permission);
JSONObject requestStatuses = permissionStatuses.get(String.valueOf(requestId));
requestStatuses.put(permission, Diagnostic.STATUS_GRANTED);
Expand All @@ -609,6 +609,26 @@ protected void _requestRuntimePermissions(JSONArray permissions, int requestId)
}
}

protected boolean isPermissionImplicitlyGranted(String permission) throws Exception{
boolean isImplicitlyGranted = false;
int buildTargetSdkVersion = getBuildTargetSdkVersion();
int deviceRuntimeSdkVersion = getDeviceRuntimeSdkVersion();

if(buildTargetSdkVersion >= 33 && deviceRuntimeSdkVersion < 33 && (
permission.equals("ACCESS_BACKGROUND_LOCATION") ||
permission.equals("POST_NOTIFICATIONS") ||
permission.equals("READ_MEDIA_AUDIO") ||
permission.equals("READ_MEDIA_IMAGES") ||
permission.equals("READ_MEDIA_VIDEO") ||
permission.equals("BODY_SENSORS_BACKGROUND") ||
permission.equals("NEARBY_WIFI_DEVICES")

)) {
isImplicitlyGranted = true;
}
return isImplicitlyGranted;
}

protected void sendRuntimeRequestResult(int requestId){
String sRequestId = String.valueOf(requestId);
CallbackContext context = callbackContexts.get(sRequestId);
Expand Down Expand Up @@ -863,31 +883,51 @@ public boolean isAirplaneModeEnabled() {
public JSONObject getDeviceOSVersion() throws Exception{
JSONObject details = new JSONObject();
details.put("version", Build.VERSION.RELEASE);
details.put("apiLevel", Build.VERSION.SDK_INT);
details.put("apiName", getNameForApiLevel(Build.VERSION.SDK_INT));
int buildVersion = getDeviceRuntimeSdkVersion();
details.put("apiLevel", buildVersion);
details.put("apiName", getNameForApiLevel(buildVersion));
return details;
}

protected int getDeviceRuntimeSdkVersion() {
return Build.VERSION.SDK_INT;
}

public JSONObject getBuildOSVersion() throws Exception{
JSONObject details = new JSONObject();
int targetVersion = getBuildTargetSdkVersion();
int minVersion = getBuildMinimumSdkVersion();

details.put("targetApiLevel", targetVersion);
details.put("targetApiName", getNameForApiLevel(targetVersion));
details.put("minApiLevel", minVersion);
details.put("minApiName", getNameForApiLevel(minVersion));
return details;
}

protected int getBuildTargetSdkVersion() throws Exception{
int targetVersion = 0;
int minVersion = 0;
Activity activity = instance.cordova.getActivity();
ApplicationInfo applicationInfo = activity.getPackageManager().getApplicationInfo(activity.getPackageName(), 0);
if (applicationInfo != null) {
targetVersion = applicationInfo.targetSdkVersion;
}
return targetVersion;
}

protected int getBuildMinimumSdkVersion() throws Exception{
int minVersion = 0;
Activity activity = instance.cordova.getActivity();
ApplicationInfo applicationInfo = activity.getPackageManager().getApplicationInfo(activity.getPackageName(), 0);
if (applicationInfo != null) {
if(Build.VERSION.SDK_INT >= 24){
minVersion = applicationInfo.minSdkVersion;
}
}

details.put("targetApiLevel", targetVersion);
details.put("targetApiName", getNameForApiLevel(targetVersion));
details.put("minApiLevel", minVersion);
details.put("minApiName", getNameForApiLevel(minVersion));
return details;
return minVersion;
}


// https://stackoverflow.com/a/55946200/777265
protected String getNameForApiLevel(int apiLevel) throws Exception{
Field[] fields = Build.VERSION_CODES.class.getFields();
Expand Down

0 comments on commit ec97085

Please sign in to comment.