Use evaluateJavascript in ArticleWebView and guard external startActivity calls#211
Open
jim-daf wants to merge 1 commit into
Open
Use evaluateJavascript in ArticleWebView and guard external startActivity calls#211jim-daf wants to merge 1 commit into
jim-daf wants to merge 1 commit into
Conversation
…vity calls
Two related WebView hygiene fixes in ArticleWebView.
1) Legacy javascript: URI form (D06)
The onPageStarted and onPageFinished handlers used:
view.loadUrl("javascript:" + styleSwitcherJs);
view.loadUrl("javascript:" + styleSwitcherJs +
";$SLOB.setStyleTitles($styleSwitcher.getTitles())");
loadUrl("javascript:...") was deprecated in Android 4.4 in
favour of WebView.evaluateJavascript. The legacy form adds a
navigation entry to the back/forward history, so the style
bootstrap runs on every onPageStarted/onPageFinished pollute
the back stack on the article view.
Switch both call sites to view.evaluateJavascript(script, null).
2) startActivity without try/catch inside shouldOverrideUrlLoading
(U04)
shouldOverrideUrlLoading dispatched two intents directly:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, uri);
getContext().startActivity(browserIntent);
Intent intent = new Intent(getContext(),
ArticleCollectionActivity.class);
intent.setData(uri);
getContext().startActivity(intent);
If the device has no app registered to handle ACTION_VIEW for
that scheme, startActivity raises ActivityNotFoundException and
the activity hosting the WebView crashes. The second call is
internal and should not raise, but ActivityNotFound is still
theoretically reachable through component aliasing or split
profiles.
Wrap both in try { ... } catch (ActivityNotFoundException e)
{ Log.w(...) }. The external case falls through (the user sees
no app open). The internal case logs and returns false so the
WebView keeps the existing page.
Assisted-by: Claude (Anthropic)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #210.
Two related WebView hygiene fixes in
ArticleWebView.loadUrl("javascript:...")->evaluateJavascriptonPageStartedandonPageFinishedused the legacyloadUrl("javascript:...")form to inject the style switcher script. The Android docs deprecated that pattern in KitKat (4.4) in favour ofWebView.evaluateJavascript, because the legacy form adds a navigation entry to the back/forward history on every call.Both call sites now use
view.evaluateJavascript(script, null). Same script payload, no back-stack pollution.startActivityinsideshouldOverrideUrlLoadingshouldOverrideUrlLoadingpreviously dispatched two intents withouttry/catch:ACTION_VIEWfor off-app URLs (mailto:,tel:, custom schemes from dictionaries) which can raiseActivityNotFoundExceptionif no handler is installed and crash the host activity.ArticleCollectionActivityfor localhost article links.Both calls are now wrapped in
try { ... } catch (ActivityNotFoundException e). The external case logs and falls through silently. The internal case logs and returnsfalse, so the WebView keeps the existing page if the dispatch ever fails.