Skip to content

Commit

Permalink
[Refactor] Make notification permission optional
Browse files Browse the repository at this point in the history
Although denying notification permission will render many features useless, it
is made optional for those who insists on denying it. However, the permission
shall still be asked everytime the app is launched until it times out.

Signed-off-by: Muntashir Al-Islam <muntashirakon@riseup.net>
  • Loading branch information
MuntashirAkon committed Jul 6, 2024
1 parent 00990dd commit 93ce56c
Showing 1 changed file with 21 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import androidx.lifecycle.ViewModelProvider;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.github.muntashirakon.AppManager.crypto.ks.KeyStoreActivity;
import io.github.muntashirakon.AppManager.crypto.ks.KeyStoreManager;
Expand All @@ -39,16 +41,12 @@
public abstract class BaseActivity extends AppCompatActivity {
public static final String TAG = BaseActivity.class.getSimpleName();

private static final String[] REQUIRED_PERMISSIONS;

static {
REQUIRED_PERMISSIONS = new ArrayList<String>() {{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
add(Manifest.permission.POST_NOTIFICATIONS);
}
}}.toArray(new String[0]);

}
private static final HashMap<String, Boolean> ASKED_PERMISSIONS = new HashMap<String, Boolean>() {{
// (permission, required) pairs
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
put(Manifest.permission.POST_NOTIFICATIONS, false);
}
}};

@Nullable
private AlertDialog mAlertDialog;
Expand Down Expand Up @@ -77,7 +75,7 @@ public abstract class BaseActivity extends AppCompatActivity {
if (permissionStatusMap == null) {
return;
}
initPermissionChecks();
initPermissionChecks(false);
});

@Override
Expand All @@ -87,7 +85,7 @@ protected final void onCreate(@Nullable Bundle savedInstanceState) {
if (Ops.isAuthenticated()) {
Log.d(TAG, "Already authenticated.");
onAuthenticated(savedInstanceState);
initPermissionChecks();
initPermissionChecks(false);
return;
}
if (Boolean.TRUE.equals(BuildExpiryChecker.buildExpired())) {
Expand Down Expand Up @@ -136,7 +134,7 @@ protected final void onCreate(@Nullable Bundle savedInstanceState) {
if (mAlertDialog != null) mAlertDialog.dismiss();
Ops.setAuthenticated(this, true);
onAuthenticated(savedInstanceState);
initPermissionChecks();
initPermissionChecks(true);
InternalCacheCleanerService.scheduleAlarm(getApplicationContext());
}
});
Expand Down Expand Up @@ -243,12 +241,17 @@ private void handleMigrationAndModeOfOp() {
}
}

private void initPermissionChecks() {
for (String permission : REQUIRED_PERMISSIONS) {
if (!SelfPermissions.checkSelfPermission(permission)) {
mPermissionCheckActivity.launch(REQUIRED_PERMISSIONS);
return;
private void initPermissionChecks(boolean checkAll) {
List<String> permissionsToBeAsked = new ArrayList<>(ASKED_PERMISSIONS.size());
for (String permission : ASKED_PERMISSIONS.keySet()) {
boolean required = Boolean.TRUE.equals(ASKED_PERMISSIONS.get(permission));
if (!SelfPermissions.checkSelfPermission(permission) && (required || checkAll)) {
permissionsToBeAsked.add(permission);
}
}
if (!permissionsToBeAsked.isEmpty()) {
// Ask required permissions
mPermissionCheckActivity.launch(permissionsToBeAsked.toArray(new String[0]));
}
}
}

0 comments on commit 93ce56c

Please sign in to comment.