Skip to content

(android) New 'enablethirdpartycookies' option and code clean-ups #967

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 31 additions & 19 deletions src/android/InAppBrowser.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public class InAppBrowser extends CordovaPlugin {
private static final String MESSAGE_EVENT = "message";
private static final String CLEAR_ALL_CACHE = "clearcache";
private static final String CLEAR_SESSION_CACHE = "clearsessioncache";
private static final String ENABLE_THIRD_PARTY_COOKIES = "enablethirdpartycookies";
private static final String HARDWARE_BACK_BUTTON = "hardwareback";
private static final String MEDIA_PLAYBACK_REQUIRES_USER_ACTION = "mediaPlaybackRequiresUserAction";
private static final String SHOULD_PAUSE = "shouldPauseOnSuspend";
Expand All @@ -119,7 +120,13 @@ public class InAppBrowser extends CordovaPlugin {

private static final int TOOLBAR_HEIGHT = 48;

private static final List customizableOptions = Arrays.asList(CLOSE_BUTTON_CAPTION, TOOLBAR_COLOR, NAVIGATION_COLOR, CLOSE_BUTTON_COLOR, FOOTER_COLOR);
private static final List customizableOptions = Arrays.asList(
CLOSE_BUTTON_CAPTION, TOOLBAR_COLOR, NAVIGATION_COLOR, CLOSE_BUTTON_COLOR, FOOTER_COLOR);

// Resources
private static final String TOOLBAR_CLOSE_BUTTON = "ic_action_remove";
private static final String TOOLBAR_BACK_BUTTON = "ic_action_previous_item";
private static final String TOOLBAR_FORWARD_BUTTON = "ic_action_next_item";

private InAppBrowserDialog dialog;
private WebView inAppWebView;
Expand All @@ -130,7 +137,8 @@ public class InAppBrowser extends CordovaPlugin {
private boolean openWindowHidden = false;
private boolean clearAllCache = false;
private boolean clearSessionCache = false;
private boolean hadwareBackButton = true;
private boolean enableThirdPartyCookies = true;
private boolean hardwareBackButton = true;
private boolean mediaPlaybackRequiresUserGesture = false;
private boolean shouldPauseInAppBrowser = false;
private boolean useWideViewPort = true;
Expand Down Expand Up @@ -576,7 +584,7 @@ public boolean canGoBack() {
* @return boolean
*/
public boolean hardwareBack() {
return hadwareBackButton;
return hardwareBackButton;
}

/**
Expand Down Expand Up @@ -618,6 +626,15 @@ private boolean getShowLocationBar() {
private InAppBrowser getInAppBrowser() {
return this;
}

/**
* Get a drawable by name from the main package.
*/
private Drawable getDrawableFromResources(String name){
Resources activityRes = cordova.getActivity().getResources();
int resId = activityRes.getIdentifier(name, "drawable", cordova.getActivity().getPackageName());
return activityRes.getDrawable(resId, cordova.getActivity().getTheme());
}

/**
* Display a new browser with the specified URL.
Expand All @@ -626,12 +643,6 @@ private InAppBrowser getInAppBrowser() {
* @param features jsonObject
*/
public String showWebPage(final String url, HashMap<String, String> features) {
// Determine if we should hide the location bar.
showLocationBar = true;
showZoomControls = true;
openWindowHidden = false;
mediaPlaybackRequiresUserGesture = false;

if (features != null) {
String show = features.get(LOCATION);
if (show != null) {
Expand All @@ -653,9 +664,9 @@ public String showWebPage(final String url, HashMap<String, String> features) {
}
String hardwareBack = features.get(HARDWARE_BACK_BUTTON);
if (hardwareBack != null) {
hadwareBackButton = hardwareBack.equals("yes") ? true : false;
hardwareBackButton = hardwareBack.equals("yes") ? true : false;
} else {
hadwareBackButton = DEFAULT_HARDWARE_BACK;
hardwareBackButton = DEFAULT_HARDWARE_BACK;
}
String mediaPlayback = features.get(MEDIA_PLAYBACK_REQUIRES_USER_ACTION);
if (mediaPlayback != null) {
Expand All @@ -670,6 +681,10 @@ public String showWebPage(final String url, HashMap<String, String> features) {
clearSessionCache = cache.equals("yes") ? true : false;
}
}
String thirdPartyCookies = features.get(ENABLE_THIRD_PARTY_COOKIES);
if (thirdPartyCookies != null) {
enableThirdPartyCookies = thirdPartyCookies.equals("yes") ? true : false;
}
String shouldPause = features.get(SHOULD_PAUSE);
if (shouldPause != null) {
shouldPauseInAppBrowser = shouldPause.equals("yes") ? true : false;
Expand Down Expand Up @@ -748,8 +763,7 @@ private View createCloseButton(int id) {
}
else {
ImageButton close = new ImageButton(cordova.getActivity());
int closeResId = activityRes.getIdentifier("ic_action_remove", "drawable", cordova.getActivity().getPackageName());
Drawable closeIcon = activityRes.getDrawable(closeResId);
Drawable closeIcon = getDrawableFromResources(TOOLBAR_CLOSE_BUTTON);
if (closeButtonColor != "") close.setColorFilter(android.graphics.Color.parseColor(closeButtonColor));
close.setImageDrawable(closeIcon);
close.setScaleType(ImageView.ScaleType.FIT_CENTER);
Expand Down Expand Up @@ -799,7 +813,7 @@ public void run() {

// Toolbar layout
RelativeLayout toolbar = new RelativeLayout(cordova.getActivity());
//Please, no more black!
//"Please, no more black!" <- who said this?!? Black is awesome, especially on OLED! ^^
toolbar.setBackgroundColor(toolbarColor);
toolbar.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, this.dpToPixels(TOOLBAR_HEIGHT)));
toolbar.setPadding(this.dpToPixels(2), this.dpToPixels(2), this.dpToPixels(2), this.dpToPixels(2));
Expand Down Expand Up @@ -828,8 +842,7 @@ public void run() {
back.setContentDescription("Back Button");
back.setId(Integer.valueOf(2));
Resources activityRes = cordova.getActivity().getResources();
int backResId = activityRes.getIdentifier("ic_action_previous_item", "drawable", cordova.getActivity().getPackageName());
Drawable backIcon = activityRes.getDrawable(backResId);
Drawable backIcon = getDrawableFromResources(TOOLBAR_BACK_BUTTON);
if (navigationButtonColor != "") back.setColorFilter(android.graphics.Color.parseColor(navigationButtonColor));
back.setBackground(null);
back.setImageDrawable(backIcon);
Expand All @@ -850,8 +863,7 @@ public void onClick(View v) {
forward.setLayoutParams(forwardLayoutParams);
forward.setContentDescription("Forward Button");
forward.setId(Integer.valueOf(3));
int fwdResId = activityRes.getIdentifier("ic_action_next_item", "drawable", cordova.getActivity().getPackageName());
Drawable fwdIcon = activityRes.getDrawable(fwdResId);
Drawable fwdIcon = getDrawableFromResources(TOOLBAR_FORWARD_BUTTON);
if (navigationButtonColor != "") forward.setColorFilter(android.graphics.Color.parseColor(navigationButtonColor));
forward.setBackground(null);
forward.setImageDrawable(fwdIcon);
Expand Down Expand Up @@ -992,7 +1004,7 @@ public void postMessage(String data) {
}

// Enable Thirdparty Cookies
CookieManager.getInstance().setAcceptThirdPartyCookies(inAppWebView,true);
CookieManager.getInstance().setAcceptThirdPartyCookies(inAppWebView, enableThirdPartyCookies);

inAppWebView.loadUrl(url);
inAppWebView.setId(Integer.valueOf(6));
Expand Down