Skip to content

Commit

Permalink
Fix WebView deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
equeim committed Aug 21, 2024
1 parent b61b5c2 commit 9f1d85f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.webkit.ConsoleMessage;
import android.webkit.CookieManager;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
Expand All @@ -34,17 +36,23 @@
import org.quantumbadger.redreader.common.TorCommon;
import org.quantumbadger.redreader.reddit.api.RedditOAuth;

import java.util.Objects;

import info.guardianproject.netcipher.webkit.WebkitProxy;

public class OAuthLoginActivity extends ViewsBaseActivity {

private static final String OAUTH_HOST = "rr_oauth_redir";
private static final String REDREADER_SCHEME = "redreader";
private static final String HTTP_SCHEME = "http";

private WebView mWebView;

@Override
protected void onDestroy() {
super.onDestroy();
final CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
cookieManager.removeAllCookies(null);
}

@SuppressLint("SetJavaScriptEnabled")
Expand All @@ -57,20 +65,20 @@ public void onCreate(final Bundle savedInstanceState) {

mWebView = new WebView(this);

if(TorCommon.isTorEnabled()) {
if (TorCommon.isTorEnabled()) {
try {
final boolean result = WebkitProxy.setProxy(
RedReader.class.getCanonicalName(),
getApplicationContext(),
mWebView,
"127.0.0.1",
8118);
if(!result) {
if (!result) {
BugReportActivity.handleGlobalError(
this,
getResources().getString(R.string.error_tor_setting_failed));
}
} catch(final Exception e) {
} catch (final Exception e) {
BugReportActivity.handleGlobalError(this, e);
}
}
Expand All @@ -83,8 +91,9 @@ public void onCreate(final Bundle savedInstanceState) {
settings.setUseWideViewPort(true);
settings.setLoadWithOverviewMode(true);
settings.setDomStorageEnabled(true);
settings.setSaveFormData(false);
settings.setSavePassword(false);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
settings.setSaveFormData(false);
}
settings.setDatabaseEnabled(false);
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
settings.setDisplayZoomControls(false);
Expand All @@ -100,18 +109,19 @@ public boolean onConsoleMessage(final ConsoleMessage consoleMessage) {
@Override
public boolean shouldOverrideUrlLoading(
final WebView view,
final String url) {

if(url.startsWith("http://rr_oauth_redir")
|| url.startsWith("redreader://rr_oauth_redir")) { // TODO constant
final WebResourceRequest request) {

final Uri url = request.getUrl();
if (Objects.equals(url.getHost(), OAUTH_HOST) &&
(Objects.equals(url.getScheme(), REDREADER_SCHEME) ||
Objects.equals(url.getScheme(), HTTP_SCHEME))) {
final Intent intent = new Intent();
intent.putExtra("url", url);
setResult(123, intent);
finish();

} else {
setTitle(Uri.parse(url).getHost());
setTitle(url.getHost());
return false;
}

Expand All @@ -129,7 +139,7 @@ protected void onPause() {

super.onPause();

if(mWebView != null) {
if (mWebView != null) {
mWebView.onPause();
mWebView.pauseTimers();
}
Expand All @@ -139,7 +149,7 @@ protected void onPause() {
protected void onResume() {
super.onResume();

if(mWebView != null) {
if (mWebView != null) {
mWebView.resumeTimers();
mWebView.onResume();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import android.view.ViewGroup;
import android.view.WindowManager;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
Expand Down Expand Up @@ -64,6 +64,7 @@

import java.net.URISyntaxException;
import java.util.Locale;
import java.util.Objects;
import java.util.Timer;
import java.util.TimerTask;

Expand Down Expand Up @@ -124,8 +125,6 @@ public View onCreateView(

mActivity = (BaseActivity)getActivity();

CookieSyncManager.createInstance(mActivity);

outer = (FrameLayout)inflater.inflate(R.layout.web_view_fragment, null);

final RedditPost srcPost = BundleCompat.getParcelable(requireArguments(),
Expand Down Expand Up @@ -278,13 +277,15 @@ public void onProgressChanged(final WebView view, final int newProgress) {
@Override
public boolean shouldOverrideUrlLoading(
final WebView view,
final String url) {
final WebResourceRequest request) {

if(url == null) {
if(request == null) {
return false;
}

if(url.startsWith("data:")) {
final Uri url = request.getUrl();

if (Objects.equals(url.getScheme(), "data")) {
// Prevent imgur bug where we're directed to some random data URI
return true;
}
Expand All @@ -307,8 +308,8 @@ public boolean shouldOverrideUrlLoading(
}
} else {

if(RedditURLParser.parse(Uri.parse(url)) != null) {
LinkHandler.onLinkClicked(mActivity, new UriString(url), false);
if(RedditURLParser.parse(url) != null) {
LinkHandler.onLinkClicked(mActivity, UriString.from(url), false);
} else {
// When websites recognize the user agent is on Android, they sometimes
// redirect or offer deep links into native apps. These come in two flavors:
Expand All @@ -328,29 +329,30 @@ public boolean shouldOverrideUrlLoading(
// fail, in which case the logic falls through and treats these URLs as
// HTTP URLs.

if (url.startsWith("intent:")) {
if (Objects.equals(url.getScheme(), "intent")) {
if (onEncounteredIntentUrl(url)) {
return true;
}
} else if (!url.startsWith("http:") && !url.startsWith("https:")) {
} else if (!Objects.equals(url.getScheme(), "http")
&& !Objects.equals(url.getScheme(), "https")) {
if (onEncounteredCustomSchemeUrl(url)) {
return true;
}
}

if(!PrefsUtility.pref_behaviour_useinternalbrowser()) {
if (!PrefsUtility.pref_behaviour_useinternalbrowser()) {
LinkHandler.openWebBrowser(
mActivity,
Uri.parse(url),
url,
true);
} else if(PrefsUtility.pref_behaviour_usecustomtabs()) {
} else if (PrefsUtility.pref_behaviour_usecustomtabs()) {
LinkHandler.openCustomTab(
mActivity,
Uri.parse(url),
url,
null);
} else {
webView.loadUrl(url);
currentUrl = new UriString(url);
webView.loadUrl(url.toString());
currentUrl = UriString.from(url);
}
}
}
Expand All @@ -361,10 +363,10 @@ public boolean shouldOverrideUrlLoading(
/**
* Assumes the {@code url} starts with `intent://`
*/
private boolean onEncounteredIntentUrl(final String url) {
private boolean onEncounteredIntentUrl(final Uri url) {
final Intent nativeAppIntent;
try {
nativeAppIntent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
nativeAppIntent = Intent.parseUri(url.toString(), Intent.URI_INTENT_SCHEME);
} catch (final URISyntaxException e) {
return false;
}
Expand All @@ -387,8 +389,8 @@ private boolean onEncounteredIntentUrl(final String url) {
* Assumes the {@code url} starts with something other than `intent://`, `http://` or
* `https://`
*/
private boolean onEncounteredCustomSchemeUrl(final String url) {
final Intent nativeAppIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
private boolean onEncounteredCustomSchemeUrl(final Uri url) {
final Intent nativeAppIntent = new Intent(Intent.ACTION_VIEW, url);
try {
startActivity(nativeAppIntent);
return true;
Expand Down Expand Up @@ -521,7 +523,7 @@ public void onDestroyView() {
webView.destroy();

final CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
cookieManager.removeAllCookies(null);

super.onDestroyView();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,6 @@ public void clearBrowser() {
this.clearCache(true);
this.clearFormData();
this.clearHistory();
CookieManager.getInstance().removeAllCookie();
CookieManager.getInstance().removeAllCookies(null);
}
}

0 comments on commit 9f1d85f

Please sign in to comment.