|
1 | 1 | package com.compass.app; |
2 | 2 |
|
3 | 3 | import android.content.Intent; |
4 | | -import android.net.Uri; |
5 | 4 | import android.os.Bundle; |
6 | 5 | import android.util.Log; |
| 6 | +import android.webkit.JavascriptInterface; |
7 | 7 | import android.webkit.WebView; |
8 | | -import android.webkit.WebViewClient; |
9 | 8 |
|
10 | 9 | import com.getcapacitor.BridgeActivity; |
| 10 | +import com.getcapacitor.BridgeWebViewClient; |
11 | 11 | import com.getcapacitor.Plugin; |
12 | 12 | import com.getcapacitor.PluginHandle; |
13 | 13 |
|
|
16 | 16 | import ee.forgr.capacitor.social.login.SocialLoginPlugin; |
17 | 17 |
|
18 | 18 | public class MainActivity extends BridgeActivity implements ModifiedMainActivityForSocialLoginPlugin { |
| 19 | + public class NativeBridge { |
| 20 | + @JavascriptInterface |
| 21 | + public boolean isNativeApp() { |
| 22 | + return true; |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + private static final String LOCAL_URL = "file:///android_asset/public/server/pages"; |
| 27 | + private static final String REMOTE_URL = "https://www.compassmeet.com"; |
| 28 | + |
| 29 | + // Optional helper for future use |
| 30 | + public void loadLocalContent() { |
| 31 | + Log.i("CompassApp", "Loading local assets..."); |
| 32 | + this.bridge.getWebView().loadUrl(LOCAL_URL); |
| 33 | + } |
| 34 | + |
| 35 | + public void loadRemoteContent() { |
| 36 | + Log.i("CompassApp", "Loading remote content..."); |
| 37 | + this.bridge.getWebView().loadUrl(REMOTE_URL); |
| 38 | + } |
| 39 | + |
19 | 40 | @Override |
20 | 41 | public void onCreate(Bundle savedInstanceState) { |
21 | 42 | super.onCreate(savedInstanceState); |
22 | | - WebView.setWebContentsDebuggingEnabled(true); |
23 | 43 |
|
24 | 44 | WebView webView = this.bridge.getWebView(); |
| 45 | + webView.setWebViewClient(new BridgeWebViewClient(this.bridge)); |
| 46 | + |
| 47 | + WebView.setWebContentsDebuggingEnabled(true); |
25 | 48 |
|
26 | 49 | // Set a recognizable User-Agent (always reliable) |
27 | 50 | webView.getSettings().setUserAgentString( |
28 | 51 | webView.getSettings().getUserAgentString() + " CompassAppWebView" |
29 | 52 | ); |
30 | 53 |
|
31 | | - // To know in JS if we are running from an APK |
32 | | - webView.evaluateJavascript("window.IS_APK = true;", null); |
| 54 | + webView.getSettings().setJavaScriptEnabled(true); |
| 55 | + webView.addJavascriptInterface(new NativeBridge(), "AndroidBridge"); |
33 | 56 |
|
34 | | - webView.setWebViewClient(new WebViewClient() { |
35 | | - @Override |
36 | | - public void onPageFinished(WebView view, String url) { |
37 | | - super.onPageFinished(view, url); |
38 | | - // This runs once the document exists |
39 | | - view.evaluateJavascript("window.IS_APK = true;", null); |
40 | | - } |
41 | | - |
42 | | - @Override |
43 | | - public boolean shouldOverrideUrlLoading(WebView view, String url) { |
44 | | - // Load only compassmeet.com URLs in WebView |
45 | | - if (url.startsWith("https://www.compassmeet.com")) { |
46 | | - view.loadUrl(url); |
47 | | - return true; |
48 | | - } |
49 | | - |
50 | | - // All other URLs open externally |
51 | | - Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); |
52 | | - startActivity(intent); |
53 | | - return true; // handled externally |
54 | | - } |
55 | | - }); |
| 57 | + // Allow remote URLs to still have access to Capacitor bridge |
| 58 | +// webView.setWebViewClient(new BridgeWebViewClient(this.bridge) { |
| 59 | +// @Override |
| 60 | +// public boolean shouldOverrideUrlLoading(android.webkit.WebView view, String url) { |
| 61 | +// if (url.startsWith("https://www.compassmeet.com")) { |
| 62 | +// view.loadUrl(url); |
| 63 | +// return true; |
| 64 | +// } |
| 65 | +// return super.shouldOverrideUrlLoading(view, url); |
| 66 | +// } |
| 67 | +// }); |
| 68 | +// |
| 69 | +// // Load your remote site instead of local assets |
| 70 | +// this.bridge.getWebView().loadUrl("https://www.compassmeet.com"); |
56 | 71 | } |
57 | 72 |
|
58 | 73 | @Override |
|
0 commit comments