breaking: cleanup code for old android versions (#824)
This commit is contained in:
parent
80720a0850
commit
b496e03169
@ -20,7 +20,6 @@ package org.apache.cordova.inappbrowser;
|
|||||||
|
|
||||||
import android.annotation.SuppressLint;
|
import android.annotation.SuppressLint;
|
||||||
import android.annotation.TargetApi;
|
import android.annotation.TargetApi;
|
||||||
import android.content.ComponentName;
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
@ -30,8 +29,6 @@ import android.provider.Browser;
|
|||||||
import android.content.res.Resources;
|
import android.content.res.Resources;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
import android.graphics.PorterDuff;
|
|
||||||
import android.graphics.PorterDuffColorFilter;
|
|
||||||
import android.graphics.Color;
|
import android.graphics.Color;
|
||||||
import android.net.http.SslError;
|
import android.net.http.SslError;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
@ -48,7 +45,6 @@ import android.view.WindowManager.LayoutParams;
|
|||||||
import android.view.inputmethod.EditorInfo;
|
import android.view.inputmethod.EditorInfo;
|
||||||
import android.view.inputmethod.InputMethodManager;
|
import android.view.inputmethod.InputMethodManager;
|
||||||
import android.webkit.CookieManager;
|
import android.webkit.CookieManager;
|
||||||
import android.webkit.CookieSyncManager;
|
|
||||||
import android.webkit.HttpAuthHandler;
|
import android.webkit.HttpAuthHandler;
|
||||||
import android.webkit.JavascriptInterface;
|
import android.webkit.JavascriptInterface;
|
||||||
import android.webkit.SslErrorHandler;
|
import android.webkit.SslErrorHandler;
|
||||||
@ -136,10 +132,8 @@ public class InAppBrowser extends CordovaPlugin {
|
|||||||
private boolean mediaPlaybackRequiresUserGesture = false;
|
private boolean mediaPlaybackRequiresUserGesture = false;
|
||||||
private boolean shouldPauseInAppBrowser = false;
|
private boolean shouldPauseInAppBrowser = false;
|
||||||
private boolean useWideViewPort = true;
|
private boolean useWideViewPort = true;
|
||||||
private ValueCallback<Uri> mUploadCallback;
|
private ValueCallback<Uri[]> mUploadCallback;
|
||||||
private ValueCallback<Uri[]> mUploadCallbackLollipop;
|
|
||||||
private final static int FILECHOOSER_REQUESTCODE = 1;
|
private final static int FILECHOOSER_REQUESTCODE = 1;
|
||||||
private final static int FILECHOOSER_REQUESTCODE_LOLLIPOP = 2;
|
|
||||||
private String closeButtonCaption = "";
|
private String closeButtonCaption = "";
|
||||||
private String closeButtonColor = "";
|
private String closeButtonColor = "";
|
||||||
private boolean leftToRight = false;
|
private boolean leftToRight = false;
|
||||||
@ -414,12 +408,7 @@ public class InAppBrowser extends CordovaPlugin {
|
|||||||
@SuppressLint("NewApi")
|
@SuppressLint("NewApi")
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
|
inAppWebView.evaluateJavascript(finalScriptToInject, null);
|
||||||
// This action will have the side-effect of blurring the currently focused element
|
|
||||||
inAppWebView.loadUrl("javascript:" + finalScriptToInject);
|
|
||||||
} else {
|
|
||||||
inAppWebView.evaluateJavascript(finalScriptToInject, null);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
@ -762,8 +751,7 @@ public class InAppBrowser extends CordovaPlugin {
|
|||||||
if (closeButtonColor != "") close.setColorFilter(android.graphics.Color.parseColor(closeButtonColor));
|
if (closeButtonColor != "") close.setColorFilter(android.graphics.Color.parseColor(closeButtonColor));
|
||||||
close.setImageDrawable(closeIcon);
|
close.setImageDrawable(closeIcon);
|
||||||
close.setScaleType(ImageView.ScaleType.FIT_CENTER);
|
close.setScaleType(ImageView.ScaleType.FIT_CENTER);
|
||||||
if (Build.VERSION.SDK_INT >= 16)
|
close.getAdjustViewBounds();
|
||||||
close.getAdjustViewBounds();
|
|
||||||
|
|
||||||
_close = close;
|
_close = close;
|
||||||
}
|
}
|
||||||
@ -772,11 +760,7 @@ public class InAppBrowser extends CordovaPlugin {
|
|||||||
if (leftToRight) closeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
|
if (leftToRight) closeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
|
||||||
else closeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
|
else closeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
|
||||||
_close.setLayoutParams(closeLayoutParams);
|
_close.setLayoutParams(closeLayoutParams);
|
||||||
|
_close.setBackground(null);
|
||||||
if (Build.VERSION.SDK_INT >= 16)
|
|
||||||
_close.setBackground(null);
|
|
||||||
else
|
|
||||||
_close.setBackgroundDrawable(null);
|
|
||||||
|
|
||||||
_close.setContentDescription("Close Button");
|
_close.setContentDescription("Close Button");
|
||||||
_close.setId(Integer.valueOf(id));
|
_close.setId(Integer.valueOf(id));
|
||||||
@ -845,15 +829,11 @@ public class InAppBrowser extends CordovaPlugin {
|
|||||||
int backResId = activityRes.getIdentifier("ic_action_previous_item", "drawable", cordova.getActivity().getPackageName());
|
int backResId = activityRes.getIdentifier("ic_action_previous_item", "drawable", cordova.getActivity().getPackageName());
|
||||||
Drawable backIcon = activityRes.getDrawable(backResId);
|
Drawable backIcon = activityRes.getDrawable(backResId);
|
||||||
if (navigationButtonColor != "") back.setColorFilter(android.graphics.Color.parseColor(navigationButtonColor));
|
if (navigationButtonColor != "") back.setColorFilter(android.graphics.Color.parseColor(navigationButtonColor));
|
||||||
if (Build.VERSION.SDK_INT >= 16)
|
back.setBackground(null);
|
||||||
back.setBackground(null);
|
|
||||||
else
|
|
||||||
back.setBackgroundDrawable(null);
|
|
||||||
back.setImageDrawable(backIcon);
|
back.setImageDrawable(backIcon);
|
||||||
back.setScaleType(ImageView.ScaleType.FIT_CENTER);
|
back.setScaleType(ImageView.ScaleType.FIT_CENTER);
|
||||||
back.setPadding(0, this.dpToPixels(10), 0, this.dpToPixels(10));
|
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() {
|
back.setOnClickListener(new View.OnClickListener() {
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
@ -871,15 +851,11 @@ public class InAppBrowser extends CordovaPlugin {
|
|||||||
int fwdResId = activityRes.getIdentifier("ic_action_next_item", "drawable", cordova.getActivity().getPackageName());
|
int fwdResId = activityRes.getIdentifier("ic_action_next_item", "drawable", cordova.getActivity().getPackageName());
|
||||||
Drawable fwdIcon = activityRes.getDrawable(fwdResId);
|
Drawable fwdIcon = activityRes.getDrawable(fwdResId);
|
||||||
if (navigationButtonColor != "") forward.setColorFilter(android.graphics.Color.parseColor(navigationButtonColor));
|
if (navigationButtonColor != "") forward.setColorFilter(android.graphics.Color.parseColor(navigationButtonColor));
|
||||||
if (Build.VERSION.SDK_INT >= 16)
|
forward.setBackground(null);
|
||||||
forward.setBackground(null);
|
|
||||||
else
|
|
||||||
forward.setBackgroundDrawable(null);
|
|
||||||
forward.setImageDrawable(fwdIcon);
|
forward.setImageDrawable(fwdIcon);
|
||||||
forward.setScaleType(ImageView.ScaleType.FIT_CENTER);
|
forward.setScaleType(ImageView.ScaleType.FIT_CENTER);
|
||||||
forward.setPadding(0, this.dpToPixels(10), 0, this.dpToPixels(10));
|
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() {
|
forward.setOnClickListener(new View.OnClickListener() {
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
@ -942,15 +918,14 @@ public class InAppBrowser extends CordovaPlugin {
|
|||||||
inAppWebView.setId(Integer.valueOf(6));
|
inAppWebView.setId(Integer.valueOf(6));
|
||||||
// File Chooser Implemented ChromeClient
|
// File Chooser Implemented ChromeClient
|
||||||
inAppWebView.setWebChromeClient(new InAppChromeClient(thatWebView) {
|
inAppWebView.setWebChromeClient(new InAppChromeClient(thatWebView) {
|
||||||
// For Android 5.0+
|
|
||||||
public boolean onShowFileChooser (WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams)
|
public boolean onShowFileChooser (WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams)
|
||||||
{
|
{
|
||||||
LOG.d(LOG_TAG, "File Chooser 5.0+");
|
LOG.d(LOG_TAG, "File Chooser 5.0+");
|
||||||
// If callback exists, finish it.
|
// If callback exists, finish it.
|
||||||
if(mUploadCallbackLollipop != null) {
|
if(mUploadCallback != null) {
|
||||||
mUploadCallbackLollipop.onReceiveValue(null);
|
mUploadCallback.onReceiveValue(null);
|
||||||
}
|
}
|
||||||
mUploadCallbackLollipop = filePathCallback;
|
mUploadCallback = filePathCallback;
|
||||||
|
|
||||||
// Create File Chooser Intent
|
// Create File Chooser Intent
|
||||||
Intent content = new Intent(Intent.ACTION_GET_CONTENT);
|
Intent content = new Intent(Intent.ACTION_GET_CONTENT);
|
||||||
@ -958,30 +933,9 @@ public class InAppBrowser extends CordovaPlugin {
|
|||||||
content.setType("*/*");
|
content.setType("*/*");
|
||||||
|
|
||||||
// Run cordova startActivityForResult
|
// Run cordova startActivityForResult
|
||||||
cordova.startActivityForResult(InAppBrowser.this, Intent.createChooser(content, "Select File"), FILECHOOSER_REQUESTCODE_LOLLIPOP);
|
cordova.startActivityForResult(InAppBrowser.this, Intent.createChooser(content, "Select File"), FILECHOOSER_REQUESTCODE);
|
||||||
return true;
|
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
});
|
||||||
currentClient = new InAppBrowserClient(thatWebView, edittext, beforeload);
|
currentClient = new InAppBrowserClient(thatWebView, edittext, beforeload);
|
||||||
inAppWebView.setWebViewClient(currentClient);
|
inAppWebView.setWebViewClient(currentClient);
|
||||||
@ -1006,10 +960,8 @@ public class InAppBrowser extends CordovaPlugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
|
settings.setMediaPlaybackRequiresUserGesture(mediaPlaybackRequiresUserGesture);
|
||||||
settings.setMediaPlaybackRequiresUserGesture(mediaPlaybackRequiresUserGesture);
|
inAppWebView.addJavascriptInterface(new JsObject(), "cordova_iab");
|
||||||
inAppWebView.addJavascriptInterface(new JsObject(), "cordova_iab");
|
|
||||||
}
|
|
||||||
|
|
||||||
String overrideUserAgent = preferences.getString("OverrideUserAgent", null);
|
String overrideUserAgent = preferences.getString("OverrideUserAgent", null);
|
||||||
String appendUserAgent = preferences.getString("AppendUserAgent", null);
|
String appendUserAgent = preferences.getString("AppendUserAgent", null);
|
||||||
@ -1037,10 +989,8 @@ public class InAppBrowser extends CordovaPlugin {
|
|||||||
CookieManager.getInstance().removeSessionCookie();
|
CookieManager.getInstance().removeSessionCookie();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Enable Thirdparty Cookies on >=Android 5.0 device
|
// Enable Thirdparty Cookies
|
||||||
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
|
CookieManager.getInstance().setAcceptThirdPartyCookies(inAppWebView,true);
|
||||||
CookieManager.getInstance().setAcceptThirdPartyCookies(inAppWebView,true);
|
|
||||||
}
|
|
||||||
|
|
||||||
inAppWebView.loadUrl(url);
|
inAppWebView.loadUrl(url);
|
||||||
inAppWebView.setId(Integer.valueOf(6));
|
inAppWebView.setId(Integer.valueOf(6));
|
||||||
@ -1131,32 +1081,14 @@ public class InAppBrowser extends CordovaPlugin {
|
|||||||
* @param intent the data from android file chooser
|
* @param intent the data from android file chooser
|
||||||
*/
|
*/
|
||||||
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
|
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
|
||||||
// For Android >= 5.0
|
LOG.d(LOG_TAG, "onActivityResult");
|
||||||
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
// If RequestCode or Callback is Invalid
|
||||||
LOG.d(LOG_TAG, "onActivityResult (For Android >= 5.0)");
|
if(requestCode != FILECHOOSER_REQUESTCODE || mUploadCallback == null) {
|
||||||
// If RequestCode or Callback is Invalid
|
super.onActivityResult(requestCode, resultCode, intent);
|
||||||
if(requestCode != FILECHOOSER_REQUESTCODE_LOLLIPOP || mUploadCallbackLollipop == null) {
|
return;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
mUploadCallback.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent));
|
||||||
|
mUploadCallback = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1356,28 +1288,13 @@ public class InAppBrowser extends CordovaPlugin {
|
|||||||
return false;
|
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)
|
* New (added in API 21)
|
||||||
* For Android 5.0 and above.
|
* For Android 5.0 and above.
|
||||||
*
|
*
|
||||||
* @param webView
|
* @param view
|
||||||
* @param request
|
* @param request
|
||||||
*/
|
*/
|
||||||
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
|
|
||||||
@Override
|
@Override
|
||||||
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
|
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
|
||||||
return shouldInterceptRequest(request.getUrl().toString(), super.shouldInterceptRequest(view, request), request.getMethod());
|
return shouldInterceptRequest(request.getUrl().toString(), super.shouldInterceptRequest(view, request), request.getMethod());
|
||||||
@ -1428,16 +1345,10 @@ public class InAppBrowser extends CordovaPlugin {
|
|||||||
super.onPageFinished(view, url);
|
super.onPageFinished(view, url);
|
||||||
|
|
||||||
// Set the namespace for postMessage()
|
// 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
|
// 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();
|
||||||
CookieManager.getInstance().flush();
|
|
||||||
} else {
|
|
||||||
CookieSyncManager.getInstance().sync();
|
|
||||||
}
|
|
||||||
|
|
||||||
// https://issues.apache.org/jira/browse/CB-11248
|
// https://issues.apache.org/jira/browse/CB-11248
|
||||||
view.clearFocus();
|
view.clearFocus();
|
||||||
|
@ -157,7 +157,6 @@ public class InAppChromeClient extends WebChromeClient {
|
|||||||
WebView inAppWebView = view;
|
WebView inAppWebView = view;
|
||||||
final WebViewClient webViewClient =
|
final WebViewClient webViewClient =
|
||||||
new WebViewClient() {
|
new WebViewClient() {
|
||||||
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
|
|
||||||
@Override
|
@Override
|
||||||
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
|
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
|
||||||
inAppWebView.loadUrl(request.getUrl().toString());
|
inAppWebView.loadUrl(request.getUrl().toString());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user