Lets user adjust color of toolbar, hide navigation buttons and set custom text on close button

This commit is contained in:
Landsbankinn 2017-10-04 16:41:31 +00:00
parent 71201b10a6
commit f3d7f72c9e
3 changed files with 126 additions and 57 deletions

View File

@ -25,6 +25,9 @@ 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.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
@ -51,6 +54,7 @@ import android.widget.ImageButton;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.RelativeLayout; import android.widget.RelativeLayout;
import android.widget.TextView;
import org.apache.cordova.CallbackContext; import org.apache.cordova.CallbackContext;
import org.apache.cordova.Config; import org.apache.cordova.Config;
@ -91,6 +95,8 @@ public class InAppBrowser extends CordovaPlugin {
private static final String SHOULD_PAUSE = "shouldPauseOnSuspend"; private static final String SHOULD_PAUSE = "shouldPauseOnSuspend";
private static final Boolean DEFAULT_HARDWARE_BACK = true; private static final Boolean DEFAULT_HARDWARE_BACK = true;
private static final String USER_WIDE_VIEW_PORT = "useWideViewPort"; private static final String USER_WIDE_VIEW_PORT = "useWideViewPort";
private static final String CLOSE_BUTTON_TEXT = "closeButtonText";
private static final String CLOSE_BUTTON_COLOR = "closeButtonColor";
private InAppBrowserDialog dialog; private InAppBrowserDialog dialog;
private WebView inAppWebView; private WebView inAppWebView;
@ -109,6 +115,8 @@ public class InAppBrowser extends CordovaPlugin {
private ValueCallback<Uri[]> mUploadCallbackLollipop; 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 final static int FILECHOOSER_REQUESTCODE_LOLLIPOP = 2;
private String closeButtonText = "";
private int closeButtonColor = android.graphics.Color.LTGRAY;
/** /**
* Executes the request and returns PluginResult. * Executes the request and returns PluginResult.
@ -127,7 +135,7 @@ public class InAppBrowser extends CordovaPlugin {
t = SELF; t = SELF;
} }
final String target = t; final String target = t;
final HashMap<String, Boolean> features = parseFeature(args.optString(2)); final HashMap<String, String> features = parseFeature(args.optString(2));
LOG.d(LOG_TAG, "target = " + target); LOG.d(LOG_TAG, "target = " + target);
@ -366,18 +374,23 @@ public class InAppBrowser extends CordovaPlugin {
* @param optString * @param optString
* @return * @return
*/ */
private HashMap<String, Boolean> parseFeature(String optString) { private HashMap<String, String> parseFeature(String optString) {
if (optString.equals(NULL)) { if (optString.equals(NULL)) {
return null; return null;
} else { } else {
HashMap<String, Boolean> map = new HashMap<String, Boolean>(); HashMap<String, String> map = new HashMap<String, String>();
StringTokenizer features = new StringTokenizer(optString, ","); StringTokenizer features = new StringTokenizer(optString, ",");
StringTokenizer option; StringTokenizer option;
while(features.hasMoreElements()) { while(features.hasMoreElements()) {
option = new StringTokenizer(features.nextToken(), "="); option = new StringTokenizer(features.nextToken(), "=");
if (option.hasMoreElements()) { if (option.hasMoreElements()) {
String key = option.nextToken(); String key = option.nextToken();
Boolean value = option.nextToken().equals("no") ? Boolean.FALSE : Boolean.TRUE; String value = null;
if (key.equals(CLOSE_BUTTON_TEXT)) value = option.nextToken();
else {
String token = option.nextToken();
value = token.equals("yes") || token.equals("no") ? token : "yes"; // hér!!
}
map.put(key, value); map.put(key, value);
} }
} }
@ -523,7 +536,7 @@ public class InAppBrowser extends CordovaPlugin {
* @param url the url to load. * @param url the url to load.
* @param features jsonObject * @param features jsonObject
*/ */
public String showWebPage(final String url, HashMap<String, Boolean> features) { public String showWebPage(final String url, HashMap<String, String> features) {
// Determine if we should hide the location bar. // Determine if we should hide the location bar.
showLocationBar = true; showLocationBar = true;
showZoomControls = true; showZoomControls = true;
@ -531,44 +544,52 @@ public class InAppBrowser extends CordovaPlugin {
mediaPlaybackRequiresUserGesture = false; mediaPlaybackRequiresUserGesture = false;
if (features != null) { if (features != null) {
Boolean show = features.get(LOCATION); String show = features.get(LOCATION);
if (show != null) { if (show != null) {
showLocationBar = show.booleanValue(); showLocationBar = show.equals("yes") ? true : false;
} }
Boolean zoom = features.get(ZOOM); String zoom = features.get(ZOOM);
if (zoom != null) { if (zoom != null) {
showZoomControls = zoom.booleanValue(); showZoomControls = zoom.equals("yes") ? true : false;
} }
Boolean hidden = features.get(HIDDEN); String hidden = features.get(HIDDEN);
if (hidden != null) { if (hidden != null) {
openWindowHidden = hidden.booleanValue(); openWindowHidden = hidden.equals("yes") ? true : false;
} }
Boolean hardwareBack = features.get(HARDWARE_BACK_BUTTON); String hardwareBack = features.get(HARDWARE_BACK_BUTTON);
if (hardwareBack != null) { if (hardwareBack != null) {
hadwareBackButton = hardwareBack.booleanValue(); hadwareBackButton = hardwareBack.equals("yes") ? true : false;
} else { } else {
hadwareBackButton = DEFAULT_HARDWARE_BACK; hadwareBackButton = DEFAULT_HARDWARE_BACK;
} }
Boolean mediaPlayback = features.get(MEDIA_PLAYBACK_REQUIRES_USER_ACTION); String mediaPlayback = features.get(MEDIA_PLAYBACK_REQUIRES_USER_ACTION);
if (mediaPlayback != null) { if (mediaPlayback != null) {
mediaPlaybackRequiresUserGesture = mediaPlayback.booleanValue(); mediaPlaybackRequiresUserGesture = mediaPlayback.equals("yes") ? true : false;
} }
Boolean cache = features.get(CLEAR_ALL_CACHE); String cache = features.get(CLEAR_ALL_CACHE);
if (cache != null) { if (cache != null) {
clearAllCache = cache.booleanValue(); clearAllCache = cache.equals("yes") ? true : false;
} else { } else {
cache = features.get(CLEAR_SESSION_CACHE); cache = features.get(CLEAR_SESSION_CACHE);
if (cache != null) { if (cache != null) {
clearSessionCache = cache.booleanValue(); clearSessionCache = cache.equals("yes") ? true : false;
} }
} }
Boolean shouldPause = features.get(SHOULD_PAUSE); String shouldPause = features.get(SHOULD_PAUSE);
if (shouldPause != null) { if (shouldPause != null) {
shouldPauseInAppBrowser = shouldPause.booleanValue(); shouldPauseInAppBrowser = shouldPause.equals("yes") ? true : false;
} }
Boolean wideViewPort = features.get(USER_WIDE_VIEW_PORT); String wideViewPort = features.get(USER_WIDE_VIEW_PORT);
if (wideViewPort != null ) { if (wideViewPort != null ) {
useWideViewPort = wideViewPort.booleanValue(); useWideViewPort = wideViewPort.equals("yes") ? true : false;
}
String closeButtonTextSet = features.get(CLOSE_BUTTON_TEXT);
if (closeButtonTextSet != null) {
closeButtonText = closeButtonTextSet;
}
String closeButtonTextColorSet = features.get(CLOSE_BUTTON_COLOR);
if (closeButtonTextColorSet != null) {
closeButtonColor = Color.parseColor(closeButtonTextColorSet);
} }
} }
@ -612,7 +633,7 @@ public class InAppBrowser extends CordovaPlugin {
// Toolbar layout // Toolbar layout
RelativeLayout toolbar = new RelativeLayout(cordova.getActivity()); RelativeLayout toolbar = new RelativeLayout(cordova.getActivity());
//Please, no more black! //Please, no more black!
toolbar.setBackgroundColor(android.graphics.Color.LTGRAY); toolbar.setBackgroundColor(closeButtonColor);
toolbar.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, this.dpToPixels(44))); toolbar.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, this.dpToPixels(44)));
toolbar.setPadding(this.dpToPixels(2), this.dpToPixels(2), this.dpToPixels(2), this.dpToPixels(2)); toolbar.setPadding(this.dpToPixels(2), this.dpToPixels(2), this.dpToPixels(2), this.dpToPixels(2));
toolbar.setHorizontalGravity(Gravity.LEFT); toolbar.setHorizontalGravity(Gravity.LEFT);
@ -700,6 +721,21 @@ public class InAppBrowser extends CordovaPlugin {
}); });
// Close/Done button // Close/Done button
if (closeButtonText != "") {
/* Use TextView for text */
TextView close = new TextView(cordova.getActivity());
close.setText(closeButtonText);
close.setTextSize(25);
back.setPadding(0, this.dpToPixels(10), 0, this.dpToPixels(10));
close.setId(Integer.valueOf(5));
close.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
closeDialog();
}
});
toolbar.addView(close);
}
else {
ImageButton close = new ImageButton(cordova.getActivity()); ImageButton close = new ImageButton(cordova.getActivity());
RelativeLayout.LayoutParams closeLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT); RelativeLayout.LayoutParams closeLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
closeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); closeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
@ -723,6 +759,8 @@ public class InAppBrowser extends CordovaPlugin {
closeDialog(); closeDialog();
} }
}); });
toolbar.addView(close);
}
// WebView // WebView
inAppWebView = new WebView(cordova.getActivity()); inAppWebView = new WebView(cordova.getActivity());
@ -828,7 +866,7 @@ public class InAppBrowser extends CordovaPlugin {
// Add the views to our toolbar // Add the views to our toolbar
toolbar.addView(actionButtonContainer); toolbar.addView(actionButtonContainer);
toolbar.addView(edittext); toolbar.addView(edittext);
toolbar.addView(close); // toolbar.addView(close);
// Don't add the toolbar if its been disabled // Don't add the toolbar if its been disabled
if (getShowLocationBar()) { if (getShowLocationBar()) {

View File

@ -49,7 +49,11 @@
@property (nonatomic, assign) BOOL location; @property (nonatomic, assign) BOOL location;
@property (nonatomic, assign) BOOL toolbar; @property (nonatomic, assign) BOOL toolbar;
@property (nonatomic, copy) NSString* closebuttoncaption; @property (nonatomic, copy) NSString* closebuttoncaption;
@property (nonatomic, copy) NSString* closebuttoncolor;
@property (nonatomic, copy) NSString* toolbarposition; @property (nonatomic, copy) NSString* toolbarposition;
@property (nonatomic, copy) NSString* toolbarcolor;
@property (nonatomic, assign) BOOL toolbartranslucent;
@property (nonatomic, assign) BOOL hideToolbarNavigationButtons;
@property (nonatomic, assign) BOOL clearcache; @property (nonatomic, assign) BOOL clearcache;
@property (nonatomic, assign) BOOL clearsessioncache; @property (nonatomic, assign) BOOL clearsessioncache;
@ -99,7 +103,7 @@
- (void)navigateTo:(NSURL*)url; - (void)navigateTo:(NSURL*)url;
- (void)showLocationBar:(BOOL)show; - (void)showLocationBar:(BOOL)show;
- (void)showToolBar:(BOOL)show : (NSString *) toolbarPosition; - (void)showToolBar:(BOOL)show : (NSString *) toolbarPosition;
- (void)setCloseButtonTitle:(NSString*)title; - (void)setCloseButtonTitle:(NSString*)title : (NSString*) colorString;
- (id)initWithUserAgent:(NSString*)userAgent prevUserAgent:(NSString*)prevUserAgent browserOptions: (CDVInAppBrowserOptions*) browserOptions; - (id)initWithUserAgent:(NSString*)userAgent prevUserAgent:(NSString*)prevUserAgent browserOptions: (CDVInAppBrowserOptions*) browserOptions;
@ -110,4 +114,3 @@
@property (nonatomic, weak) id <CDVScreenOrientationDelegate> orientationDelegate; @property (nonatomic, weak) id <CDVScreenOrientationDelegate> orientationDelegate;
@end @end

View File

@ -162,8 +162,8 @@
[self.inAppBrowserViewController showLocationBar:browserOptions.location]; [self.inAppBrowserViewController showLocationBar:browserOptions.location];
[self.inAppBrowserViewController showToolBar:browserOptions.toolbar :browserOptions.toolbarposition]; [self.inAppBrowserViewController showToolBar:browserOptions.toolbar :browserOptions.toolbarposition];
if (browserOptions.closebuttoncaption != nil) { if (browserOptions.closebuttoncaption != nil || browserOptions.closebuttoncolor != nil) {
[self.inAppBrowserViewController setCloseButtonTitle:browserOptions.closebuttoncaption]; [self.inAppBrowserViewController setCloseButtonTitle:browserOptions.closebuttoncaption :browserOptions.closebuttoncolor];
} }
// Set Presentation Style // Set Presentation Style
UIModalPresentationStyle presentationStyle = UIModalPresentationFullScreen; // default UIModalPresentationStyle presentationStyle = UIModalPresentationFullScreen; // default
@ -599,6 +599,12 @@
self.toolbar.multipleTouchEnabled = NO; self.toolbar.multipleTouchEnabled = NO;
self.toolbar.opaque = NO; self.toolbar.opaque = NO;
self.toolbar.userInteractionEnabled = YES; self.toolbar.userInteractionEnabled = YES;
if (_browserOptions.toolbarcolor != nil) { // Set toolbar color if user sets it in options
self.toolbar.barTintColor = [self colorFromHexString:_browserOptions.toolbarcolor];
}
if (!_browserOptions.toolbartranslucent) { // Set toolbar translucent to no if user sets it in options
self.toolbar.translucent = NO;
}
CGFloat labelInset = 5.0; CGFloat labelInset = 5.0;
float locationBarY = toolbarIsAtBottom ? self.view.bounds.size.height - FOOTER_HEIGHT : self.view.bounds.size.height - LOCATIONBAR_HEIGHT; float locationBarY = toolbarIsAtBottom ? self.view.bounds.size.height - FOOTER_HEIGHT : self.view.bounds.size.height - LOCATIONBAR_HEIGHT;
@ -642,7 +648,13 @@
self.backButton.enabled = YES; self.backButton.enabled = YES;
self.backButton.imageInsets = UIEdgeInsetsZero; self.backButton.imageInsets = UIEdgeInsetsZero;
// Filter out Navigation Buttons if user requests so
if (_browserOptions.hideToolbarNavigationButtons) {
[self.toolbar setItems:@[self.closeButton, flexibleSpaceButton]];
} else {
[self.toolbar setItems:@[self.closeButton, flexibleSpaceButton, self.backButton, fixedSpaceButton, self.forwardButton]]; [self.toolbar setItems:@[self.closeButton, flexibleSpaceButton, self.backButton, fixedSpaceButton, self.forwardButton]];
}
[self.toolbar setItems:@[self.closeButton, flexibleSpaceButton]];
self.view.backgroundColor = [UIColor grayColor]; self.view.backgroundColor = [UIColor grayColor];
[self.view addSubview:self.toolbar]; [self.view addSubview:self.toolbar];
@ -655,14 +667,16 @@
[self.webView setFrame:frame]; [self.webView setFrame:frame];
} }
- (void)setCloseButtonTitle:(NSString*)title - (void)setCloseButtonTitle:(NSString*)title : (NSString*) colorString
{ {
// the advantage of using UIBarButtonSystemItemDone is the system will localize it for you automatically // the advantage of using UIBarButtonSystemItemDone is the system will localize it for you automatically
// but, if you want to set this yourself, knock yourself out (we can't set the title for a system Done button, so we have to create a new one) // but, if you want to set this yourself, knock yourself out (we can't set the title for a system Done button, so we have to create a new one)
self.closeButton = nil; self.closeButton = nil;
self.closeButton = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStyleBordered target:self action:@selector(close)]; // Initialize with title if title is set, otherwise the title will be 'Done' localized
self.closeButton = title != nil ? [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStyleBordered target:self action:@selector(close)] : [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(close)];
self.closeButton.enabled = YES; self.closeButton.enabled = YES;
self.closeButton.tintColor = [UIColor colorWithRed:60.0 / 255.0 green:136.0 / 255.0 blue:230.0 / 255.0 alpha:1]; // If color on closebutton is requested then initialize with that that color, otherwise use initialize with default
self.closeButton.tintColor = colorString != nil ? [self colorFromHexString:colorString] : [UIColor colorWithRed:60.0 / 255.0 green:136.0 / 255.0 blue:230.0 / 255.0 alpha:1];
NSMutableArray* items = [self.toolbar.items mutableCopy]; NSMutableArray* items = [self.toolbar.items mutableCopy];
[items replaceObjectAtIndex:0 withObject:self.closeButton]; [items replaceObjectAtIndex:0 withObject:self.closeButton];
@ -877,6 +891,17 @@
} }
} }
// Helper function to convert hex color string to UIColor
// Assumes input like "#00FF00" (#RRGGBB).
// Taken from https://stackoverflow.com/questions/1560081/how-can-i-create-a-uicolor-from-a-hex-string
- (UIColor *)colorFromHexString:(NSString *)hexString {
unsigned rgbValue = 0;
NSScanner *scanner = [NSScanner scannerWithString:hexString];
[scanner setScanLocation:1]; // bypass '#' character
[scanner scanHexInt:&rgbValue];
return [UIColor colorWithRed:((rgbValue & 0xFF0000) >> 16)/255.0 green:((rgbValue & 0xFF00) >> 8)/255.0 blue:(rgbValue & 0xFF)/255.0 alpha:1.0];
}
#pragma mark UIWebViewDelegate #pragma mark UIWebViewDelegate
- (void)webViewDidStartLoad:(UIWebView*)theWebView - (void)webViewDidStartLoad:(UIWebView*)theWebView
@ -995,6 +1020,10 @@
self.suppressesincrementalrendering = NO; self.suppressesincrementalrendering = NO;
self.hidden = NO; self.hidden = NO;
self.disallowoverscroll = NO; self.disallowoverscroll = NO;
self.hideToolbarNavigationButtons = NO;
self.closebuttoncolor = nil;
self.toolbarcolor = nil;
self.toolbartranslucent = YES;
} }
return self; return self;
@ -1104,4 +1133,3 @@
@end @end