Request permission to show notifications

This is needed since API 33, because we show a "Vault unlocked"
notification if the user has enabled encryption and has unlocked the
vault.
This commit is contained in:
Alexander Bakker 2022-09-14 19:29:36 +02:00
parent 642864fca1
commit 1e3ceefeec
7 changed files with 53 additions and 2 deletions

View file

@ -1,5 +1,6 @@
package com.beemdevelopment.aegis.ui;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
@ -30,6 +31,7 @@ import com.beemdevelopment.aegis.crypto.MasterKey;
import com.beemdevelopment.aegis.helpers.BiometricsHelper;
import com.beemdevelopment.aegis.helpers.EditTextHelper;
import com.beemdevelopment.aegis.helpers.MetricsHelper;
import com.beemdevelopment.aegis.helpers.PermissionHelper;
import com.beemdevelopment.aegis.helpers.UiThreadExecutor;
import com.beemdevelopment.aegis.ui.dialogs.Dialogs;
import com.beemdevelopment.aegis.ui.tasks.PasswordSlotDecryptTask;
@ -49,6 +51,9 @@ import javax.crypto.Cipher;
import javax.crypto.SecretKey;
public class AuthActivity extends AegisActivity {
// Permission request codes
private static final int CODE_PERM_NOTIFICATIONS = 0;
private EditText _textPassword;
private SlotList _slots;
@ -85,6 +90,13 @@ public class AuthActivity extends AegisActivity {
Intent intent = getIntent();
if (savedInstanceState == null) {
_inhibitBioPrompt = intent.getBooleanExtra("inhibitBioPrompt", false);
// A persistent notification is shown to let the user know that the vault is unlocked. Permission
// to do so is required since API 33, so for existing users, we have to request permission here
// in order to be able to show the notification after unlock.
if (Build.VERSION.SDK_INT >= 33) {
PermissionHelper.request(this, CODE_PERM_NOTIFICATIONS, Manifest.permission.POST_NOTIFICATIONS);
}
} else {
_inhibitBioPrompt = savedInstanceState.getBoolean("inhibitBioPrompt", false);
}

View file

@ -5,12 +5,18 @@ import static com.beemdevelopment.aegis.ui.slides.SecurityPickerSlide.CRYPT_TYPE
import static com.beemdevelopment.aegis.ui.slides.SecurityPickerSlide.CRYPT_TYPE_NONE;
import static com.beemdevelopment.aegis.ui.slides.SecurityPickerSlide.CRYPT_TYPE_PASS;
import android.Manifest;
import android.os.Build;
import android.os.Bundle;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.beemdevelopment.aegis.R;
import com.beemdevelopment.aegis.ThemeMap;
import com.beemdevelopment.aegis.helpers.PermissionHelper;
import com.beemdevelopment.aegis.ui.dialogs.Dialogs;
import com.beemdevelopment.aegis.ui.intro.IntroBaseActivity;
import com.beemdevelopment.aegis.ui.intro.SlideFragment;
@ -24,6 +30,9 @@ import com.beemdevelopment.aegis.vault.slots.BiometricSlot;
import com.beemdevelopment.aegis.vault.slots.PasswordSlot;
public class IntroActivity extends IntroBaseActivity {
// Permission request codes
private static final int CODE_PERM_NOTIFICATIONS = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -40,7 +49,7 @@ public class IntroActivity extends IntroBaseActivity {
}
@Override
protected boolean onBeforeSlideChanged(Class<? extends SlideFragment> oldSlide, Class<? extends SlideFragment> newSlide) {
protected boolean onBeforeSlideChanged(Class<? extends SlideFragment> oldSlide, @NonNull Class<? extends SlideFragment> newSlide) {
// hide the keyboard before every slide change
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(findViewById(android.R.id.content).getWindowToken(), 0);
@ -67,6 +76,17 @@ public class IntroActivity extends IntroBaseActivity {
return false;
}
@Override
protected void onAfterSlideChanged(@Nullable Class<? extends SlideFragment> oldSlide, @NonNull Class<? extends SlideFragment> newSlide) {
// If the user has enabled encryption, we need to request permission to show notifications
// in order to be able to show the "Vault unlocked" notification.
if (newSlide == DoneSlide.class && getState().getSerializable("creds") != null) {
if (Build.VERSION.SDK_INT >= 33) {
PermissionHelper.request(this, CODE_PERM_NOTIFICATIONS, Manifest.permission.POST_NOTIFICATIONS);
}
}
}
@Override
protected void onDonePressed() {
Bundle state = getState();

View file

@ -1,5 +1,6 @@
package com.beemdevelopment.aegis.vault;
import android.Manifest;
import android.app.Activity;
import android.app.backup.BackupManager;
import android.content.ActivityNotFoundException;
@ -14,6 +15,7 @@ import com.beemdevelopment.aegis.Preferences;
import com.beemdevelopment.aegis.R;
import com.beemdevelopment.aegis.crypto.KeyStoreHandle;
import com.beemdevelopment.aegis.crypto.KeyStoreHandleException;
import com.beemdevelopment.aegis.helpers.PermissionHelper;
import com.beemdevelopment.aegis.services.NotificationService;
import com.beemdevelopment.aegis.ui.dialogs.Dialogs;
@ -350,7 +352,9 @@ public class VaultManager {
}
private void startNotificationService() {
_context.startService(getNotificationServiceIntent());
if (PermissionHelper.granted(_context, Manifest.permission.POST_NOTIFICATIONS)) {
_context.startService(getNotificationServiceIntent());
}
}
private void stopNotificationService() {