Skip to content

Commit f3d7f72

Browse files
Lets user adjust color of toolbar, hide navigation buttons and set custom text on close button
1 parent 71201b1 commit f3d7f72

File tree

3 files changed

+127
-58
lines changed

3 files changed

+127
-58
lines changed

src/android/InAppBrowser.java

Lines changed: 85 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ Licensed to the Apache Software Foundation (ASF) under one
2525
import android.content.res.Resources;
2626
import android.graphics.Bitmap;
2727
import android.graphics.drawable.Drawable;
28+
import android.graphics.PorterDuff;
29+
import android.graphics.PorterDuffColorFilter;
30+
import android.graphics.Color;
2831
import android.net.Uri;
2932
import android.os.Build;
3033
import android.os.Bundle;
@@ -51,6 +54,7 @@ Licensed to the Apache Software Foundation (ASF) under one
5154
import android.widget.ImageView;
5255
import android.widget.LinearLayout;
5356
import android.widget.RelativeLayout;
57+
import android.widget.TextView;
5458

5559
import org.apache.cordova.CallbackContext;
5660
import org.apache.cordova.Config;
@@ -91,6 +95,8 @@ public class InAppBrowser extends CordovaPlugin {
9195
private static final String SHOULD_PAUSE = "shouldPauseOnSuspend";
9296
private static final Boolean DEFAULT_HARDWARE_BACK = true;
9397
private static final String USER_WIDE_VIEW_PORT = "useWideViewPort";
98+
private static final String CLOSE_BUTTON_TEXT = "closeButtonText";
99+
private static final String CLOSE_BUTTON_COLOR = "closeButtonColor";
94100

95101
private InAppBrowserDialog dialog;
96102
private WebView inAppWebView;
@@ -109,6 +115,8 @@ public class InAppBrowser extends CordovaPlugin {
109115
private ValueCallback<Uri[]> mUploadCallbackLollipop;
110116
private final static int FILECHOOSER_REQUESTCODE = 1;
111117
private final static int FILECHOOSER_REQUESTCODE_LOLLIPOP = 2;
118+
private String closeButtonText = "";
119+
private int closeButtonColor = android.graphics.Color.LTGRAY;
112120

113121
/**
114122
* Executes the request and returns PluginResult.
@@ -127,7 +135,7 @@ public boolean execute(String action, CordovaArgs args, final CallbackContext ca
127135
t = SELF;
128136
}
129137
final String target = t;
130-
final HashMap<String, Boolean> features = parseFeature(args.optString(2));
138+
final HashMap<String, String> features = parseFeature(args.optString(2));
131139

132140
LOG.d(LOG_TAG, "target = " + target);
133141

@@ -366,18 +374,23 @@ public void run() {
366374
* @param optString
367375
* @return
368376
*/
369-
private HashMap<String, Boolean> parseFeature(String optString) {
377+
private HashMap<String, String> parseFeature(String optString) {
370378
if (optString.equals(NULL)) {
371379
return null;
372380
} else {
373-
HashMap<String, Boolean> map = new HashMap<String, Boolean>();
381+
HashMap<String, String> map = new HashMap<String, String>();
374382
StringTokenizer features = new StringTokenizer(optString, ",");
375383
StringTokenizer option;
376384
while(features.hasMoreElements()) {
377385
option = new StringTokenizer(features.nextToken(), "=");
378386
if (option.hasMoreElements()) {
379387
String key = option.nextToken();
380-
Boolean value = option.nextToken().equals("no") ? Boolean.FALSE : Boolean.TRUE;
388+
String value = null;
389+
if (key.equals(CLOSE_BUTTON_TEXT)) value = option.nextToken();
390+
else {
391+
String token = option.nextToken();
392+
value = token.equals("yes") || token.equals("no") ? token : "yes"; // hér!!
393+
}
381394
map.put(key, value);
382395
}
383396
}
@@ -523,52 +536,60 @@ private InAppBrowser getInAppBrowser(){
523536
* @param url the url to load.
524537
* @param features jsonObject
525538
*/
526-
public String showWebPage(final String url, HashMap<String, Boolean> features) {
539+
public String showWebPage(final String url, HashMap<String, String> features) {
527540
// Determine if we should hide the location bar.
528541
showLocationBar = true;
529542
showZoomControls = true;
530543
openWindowHidden = false;
531544
mediaPlaybackRequiresUserGesture = false;
532545

533546
if (features != null) {
534-
Boolean show = features.get(LOCATION);
547+
String show = features.get(LOCATION);
535548
if (show != null) {
536-
showLocationBar = show.booleanValue();
549+
showLocationBar = show.equals("yes") ? true : false;
537550
}
538-
Boolean zoom = features.get(ZOOM);
551+
String zoom = features.get(ZOOM);
539552
if (zoom != null) {
540-
showZoomControls = zoom.booleanValue();
553+
showZoomControls = zoom.equals("yes") ? true : false;
541554
}
542-
Boolean hidden = features.get(HIDDEN);
555+
String hidden = features.get(HIDDEN);
543556
if (hidden != null) {
544-
openWindowHidden = hidden.booleanValue();
557+
openWindowHidden = hidden.equals("yes") ? true : false;
545558
}
546-
Boolean hardwareBack = features.get(HARDWARE_BACK_BUTTON);
559+
String hardwareBack = features.get(HARDWARE_BACK_BUTTON);
547560
if (hardwareBack != null) {
548-
hadwareBackButton = hardwareBack.booleanValue();
561+
hadwareBackButton = hardwareBack.equals("yes") ? true : false;
549562
} else {
550563
hadwareBackButton = DEFAULT_HARDWARE_BACK;
551564
}
552-
Boolean mediaPlayback = features.get(MEDIA_PLAYBACK_REQUIRES_USER_ACTION);
565+
String mediaPlayback = features.get(MEDIA_PLAYBACK_REQUIRES_USER_ACTION);
553566
if (mediaPlayback != null) {
554-
mediaPlaybackRequiresUserGesture = mediaPlayback.booleanValue();
567+
mediaPlaybackRequiresUserGesture = mediaPlayback.equals("yes") ? true : false;
555568
}
556-
Boolean cache = features.get(CLEAR_ALL_CACHE);
569+
String cache = features.get(CLEAR_ALL_CACHE);
557570
if (cache != null) {
558-
clearAllCache = cache.booleanValue();
571+
clearAllCache = cache.equals("yes") ? true : false;
559572
} else {
560573
cache = features.get(CLEAR_SESSION_CACHE);
561574
if (cache != null) {
562-
clearSessionCache = cache.booleanValue();
575+
clearSessionCache = cache.equals("yes") ? true : false;
563576
}
564577
}
565-
Boolean shouldPause = features.get(SHOULD_PAUSE);
578+
String shouldPause = features.get(SHOULD_PAUSE);
566579
if (shouldPause != null) {
567-
shouldPauseInAppBrowser = shouldPause.booleanValue();
580+
shouldPauseInAppBrowser = shouldPause.equals("yes") ? true : false;
568581
}
569-
Boolean wideViewPort = features.get(USER_WIDE_VIEW_PORT);
582+
String wideViewPort = features.get(USER_WIDE_VIEW_PORT);
570583
if (wideViewPort != null ) {
571-
useWideViewPort = wideViewPort.booleanValue();
584+
useWideViewPort = wideViewPort.equals("yes") ? true : false;
585+
}
586+
String closeButtonTextSet = features.get(CLOSE_BUTTON_TEXT);
587+
if (closeButtonTextSet != null) {
588+
closeButtonText = closeButtonTextSet;
589+
}
590+
String closeButtonTextColorSet = features.get(CLOSE_BUTTON_COLOR);
591+
if (closeButtonTextColorSet != null) {
592+
closeButtonColor = Color.parseColor(closeButtonTextColorSet);
572593
}
573594
}
574595

@@ -612,7 +633,7 @@ public void run() {
612633
// Toolbar layout
613634
RelativeLayout toolbar = new RelativeLayout(cordova.getActivity());
614635
//Please, no more black!
615-
toolbar.setBackgroundColor(android.graphics.Color.LTGRAY);
636+
toolbar.setBackgroundColor(closeButtonColor);
616637
toolbar.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, this.dpToPixels(44)));
617638
toolbar.setPadding(this.dpToPixels(2), this.dpToPixels(2), this.dpToPixels(2), this.dpToPixels(2));
618639
toolbar.setHorizontalGravity(Gravity.LEFT);
@@ -700,29 +721,46 @@ public boolean onKey(View v, int keyCode, KeyEvent event) {
700721
});
701722

702723
// Close/Done button
703-
ImageButton close = new ImageButton(cordova.getActivity());
704-
RelativeLayout.LayoutParams closeLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
705-
closeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
706-
close.setLayoutParams(closeLayoutParams);
707-
close.setContentDescription("Close Button");
708-
close.setId(Integer.valueOf(5));
709-
int closeResId = activityRes.getIdentifier("ic_action_remove", "drawable", cordova.getActivity().getPackageName());
710-
Drawable closeIcon = activityRes.getDrawable(closeResId);
711-
if (Build.VERSION.SDK_INT >= 16)
712-
close.setBackground(null);
713-
else
714-
close.setBackgroundDrawable(null);
715-
close.setImageDrawable(closeIcon);
716-
close.setScaleType(ImageView.ScaleType.FIT_CENTER);
717-
back.setPadding(0, this.dpToPixels(10), 0, this.dpToPixels(10));
718-
if (Build.VERSION.SDK_INT >= 16)
719-
close.getAdjustViewBounds();
720-
721-
close.setOnClickListener(new View.OnClickListener() {
722-
public void onClick(View v) {
723-
closeDialog();
724-
}
725-
});
724+
if (closeButtonText != "") {
725+
/* Use TextView for text */
726+
TextView close = new TextView(cordova.getActivity());
727+
close.setText(closeButtonText);
728+
close.setTextSize(25);
729+
back.setPadding(0, this.dpToPixels(10), 0, this.dpToPixels(10));
730+
close.setId(Integer.valueOf(5));
731+
close.setOnClickListener(new View.OnClickListener() {
732+
public void onClick(View v) {
733+
closeDialog();
734+
}
735+
});
736+
toolbar.addView(close);
737+
}
738+
else {
739+
ImageButton close = new ImageButton(cordova.getActivity());
740+
RelativeLayout.LayoutParams closeLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
741+
closeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
742+
close.setLayoutParams(closeLayoutParams);
743+
close.setContentDescription("Close Button");
744+
close.setId(Integer.valueOf(5));
745+
int closeResId = activityRes.getIdentifier("ic_action_remove", "drawable", cordova.getActivity().getPackageName());
746+
Drawable closeIcon = activityRes.getDrawable(closeResId);
747+
if (Build.VERSION.SDK_INT >= 16)
748+
close.setBackground(null);
749+
else
750+
close.setBackgroundDrawable(null);
751+
close.setImageDrawable(closeIcon);
752+
close.setScaleType(ImageView.ScaleType.FIT_CENTER);
753+
back.setPadding(0, this.dpToPixels(10), 0, this.dpToPixels(10));
754+
if (Build.VERSION.SDK_INT >= 16)
755+
close.getAdjustViewBounds();
756+
757+
close.setOnClickListener(new View.OnClickListener() {
758+
public void onClick(View v) {
759+
closeDialog();
760+
}
761+
});
762+
toolbar.addView(close);
763+
}
726764

727765
// WebView
728766
inAppWebView = new WebView(cordova.getActivity());
@@ -828,7 +866,7 @@ public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType)
828866
// Add the views to our toolbar
829867
toolbar.addView(actionButtonContainer);
830868
toolbar.addView(edittext);
831-
toolbar.addView(close);
869+
// toolbar.addView(close);
832870

833871
// Don't add the toolbar if its been disabled
834872
if (getShowLocationBar()) {

src/ios/CDVInAppBrowser.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@
4949
@property (nonatomic, assign) BOOL location;
5050
@property (nonatomic, assign) BOOL toolbar;
5151
@property (nonatomic, copy) NSString* closebuttoncaption;
52+
@property (nonatomic, copy) NSString* closebuttoncolor;
5253
@property (nonatomic, copy) NSString* toolbarposition;
54+
@property (nonatomic, copy) NSString* toolbarcolor;
55+
@property (nonatomic, assign) BOOL toolbartranslucent;
56+
@property (nonatomic, assign) BOOL hideToolbarNavigationButtons;
5357
@property (nonatomic, assign) BOOL clearcache;
5458
@property (nonatomic, assign) BOOL clearsessioncache;
5559

@@ -74,13 +78,13 @@
7478
NSString* _prevUserAgent;
7579
NSInteger _userAgentLockToken;
7680
CDVInAppBrowserOptions *_browserOptions;
77-
81+
7882
#ifdef __CORDOVA_4_0_0
7983
CDVUIWebViewDelegate* _webViewDelegate;
8084
#else
8185
CDVWebViewDelegate* _webViewDelegate;
8286
#endif
83-
87+
8488
}
8589

8690
@property (nonatomic, strong) IBOutlet UIWebView* webView;
@@ -99,7 +103,7 @@
99103
- (void)navigateTo:(NSURL*)url;
100104
- (void)showLocationBar:(BOOL)show;
101105
- (void)showToolBar:(BOOL)show : (NSString *) toolbarPosition;
102-
- (void)setCloseButtonTitle:(NSString*)title;
106+
- (void)setCloseButtonTitle:(NSString*)title : (NSString*) colorString;
103107

104108
- (id)initWithUserAgent:(NSString*)userAgent prevUserAgent:(NSString*)prevUserAgent browserOptions: (CDVInAppBrowserOptions*) browserOptions;
105109

@@ -110,4 +114,3 @@
110114
@property (nonatomic, weak) id <CDVScreenOrientationDelegate> orientationDelegate;
111115

112116
@end
113-

src/ios/CDVInAppBrowser.m

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ - (void)openInInAppBrowser:(NSURL*)url withOptions:(NSString*)options
162162

163163
[self.inAppBrowserViewController showLocationBar:browserOptions.location];
164164
[self.inAppBrowserViewController showToolBar:browserOptions.toolbar :browserOptions.toolbarposition];
165-
if (browserOptions.closebuttoncaption != nil) {
166-
[self.inAppBrowserViewController setCloseButtonTitle:browserOptions.closebuttoncaption];
165+
if (browserOptions.closebuttoncaption != nil || browserOptions.closebuttoncolor != nil) {
166+
[self.inAppBrowserViewController setCloseButtonTitle:browserOptions.closebuttoncaption :browserOptions.closebuttoncolor];
167167
}
168168
// Set Presentation Style
169169
UIModalPresentationStyle presentationStyle = UIModalPresentationFullScreen; // default
@@ -599,6 +599,12 @@ - (void)createViews
599599
self.toolbar.multipleTouchEnabled = NO;
600600
self.toolbar.opaque = NO;
601601
self.toolbar.userInteractionEnabled = YES;
602+
if (_browserOptions.toolbarcolor != nil) { // Set toolbar color if user sets it in options
603+
self.toolbar.barTintColor = [self colorFromHexString:_browserOptions.toolbarcolor];
604+
}
605+
if (!_browserOptions.toolbartranslucent) { // Set toolbar translucent to no if user sets it in options
606+
self.toolbar.translucent = NO;
607+
}
602608

603609
CGFloat labelInset = 5.0;
604610
float locationBarY = toolbarIsAtBottom ? self.view.bounds.size.height - FOOTER_HEIGHT : self.view.bounds.size.height - LOCATIONBAR_HEIGHT;
@@ -642,7 +648,13 @@ - (void)createViews
642648
self.backButton.enabled = YES;
643649
self.backButton.imageInsets = UIEdgeInsetsZero;
644650

645-
[self.toolbar setItems:@[self.closeButton, flexibleSpaceButton, self.backButton, fixedSpaceButton, self.forwardButton]];
651+
// Filter out Navigation Buttons if user requests so
652+
if (_browserOptions.hideToolbarNavigationButtons) {
653+
[self.toolbar setItems:@[self.closeButton, flexibleSpaceButton]];
654+
} else {
655+
[self.toolbar setItems:@[self.closeButton, flexibleSpaceButton, self.backButton, fixedSpaceButton, self.forwardButton]];
656+
}
657+
[self.toolbar setItems:@[self.closeButton, flexibleSpaceButton]];
646658

647659
self.view.backgroundColor = [UIColor grayColor];
648660
[self.view addSubview:self.toolbar];
@@ -655,14 +667,16 @@ - (void) setWebViewFrame : (CGRect) frame {
655667
[self.webView setFrame:frame];
656668
}
657669

658-
- (void)setCloseButtonTitle:(NSString*)title
670+
- (void)setCloseButtonTitle:(NSString*)title : (NSString*) colorString
659671
{
660672
// the advantage of using UIBarButtonSystemItemDone is the system will localize it for you automatically
661673
// 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)
662674
self.closeButton = nil;
663-
self.closeButton = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStyleBordered target:self action:@selector(close)];
675+
// Initialize with title if title is set, otherwise the title will be 'Done' localized
676+
self.closeButton = title != nil ? [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStyleBordered target:self action:@selector(close)] : [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(close)];
664677
self.closeButton.enabled = YES;
665-
self.closeButton.tintColor = [UIColor colorWithRed:60.0 / 255.0 green:136.0 / 255.0 blue:230.0 / 255.0 alpha:1];
678+
// If color on closebutton is requested then initialize with that that color, otherwise use initialize with default
679+
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];
666680

667681
NSMutableArray* items = [self.toolbar.items mutableCopy];
668682
[items replaceObjectAtIndex:0 withObject:self.closeButton];
@@ -877,6 +891,17 @@ - (void) rePositionViews {
877891
}
878892
}
879893

894+
// Helper function to convert hex color string to UIColor
895+
// Assumes input like "#00FF00" (#RRGGBB).
896+
// Taken from https://stackoverflow.com/questions/1560081/how-can-i-create-a-uicolor-from-a-hex-string
897+
- (UIColor *)colorFromHexString:(NSString *)hexString {
898+
unsigned rgbValue = 0;
899+
NSScanner *scanner = [NSScanner scannerWithString:hexString];
900+
[scanner setScanLocation:1]; // bypass '#' character
901+
[scanner scanHexInt:&rgbValue];
902+
return [UIColor colorWithRed:((rgbValue & 0xFF0000) >> 16)/255.0 green:((rgbValue & 0xFF00) >> 8)/255.0 blue:(rgbValue & 0xFF)/255.0 alpha:1.0];
903+
}
904+
880905
#pragma mark UIWebViewDelegate
881906

882907
- (void)webViewDidStartLoad:(UIWebView*)theWebView
@@ -995,6 +1020,10 @@ - (id)init
9951020
self.suppressesincrementalrendering = NO;
9961021
self.hidden = NO;
9971022
self.disallowoverscroll = NO;
1023+
self.hideToolbarNavigationButtons = NO;
1024+
self.closebuttoncolor = nil;
1025+
self.toolbarcolor = nil;
1026+
self.toolbartranslucent = YES;
9981027
}
9991028

10001029
return self;
@@ -1104,4 +1133,3 @@ - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interface
11041133

11051134

11061135
@end
1107-

0 commit comments

Comments
 (0)