Improve permissions handling
Don't ask permissions for disabled functions. Show detailed message.
This commit is contained in:
parent
ace3f05ed7
commit
7ad9cecc73
|
@ -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<CallLogItem> 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")
|
||||
|
|
|
@ -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<String> 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<String> INFO_PERMISSIONS = new HashSet<>();
|
||||
private static final Set<String> 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<String> 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<String> requiredPermissions = block ? BLOCKING_PERMISSIONS
|
||||
: info ? INFO_PERMISSIONS : Collections.emptyList();
|
||||
|
||||
List<String> 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();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -50,7 +50,9 @@
|
|||
<string name="title_activity_reviews">Reviews</string>
|
||||
<string name="reviews_loading">Loading reviews…</string>
|
||||
|
||||
<string name="denied_permissions_message">Some functionality may not work due to denied permissions</string>
|
||||
<string name="denied_permissions_message_blocking_and_info">Blocking and notification functionality may not work due to denied permissions</string>
|
||||
<string name="denied_permissions_message_info">Notification functionality may not work due to denied permissions</string>
|
||||
<string name="denied_permissions_message_blocking">Blocking functionality may not work due to denied permissions</string>
|
||||
|
||||
<string name="general_settings">General settings</string>
|
||||
|
||||
|
|
Loading…
Reference in New Issue