arnaucube
arnaucube/commonroutesServerPublic Self-hosted repository
commonroutesServer/www/plugins/cordova-plugin-statusbar/src/android/StatusBar.java
www/plugins/cordova-plugin-statusbar/src/android/StatusBar.javaRaw
  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*/
 20package org.apache.cordova.statusbar;
 21
 22import android.app.Activity;
 23import android.graphics.Color;
 24import android.os.Build;
 25import android.view.View;
 26import android.view.Window;
 27import android.view.WindowManager;
 28
 29import org.apache.cordova.CallbackContext;
 30import org.apache.cordova.CordovaArgs;
 31import org.apache.cordova.CordovaInterface;
 32import org.apache.cordova.CordovaPlugin;
 33import org.apache.cordova.CordovaWebView;
 34import org.apache.cordova.LOG;
 35import org.apache.cordova.PluginResult;
 36import org.json.JSONException;
 37
 38public class StatusBar extends CordovaPlugin {
 39    private static final String TAG = "StatusBar";
 40
 41    /**
 42     * Sets the context of the Command. This can then be used to do things like
 43     * get file paths associated with the Activity.
 44     *
 45     * @param cordova The context of the main Activity.
 46     * @param webView The CordovaWebView Cordova is running in.
 47     */
 48    @Override
 49    public void initialize(final CordovaInterface cordova, CordovaWebView webView) {
 50        LOG.v(TAG, "StatusBar: initialization");
 51        super.initialize(cordova, webView);
 52
 53        this.cordova.getActivity().runOnUiThread(new Runnable() {
 54            @Override
 55            public void run() {
 56                // Clear flag FLAG_FORCE_NOT_FULLSCREEN which is set initially
 57                // by the Cordova.
 58                Window window = cordova.getActivity().getWindow();
 59                window.clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
 60
 61                // Read 'StatusBarBackgroundColor' from config.xml, default is #000000.
 62                setStatusBarBackgroundColor(preferences.getString("StatusBarBackgroundColor", "#000000"));
 63            }
 64        });
 65    }
 66
 67    /**
 68     * Executes the request and returns PluginResult.
 69     *
 70     * @param action            The action to execute.
 71     * @param args              JSONArry of arguments for the plugin.
 72     * @param callbackContext   The callback id used when calling back into JavaScript.
 73     * @return                  True if the action was valid, false otherwise.
 74     */
 75    @Override
 76    public boolean execute(final String action, final CordovaArgs args, final CallbackContext callbackContext) throws JSONException {
 77        LOG.v(TAG, "Executing action: " + action);
 78        final Activity activity = this.cordova.getActivity();
 79        final Window window = activity.getWindow();
 80
 81        if ("_ready".equals(action)) {
 82            boolean statusBarVisible = (window.getAttributes().flags & WindowManager.LayoutParams.FLAG_FULLSCREEN) == 0;
 83            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, statusBarVisible));
 84            return true;
 85        }
 86
 87        if ("show".equals(action)) {
 88            this.cordova.getActivity().runOnUiThread(new Runnable() {
 89                @Override
 90                public void run() {
 91                    // SYSTEM_UI_FLAG_FULLSCREEN is available since JellyBean, but we
 92                    // use KitKat here to be aligned with "Fullscreen"  preference
 93                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
 94                        int uiOptions = window.getDecorView().getSystemUiVisibility();
 95                        uiOptions &= ~View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
 96                        uiOptions &= ~View.SYSTEM_UI_FLAG_FULLSCREEN;
 97
 98                        window.getDecorView().setSystemUiVisibility(uiOptions);
 99                    }
100
101                    // CB-11197 We still need to update LayoutParams to force status bar
102                    // to be hidden when entering e.g. text fields
103                    window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
104                }
105            });
106            return true;
107        }
108
109        if ("hide".equals(action)) {
110            this.cordova.getActivity().runOnUiThread(new Runnable() {
111                @Override
112                public void run() {
113                    // SYSTEM_UI_FLAG_FULLSCREEN is available since JellyBean, but we
114                    // use KitKat here to be aligned with "Fullscreen"  preference
115                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
116                        int uiOptions = window.getDecorView().getSystemUiVisibility()
117                                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
118                                | View.SYSTEM_UI_FLAG_FULLSCREEN;
119
120                        window.getDecorView().setSystemUiVisibility(uiOptions);
121                    }
122
123                    // CB-11197 We still need to update LayoutParams to force status bar
124                    // to be hidden when entering e.g. text fields
125                    window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
126                }
127            });
128            return true;
129        }
130
131        if ("backgroundColorByHexString".equals(action)) {
132            this.cordova.getActivity().runOnUiThread(new Runnable() {
133                @Override
134                public void run() {
135                    try {
136                        setStatusBarBackgroundColor(args.getString(0));
137                    } catch (JSONException ignore) {
138                        LOG.e(TAG, "Invalid hexString argument, use f.i. '#777777'");
139                    }
140                }
141            });
142            return true;
143        }
144
145        return false;
146    }
147
148    private void setStatusBarBackgroundColor(final String colorPref) {
149        if (Build.VERSION.SDK_INT >= 21) {
150            if (colorPref != null && !colorPref.isEmpty()) {
151                final Window window = cordova.getActivity().getWindow();
152                // Method and constants not available on all SDKs but we want to be able to compile this code with any SDK
153                window.clearFlags(0x04000000); // SDK 19: WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
154                window.addFlags(0x80000000); // SDK 21: WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
155                try {
156                    // Using reflection makes sure any 5.0+ device will work without having to compile with SDK level 21
157                    window.getClass().getDeclaredMethod("setStatusBarColor", int.class).invoke(window, Color.parseColor(colorPref));
158                } catch (IllegalArgumentException ignore) {
159                    LOG.e(TAG, "Invalid hexString argument, use f.i. '#999999'");
160                } catch (Exception ignore) {
161                    // this should not happen, only in case Android removes this method in a version > 21
162                    LOG.w(TAG, "Method window.setStatusBarColor not found for SDK level " + Build.VERSION.SDK_INT);
163                }
164            }
165        }
166    }
167}