aboutsummaryrefslogtreecommitdiffstats
path: root/android/app/src/webview
diff options
context:
space:
mode:
Diffstat (limited to 'android/app/src/webview')
-rw-r--r--android/app/src/webview/java/io/coinos/app/MainActivity.java85
1 files changed, 79 insertions, 6 deletions
diff --git a/android/app/src/webview/java/io/coinos/app/MainActivity.java b/android/app/src/webview/java/io/coinos/app/MainActivity.java
index d88a969..806fd8f 100644
--- a/android/app/src/webview/java/io/coinos/app/MainActivity.java
+++ b/android/app/src/webview/java/io/coinos/app/MainActivity.java
@@ -38,6 +38,7 @@ public class MainActivity extends Activity {
static final String BASE = "https://v3.coinos.io/";
private WebView web;
+ private android.widget.FrameLayout root;
private ValueCallback<Uri[]> filePicker;
private static final int REQ_FILE = 10, REQ_PERMS = 11;
@@ -45,8 +46,27 @@ public class MainActivity extends Activity {
protected void onCreate(Bundle saved) {
super.onCreate(saved);
web = new WebView(this);
- setContentView(web, new ViewGroup.LayoutParams(
+ // Android 15 draws every app edge to edge, so the page ran under the
+ // status bar and the navigation buttons sat on top of its bottom nav.
+ // The page goes in a frame padded by the system bars (and the keyboard,
+ // which then resizes the page as adjustResize did before); the frame's
+ // background is the theme's bar colour.
+ root = new android.widget.FrameLayout(this);
+ root.addView(web, new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
+ // 3-button navigation gets a light scrim of the system's own over
+ // whatever colour is behind it; the frame's colour is the one wanted
+ if (Build.VERSION.SDK_INT >= 29) getWindow().setNavigationBarContrastEnforced(false);
+ if (Build.VERSION.SDK_INT >= 30) {
+ root.setOnApplyWindowInsetsListener((v, insets) -> {
+ android.graphics.Insets b = insets.getInsets(
+ android.view.WindowInsets.Type.systemBars() | android.view.WindowInsets.Type.ime()
+ | android.view.WindowInsets.Type.displayCutout());
+ v.setPadding(b.left, b.top, b.right, b.bottom);
+ return android.view.WindowInsets.CONSUMED;
+ });
+ }
+ setContentView(root);
// A debug build can be inspected from a desktop (chrome://inspect); a
// release build of a wallet must not be — anything with ADB access could
@@ -63,6 +83,16 @@ public class MainActivity extends Activity {
s.setLoadWithOverviewMode(true);
s.setUseWideViewPort(true);
CookieManager.getInstance().setAcceptCookie(true);
+ // Passkeys: a WebView offers the page no WebAuthn unless the app asks for
+ // it, so passkey sign-in and the fingerprint lock were missing here. The
+ // site's assetlinks.json vouches for this app (get_login_creds), which
+ // is what lets it use v3.coinos.io's passkeys.
+ try {
+ if (androidx.webkit.WebViewFeature.isFeatureSupported(androidx.webkit.WebViewFeature.WEB_AUTHENTICATION)) {
+ androidx.webkit.WebSettingsCompat.setWebAuthenticationSupport(s,
+ androidx.webkit.WebSettingsCompat.WEB_AUTHENTICATION_SUPPORT_FOR_APP);
+ }
+ } catch (Throwable ignored) {}
web.setWebViewClient(new WebViewClient() {
// The bundled build answers the site's own files from assets; the
@@ -77,6 +107,15 @@ public class MainActivity extends Activity {
}
@Override
+ public void onPageFinished(WebView v, String url) {
+ endpointToPage();
+ // the bars take the page's colour, now and whenever its theme changes
+ v.evaluateJavascript("(function(){var m=document.querySelector('meta[name=theme-color]');"
+ + "if(!m||!window.CoinosHost||!CoinosHost.bars)return;var f=function(){CoinosHost.bars(m.content||'')};"
+ + "f();new MutationObserver(f).observe(m,{attributes:true,attributeFilter:['content']})})()", null);
+ }
+
+ @Override
public boolean shouldOverrideUrlLoading(WebView v, WebResourceRequest r) {
Uri u = r.getUrl();
String host = u.getHost();
@@ -118,9 +157,11 @@ public class MainActivity extends Activity {
}
});
- // Notifications only ever come from UnifiedPush, so the permission is
- // asked for only when a distributor is installed to deliver them.
- if (Build.VERSION.SDK_INT >= 33 && !Push.distributors(this).isEmpty()
+ // Notifications come from Firebase (the fcm build, with Play Services)
+ // or a UnifiedPush distributor, so the permission is asked for only when
+ // one of them is there to deliver them.
+ boolean fcm = Fcm.available(this);
+ if (Build.VERSION.SDK_INT >= 33 && (fcm || !Push.distributors(this).isEmpty())
&& checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
try { requestPermissions(new String[] { Manifest.permission.POST_NOTIFICATIONS }, REQ_PERMS); } catch (Exception ignored) {}
}
@@ -128,10 +169,25 @@ public class MainActivity extends Activity {
// can deliver a push — the advice it gives is useless otherwise ("use
// Firefox" makes no sense inside a WebView that IS the app).
web.addJavascriptInterface(new Host(), "CoinosHost");
- Push.register(this);
+ // Firebase when it can; otherwise a distributor. A Firebase token
+ // arrives after the page has started loading, so it goes straight into
+ // the page rather than waiting for the next launch's ?up=.
+ if (!Fcm.register(this, () -> runOnUiThread(this::endpointToPage))) Push.register(this);
web.loadUrl(urlFor(getIntent()));
}
+ /**
+ * The push endpoint, put where the page keeps it (messages.js and nwc.js
+ * read 'coinos-unifiedpush') — once the page is ours to write into.
+ */
+ private void endpointToPage() {
+ String endpoint = Push.endpoint(this);
+ String url = web == null ? null : web.getUrl();
+ if (endpoint == null || Push.endpointDelivered(this) || url == null || !url.startsWith(BASE)) return;
+ web.evaluateJavascript("try{localStorage.setItem('coinos-unifiedpush'," + org.json.JSONObject.quote(endpoint) + ")}catch(e){}", null);
+ Push.markEndpointDelivered(this);
+ }
+
/** What the page is allowed to ask about its surroundings. */
private class Host {
@android.webkit.JavascriptInterface
@@ -139,10 +195,27 @@ public class MainActivity extends Activity {
String dist = Push.distributor(MainActivity.this);
List<String> all = Push.distributors(MainActivity.this);
String endpoint = Push.endpoint(MainActivity.this);
- return "{\"app\":\"graphene\",\"distributors\":" + all.size()
+ return "{\"app\":\"" + (Fcm.available(MainActivity.this) ? "fcm" : "graphene") + "\",\"distributors\":" + all.size()
+ ",\"distributor\":" + (dist == null ? "null" : "\"" + dist + "\"")
+ ",\"endpoint\":" + (endpoint != null) + "}";
}
+ /** The page's theme colour, for the bar areas around it (see onCreate). */
+ @android.webkit.JavascriptInterface
+ public void bars(String css) {
+ final int color;
+ try { color = android.graphics.Color.parseColor(css.trim()); } catch (Exception e) { return; }
+ runOnUiThread(() -> {
+ if (root != null) root.setBackgroundColor(color);
+ if (Build.VERSION.SDK_INT >= 30 && getWindow().getInsetsController() != null) {
+ // dark icons on a light colour, light icons on a dark one
+ double lum = (0.299 * android.graphics.Color.red(color) + 0.587 * android.graphics.Color.green(color)
+ + 0.114 * android.graphics.Color.blue(color)) / 255;
+ int light = android.view.WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS
+ | android.view.WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS;
+ getWindow().getInsetsController().setSystemBarsAppearance(lum > 0.5 ? light : 0, light);
+ }
+ });
+ }
/** The wake job's bridge has this too; here it's a no-op. */
@android.webkit.JavascriptInterface
public void done() {}