Skip to content

Commit b496e03

Browse files
authored
breaking: cleanup code for old android versions (apache#824)
1 parent 80720a0 commit b496e03

File tree

2 files changed

+26
-116
lines changed

2 files changed

+26
-116
lines changed

src/android/InAppBrowser.java

Lines changed: 26 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +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.ComponentName;
2423
import android.content.Context;
2524
import android.content.Intent;
2625
import android.content.pm.PackageManager;
@@ -30,8 +29,6 @@ Licensed to the Apache Software Foundation (ASF) under one
3029
import android.content.res.Resources;
3130
import android.graphics.Bitmap;
3231
import android.graphics.drawable.Drawable;
33-
import android.graphics.PorterDuff;
34-
import android.graphics.PorterDuffColorFilter;
3532
import android.graphics.Color;
3633
import android.net.http.SslError;
3734
import android.net.Uri;
@@ -48,7 +45,6 @@ Licensed to the Apache Software Foundation (ASF) under one
4845
import android.view.inputmethod.EditorInfo;
4946
import android.view.inputmethod.InputMethodManager;
5047
import android.webkit.CookieManager;
51-
import android.webkit.CookieSyncManager;
5248
import android.webkit.HttpAuthHandler;
5349
import android.webkit.JavascriptInterface;
5450
import android.webkit.SslErrorHandler;
@@ -136,10 +132,8 @@ public class InAppBrowser extends CordovaPlugin {
136132
private boolean mediaPlaybackRequiresUserGesture = false;
137133
private boolean shouldPauseInAppBrowser = false;
138134
private boolean useWideViewPort = true;
139-
private ValueCallback<Uri> mUploadCallback;
140-
private ValueCallback<Uri[]> mUploadCallbackLollipop;
135+
private ValueCallback<Uri[]> mUploadCallback;
141136
private final static int FILECHOOSER_REQUESTCODE = 1;
142-
private final static int FILECHOOSER_REQUESTCODE_LOLLIPOP = 2;
143137
private String closeButtonCaption = "";
144138
private String closeButtonColor = "";
145139
private boolean leftToRight = false;
@@ -414,12 +408,7 @@ private void injectDeferredObject(String source, String jsWrapper) {
414408
@SuppressLint("NewApi")
415409
@Override
416410
public void run() {
417-
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
418-
// This action will have the side-effect of blurring the currently focused element
419-
inAppWebView.loadUrl("javascript:" + finalScriptToInject);
420-
} else {
421-
inAppWebView.evaluateJavascript(finalScriptToInject, null);
422-
}
411+
inAppWebView.evaluateJavascript(finalScriptToInject, null);
423412
}
424413
});
425414
} else {
@@ -762,8 +751,7 @@ private View createCloseButton(int id) {
762751
if (closeButtonColor != "") close.setColorFilter(android.graphics.Color.parseColor(closeButtonColor));
763752
close.setImageDrawable(closeIcon);
764753
close.setScaleType(ImageView.ScaleType.FIT_CENTER);
765-
if (Build.VERSION.SDK_INT >= 16)
766-
close.getAdjustViewBounds();
754+
close.getAdjustViewBounds();
767755

768756
_close = close;
769757
}
@@ -772,11 +760,7 @@ private View createCloseButton(int id) {
772760
if (leftToRight) closeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
773761
else closeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
774762
_close.setLayoutParams(closeLayoutParams);
775-
776-
if (Build.VERSION.SDK_INT >= 16)
777-
_close.setBackground(null);
778-
else
779-
_close.setBackgroundDrawable(null);
763+
_close.setBackground(null);
780764

781765
_close.setContentDescription("Close Button");
782766
_close.setId(Integer.valueOf(id));
@@ -845,15 +829,11 @@ public void run() {
845829
int backResId = activityRes.getIdentifier("ic_action_previous_item", "drawable", cordova.getActivity().getPackageName());
846830
Drawable backIcon = activityRes.getDrawable(backResId);
847831
if (navigationButtonColor != "") back.setColorFilter(android.graphics.Color.parseColor(navigationButtonColor));
848-
if (Build.VERSION.SDK_INT >= 16)
849-
back.setBackground(null);
850-
else
851-
back.setBackgroundDrawable(null);
832+
back.setBackground(null);
852833
back.setImageDrawable(backIcon);
853834
back.setScaleType(ImageView.ScaleType.FIT_CENTER);
854835
back.setPadding(0, this.dpToPixels(10), 0, this.dpToPixels(10));
855-
if (Build.VERSION.SDK_INT >= 16)
856-
back.getAdjustViewBounds();
836+
back.getAdjustViewBounds();
857837

858838
back.setOnClickListener(new View.OnClickListener() {
859839
public void onClick(View v) {
@@ -871,15 +851,11 @@ public void onClick(View v) {
871851
int fwdResId = activityRes.getIdentifier("ic_action_next_item", "drawable", cordova.getActivity().getPackageName());
872852
Drawable fwdIcon = activityRes.getDrawable(fwdResId);
873853
if (navigationButtonColor != "") forward.setColorFilter(android.graphics.Color.parseColor(navigationButtonColor));
874-
if (Build.VERSION.SDK_INT >= 16)
875-
forward.setBackground(null);
876-
else
877-
forward.setBackgroundDrawable(null);
854+
forward.setBackground(null);
878855
forward.setImageDrawable(fwdIcon);
879856
forward.setScaleType(ImageView.ScaleType.FIT_CENTER);
880857
forward.setPadding(0, this.dpToPixels(10), 0, this.dpToPixels(10));
881-
if (Build.VERSION.SDK_INT >= 16)
882-
forward.getAdjustViewBounds();
858+
forward.getAdjustViewBounds();
883859

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

955930
// Create File Chooser Intent
956931
Intent content = new Intent(Intent.ACTION_GET_CONTENT);
957932
content.addCategory(Intent.CATEGORY_OPENABLE);
958933
content.setType("*/*");
959934

960935
// Run cordova startActivityForResult
961-
cordova.startActivityForResult(InAppBrowser.this, Intent.createChooser(content, "Select File"), FILECHOOSER_REQUESTCODE_LOLLIPOP);
962-
return true;
963-
}
964-
965-
// For Android 4.1+
966-
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture)
967-
{
968-
LOG.d(LOG_TAG, "File Chooser 4.1+");
969-
// Call file chooser for Android 3.0+
970-
openFileChooser(uploadMsg, acceptType);
971-
}
972-
973-
// For Android 3.0+
974-
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType)
975-
{
976-
LOG.d(LOG_TAG, "File Chooser 3.0+");
977-
mUploadCallback = uploadMsg;
978-
Intent content = new Intent(Intent.ACTION_GET_CONTENT);
979-
content.addCategory(Intent.CATEGORY_OPENABLE);
980-
981-
// run startActivityForResult
982936
cordova.startActivityForResult(InAppBrowser.this, Intent.createChooser(content, "Select File"), FILECHOOSER_REQUESTCODE);
937+
return true;
983938
}
984-
985939
});
986940
currentClient = new InAppBrowserClient(thatWebView, edittext, beforeload);
987941
inAppWebView.setWebViewClient(currentClient);
@@ -1006,10 +960,8 @@ public void postMessage(String data) {
1006960
}
1007961
}
1008962

1009-
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
1010-
settings.setMediaPlaybackRequiresUserGesture(mediaPlaybackRequiresUserGesture);
1011-
inAppWebView.addJavascriptInterface(new JsObject(), "cordova_iab");
1012-
}
963+
settings.setMediaPlaybackRequiresUserGesture(mediaPlaybackRequiresUserGesture);
964+
inAppWebView.addJavascriptInterface(new JsObject(), "cordova_iab");
1013965

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

1040-
// Enable Thirdparty Cookies on >=Android 5.0 device
1041-
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
1042-
CookieManager.getInstance().setAcceptThirdPartyCookies(inAppWebView,true);
1043-
}
992+
// Enable Thirdparty Cookies
993+
CookieManager.getInstance().setAcceptThirdPartyCookies(inAppWebView,true);
1044994

1045995
inAppWebView.loadUrl(url);
1046996
inAppWebView.setId(Integer.valueOf(6));
@@ -1131,32 +1081,14 @@ private void sendUpdate(JSONObject obj, boolean keepCallback, PluginResult.Statu
11311081
* @param intent the data from android file chooser
11321082
*/
11331083
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
1134-
// For Android >= 5.0
1135-
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
1136-
LOG.d(LOG_TAG, "onActivityResult (For Android >= 5.0)");
1137-
// If RequestCode or Callback is Invalid
1138-
if(requestCode != FILECHOOSER_REQUESTCODE_LOLLIPOP || mUploadCallbackLollipop == null) {
1139-
super.onActivityResult(requestCode, resultCode, intent);
1140-
return;
1141-
}
1142-
mUploadCallbackLollipop.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent));
1143-
mUploadCallbackLollipop = null;
1144-
}
1145-
// For Android < 5.0
1146-
else {
1147-
LOG.d(LOG_TAG, "onActivityResult (For Android < 5.0)");
1148-
// If RequestCode or Callback is Invalid
1149-
if(requestCode != FILECHOOSER_REQUESTCODE || mUploadCallback == null) {
1150-
super.onActivityResult(requestCode, resultCode, intent);
1151-
return;
1152-
}
1153-
1154-
if (null == mUploadCallback) return;
1155-
Uri result = intent == null || resultCode != cordova.getActivity().RESULT_OK ? null : intent.getData();
1156-
1157-
mUploadCallback.onReceiveValue(result);
1158-
mUploadCallback = null;
1084+
LOG.d(LOG_TAG, "onActivityResult");
1085+
// If RequestCode or Callback is Invalid
1086+
if(requestCode != FILECHOOSER_REQUESTCODE || mUploadCallback == null) {
1087+
super.onActivityResult(requestCode, resultCode, intent);
1088+
return;
11591089
}
1090+
mUploadCallback.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent));
1091+
mUploadCallback = null;
11601092
}
11611093

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

1359-
1360-
/**
1361-
* Legacy (deprecated in API 21)
1362-
* For Android 4.4 and below.
1363-
* @param view
1364-
* @param url
1365-
* @return
1366-
*/
1367-
@SuppressWarnings("deprecation")
1368-
@Override
1369-
public WebResourceResponse shouldInterceptRequest (final WebView view, String url) {
1370-
return shouldInterceptRequest(url, super.shouldInterceptRequest(view, url), null);
1371-
}
1372-
13731291
/**
13741292
* New (added in API 21)
13751293
* For Android 5.0 and above.
13761294
*
1377-
* @param webView
1295+
* @param view
13781296
* @param request
13791297
*/
1380-
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
13811298
@Override
13821299
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
13831300
return shouldInterceptRequest(request.getUrl().toString(), super.shouldInterceptRequest(view, request), request.getMethod());
@@ -1428,16 +1345,10 @@ public void onPageFinished(WebView view, String url) {
14281345
super.onPageFinished(view, url);
14291346

14301347
// Set the namespace for postMessage()
1431-
if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
1432-
injectDeferredObject("window.webkit={messageHandlers:{cordova_iab:cordova_iab}}", null);
1433-
}
1348+
injectDeferredObject("window.webkit={messageHandlers:{cordova_iab:cordova_iab}}", null);
14341349

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

14421353
// https://issues.apache.org/jira/browse/CB-11248
14431354
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)