From 9c0881b4fd25139269043291008c5833a2725ad9 Mon Sep 17 00:00:00 2001 From: toven Date: Fri, 16 Mar 2018 17:21:27 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E6=8C=89=E9=92=AE=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E7=9A=84=E6=96=B9=E6=B3=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/android/ThemeableBrowser.java | 78 ++++++++++++++++++++++++++++--- src/ios/CDVThemeableBrowser.h | 2 + src/ios/CDVThemeableBrowser.m | 20 ++++++-- 3 files changed, 89 insertions(+), 11 deletions(-) diff --git a/src/android/ThemeableBrowser.java b/src/android/ThemeableBrowser.java index 673e0d828..86f49c2a6 100644 --- a/src/android/ThemeableBrowser.java +++ b/src/android/ThemeableBrowser.java @@ -106,6 +106,8 @@ public class ThemeableBrowser extends CordovaPlugin { private ThemeableBrowserDialog dialog; private WebView inAppWebView; + private LinearLayout leftButtonContainer; + private LinearLayout rightButtonContainer; private EditText edittext; private CallbackContext callbackContext; @@ -252,7 +254,12 @@ public void run() { }); } } - else { + else if (action.equals("changeButtonImage")) { + final int buttonIndex = args.getInt(0); + BrowserButton buttonProps = parseButtonProps(args.getString(1)); + this.changeButtonImage(buttonIndex, buttonProps); + } + else { return false; } return true; @@ -581,15 +588,17 @@ public void run() { } // Left Button Container layout - LinearLayout leftButtonContainer = new LinearLayout(cordova.getActivity()); - FrameLayout.LayoutParams leftButtonContainerParams = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); + leftButtonContainer = new LinearLayout(cordova.getActivity()); + FrameLayout.LayoutParams leftButtonContainerParams = new FrameLayout.LayoutParams( + LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); leftButtonContainerParams.gravity = Gravity.LEFT | Gravity.CENTER_VERTICAL; leftButtonContainer.setLayoutParams(leftButtonContainerParams); leftButtonContainer.setVerticalGravity(Gravity.CENTER_VERTICAL); // Right Button Container layout - LinearLayout rightButtonContainer = new LinearLayout(cordova.getActivity()); - FrameLayout.LayoutParams rightButtonContainerParams = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); + rightButtonContainer = new LinearLayout(cordova.getActivity()); + FrameLayout.LayoutParams rightButtonContainerParams = new FrameLayout.LayoutParams( + LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); rightButtonContainerParams.gravity = Gravity.RIGHT | Gravity.CENTER_VERTICAL; rightButtonContainer.setLayoutParams(rightButtonContainerParams); rightButtonContainer.setVerticalGravity(Gravity.CENTER_VERTICAL); @@ -1133,8 +1142,63 @@ private void setBackground(View view, Drawable drawable) { } } - private Button createButton(BrowserButton buttonProps, String description, - View.OnClickListener listener) { + private void changeButtonImage(int buttonIndex, BrowserButton buttonProps) { + Resources activityRes = cordova.getActivity().getResources(); + File file = new File("www", buttonProps.wwwImage); + InputStream is = null; + BitmapDrawable drawable; + Button view; + if (ALIGN_RIGHT.equals(buttonProps.align)) { + //rightButtonContainer + view = (Button) rightButtonContainer.getChildAt(buttonIndex); + } else { + //leftButtonContainer + view = (Button) leftButtonContainer.getChildAt(buttonIndex); + } + + if (file != null) { + try { + is = cordova.getActivity().getAssets().open(file.getPath()); + Bitmap bitmap = BitmapFactory.decodeStream(is); + bitmap.setDensity((int) (DisplayMetrics.DENSITY_MEDIUM * buttonProps.wwwImageDensity)); + drawable = new BitmapDrawable(activityRes, bitmap); + + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { + view.setBackgroundDrawable(drawable); + } else { + view.setBackground(drawable); + } + } catch (IOException ex) { + Log.e(LOG_TAG, ex.getMessage()); + } finally { + try { + is.close(); + } catch (Exception e) { + } + } + } + } + + private BrowserButton parseButtonProps(String buttonProps) { + BrowserButton result = null; + if (buttonProps != null && !buttonProps.isEmpty()) { + try { + result = ThemeableBrowserUnmarshaller.JSONToObj(buttonProps, BrowserButton.class); + } catch (Exception e) { + emitError(ERR_CRITICAL, String.format("Invalid JSON @s", e.toString())); + } + } else { + emitWarning(WRN_UNDEFINED, "No config was given."); + } + + if (result == null) { + result = new BrowserButton(); + } + + return result; + } + + private Button createButton(BrowserButton buttonProps, String description, View.OnClickListener listener) { Button result = null; if (buttonProps != null) { result = new Button(cordova.getActivity()); diff --git a/src/ios/CDVThemeableBrowser.h b/src/ios/CDVThemeableBrowser.h index ed59e22f1..06067688c 100644 --- a/src/ios/CDVThemeableBrowser.h +++ b/src/ios/CDVThemeableBrowser.h @@ -77,6 +77,7 @@ - (void)show:(CDVInvokedUrlCommand*)command; - (void)show:(CDVInvokedUrlCommand*)command withAnimation:(BOOL)animated; - (void)reload:(CDVInvokedUrlCommand*)command; +- (void)changeButtonImage:(CDVInvokedUrlCommand*)command; @end @@ -120,6 +121,7 @@ - (void)showLocationBar:(BOOL)show; - (void)showToolBar:(BOOL)show : (NSString*) toolbarPosition; - (void)setCloseButtonTitle:(NSString*)title; +- (void)changeButtonImage:(int)buttonIndex buttonProps:(NSDictionary*)buttonProps; - (id)initWithUserAgent:(NSString*)userAgent prevUserAgent:(NSString*)prevUserAgent browserOptions: (CDVThemeableBrowserOptions*) browserOptions navigationDelete:(CDVThemeableBrowser*) navigationDelegate statusBarStyle:(UIStatusBarStyle) statusBarStyle; diff --git a/src/ios/CDVThemeableBrowser.m b/src/ios/CDVThemeableBrowser.m index ce09b20ce..4178dff13 100644 --- a/src/ios/CDVThemeableBrowser.m +++ b/src/ios/CDVThemeableBrowser.m @@ -57,6 +57,7 @@ Licensed to the Apache Software Foundation (ASF) under one #define TOOLBAR_DEF_HEIGHT 44.0 #define LOCATIONBAR_HEIGHT 21.0 #define FOOTER_HEIGHT ((TOOLBAR_HEIGHT) + (LOCATIONBAR_HEIGHT)) +#define TAG_SALT 100 #pragma mark CDVThemeableBrowser @@ -129,7 +130,7 @@ - (void)open:(CDVInvokedUrlCommand*)command NSString* url = [command argumentAtIndex:0]; NSString* target = [command argumentAtIndex:1 withDefault:kThemeableBrowserTargetSelf]; NSString* options = [command argumentAtIndex:2 withDefault:@"" andClass:[NSString class]]; - + self.callbackId = command.callbackId; if (url != nil) { @@ -607,7 +608,7 @@ - (void)emitEvent:(NSDictionary*)event CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:event]; [pluginResult setKeepCallback:[NSNumber numberWithBool:YES]]; - + [self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId]; } } @@ -840,7 +841,7 @@ - (void)createViews for (NSDictionary* customButton in [customButtons reverseObjectEnumerator]) { UIButton* button = [self createButton:customButton action:@selector(goCustomButton:) withDescription:[NSString stringWithFormat:@"custom button at %ld", (long)cnt]]; if (button) { - button.tag = cnt; + button.tag = cnt+TAG_SALT; CGFloat width = [self getWidthFromButton:button]; if ([kThemeableBrowserAlignRight isEqualToString:customButton[kThemeableBrowserPropAlign]]) { [rightButtons addObject:button]; @@ -1196,6 +1197,17 @@ - (void)reload [self.webView reload]; } +- (void)changeButtonImage:(NSInteger) buttonIndex buttonProps:(NSDictionary *)buttonProps { + + UIButton* button = (UIButton *)[self.toolbar viewWithTag:buttonIndex+TAG_SALT]; + UIImage *image = [self getImage:buttonProps[kThemeableBrowserPropImage] + altPath:buttonProps[kThemeableBrowserPropWwwImage] + altDensity:[buttonProps[kThemeableBrowserPropWwwImageDensity] doubleValue]]; + if(button && image){ + [button setImage:image forState:UIControlStateNormal]; + } +} + - (void)navigateTo:(NSURL*)url { NSURLRequest* request = [NSURLRequest requestWithURL:url]; @@ -1234,7 +1246,7 @@ - (void)goForward:(id)sender - (void)goCustomButton:(id)sender { UIButton* button = sender; - NSInteger index = button.tag; + NSInteger index = button.tag-TAG_SALT; [self emitEventForButton:_browserOptions.customButtons[index] withIndex:[NSNumber numberWithLong:index]]; } From f8ab58dd9ffec9e4cdd34d97879a9cd4901b23bb Mon Sep 17 00:00:00 2001 From: toven Date: Fri, 16 Mar 2018 17:22:19 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20ios=20=E4=B8=AD=20send?= =?UTF-8?q?PluginResult=20=E5=8F=91=E9=80=81=E5=A4=B1=E8=B4=A5=E7=9A=84BUG?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ios/CDVThemeableBrowser.m | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/ios/CDVThemeableBrowser.m b/src/ios/CDVThemeableBrowser.m index 4178dff13..4ea99c8f6 100644 --- a/src/ios/CDVThemeableBrowser.m +++ b/src/ios/CDVThemeableBrowser.m @@ -67,6 +67,8 @@ @interface CDVThemeableBrowser () { NSURL *initUrl; // initial URL ThemeableBrowser opened with NSURL *originalUrl; } +@property (nonatomic,strong) dispatch_source_t timer; + @end @implementation CDVThemeableBrowser @@ -104,6 +106,8 @@ - (void)close:(CDVInvokedUrlCommand*)command withMessage:@"Close called but already closed."]; return; } + + dispatch_source_cancel(_timer); // Things are cleaned up in browserExit. [self.themeableBrowserViewController close]; } @@ -162,6 +166,19 @@ - (void)open:(CDVInvokedUrlCommand*)command [pluginResult setKeepCallback:[NSNumber numberWithBool:YES]]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + NSTimeInterval delayTime=3.0f; + NSTimeInterval timeInterval=1.0f; + dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); + _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue); + dispatch_time_t startDelayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayTime * NSEC_PER_SEC)); + dispatch_source_set_timer(_timer, startDelayTime, timeInterval * NSEC_PER_SEC, 0.1 * NSEC_PER_SEC); + dispatch_source_set_event_handler(_timer, ^{ + CDVPluginResult* pluginResult; + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; + [pluginResult setKeepCallback:[NSNumber numberWithBool:YES]]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + }); + dispatch_resume(_timer); } - (void)reload:(CDVInvokedUrlCommand*)command @@ -171,6 +188,14 @@ - (void)reload:(CDVInvokedUrlCommand*)command } } +- (void)changeButtonImage:(CDVInvokedUrlCommand *)command { + if (self.themeableBrowserViewController) { + NSInteger buttonIndex = [[command.arguments objectAtIndex:0] integerValue]; + NSDictionary* buttonProps = [command.arguments objectAtIndex:1];; + [self.themeableBrowserViewController changeButtonImage:buttonIndex buttonProps:buttonProps]; + } +} + - (CDVThemeableBrowserOptions*)parseOptions:(NSString*)options { CDVThemeableBrowserOptions* obj = [[CDVThemeableBrowserOptions alloc] init]; From d48a2502e1bec7303c51b1b3e460d6ec2507aaaf Mon Sep 17 00:00:00 2001 From: toven Date: Sun, 18 Mar 2018 15:13:59 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20js=20=E8=B0=83?= =?UTF-8?q?=E7=94=A8=E5=8E=9F=E7=94=9F=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- www/themeablebrowser.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/www/themeablebrowser.js b/www/themeablebrowser.js index a6931bf4d..e2e7fae3d 100644 --- a/www/themeablebrowser.js +++ b/www/themeablebrowser.js @@ -80,6 +80,10 @@ ThemeableBrowser.prototype = { throw new Error('insertCSS requires exactly one of code or file to be specified'); } return this; + }, + + changeButtonImage: function(buttonIndex, buttonProps){ + exec(null, null, 'ThemeableBrowser', 'changeButtonImage', [buttonIndex, buttonProps]); } }; From 4e5f27eff69d3110c1ef203299aa16d953671e30 Mon Sep 17 00:00:00 2001 From: toven Date: Sun, 18 Mar 2018 22:08:20 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20iOS=20=E7=AB=AF?= =?UTF-8?q?=E5=9B=A0=E6=8C=89=E9=92=AE=E5=80=92=E5=BA=8F=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E4=B8=8E=E6=8C=89=E9=92=AE=20tag=20=E9=A1=BA?= =?UTF-8?q?=E5=BA=8F=E4=B8=8D=E4=B8=80=E8=87=B4=E7=AB=AF=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ios/CDVThemeableBrowser.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ios/CDVThemeableBrowser.m b/src/ios/CDVThemeableBrowser.m index 4ea99c8f6..fbdab2ecf 100644 --- a/src/ios/CDVThemeableBrowser.m +++ b/src/ios/CDVThemeableBrowser.m @@ -861,7 +861,7 @@ - (void)createViews NSArray* customButtons = _browserOptions.customButtons; if (customButtons) { - NSInteger cnt = 0; + NSInteger cnt = [customButtons count]-1; // Reverse loop because we are laying out from outer to inner. for (NSDictionary* customButton in [customButtons reverseObjectEnumerator]) { UIButton* button = [self createButton:customButton action:@selector(goCustomButton:) withDescription:[NSString stringWithFormat:@"custom button at %ld", (long)cnt]]; @@ -877,7 +877,7 @@ - (void)createViews } } - cnt += 1; + cnt -= 1; } }