|
1 | 1 | package dev.cfb.manavault; |
2 | 2 |
|
| 3 | +import android.content.ActivityNotFoundException; |
3 | 4 | import android.content.Context; |
| 5 | +import android.content.Intent; |
4 | 6 | import android.graphics.Color; |
5 | 7 | import android.net.Uri; |
6 | 8 | import android.os.Bundle; |
| 9 | +import android.util.Log; |
7 | 10 | import android.webkit.CookieManager; |
8 | 11 |
|
9 | 12 | import androidx.core.view.WindowCompat; |
|
15 | 18 | import com.getcapacitor.annotation.CapacitorPlugin; |
16 | 19 |
|
17 | 20 | public class MainActivity extends BridgeActivity { |
| 21 | + private static final String TAG = "MainActivity"; |
18 | 22 | private static final int APP_CHROME_COLOR = Color.rgb(24, 4, 13); |
19 | 23 | private static final String PREFERENCES_NAME = "NativeShell"; |
20 | 24 | private static final String SERVER_URL_KEY = "serverUrl"; |
@@ -80,10 +84,60 @@ public Boolean shouldOverrideLoad(Uri url) { |
80 | 84 | String scheme = url.getScheme(); |
81 | 85 |
|
82 | 86 | if ("http".equals(scheme) || "https".equals(scheme)) { |
83 | | - return false; |
| 87 | + if (isAppNavigation(url)) return false; |
| 88 | + |
| 89 | + try { |
| 90 | + Intent intent = new Intent(Intent.ACTION_VIEW, url); |
| 91 | + intent.addCategory(Intent.CATEGORY_BROWSABLE); |
| 92 | + getActivity().startActivity(intent); |
| 93 | + } catch (ActivityNotFoundException exception) { |
| 94 | + Log.w(TAG, "No browser available to open external URL", exception); |
| 95 | + return true; |
| 96 | + } |
| 97 | + |
| 98 | + return true; |
84 | 99 | } |
85 | 100 |
|
86 | 101 | return null; |
87 | 102 | } |
| 103 | + |
| 104 | + private boolean isAppNavigation(Uri url) { |
| 105 | + String host = url.getHost(); |
| 106 | + if ("manavault.cfb.dev".equalsIgnoreCase(host) || "www.manavault.cfb.dev".equalsIgnoreCase(host)) { |
| 107 | + return true; |
| 108 | + } |
| 109 | + |
| 110 | + String serverUrl = getContext() |
| 111 | + .getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE) |
| 112 | + .getString(SERVER_URL_KEY, ""); |
| 113 | + if (serverUrl.trim().isEmpty()) return false; |
| 114 | + |
| 115 | + Uri serverUri = Uri.parse(serverUrl.trim()); |
| 116 | + return sameOrigin(url, serverUri); |
| 117 | + } |
| 118 | + |
| 119 | + private boolean sameOrigin(Uri left, Uri right) { |
| 120 | + int leftPort = effectivePort(left); |
| 121 | + int rightPort = effectivePort(right); |
| 122 | + |
| 123 | + return leftPort == rightPort |
| 124 | + && stringEqualsIgnoreCase(left.getScheme(), right.getScheme()) |
| 125 | + && stringEqualsIgnoreCase(left.getHost(), right.getHost()); |
| 126 | + } |
| 127 | + |
| 128 | + private int effectivePort(Uri uri) { |
| 129 | + int port = uri.getPort(); |
| 130 | + if (port >= 0) return port; |
| 131 | + |
| 132 | + String scheme = uri.getScheme(); |
| 133 | + if ("http".equalsIgnoreCase(scheme)) return 80; |
| 134 | + if ("https".equalsIgnoreCase(scheme)) return 443; |
| 135 | + return -1; |
| 136 | + } |
| 137 | + |
| 138 | + private boolean stringEqualsIgnoreCase(String left, String right) { |
| 139 | + if (left == null || right == null) return left == null && right == null; |
| 140 | + return left.equalsIgnoreCase(right); |
| 141 | + } |
88 | 142 | } |
89 | 143 | } |
0 commit comments