From ec08d0ec6514a04d612152a852a42ca7d256eafc Mon Sep 17 00:00:00 2001 From: Yaroslav Dmytrotsa Date: Tue, 11 Aug 2015 10:25:19 +0300 Subject: [PATCH] add Cordova statusbar plugin --- .../boilerplate/cordova/cordova_plugins.js | 10 +- .../cordova-plugin-statusbar/www/statusbar.js | 111 ++++++++++++++ .../apache/cordova/statusbar/StatusBar.java | 140 ++++++++++++++++++ app/src/main/res/xml/config.xml | 5 + 4 files changed, 265 insertions(+), 1 deletion(-) create mode 100644 app/src/main/assets/www/boilerplate/cordova/plugins/cordova-plugin-statusbar/www/statusbar.js create mode 100644 app/src/main/java/org/apache/cordova/statusbar/StatusBar.java diff --git a/app/src/main/assets/www/boilerplate/cordova/cordova_plugins.js b/app/src/main/assets/www/boilerplate/cordova/cordova_plugins.js index fa392f0..26bba44 100644 --- a/app/src/main/assets/www/boilerplate/cordova/cordova_plugins.js +++ b/app/src/main/assets/www/boilerplate/cordova/cordova_plugins.js @@ -118,6 +118,13 @@ module.exports = [ "navigator.notification", "navigator" ] + }, + { + "file": "plugins/cordova-plugin-statusbar/www/statusbar.js", + "id": "cordova-plugin-statusbar.statusbar", + "clobbers": [ + "window.StatusBar" + ] } ]; module.exports.metadata = @@ -132,7 +139,8 @@ module.exports.metadata = "cordova-plugin-device-orientation": "1.0.1", "cordova-plugin-camera": "1.2.0", "cordova-plugin-dialogs": "1.1.1", - "cordova-plugin-vibration": "1.2.0" + "cordova-plugin-vibration": "1.2.0", + "cordova-plugin-statusbar": "1.0.1" } // BOTTOM OF METADATA }); \ No newline at end of file diff --git a/app/src/main/assets/www/boilerplate/cordova/plugins/cordova-plugin-statusbar/www/statusbar.js b/app/src/main/assets/www/boilerplate/cordova/plugins/cordova-plugin-statusbar/www/statusbar.js new file mode 100644 index 0000000..cd2b9e2 --- /dev/null +++ b/app/src/main/assets/www/boilerplate/cordova/plugins/cordova-plugin-statusbar/www/statusbar.js @@ -0,0 +1,111 @@ +cordova.define("cordova-plugin-statusbar.statusbar", function(require, exports, module) { /* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * +*/ + +var exec = require('cordova/exec'); + +var namedColors = { + "black": "#000000", + "darkGray": "#A9A9A9", + "lightGray": "#D3D3D3", + "white": "#FFFFFF", + "gray": "#808080", + "red": "#FF0000", + "green": "#00FF00", + "blue": "#0000FF", + "cyan": "#00FFFF", + "yellow": "#FFFF00", + "magenta": "#FF00FF", + "orange": "#FFA500", + "purple": "#800080", + "brown": "#A52A2A" +}; + +var StatusBar = { + + isVisible: true, + + overlaysWebView: function (doOverlay) { + exec(null, null, "StatusBar", "overlaysWebView", [doOverlay]); + }, + + styleDefault: function () { + // dark text ( to be used on a light background ) + exec(null, null, "StatusBar", "styleDefault", []); + }, + + styleLightContent: function () { + // light text ( to be used on a dark background ) + exec(null, null, "StatusBar", "styleLightContent", []); + }, + + styleBlackTranslucent: function () { + // #88000000 ? Apple says to use lightContent instead + exec(null, null, "StatusBar", "styleBlackTranslucent", []); + }, + + styleBlackOpaque: function () { + // #FF000000 ? Apple says to use lightContent instead + exec(null, null, "StatusBar", "styleBlackOpaque", []); + }, + + backgroundColorByName: function (colorname) { + return StatusBar.backgroundColorByHexString(namedColors[colorname]); + }, + + backgroundColorByHexString: function (hexString) { + if (hexString.charAt(0) !== "#") { + hexString = "#" + hexString; + } + + if (hexString.length === 4) { + var split = hexString.split(""); + hexString = "#" + split[1] + split[1] + split[2] + split[2] + split[3] + split[3]; + } + + exec(null, null, "StatusBar", "backgroundColorByHexString", [hexString]); + }, + + hide: function () { + exec(null, null, "StatusBar", "hide", []); + StatusBar.isVisible = false; + }, + + show: function () { + exec(null, null, "StatusBar", "show", []); + StatusBar.isVisible = true; + } + +}; + +// prime it +exec(function (res) { + if (typeof res == 'object') { + if (res.type == 'tap') { + cordova.fireWindowEvent('statusTap'); + } + } else { + StatusBar.isVisible = res; + } +}, null, "StatusBar", "_ready", []); + +module.exports = StatusBar; + +}); diff --git a/app/src/main/java/org/apache/cordova/statusbar/StatusBar.java b/app/src/main/java/org/apache/cordova/statusbar/StatusBar.java new file mode 100644 index 0000000..4d3b85b --- /dev/null +++ b/app/src/main/java/org/apache/cordova/statusbar/StatusBar.java @@ -0,0 +1,140 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * +*/ +package org.apache.cordova.statusbar; + +import android.app.Activity; +import android.graphics.Color; +import android.os.Build; +import android.util.Log; +import android.view.Window; +import android.view.WindowManager; + +import org.apache.cordova.CallbackContext; +import org.apache.cordova.CordovaArgs; +import org.apache.cordova.CordovaInterface; +import org.apache.cordova.CordovaPlugin; +import org.apache.cordova.CordovaWebView; +import org.apache.cordova.PluginResult; +import org.json.JSONException; + +public class StatusBar extends CordovaPlugin { + private static final String TAG = "StatusBar"; + + /** + * Sets the context of the Command. This can then be used to do things like + * get file paths associated with the Activity. + * + * @param cordova The context of the main Activity. + * @param webView The CordovaWebView Cordova is running in. + */ + @Override + public void initialize(final CordovaInterface cordova, CordovaWebView webView) { + Log.v(TAG, "StatusBar: initialization"); + super.initialize(cordova, webView); + + this.cordova.getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + // Clear flag FLAG_FORCE_NOT_FULLSCREEN which is set initially + // by the Cordova. + Window window = cordova.getActivity().getWindow(); + window.clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); + + // Read 'StatusBarBackgroundColor' from config.xml, default is #000000. + setStatusBarBackgroundColor(preferences.getString("StatusBarBackgroundColor", "#000000")); + } + }); + } + + /** + * Executes the request and returns PluginResult. + * + * @param action The action to execute. + * @param args JSONArry of arguments for the plugin. + * @param callbackContext The callback id used when calling back into JavaScript. + * @return True if the action was valid, false otherwise. + */ + @Override + public boolean execute(final String action, final CordovaArgs args, final CallbackContext callbackContext) throws JSONException { + Log.v(TAG, "Executing action: " + action); + final Activity activity = this.cordova.getActivity(); + final Window window = activity.getWindow(); + if ("_ready".equals(action)) { + boolean statusBarVisible = (window.getAttributes().flags & WindowManager.LayoutParams.FLAG_FULLSCREEN) == 0; + callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, statusBarVisible)); + } + + if ("show".equals(action)) { + this.cordova.getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); + } + }); + return true; + } + + if ("hide".equals(action)) { + this.cordova.getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); + } + }); + return true; + } + + if ("backgroundColorByHexString".equals(action)) { + this.cordova.getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + try { + setStatusBarBackgroundColor(args.getString(0)); + } catch (JSONException ignore) { + Log.e(TAG, "Invalid hexString argument, use f.i. '#777777'"); + } + } + }); + return true; + } + + return false; + } + + private void setStatusBarBackgroundColor(final String colorPref) { + if (Build.VERSION.SDK_INT >= 21) { + if (colorPref != null && !colorPref.isEmpty()) { + final Window window = cordova.getActivity().getWindow(); + // Method and constants not available on all SDKs but we want to be able to compile this code with any SDK + window.clearFlags(0x04000000); // SDK 19: WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); + window.addFlags(0x80000000); // SDK 21: WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); + try { + // Using reflection makes sure any 5.0+ device will work without having to compile with SDK level 21 + window.getClass().getDeclaredMethod("setStatusBarColor", int.class).invoke(window, Color.parseColor(colorPref)); + } catch (IllegalArgumentException ignore) { + Log.e(TAG, "Invalid hexString argument, use f.i. '#999999'"); + } catch (Exception ignore) { + // this should not happen, only in case Android removes this method in a version > 21 + Log.w(TAG, "Method window.setStatusBarColor not found for SDK level " + Build.VERSION.SDK_INT); + } + } + } + } +} diff --git a/app/src/main/res/xml/config.xml b/app/src/main/res/xml/config.xml index 89305ea..90684da 100644 --- a/app/src/main/res/xml/config.xml +++ b/app/src/main/res/xml/config.xml @@ -60,6 +60,11 @@ + + + + +