Phase 02 OpenPGP implementation
This commit is contained in:
@@ -1,19 +1,211 @@
|
||||
package studio.daybreak.mail;
|
||||
|
||||
import android.Manifest;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import com.getcapacitor.BridgeActivity;
|
||||
|
||||
public class MainActivity extends BridgeActivity {
|
||||
import android.Manifest;
|
||||
import android.app.KeyguardManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.Color;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.view.Gravity;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.webkit.WebView;
|
||||
import android.widget.Button;
|
||||
import android.widget.FrameLayout;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import com.getcapacitor.BridgeActivity;
|
||||
|
||||
public class MainActivity extends BridgeActivity {
|
||||
private static final int UNLOCK_REQUEST_CODE = 705;
|
||||
private FrameLayout appLockOverlay;
|
||||
private TextView appLockMessage;
|
||||
private Button appLockButton;
|
||||
private boolean appLocked;
|
||||
private boolean awaitingUnlockResult;
|
||||
private boolean activityResumed;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
registerPlugin(EpMailPlugin.class);
|
||||
super.onCreate(savedInstanceState);
|
||||
if (Build.VERSION.SDK_INT >= 33 && checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
|
||||
ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.POST_NOTIFICATIONS }, 701);
|
||||
}
|
||||
}
|
||||
}
|
||||
AndroidAccountVault.INSTANCE.initialize(getApplicationContext());
|
||||
createAppLockOverlay();
|
||||
if (isAppLockEnabled()) lockWebView();
|
||||
if (Build.VERSION.SDK_INT >= 33 && checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
|
||||
ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.POST_NOTIFICATIONS }, 701);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
activityResumed = true;
|
||||
if (isAppLockEnabled() && appLocked) {
|
||||
lockWebView();
|
||||
configureUnlockButton();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
activityResumed = false;
|
||||
if (isAppLockEnabled()) lockWebView();
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
if (requestCode != UNLOCK_REQUEST_CODE) return;
|
||||
awaitingUnlockResult = false;
|
||||
if (resultCode == RESULT_OK && isAppLockEnabled()) unlockWebView(true);
|
||||
else updateAppLockMessage("Unlock with your Android screen lock to view mail.");
|
||||
}
|
||||
|
||||
public void onAppLockSettingChanged(boolean enabled) {
|
||||
runOnUiThread(() -> {
|
||||
if (!enabled) {
|
||||
awaitingUnlockResult = false;
|
||||
unlockWebView(false);
|
||||
} else if (activityResumed) {
|
||||
appLocked = false;
|
||||
} else {
|
||||
lockWebView();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private boolean isAppLockEnabled() {
|
||||
try {
|
||||
return AndroidAccountVault.INSTANCE.appLockEnabled();
|
||||
} catch (Exception ignored) {
|
||||
// If the encrypted setting cannot be read, prefer locking over exposing stale UI.
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
private void createAppLockOverlay() {
|
||||
View content = findViewById(android.R.id.content);
|
||||
if (!(content instanceof FrameLayout)) return;
|
||||
|
||||
appLockOverlay = new FrameLayout(this);
|
||||
appLockOverlay.setBackgroundColor(Color.rgb(25, 21, 18));
|
||||
appLockOverlay.setClickable(true);
|
||||
appLockOverlay.setFocusable(true);
|
||||
|
||||
LinearLayout panel = new LinearLayout(this);
|
||||
panel.setOrientation(LinearLayout.VERTICAL);
|
||||
panel.setGravity(Gravity.CENTER);
|
||||
panel.setPadding(dp(28), dp(28), dp(28), dp(28));
|
||||
|
||||
TextView title = new TextView(this);
|
||||
title.setText("ep-mail is locked");
|
||||
title.setTextColor(Color.rgb(238, 230, 215));
|
||||
title.setTextSize(22);
|
||||
title.setGravity(Gravity.CENTER);
|
||||
panel.addView(title, new LinearLayout.LayoutParams(
|
||||
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
|
||||
|
||||
appLockMessage = new TextView(this);
|
||||
appLockMessage.setText("Unlock with your Android screen lock to view mail.");
|
||||
appLockMessage.setTextColor(Color.rgb(185, 171, 151));
|
||||
appLockMessage.setTextSize(15);
|
||||
appLockMessage.setGravity(Gravity.CENTER);
|
||||
LinearLayout.LayoutParams messageParams = new LinearLayout.LayoutParams(
|
||||
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
|
||||
messageParams.topMargin = dp(12);
|
||||
panel.addView(appLockMessage, messageParams);
|
||||
|
||||
appLockButton = new Button(this);
|
||||
appLockButton.setText("Unlock");
|
||||
appLockButton.setOnClickListener(view -> requestDeviceUnlock());
|
||||
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(
|
||||
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
|
||||
buttonParams.gravity = Gravity.CENTER_HORIZONTAL;
|
||||
buttonParams.topMargin = dp(24);
|
||||
panel.addView(appLockButton, buttonParams);
|
||||
|
||||
FrameLayout.LayoutParams panelParams = new FrameLayout.LayoutParams(
|
||||
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
|
||||
appLockOverlay.addView(panel, panelParams);
|
||||
appLockOverlay.setVisibility(View.GONE);
|
||||
((FrameLayout) content).addView(appLockOverlay, new FrameLayout.LayoutParams(
|
||||
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
|
||||
}
|
||||
|
||||
private void requestDeviceUnlock() {
|
||||
if (awaitingUnlockResult) return;
|
||||
KeyguardManager keyguard = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
|
||||
if (keyguard == null || !keyguard.isDeviceSecure()) {
|
||||
updateAppLockMessage("Set a PIN, pattern, or password in Android Settings to unlock ep-mail.");
|
||||
configureUnlockButton();
|
||||
return;
|
||||
}
|
||||
configureUnlockButton();
|
||||
Intent confirmIntent = keyguard.createConfirmDeviceCredentialIntent(
|
||||
"Unlock ep-mail",
|
||||
"Confirm your Android screen lock to view mail."
|
||||
);
|
||||
if (confirmIntent == null) {
|
||||
updateAppLockMessage("Android could not start screen-lock verification. Try again after unlocking your device.");
|
||||
return;
|
||||
}
|
||||
awaitingUnlockResult = true;
|
||||
try {
|
||||
startActivityForResult(confirmIntent, UNLOCK_REQUEST_CODE);
|
||||
} catch (Exception error) {
|
||||
awaitingUnlockResult = false;
|
||||
updateAppLockMessage("Could not start Android screen-lock verification. Try again.");
|
||||
}
|
||||
}
|
||||
|
||||
private void configureUnlockButton() {
|
||||
if (appLockButton == null) return;
|
||||
KeyguardManager keyguard = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
|
||||
if (keyguard != null && keyguard.isDeviceSecure()) {
|
||||
appLockButton.setText("Unlock");
|
||||
appLockButton.setOnClickListener(view -> requestDeviceUnlock());
|
||||
} else {
|
||||
appLockButton.setText("Open security settings");
|
||||
appLockButton.setOnClickListener(view -> startActivity(
|
||||
new Intent(android.provider.Settings.ACTION_SECURITY_SETTINGS)));
|
||||
}
|
||||
}
|
||||
|
||||
private void lockWebView() {
|
||||
appLocked = true;
|
||||
if (appLockOverlay != null) appLockOverlay.setVisibility(View.VISIBLE);
|
||||
if (getBridge() != null) {
|
||||
WebView webView = getBridge().getWebView();
|
||||
webView.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
|
||||
webView.getSettings().setJavaScriptEnabled(false);
|
||||
}
|
||||
}
|
||||
|
||||
private void unlockWebView(boolean reload) {
|
||||
appLocked = false;
|
||||
if (getBridge() != null) {
|
||||
WebView webView = getBridge().getWebView();
|
||||
webView.getSettings().setJavaScriptEnabled(true);
|
||||
webView.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_AUTO);
|
||||
if (reload) webView.reload();
|
||||
}
|
||||
if (appLockOverlay != null) appLockOverlay.setVisibility(View.GONE);
|
||||
if (appLockButton != null) {
|
||||
appLockButton.setText("Unlock");
|
||||
appLockButton.setOnClickListener(view -> requestDeviceUnlock());
|
||||
}
|
||||
}
|
||||
|
||||
private void updateAppLockMessage(String message) {
|
||||
if (appLockMessage != null) appLockMessage.setText(message);
|
||||
}
|
||||
|
||||
private int dp(int value) {
|
||||
return Math.round(value * getResources().getDisplayMetrics().density);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user