Skip to content

Commit 124e4a8

Browse files
jcesarmobileFE-Roger
authored andcommitted
breaking: cleanup code for old android versions (apache#824)
1 parent 5e92c98 commit 124e4a8

File tree

2 files changed

+26
-117
lines changed

2 files changed

+26
-117
lines changed

src/android/InAppBrowser.java

Lines changed: 26 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ Licensed to the Apache Software Foundation (ASF) under one
2020

2121
import android.annotation.SuppressLint;
2222
import android.annotation.TargetApi;
23-
import android.content.ActivityNotFoundException;
24-
import android.content.ComponentName;
2523
import android.content.Context;
2624
import android.content.Intent;
2725
import android.content.pm.PackageManager;
@@ -31,8 +29,6 @@ Licensed to the Apache Software Foundation (ASF) under one
3129
import android.content.res.Resources;
3230
import android.graphics.Bitmap;
3331
import android.graphics.drawable.Drawable;
34-
import android.graphics.PorterDuff;
35-
import android.graphics.PorterDuffColorFilter;
3632
import android.graphics.Color;
3733
import android.net.http.SslError;
3834
import android.net.Uri;
@@ -49,7 +45,6 @@ Licensed to the Apache Software Foundation (ASF) under one
4945
import android.view.inputmethod.EditorInfo;
5046
import android.view.inputmethod.InputMethodManager;
5147
import android.webkit.CookieManager;
52-
import android.webkit.CookieSyncManager;
5348
import android.webkit.HttpAuthHandler;
5449
import android.webkit.JavascriptInterface;
5550
import android.webkit.SslErrorHandler;
@@ -142,10 +137,8 @@ public class InAppBrowser extends CordovaPlugin {
142137
private boolean mediaPlaybackRequiresUserGesture = false;
143138
private boolean shouldPauseInAppBrowser = false;
144139
private boolean useWideViewPort = true;
145-
private ValueCallback<Uri> mUploadCallback;
146-
private ValueCallback<Uri[]> mUploadCallbackLollipop;
140+
private ValueCallback<Uri[]> mUploadCallback;
147141
private final static int FILECHOOSER_REQUESTCODE = 1;
148-
private final static int FILECHOOSER_REQUESTCODE_LOLLIPOP = 2;
149142
private String closeButtonCaption = "";
150143
private String closeButtonColor = "";
151144
private boolean leftToRight = false;
@@ -420,12 +413,7 @@ private void injectDeferredObject(String source, String jsWrapper) {
420413
@SuppressLint("NewApi")
421414
@Override
422415
public void run() {
423-
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
424-
// This action will have the side-effect of blurring the currently focused element
425-
inAppWebView.loadUrl("javascript:" + finalScriptToInject);
426-
} else {
427-
inAppWebView.evaluateJavascript(finalScriptToInject, null);
428-
}
416+
inAppWebView.evaluateJavascript(finalScriptToInject, null);
429417
}
430418
});
431419
} else {
@@ -768,8 +756,7 @@ private View createCloseButton(int id) {
768756
if (closeButtonColor != "") close.setColorFilter(android.graphics.Color.parseColor(closeButtonColor));
769757
close.setImageDrawable(closeIcon);
770758
close.setScaleType(ImageView.ScaleType.FIT_CENTER);
771-
if (Build.VERSION.SDK_INT >= 16)
772-
close.getAdjustViewBounds();
759+
close.getAdjustViewBounds();
773760

774761
_close = close;
775762
}
@@ -778,11 +765,7 @@ private View createCloseButton(int id) {
778765
if (leftToRight) closeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
779766
else closeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
780767
_close.setLayoutParams(closeLayoutParams);
781-
782-
if (Build.VERSION.SDK_INT >= 16)
783-
_close.setBackground(null);
784-
else
785-
_close.setBackgroundDrawable(null);
768+
_close.setBackground(null);
786769

787770
_close.setContentDescription("Close Button");
788771
_close.setId(Integer.valueOf(id));
@@ -872,15 +855,11 @@ public void run() {
872855
int backResId = activityRes.getIdentifier("ic_action_previous_item", "drawable", cordova.getActivity().getPackageName());
873856
Drawable backIcon = activityRes.getDrawable(backResId);
874857
if (navigationButtonColor != "") back.setColorFilter(android.graphics.Color.parseColor(navigationButtonColor));
875-
if (Build.VERSION.SDK_INT >= 16)
876-
back.setBackground(null);
877-
else
878-
back.setBackgroundDrawable(null);
858+
back.setBackground(null);
879859
back.setImageDrawable(backIcon);
880860
back.setScaleType(ImageView.ScaleType.FIT_CENTER);
881861
back.setPadding(0, this.dpToPixels(10), 0, this.dpToPixels(10));
882-
if (Build.VERSION.SDK_INT >= 16)
883-
back.getAdjustViewBounds();
862+
back.getAdjustViewBounds();
884863

885864
back.setOnClickListener(new View.OnClickListener() {
886865
public void onClick(View v) {
@@ -898,15 +877,11 @@ public void onClick(View v) {
898877
int fwdResId = activityRes.getIdentifier("ic_action_next_item", "drawable", cordova.getActivity().getPackageName());
899878
Drawable fwdIcon = activityRes.getDrawable(fwdResId);
900879
if (navigationButtonColor != "") forward.setColorFilter(android.graphics.Color.parseColor(navigationButtonColor));
901-
if (Build.VERSION.SDK_INT >= 16)
902-
forward.setBackground(null);
903-
else
904-
forward.setBackgroundDrawable(null);
880+
forward.setBackground(null);
905881
forward.setImageDrawable(fwdIcon);
906882
forward.setScaleType(ImageView.ScaleType.FIT_CENTER);
907883
forward.setPadding(0, this.dpToPixels(10), 0, this.dpToPixels(10));
908-
if (Build.VERSION.SDK_INT >= 16)
909-
forward.getAdjustViewBounds();
884+
forward.getAdjustViewBounds();
910885

911886
forward.setOnClickListener(new View.OnClickListener() {
912887
public void onClick(View v) {
@@ -1023,46 +998,24 @@ public void onClick(View v) {
1023998
inAppWebView.setId(Integer.valueOf(6));
1024999
// File Chooser Implemented ChromeClient
10251000
inAppWebView.setWebChromeClient(new InAppChromeClient(thatWebView) {
1026-
// For Android 5.0+
10271001
public boolean onShowFileChooser (WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams)
10281002
{
10291003
LOG.d(LOG_TAG, "File Chooser 5.0+");
10301004
// If callback exists, finish it.
1031-
if(mUploadCallbackLollipop != null) {
1032-
mUploadCallbackLollipop.onReceiveValue(null);
1005+
if(mUploadCallback != null) {
1006+
mUploadCallback.onReceiveValue(null);
10331007
}
1034-
mUploadCallbackLollipop = filePathCallback;
1008+
mUploadCallback = filePathCallback;
10351009

10361010
// Create File Chooser Intent
10371011
Intent content = new Intent(Intent.ACTION_GET_CONTENT);
10381012
content.addCategory(Intent.CATEGORY_OPENABLE);
10391013
content.setType("*/*");
10401014

10411015
// Run cordova startActivityForResult
1042-
cordova.startActivityForResult(InAppBrowser.this, Intent.createChooser(content, "Select File"), FILECHOOSER_REQUESTCODE_LOLLIPOP);
1043-
return true;
1044-
}
1045-
1046-
// For Android 4.1+
1047-
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture)
1048-
{
1049-
LOG.d(LOG_TAG, "File Chooser 4.1+");
1050-
// Call file chooser for Android 3.0+
1051-
openFileChooser(uploadMsg, acceptType);
1052-
}
1053-
1054-
// For Android 3.0+
1055-
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType)
1056-
{
1057-
LOG.d(LOG_TAG, "File Chooser 3.0+");
1058-
mUploadCallback = uploadMsg;
1059-
Intent content = new Intent(Intent.ACTION_GET_CONTENT);
1060-
content.addCategory(Intent.CATEGORY_OPENABLE);
1061-
1062-
// run startActivityForResult
10631016
cordova.startActivityForResult(InAppBrowser.this, Intent.createChooser(content, "Select File"), FILECHOOSER_REQUESTCODE);
1017+
return true;
10641018
}
1065-
10661019
});
10671020
currentClient = new InAppBrowserClient(thatWebView, edittext, beforeload);
10681021
inAppWebView.setWebViewClient(currentClient);
@@ -1087,10 +1040,8 @@ public void postMessage(String data) {
10871040
}
10881041
}
10891042

1090-
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
1091-
settings.setMediaPlaybackRequiresUserGesture(mediaPlaybackRequiresUserGesture);
1092-
inAppWebView.addJavascriptInterface(new JsObject(), "cordova_iab");
1093-
}
1043+
settings.setMediaPlaybackRequiresUserGesture(mediaPlaybackRequiresUserGesture);
1044+
inAppWebView.addJavascriptInterface(new JsObject(), "cordova_iab");
10941045

10951046
String overrideUserAgent = preferences.getString("OverrideUserAgent", null);
10961047
String appendUserAgent = preferences.getString("AppendUserAgent", null);
@@ -1118,10 +1069,8 @@ public void postMessage(String data) {
11181069
CookieManager.getInstance().removeSessionCookie();
11191070
}
11201071

1121-
// Enable Thirdparty Cookies on >=Android 5.0 device
1122-
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
1123-
CookieManager.getInstance().setAcceptThirdPartyCookies(inAppWebView,true);
1124-
}
1072+
// Enable Thirdparty Cookies
1073+
CookieManager.getInstance().setAcceptThirdPartyCookies(inAppWebView,true);
11251074

11261075
inAppWebView.loadUrl(url);
11271076
inAppWebView.setId(Integer.valueOf(6));
@@ -1213,32 +1162,14 @@ private void sendUpdate(JSONObject obj, boolean keepCallback, PluginResult.Statu
12131162
* @param intent the data from android file chooser
12141163
*/
12151164
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
1216-
// For Android >= 5.0
1217-
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
1218-
LOG.d(LOG_TAG, "onActivityResult (For Android >= 5.0)");
1219-
// If RequestCode or Callback is Invalid
1220-
if(requestCode != FILECHOOSER_REQUESTCODE_LOLLIPOP || mUploadCallbackLollipop == null) {
1221-
super.onActivityResult(requestCode, resultCode, intent);
1222-
return;
1223-
}
1224-
mUploadCallbackLollipop.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent));
1225-
mUploadCallbackLollipop = null;
1226-
}
1227-
// For Android < 5.0
1228-
else {
1229-
LOG.d(LOG_TAG, "onActivityResult (For Android < 5.0)");
1230-
// If RequestCode or Callback is Invalid
1231-
if(requestCode != FILECHOOSER_REQUESTCODE || mUploadCallback == null) {
1232-
super.onActivityResult(requestCode, resultCode, intent);
1233-
return;
1234-
}
1235-
1236-
if (null == mUploadCallback) return;
1237-
Uri result = intent == null || resultCode != cordova.getActivity().RESULT_OK ? null : intent.getData();
1238-
1239-
mUploadCallback.onReceiveValue(result);
1240-
mUploadCallback = null;
1165+
LOG.d(LOG_TAG, "onActivityResult");
1166+
// If RequestCode or Callback is Invalid
1167+
if(requestCode != FILECHOOSER_REQUESTCODE || mUploadCallback == null) {
1168+
super.onActivityResult(requestCode, resultCode, intent);
1169+
return;
12411170
}
1171+
mUploadCallback.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent));
1172+
mUploadCallback = null;
12421173
}
12431174

12441175
/**
@@ -1471,28 +1402,13 @@ private boolean sendBeforeLoad(String url, String method) {
14711402
return false;
14721403
}
14731404

1474-
1475-
/**
1476-
* Legacy (deprecated in API 21)
1477-
* For Android 4.4 and below.
1478-
* @param view
1479-
* @param url
1480-
* @return
1481-
*/
1482-
@SuppressWarnings("deprecation")
1483-
@Override
1484-
public WebResourceResponse shouldInterceptRequest (final WebView view, String url) {
1485-
return shouldInterceptRequest(url, super.shouldInterceptRequest(view, url), null);
1486-
}
1487-
14881405
/**
14891406
* New (added in API 21)
14901407
* For Android 5.0 and above.
14911408
*
1492-
* @param webView
1409+
* @param view
14931410
* @param request
14941411
*/
1495-
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
14961412
@Override
14971413
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
14981414
return shouldInterceptRequest(request.getUrl().toString(), super.shouldInterceptRequest(view, request), request.getMethod());
@@ -1543,16 +1459,10 @@ public void onPageFinished(WebView view, String url) {
15431459
super.onPageFinished(view, url);
15441460

15451461
// Set the namespace for postMessage()
1546-
if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
1547-
injectDeferredObject("window.webkit={messageHandlers:{cordova_iab:cordova_iab}}", null);
1548-
}
1462+
injectDeferredObject("window.webkit={messageHandlers:{cordova_iab:cordova_iab}}", null);
15491463

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

15571467
// https://issues.apache.org/jira/browse/CB-11248
15581468
view.clearFocus();

src/android/InAppChromeClient.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGest
157157
WebView inAppWebView = view;
158158
final WebViewClient webViewClient =
159159
new WebViewClient() {
160-
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
161160
@Override
162161
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
163162
inAppWebView.loadUrl(request.getUrl().toString());

0 commit comments

Comments
 (0)