From 7ad9cecc738a8bcfa1f3ef221c32fbb518d77e18 Mon Sep 17 00:00:00 2001 From: xynngh Date: Wed, 6 May 2020 16:25:06 +0400 Subject: [PATCH] Improve permissions handling Don't ask permissions for disabled functions. Show detailed message. --- .../yetanothercallblocker/MainActivity.java | 26 ++++-- .../PermissionHelper.java | 86 +++++++++++++++---- app/src/main/res/values/strings.xml | 4 +- 3 files changed, 88 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/dummydomain/yetanothercallblocker/MainActivity.java b/app/src/main/java/dummydomain/yetanothercallblocker/MainActivity.java index ae3f91f..5dd8586 100644 --- a/app/src/main/java/dummydomain/yetanothercallblocker/MainActivity.java +++ b/app/src/main/java/dummydomain/yetanothercallblocker/MainActivity.java @@ -34,6 +34,8 @@ import dummydomain.yetanothercallblocker.work.UpdateScheduler; public class MainActivity extends AppCompatActivity { + private final Settings settings = App.getSettings(); + private CallLogItemRecyclerViewAdapter callLogAdapter; private List callLogItems = new ArrayList<>(); @@ -49,17 +51,19 @@ public class MainActivity extends AppCompatActivity { RecyclerView recyclerView = findViewById(R.id.callLogList); recyclerView.setAdapter(callLogAdapter); - Settings settings = App.getSettings(); - SwitchCompat notificationsSwitch = findViewById(R.id.notificationsEnabledSwitch); notificationsSwitch.setChecked(settings.getIncomingCallNotifications()); - notificationsSwitch.setOnCheckedChangeListener((buttonView, isChecked) - -> settings.setIncomingCallNotifications(isChecked)); + notificationsSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> { + settings.setIncomingCallNotifications(isChecked); + checkPermissions(); + }); SwitchCompat blockCallsSwitch = findViewById(R.id.blockCallsSwitch); blockCallsSwitch.setChecked(settings.getBlockCalls()); - blockCallsSwitch.setOnCheckedChangeListener((buttonView, isChecked) - -> settings.setBlockCalls(isChecked)); + blockCallsSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> { + settings.setBlockCalls(isChecked); + checkPermissions(); + }); UpdateScheduler updateScheduler = UpdateScheduler.get(this); SwitchCompat autoUpdateSwitch = findViewById(R.id.autoUpdateEnabledSwitch); @@ -81,7 +85,8 @@ public class MainActivity extends AppCompatActivity { @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); - PermissionHelper.onRequestPermissionsResult(this, requestCode, permissions, grantResults); + PermissionHelper.onRequestPermissionsResult(this, requestCode, permissions, grantResults, + settings.getIncomingCallNotifications(), settings.getBlockCalls()); loadCallLog(); } @@ -94,7 +99,7 @@ public class MainActivity extends AppCompatActivity { startCheckMainDbTask(); - PermissionHelper.checkPermissions(this); + checkPermissions(); loadCallLog(); } @@ -124,6 +129,11 @@ public class MainActivity extends AppCompatActivity { loadCallLog(); } + private void checkPermissions() { + PermissionHelper.checkPermissions(this, + settings.getIncomingCallNotifications(), settings.getBlockCalls()); + } + private void startCheckMainDbTask() { cancelCheckMainDbTask(); @SuppressLint("StaticFieldLeak") diff --git a/app/src/main/java/dummydomain/yetanothercallblocker/PermissionHelper.java b/app/src/main/java/dummydomain/yetanothercallblocker/PermissionHelper.java index 64c06c0..a5863df 100644 --- a/app/src/main/java/dummydomain/yetanothercallblocker/PermissionHelper.java +++ b/app/src/main/java/dummydomain/yetanothercallblocker/PermissionHelper.java @@ -11,28 +11,48 @@ import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; import java.util.List; +import java.util.Set; public class PermissionHelper { private static final int PERMISSION_REQUEST_CODE = 1; - public static void checkPermissions(AppCompatActivity activity) { - List requiredPermissions = new ArrayList<>(); - requiredPermissions.add(Manifest.permission.READ_PHONE_STATE); - requiredPermissions.add(Manifest.permission.CALL_PHONE); + private static final Logger LOG = LoggerFactory.getLogger(PermissionHelper.class); + + private static final Set INFO_PERMISSIONS = new HashSet<>(); + private static final Set BLOCKING_PERMISSIONS = new HashSet<>(); + + static { + INFO_PERMISSIONS.add(Manifest.permission.READ_PHONE_STATE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { - requiredPermissions.add(Manifest.permission.READ_CALL_LOG); - } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { - requiredPermissions.add(Manifest.permission.ANSWER_PHONE_CALLS); + INFO_PERMISSIONS.add(Manifest.permission.READ_CALL_LOG); } - List missingPermissions = new ArrayList<>(); + BLOCKING_PERMISSIONS.addAll(INFO_PERMISSIONS); + + BLOCKING_PERMISSIONS.add(Manifest.permission.CALL_PHONE); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + BLOCKING_PERMISSIONS.add(Manifest.permission.ANSWER_PHONE_CALLS); + } + } + + public static void checkPermissions(AppCompatActivity activity, boolean info, boolean block) { + Collection requiredPermissions = block ? BLOCKING_PERMISSIONS + : info ? INFO_PERMISSIONS : Collections.emptyList(); + + List missingPermissions = new ArrayList<>(requiredPermissions.size()); for (String permission : requiredPermissions) { - if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) { + if (ContextCompat.checkSelfPermission(activity, permission) + != PackageManager.PERMISSION_GRANTED) { missingPermissions.add(permission); } } @@ -45,22 +65,50 @@ public class PermissionHelper { public static void onRequestPermissionsResult(@NonNull Context context, int requestCode, @NonNull String[] permissions, - @NonNull int[] grantResults) { - boolean denied = false; + @NonNull int[] grantResults, + boolean infoExpected, boolean blockingExpected) { + if (requestCode != PERMISSION_REQUEST_CODE) return; + + boolean infoDenied = false; + boolean blockingDenied = false; if (permissions.length == 0) { - denied = true; + infoDenied = true; + blockingDenied = true; } else { - for (int grantResult : grantResults) { - if (grantResult != PackageManager.PERMISSION_GRANTED) { - denied = true; - break; + for (int i = 0; i < permissions.length; i++) { + String permission = permissions[i]; + if (grantResults[i] != PackageManager.PERMISSION_GRANTED) { + if (INFO_PERMISSIONS.contains(permission)) { + infoDenied = true; + blockingDenied = true; + break; + } else if (BLOCKING_PERMISSIONS.contains(permission)) { + blockingDenied = true; + } } } } - if (denied) { - Toast.makeText(context, R.string.denied_permissions_message, Toast.LENGTH_SHORT).show(); + LOG.debug("onRequestPermissionsResult() infoDenied={}, blockingDenied={}", + infoDenied, blockingDenied); + + if (!blockingExpected) blockingDenied = false; + if (!infoExpected) infoDenied = false; + + String message; + if (infoDenied && blockingDenied) { + message = context.getString(R.string.denied_permissions_message_blocking_and_info); + } else if (infoDenied) { + message = context.getString(R.string.denied_permissions_message_info); + } else if (blockingDenied) { + message = context.getString(R.string.denied_permissions_message_blocking); + } else { + message = null; + } + + if (message != null) { + Toast.makeText(context, message, Toast.LENGTH_LONG).show(); } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0c67ea4..c34e461 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -50,7 +50,9 @@ Reviews Loading reviews… - Some functionality may not work due to denied permissions + Blocking and notification functionality may not work due to denied permissions + Notification functionality may not work due to denied permissions + Blocking functionality may not work due to denied permissions General settings