StatusBar.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. */
  20. package org.apache.cordova.statusbar;
  21. import android.app.Activity;
  22. import android.graphics.Color;
  23. import android.os.Build;
  24. import android.view.View;
  25. import android.view.Window;
  26. import android.view.WindowManager;
  27. import org.apache.cordova.CallbackContext;
  28. import org.apache.cordova.CordovaArgs;
  29. import org.apache.cordova.CordovaInterface;
  30. import org.apache.cordova.CordovaPlugin;
  31. import org.apache.cordova.CordovaWebView;
  32. import org.apache.cordova.LOG;
  33. import org.apache.cordova.PluginResult;
  34. import org.json.JSONException;
  35. import java.util.Arrays;
  36. public class StatusBar extends CordovaPlugin {
  37. private static final String TAG = "StatusBar";
  38. /**
  39. * Sets the context of the Command. This can then be used to do things like
  40. * get file paths associated with the Activity.
  41. *
  42. * @param cordova The context of the main Activity.
  43. * @param webView The CordovaWebView Cordova is running in.
  44. */
  45. @Override
  46. public void initialize(final CordovaInterface cordova, CordovaWebView webView) {
  47. LOG.v(TAG, "StatusBar: initialization");
  48. super.initialize(cordova, webView);
  49. this.cordova.getActivity().runOnUiThread(new Runnable() {
  50. @Override
  51. public void run() {
  52. // Clear flag FLAG_FORCE_NOT_FULLSCREEN which is set initially
  53. // by the Cordova.
  54. Window window = cordova.getActivity().getWindow();
  55. window.clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
  56. // Read 'StatusBarBackgroundColor' from config.xml, default is #000000.
  57. setStatusBarBackgroundColor(preferences.getString("StatusBarBackgroundColor", "#000000"));
  58. // Read 'StatusBarStyle' from config.xml, default is 'lightcontent'.
  59. setStatusBarStyle(preferences.getString("StatusBarStyle", "lightcontent"));
  60. }
  61. });
  62. }
  63. /**
  64. * Executes the request and returns PluginResult.
  65. *
  66. * @param action The action to execute.
  67. * @param args JSONArry of arguments for the plugin.
  68. * @param callbackContext The callback id used when calling back into JavaScript.
  69. * @return True if the action was valid, false otherwise.
  70. */
  71. @Override
  72. public boolean execute(final String action, final CordovaArgs args, final CallbackContext callbackContext) throws JSONException {
  73. LOG.v(TAG, "Executing action: " + action);
  74. final Activity activity = this.cordova.getActivity();
  75. final Window window = activity.getWindow();
  76. if ("_ready".equals(action)) {
  77. boolean statusBarVisible = (window.getAttributes().flags & WindowManager.LayoutParams.FLAG_FULLSCREEN) == 0;
  78. callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, statusBarVisible));
  79. return true;
  80. }
  81. if ("show".equals(action)) {
  82. this.cordova.getActivity().runOnUiThread(new Runnable() {
  83. @Override
  84. public void run() {
  85. // SYSTEM_UI_FLAG_FULLSCREEN is available since JellyBean, but we
  86. // use KitKat here to be aligned with "Fullscreen" preference
  87. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  88. int uiOptions = window.getDecorView().getSystemUiVisibility();
  89. uiOptions &= ~View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
  90. uiOptions &= ~View.SYSTEM_UI_FLAG_FULLSCREEN;
  91. window.getDecorView().setSystemUiVisibility(uiOptions);
  92. }
  93. // CB-11197 We still need to update LayoutParams to force status bar
  94. // to be hidden when entering e.g. text fields
  95. window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
  96. }
  97. });
  98. return true;
  99. }
  100. if ("hide".equals(action)) {
  101. this.cordova.getActivity().runOnUiThread(new Runnable() {
  102. @Override
  103. public void run() {
  104. // SYSTEM_UI_FLAG_FULLSCREEN is available since JellyBean, but we
  105. // use KitKat here to be aligned with "Fullscreen" preference
  106. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  107. int uiOptions = window.getDecorView().getSystemUiVisibility()
  108. | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
  109. | View.SYSTEM_UI_FLAG_FULLSCREEN;
  110. window.getDecorView().setSystemUiVisibility(uiOptions);
  111. }
  112. // CB-11197 We still need to update LayoutParams to force status bar
  113. // to be hidden when entering e.g. text fields
  114. window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
  115. }
  116. });
  117. return true;
  118. }
  119. if ("backgroundColorByHexString".equals(action)) {
  120. this.cordova.getActivity().runOnUiThread(new Runnable() {
  121. @Override
  122. public void run() {
  123. try {
  124. setStatusBarBackgroundColor(args.getString(0));
  125. } catch (JSONException ignore) {
  126. LOG.e(TAG, "Invalid hexString argument, use f.i. '#777777'");
  127. }
  128. }
  129. });
  130. return true;
  131. }
  132. if ("overlaysWebView".equals(action)) {
  133. if (Build.VERSION.SDK_INT >= 21) {
  134. this.cordova.getActivity().runOnUiThread(new Runnable() {
  135. @Override
  136. public void run() {
  137. try {
  138. setStatusBarTransparent(args.getBoolean(0));
  139. } catch (JSONException ignore) {
  140. LOG.e(TAG, "Invalid boolean argument");
  141. }
  142. }
  143. });
  144. return true;
  145. }
  146. else return args.getBoolean(0) == false;
  147. }
  148. if ("styleDefault".equals(action)) {
  149. this.cordova.getActivity().runOnUiThread(new Runnable() {
  150. @Override
  151. public void run() {
  152. setStatusBarStyle("default");
  153. }
  154. });
  155. return true;
  156. }
  157. if ("styleLightContent".equals(action)) {
  158. this.cordova.getActivity().runOnUiThread(new Runnable() {
  159. @Override
  160. public void run() {
  161. setStatusBarStyle("lightcontent");
  162. }
  163. });
  164. return true;
  165. }
  166. if ("styleBlackTranslucent".equals(action)) {
  167. this.cordova.getActivity().runOnUiThread(new Runnable() {
  168. @Override
  169. public void run() {
  170. setStatusBarStyle("blacktranslucent");
  171. }
  172. });
  173. return true;
  174. }
  175. if ("styleBlackOpaque".equals(action)) {
  176. this.cordova.getActivity().runOnUiThread(new Runnable() {
  177. @Override
  178. public void run() {
  179. setStatusBarStyle("blackopaque");
  180. }
  181. });
  182. return true;
  183. }
  184. return false;
  185. }
  186. private void setStatusBarBackgroundColor(final String colorPref) {
  187. if (Build.VERSION.SDK_INT >= 21) {
  188. if (colorPref != null && !colorPref.isEmpty()) {
  189. final Window window = cordova.getActivity().getWindow();
  190. // Method and constants not available on all SDKs but we want to be able to compile this code with any SDK
  191. window.clearFlags(0x04000000); // SDK 19: WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
  192. window.addFlags(0x80000000); // SDK 21: WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
  193. try {
  194. // Using reflection makes sure any 5.0+ device will work without having to compile with SDK level 21
  195. window.getClass().getMethod("setStatusBarColor", int.class).invoke(window, Color.parseColor(colorPref));
  196. } catch (IllegalArgumentException ignore) {
  197. LOG.e(TAG, "Invalid hexString argument, use f.i. '#999999'");
  198. } catch (Exception ignore) {
  199. // this should not happen, only in case Android removes this method in a version > 21
  200. LOG.w(TAG, "Method window.setStatusBarColor not found for SDK level " + Build.VERSION.SDK_INT);
  201. }
  202. }
  203. }
  204. }
  205. private void setStatusBarTransparent(final boolean transparent) {
  206. if (Build.VERSION.SDK_INT >= 21) {
  207. final Window window = cordova.getActivity().getWindow();
  208. if (transparent) {
  209. window.getDecorView().setSystemUiVisibility(
  210. View.SYSTEM_UI_FLAG_LAYOUT_STABLE
  211. | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
  212. window.setStatusBarColor(Color.TRANSPARENT);
  213. }
  214. else {
  215. window.getDecorView().setSystemUiVisibility(
  216. View.SYSTEM_UI_FLAG_LAYOUT_STABLE
  217. | View.SYSTEM_UI_FLAG_VISIBLE);
  218. }
  219. }
  220. }
  221. private void setStatusBarStyle(final String style) {
  222. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  223. if (style != null && !style.isEmpty()) {
  224. View decorView = cordova.getActivity().getWindow().getDecorView();
  225. int uiOptions = decorView.getSystemUiVisibility();
  226. String[] darkContentStyles = {
  227. "default",
  228. };
  229. String[] lightContentStyles = {
  230. "lightcontent",
  231. "blacktranslucent",
  232. "blackopaque",
  233. };
  234. if (Arrays.asList(darkContentStyles).contains(style.toLowerCase())) {
  235. decorView.setSystemUiVisibility(uiOptions | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
  236. return;
  237. }
  238. if (Arrays.asList(lightContentStyles).contains(style.toLowerCase())) {
  239. decorView.setSystemUiVisibility(uiOptions & ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
  240. return;
  241. }
  242. LOG.e(TAG, "Invalid style, must be either 'default', 'lightcontent' or the deprecated 'blacktranslucent' and 'blackopaque'");
  243. }
  244. }
  245. }
  246. }