Android security implementation Phase 0-3 finalization part 1
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
package studio.daybreak.mail;
|
||||
|
||||
package studio.daybreak.mail;
|
||||
|
||||
import android.Manifest;
|
||||
import android.app.KeyguardManager;
|
||||
import android.content.Context;
|
||||
@@ -9,17 +9,25 @@ import android.graphics.Color;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.os.SystemClock;
|
||||
import android.view.Gravity;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.WindowManager;
|
||||
import android.webkit.WebView;
|
||||
import android.webkit.WebStorage;
|
||||
import android.widget.Button;
|
||||
import android.widget.FrameLayout;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import androidx.biometric.BiometricManager;
|
||||
import androidx.biometric.BiometricPrompt;
|
||||
import com.getcapacitor.BridgeActivity;
|
||||
import javax.crypto.Cipher;
|
||||
|
||||
public class MainActivity extends BridgeActivity {
|
||||
private static final int UNLOCK_REQUEST_CODE = 705;
|
||||
@@ -28,8 +36,18 @@ public class MainActivity extends BridgeActivity {
|
||||
private Button appLockButton;
|
||||
private boolean appLocked;
|
||||
private boolean awaitingUnlockResult;
|
||||
private boolean pgpAuthInProgress;
|
||||
private boolean pgpAuthPaused;
|
||||
private long pgpAuthPauseAt;
|
||||
private boolean activityResumed;
|
||||
private boolean vaultRecoveryRequired;
|
||||
private final Handler appLockHandler = new Handler(Looper.getMainLooper());
|
||||
private final Runnable delayedAppLock = () -> {
|
||||
if (!activityResumed && !vaultRecoveryRequired && isAppLockEnabled()) lockWebView();
|
||||
};
|
||||
private final Runnable lockAfterPgpAuth = () -> {
|
||||
if (!activityResumed && pgpAuthPaused && !vaultRecoveryRequired && isAppLockEnabled()) lockWebView();
|
||||
};
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
@@ -56,6 +74,10 @@ public class MainActivity extends BridgeActivity {
|
||||
super.onResume();
|
||||
if (vaultRecoveryRequired) return;
|
||||
activityResumed = true;
|
||||
appLockHandler.removeCallbacks(delayedAppLock);
|
||||
appLockHandler.removeCallbacks(lockAfterPgpAuth);
|
||||
pgpAuthPaused = false;
|
||||
if (!appLocked) getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
|
||||
if (isAppLockEnabled() && appLocked) {
|
||||
lockWebView();
|
||||
configureUnlockButton();
|
||||
@@ -65,7 +87,19 @@ public class MainActivity extends BridgeActivity {
|
||||
@Override
|
||||
public void onPause() {
|
||||
activityResumed = false;
|
||||
if (!vaultRecoveryRequired && isAppLockEnabled()) lockWebView();
|
||||
if (!vaultRecoveryRequired && isAppLockEnabled() && !pgpAuthInProgress) {
|
||||
int delaySeconds = appLockDelaySeconds();
|
||||
if (delaySeconds == 0) {
|
||||
lockWebView();
|
||||
} else {
|
||||
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
|
||||
appLockHandler.postDelayed(delayedAppLock, delaySeconds * 1000L);
|
||||
}
|
||||
} else if (!vaultRecoveryRequired && isAppLockEnabled() && pgpAuthInProgress) {
|
||||
pgpAuthPaused = true;
|
||||
pgpAuthPauseAt = SystemClock.elapsedRealtime();
|
||||
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
|
||||
}
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
@@ -78,8 +112,18 @@ public class MainActivity extends BridgeActivity {
|
||||
else updateAppLockMessage("Unlock with your Android screen lock to view mail.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
appLockHandler.removeCallbacks(delayedAppLock);
|
||||
appLockHandler.removeCallbacks(lockAfterPgpAuth);
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
public void onAppLockSettingChanged(boolean enabled) {
|
||||
runOnUiThread(() -> {
|
||||
appLockHandler.removeCallbacks(delayedAppLock);
|
||||
appLockHandler.removeCallbacks(lockAfterPgpAuth);
|
||||
pgpAuthPaused = false;
|
||||
if (!enabled) {
|
||||
awaitingUnlockResult = false;
|
||||
unlockWebView(false);
|
||||
@@ -91,6 +135,77 @@ public class MainActivity extends BridgeActivity {
|
||||
});
|
||||
}
|
||||
|
||||
private int appLockDelaySeconds() {
|
||||
try {
|
||||
int delay = AndroidAccountVault.INSTANCE.appLockDelaySeconds();
|
||||
AndroidAppLockPolicy.INSTANCE.requireValidDelay(delay);
|
||||
return delay;
|
||||
} catch (Exception ignored) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public void authenticatePgpCipher(Cipher cipher, BiometricPrompt.AuthenticationCallback callback) {
|
||||
int authenticators = BiometricManager.Authenticators.BIOMETRIC_STRONG;
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
||||
authenticators |= BiometricManager.Authenticators.DEVICE_CREDENTIAL;
|
||||
}
|
||||
BiometricPrompt.PromptInfo.Builder prompt = new BiometricPrompt.PromptInfo.Builder()
|
||||
.setTitle("Unlock OpenPGP key")
|
||||
.setSubtitle("Confirm to release this key's passphrase")
|
||||
.setAllowedAuthenticators(authenticators)
|
||||
.setConfirmationRequired(true);
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
|
||||
prompt.setNegativeButtonText("Cancel");
|
||||
}
|
||||
BiometricPrompt biometricPrompt = new BiometricPrompt(
|
||||
this,
|
||||
ContextCompat.getMainExecutor(this),
|
||||
new BiometricPrompt.AuthenticationCallback() {
|
||||
@Override
|
||||
public void onAuthenticationSucceeded(BiometricPrompt.AuthenticationResult result) {
|
||||
finishPgpAuthentication();
|
||||
callback.onAuthenticationSucceeded(result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAuthenticationFailed() {
|
||||
callback.onAuthenticationFailed();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAuthenticationError(int errorCode, CharSequence errString) {
|
||||
finishPgpAuthentication();
|
||||
callback.onAuthenticationError(errorCode, errString);
|
||||
}
|
||||
}
|
||||
);
|
||||
pgpAuthInProgress = true;
|
||||
try {
|
||||
biometricPrompt.authenticate(
|
||||
prompt.build(),
|
||||
new BiometricPrompt.CryptoObject(cipher)
|
||||
);
|
||||
} catch (RuntimeException error) {
|
||||
pgpAuthInProgress = false;
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
private void finishPgpAuthentication() {
|
||||
pgpAuthInProgress = false;
|
||||
if (!pgpAuthPaused) return;
|
||||
long elapsed = SystemClock.elapsedRealtime() - pgpAuthPauseAt;
|
||||
long remaining = Math.max(1000L, appLockDelaySeconds() * 1000L - elapsed);
|
||||
appLockHandler.postDelayed(lockAfterPgpAuth, remaining);
|
||||
}
|
||||
|
||||
public void clearLocalWebData() {
|
||||
if (getBridge() == null || getBridge().getWebView() == null) return;
|
||||
getBridge().getWebView().clearCache(true);
|
||||
WebStorage.getInstance().deleteAllData();
|
||||
}
|
||||
|
||||
private boolean isAppLockEnabled() {
|
||||
try {
|
||||
return AndroidAccountVault.INSTANCE.appLockEnabled();
|
||||
|
||||
Reference in New Issue
Block a user