Skip to content
Permalink
Browse files
breaking: cleanup code for old android versions (#824)
  • Loading branch information
jcesarmobile committed Jan 20, 2021
1 parent 80720a0 commit b496e03169b45f4207e66a0c6dab77c52d939a73
Showing 2 changed files with 26 additions and 116 deletions.
@@ -20,7 +20,6 @@ Licensed to the Apache Software Foundation (ASF) under one

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
@@ -30,8 +29,6 @@ Licensed to the Apache Software Foundation (ASF) under one
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.Color;
import android.net.http.SslError;
import android.net.Uri;
@@ -48,7 +45,6 @@ Licensed to the Apache Software Foundation (ASF) under one
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.HttpAuthHandler;
import android.webkit.JavascriptInterface;
import android.webkit.SslErrorHandler;
@@ -136,10 +132,8 @@ public class InAppBrowser extends CordovaPlugin {
private boolean mediaPlaybackRequiresUserGesture = false;
private boolean shouldPauseInAppBrowser = false;
private boolean useWideViewPort = true;
private ValueCallback<Uri> mUploadCallback;
private ValueCallback<Uri[]> mUploadCallbackLollipop;
private ValueCallback<Uri[]> mUploadCallback;
private final static int FILECHOOSER_REQUESTCODE = 1;
private final static int FILECHOOSER_REQUESTCODE_LOLLIPOP = 2;
private String closeButtonCaption = "";
private String closeButtonColor = "";
private boolean leftToRight = false;
@@ -414,12 +408,7 @@ private void injectDeferredObject(String source, String jsWrapper) {
@SuppressLint("NewApi")
@Override
public void run() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
// This action will have the side-effect of blurring the currently focused element
inAppWebView.loadUrl("javascript:" + finalScriptToInject);
} else {
inAppWebView.evaluateJavascript(finalScriptToInject, null);
}
inAppWebView.evaluateJavascript(finalScriptToInject, null);
}
});
} else {
@@ -762,8 +751,7 @@ private View createCloseButton(int id) {
if (closeButtonColor != "") close.setColorFilter(android.graphics.Color.parseColor(closeButtonColor));
close.setImageDrawable(closeIcon);
close.setScaleType(ImageView.ScaleType.FIT_CENTER);
if (Build.VERSION.SDK_INT >= 16)
close.getAdjustViewBounds();
close.getAdjustViewBounds();

_close = close;
}
@@ -772,11 +760,7 @@ private View createCloseButton(int id) {
if (leftToRight) closeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
else closeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
_close.setLayoutParams(closeLayoutParams);

if (Build.VERSION.SDK_INT >= 16)
_close.setBackground(null);
else
_close.setBackgroundDrawable(null);
_close.setBackground(null);

_close.setContentDescription("Close Button");
_close.setId(Integer.valueOf(id));
@@ -845,15 +829,11 @@ public void run() {
int backResId = activityRes.getIdentifier("ic_action_previous_item", "drawable", cordova.getActivity().getPackageName());
Drawable backIcon = activityRes.getDrawable(backResId);
if (navigationButtonColor != "") back.setColorFilter(android.graphics.Color.parseColor(navigationButtonColor));
if (Build.VERSION.SDK_INT >= 16)
back.setBackground(null);
else
back.setBackgroundDrawable(null);
back.setBackground(null);
back.setImageDrawable(backIcon);
back.setScaleType(ImageView.ScaleType.FIT_CENTER);
back.setPadding(0, this.dpToPixels(10), 0, this.dpToPixels(10));
if (Build.VERSION.SDK_INT >= 16)
back.getAdjustViewBounds();
back.getAdjustViewBounds();

back.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
@@ -871,15 +851,11 @@ public void onClick(View v) {
int fwdResId = activityRes.getIdentifier("ic_action_next_item", "drawable", cordova.getActivity().getPackageName());
Drawable fwdIcon = activityRes.getDrawable(fwdResId);
if (navigationButtonColor != "") forward.setColorFilter(android.graphics.Color.parseColor(navigationButtonColor));
if (Build.VERSION.SDK_INT >= 16)
forward.setBackground(null);
else
forward.setBackgroundDrawable(null);
forward.setBackground(null);
forward.setImageDrawable(fwdIcon);
forward.setScaleType(ImageView.ScaleType.FIT_CENTER);
forward.setPadding(0, this.dpToPixels(10), 0, this.dpToPixels(10));
if (Build.VERSION.SDK_INT >= 16)
forward.getAdjustViewBounds();
forward.getAdjustViewBounds();

forward.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
@@ -942,46 +918,24 @@ public boolean onKey(View v, int keyCode, KeyEvent event) {
inAppWebView.setId(Integer.valueOf(6));
// File Chooser Implemented ChromeClient
inAppWebView.setWebChromeClient(new InAppChromeClient(thatWebView) {
// For Android 5.0+
public boolean onShowFileChooser (WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams)
{
LOG.d(LOG_TAG, "File Chooser 5.0+");
// If callback exists, finish it.
if(mUploadCallbackLollipop != null) {
mUploadCallbackLollipop.onReceiveValue(null);
if(mUploadCallback != null) {
mUploadCallback.onReceiveValue(null);
}
mUploadCallbackLollipop = filePathCallback;
mUploadCallback = filePathCallback;

// Create File Chooser Intent
Intent content = new Intent(Intent.ACTION_GET_CONTENT);
content.addCategory(Intent.CATEGORY_OPENABLE);
content.setType("*/*");

// Run cordova startActivityForResult
cordova.startActivityForResult(InAppBrowser.this, Intent.createChooser(content, "Select File"), FILECHOOSER_REQUESTCODE_LOLLIPOP);
return true;
}

// For Android 4.1+
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture)
{
LOG.d(LOG_TAG, "File Chooser 4.1+");
// Call file chooser for Android 3.0+
openFileChooser(uploadMsg, acceptType);
}

// For Android 3.0+
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType)
{
LOG.d(LOG_TAG, "File Chooser 3.0+");
mUploadCallback = uploadMsg;
Intent content = new Intent(Intent.ACTION_GET_CONTENT);
content.addCategory(Intent.CATEGORY_OPENABLE);

// run startActivityForResult
cordova.startActivityForResult(InAppBrowser.this, Intent.createChooser(content, "Select File"), FILECHOOSER_REQUESTCODE);
return true;
}

});
currentClient = new InAppBrowserClient(thatWebView, edittext, beforeload);
inAppWebView.setWebViewClient(currentClient);
@@ -1006,10 +960,8 @@ public void postMessage(String data) {
}
}

if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
settings.setMediaPlaybackRequiresUserGesture(mediaPlaybackRequiresUserGesture);
inAppWebView.addJavascriptInterface(new JsObject(), "cordova_iab");
}
settings.setMediaPlaybackRequiresUserGesture(mediaPlaybackRequiresUserGesture);
inAppWebView.addJavascriptInterface(new JsObject(), "cordova_iab");

String overrideUserAgent = preferences.getString("OverrideUserAgent", null);
String appendUserAgent = preferences.getString("AppendUserAgent", null);
@@ -1037,10 +989,8 @@ public void postMessage(String data) {
CookieManager.getInstance().removeSessionCookie();
}

// Enable Thirdparty Cookies on >=Android 5.0 device
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
CookieManager.getInstance().setAcceptThirdPartyCookies(inAppWebView,true);
}
// Enable Thirdparty Cookies
CookieManager.getInstance().setAcceptThirdPartyCookies(inAppWebView,true);

inAppWebView.loadUrl(url);
inAppWebView.setId(Integer.valueOf(6));
@@ -1131,32 +1081,14 @@ private void sendUpdate(JSONObject obj, boolean keepCallback, PluginResult.Statu
* @param intent the data from android file chooser
*/
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
// For Android >= 5.0
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
LOG.d(LOG_TAG, "onActivityResult (For Android >= 5.0)");
// If RequestCode or Callback is Invalid
if(requestCode != FILECHOOSER_REQUESTCODE_LOLLIPOP || mUploadCallbackLollipop == null) {
super.onActivityResult(requestCode, resultCode, intent);
return;
}
mUploadCallbackLollipop.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent));
mUploadCallbackLollipop = null;
}
// For Android < 5.0
else {
LOG.d(LOG_TAG, "onActivityResult (For Android < 5.0)");
// If RequestCode or Callback is Invalid
if(requestCode != FILECHOOSER_REQUESTCODE || mUploadCallback == null) {
super.onActivityResult(requestCode, resultCode, intent);
return;
}

if (null == mUploadCallback) return;
Uri result = intent == null || resultCode != cordova.getActivity().RESULT_OK ? null : intent.getData();

mUploadCallback.onReceiveValue(result);
mUploadCallback = null;
LOG.d(LOG_TAG, "onActivityResult");
// If RequestCode or Callback is Invalid
if(requestCode != FILECHOOSER_REQUESTCODE || mUploadCallback == null) {
super.onActivityResult(requestCode, resultCode, intent);
return;
}
mUploadCallback.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent));
mUploadCallback = null;
}

/**
@@ -1356,28 +1288,13 @@ private boolean sendBeforeLoad(String url, String method) {
return false;
}


/**
* Legacy (deprecated in API 21)
* For Android 4.4 and below.
* @param view
* @param url
* @return
*/
@SuppressWarnings("deprecation")
@Override
public WebResourceResponse shouldInterceptRequest (final WebView view, String url) {
return shouldInterceptRequest(url, super.shouldInterceptRequest(view, url), null);
}

/**
* New (added in API 21)
* For Android 5.0 and above.
*
* @param webView
* @param view
* @param request
*/
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
return shouldInterceptRequest(request.getUrl().toString(), super.shouldInterceptRequest(view, request), request.getMethod());
@@ -1428,16 +1345,10 @@ public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);

// Set the namespace for postMessage()
if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
injectDeferredObject("window.webkit={messageHandlers:{cordova_iab:cordova_iab}}", null);
}
injectDeferredObject("window.webkit={messageHandlers:{cordova_iab:cordova_iab}}", null);

// CB-10395 InAppBrowser's WebView not storing cookies reliable to local device storage
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
CookieManager.getInstance().flush();
} else {
CookieSyncManager.getInstance().sync();
}
CookieManager.getInstance().flush();

// https://issues.apache.org/jira/browse/CB-11248
view.clearFocus();
@@ -157,7 +157,6 @@ public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGest
WebView inAppWebView = view;
final WebViewClient webViewClient =
new WebViewClient() {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
inAppWebView.loadUrl(request.getUrl().toString());

0 comments on commit b496e03

Please sign in to comment.