Skip to content

Commit 68bef8e

Browse files
committed
Catch ClassNotFoundException and react accordingly
1 parent 4c58653 commit 68bef8e

File tree

11 files changed

+66
-13
lines changed

11 files changed

+66
-13
lines changed

flutter_inappwebview/example/integration_test/util.dart

+3
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ class MyChromeSafariBrowser extends ChromeSafariBrowser {
169169
relationshipValidationResult.complete(result);
170170
}
171171

172+
@override
173+
void onBrowserNotSupported() {}
174+
172175
@override
173176
void onClosed() {
174177
closed.complete();

flutter_inappwebview/example/lib/chrome_safari_browser_example.screen.dart

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ class MyChromeSafariBrowser extends ChromeSafariBrowser {
2020
void onClosed() {
2121
print("ChromeSafari browser closed");
2222
}
23+
24+
@override
25+
void onBrowserNotSupported() {
26+
print("ChromeSafari is not supported");
27+
}
2328
}
2429

2530
class ChromeSafariBrowserExampleScreen extends StatefulWidget {

flutter_inappwebview/lib/src/chrome_safari_browser/chrome_safari_browser.dart

+3
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,7 @@ class ChromeSafariBrowser implements PlatformChromeSafariBrowserEvents {
192192

193193
@override
194194
void onWillOpenInBrowser() {}
195+
196+
@override
197+
void onBrowserNotSupported() {}
195198
}

flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/chrome_custom_tabs/ChromeCustomTabsActivity.java

+12-5
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,16 @@ protected void onCreate(Bundle savedInstanceState) {
114114
customTabActivityHelper.setConnectionCallback(new CustomTabActivityHelper.ConnectionCallback() {
115115
@Override
116116
public void onCustomTabsConnected() {
117-
customTabsConnected();
118-
if (channelDelegate != null) {
119-
channelDelegate.onServiceConnected();
117+
try {
118+
customTabsConnected();
119+
if (channelDelegate != null) {
120+
channelDelegate.onServiceConnected();
121+
}
122+
} catch (java.lang.Exception e) {
123+
if (channelDelegate != null) {
124+
channelDelegate.onBrowserNotSupported();
125+
}
126+
Log.d(LOG_TAG, "Custom Tabs not supported", e);
120127
}
121128
}
122129

@@ -181,15 +188,15 @@ public void onRelationshipValidationResult(@CustomTabsService.Relation int relat
181188
public void launchUrl(@NonNull String url,
182189
@Nullable Map<String, String> headers,
183190
@Nullable String referrer,
184-
@Nullable List<String> otherLikelyURLs) {
191+
@Nullable List<String> otherLikelyURLs) throws ClassNotFoundException {
185192
launchUrlWithSession(customTabsSession, url, headers, referrer, otherLikelyURLs);
186193
}
187194

188195
public void launchUrlWithSession(@Nullable CustomTabsSession session,
189196
@NonNull String url,
190197
@Nullable Map<String, String> headers,
191198
@Nullable String referrer,
192-
@Nullable List<String> otherLikelyURLs) {
199+
@Nullable List<String> otherLikelyURLs) throws ClassNotFoundException {
193200
mayLaunchUrl(url, otherLikelyURLs);
194201
builder = new CustomTabsIntent.Builder(session);
195202
prepareCustomTabs();

flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/chrome_custom_tabs/ChromeCustomTabsChannelDelegate.java

+7
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,13 @@ public void onSessionEnded(boolean didUserInteract) {
243243
channel.invokeMethod("onSessionEnded", obj);
244244
}
245245

246+
public void onBrowserNotSupported() {
247+
MethodChannel channel = getChannel();
248+
if (channel == null) return;
249+
Map<String, Object> obj = new HashMap<>();
250+
channel.invokeMethod("onBrowserNotSupported", obj);
251+
}
252+
246253
@Override
247254
public void dispose() {
248255
super.dispose();

flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/chrome_custom_tabs/ChromeSafariBrowserManager.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ public void onMethodCall(final MethodCall call, final MethodChannel.Result resul
5555
HashMap<String, Object> actionButton = (HashMap<String, Object>) call.argument("actionButton");
5656
HashMap<String, Object> secondaryToolbar = (HashMap<String, Object>) call.argument("secondaryToolbar");
5757
List<HashMap<String, Object>> menuItemList = (List<HashMap<String, Object>>) call.argument("menuItemList");
58-
open(plugin.activity, viewId, url, headers, referrer, otherLikelyURLs, settings, actionButton, secondaryToolbar, menuItemList, result);
58+
try {
59+
open(plugin.activity, viewId, url, headers, referrer, otherLikelyURLs, settings, actionButton, secondaryToolbar, menuItemList, result);
60+
} catch (ClassNotFoundException e) {
61+
result.error(LOG_TAG, e.getMessage(), null);
62+
}
5963
} else {
6064
result.success(false);
6165
}

flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/chrome_custom_tabs/CustomTabActivityHelper.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public static void openCustomTab(Activity activity,
3939
Uri uri,
4040
@Nullable Map<String, String> headers,
4141
@Nullable Uri referrer,
42-
int requestCode) {
42+
int requestCode) throws ClassNotFoundException {
4343
intent.setData(uri);
4444
if (headers != null) {
4545
Bundle bundleHeaders = new Bundle();

flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/in_app_browser/InAppBrowserActivity.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import java.util.HashMap;
4646
import java.util.List;
4747
import java.util.Map;
48+
import java.util.Objects;
4849

4950
import io.flutter.plugin.common.MethodChannel;
5051

@@ -145,7 +146,11 @@ protected void onCreate(Bundle savedInstanceState) {
145146

146147
actionBar = getSupportActionBar();
147148

148-
prepareView();
149+
try {
150+
prepareView();
151+
} catch (ClassNotFoundException e) {
152+
Log.d(LOG_TAG, Objects.requireNonNull(e.getMessage()));
153+
}
149154

150155
if (windowId != -1) {
151156
if (webView.plugin != null && webView.plugin.inAppWebViewManager != null) {
@@ -187,7 +192,7 @@ else if (initialUrlRequest != null) {
187192
}
188193
}
189194

190-
private void prepareView() {
195+
private void prepareView() throws ClassNotFoundException {
191196

192197
if (webView != null) {
193198
webView.prepare();
@@ -421,7 +426,7 @@ public void hide() {
421426
}
422427
}
423428

424-
public void show() {
429+
public void show() throws ClassNotFoundException {
425430
isHidden = false;
426431
Intent openActivity = new Intent(this, InAppBrowserActivity.class);
427432
openActivity.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
@@ -451,7 +456,7 @@ public void closeButtonClicked(MenuItem item) {
451456
close(null);
452457
}
453458

454-
public void setSettings(InAppBrowserSettings newSettings, HashMap<String, Object> newSettingsMap) {
459+
public void setSettings(InAppBrowserSettings newSettings, HashMap<String, Object> newSettingsMap) throws ClassNotFoundException {
455460

456461
InAppWebViewSettings newInAppWebViewSettings = new InAppWebViewSettings();
457462
newInAppWebViewSettings.parse(newSettingsMap);

flutter_inappwebview_android/android/src/main/java/com/pichillilorenzo/flutter_inappwebview_android/webview/WebViewChannelDelegate.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,11 @@ public void onReceiveValue(String value) {
219219
InAppBrowserSettings inAppBrowserSettings = new InAppBrowserSettings();
220220
HashMap<String, Object> inAppBrowserSettingsMap = (HashMap<String, Object>) call.argument("settings");
221221
inAppBrowserSettings.parse(inAppBrowserSettingsMap);
222-
inAppBrowserActivity.setSettings(inAppBrowserSettings, inAppBrowserSettingsMap);
222+
try {
223+
inAppBrowserActivity.setSettings(inAppBrowserSettings, inAppBrowserSettingsMap);
224+
} catch (ClassNotFoundException e) {
225+
result.error(LOG_TAG, e.getMessage(), null);
226+
}
223227
} else if (webView != null) {
224228
InAppWebViewSettings inAppWebViewSettings = new InAppWebViewSettings();
225229
HashMap<String, Object> inAppWebViewSettingsMap = (HashMap<String, Object>) call.argument("settings");
@@ -247,7 +251,11 @@ public void onReceiveValue(String value) {
247251
case show:
248252
if (webView != null && webView.getInAppBrowserDelegate() instanceof InAppBrowserActivity) {
249253
InAppBrowserActivity inAppBrowserActivity = (InAppBrowserActivity) webView.getInAppBrowserDelegate();
250-
inAppBrowserActivity.show();
254+
try {
255+
inAppBrowserActivity.show();
256+
} catch (ClassNotFoundException e) {
257+
result.error(LOG_TAG, e.getMessage(), null);
258+
}
251259
result.success(true);
252260
} else {
253261
result.notImplemented();

flutter_inappwebview_android/lib/src/chrome_safari_browser/chrome_safari_browser.dart

+3
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ class AndroidChromeSafariBrowser extends PlatformChromeSafariBrowser
173173
final bool didUserInteract = call.arguments["didUserInteract"];
174174
eventHandler?.onSessionEnded(didUserInteract);
175175
break;
176+
case "onBrowserNotSupported":
177+
eventHandler?.onBrowserNotSupported();
178+
break;
176179
default:
177180
throw UnimplementedError("Unimplemented ${call.method} method");
178181
}

flutter_inappwebview_platform_interface/lib/src/chrome_safari_browser/platform_chrome_safari_browser.dart

+8
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,14 @@ abstract class PlatformChromeSafariBrowserEvents {
590590
///{@endtemplate}
591591
void onGreatestScrollPercentageIncreased(int scrollPercentage) {}
592592

593+
///{@template flutter_inappwebview_platform_interface.PlatformChromeSafariBrowser.onBrowserNotSupported}
594+
/// Called when the browser is not supported.
595+
/// This can happen if a user forces a certain browser package to be used but the browser is not installed on the user's system.
596+
void onBrowserNotSupported() {
597+
throw UnimplementedError(
598+
'onBrowserNotSupported is not implemented on the current platform');
599+
}
600+
593601
///{@template flutter_inappwebview_platform_interface.PlatformChromeSafariBrowser.onSessionEnded}
594602
///Called when a `CustomTabsSession` is ending or when no further Engagement Signals
595603
///callbacks are expected to report whether any user action has occurred during the session.

0 commit comments

Comments
 (0)