From ea40a685f59c1f21df10abe97e51549471c1a04d Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Wed, 5 Jul 2023 12:00:51 +0200 Subject: [PATCH] MOBILE-4331 native: Impl setNavigationBarColor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Based on https://github.com/fagundes/cordova-plugin-navigationbar/blob/master/src/android/NavigationBar.java Co-authored-by: Vinícius Fagundes --- .../src/android/SystemUI.java | 42 +++++++++++++++++-- .../src/ts/plugins/SystemUI.ts | 6 ++- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/cordova-plugin-moodleapp/src/android/SystemUI.java b/cordova-plugin-moodleapp/src/android/SystemUI.java index 91b47959c..217c72df9 100644 --- a/cordova-plugin-moodleapp/src/android/SystemUI.java +++ b/cordova-plugin-moodleapp/src/android/SystemUI.java @@ -14,7 +14,10 @@ package com.moodle.moodlemobile; +import android.graphics.Color; +import android.os.Build; import android.util.Log; +import android.view.Window; import org.apache.cordova.CordovaPlugin; import org.apache.cordova.CallbackContext; @@ -30,7 +33,7 @@ public class SystemUI extends CordovaPlugin { try { switch (action) { case "setNavigationBarColor": - this.setNavigationBarColor(); + this.setNavigationBarColor(args.getString(0)); callbackContext.success(); return true; @@ -42,10 +45,41 @@ public class SystemUI extends CordovaPlugin { return false; } - private void setNavigationBarColor() { - Log.e(TAG, "Setting navigation bar color"); + private void setNavigationBarColor(String color) { + if (Build.VERSION.SDK_INT < 21) { + return; + } - // TODO + if (color == null || color.isEmpty()) { + return; + } + + Log.d(TAG, "Setting navigation bar color to " + color); + + this.cordova.getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + final int FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS = 0x80000000; + final int SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR = 0x00000010; + final Window window = cordova.getActivity().getWindow(); + int uiOptions = window.getDecorView().getSystemUiVisibility(); + + uiOptions = uiOptions | FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS; + uiOptions = uiOptions & ~SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR; + + window.getDecorView().setSystemUiVisibility(uiOptions); + + try { + // Using reflection makes sure any 5.0+ device will work without having to compile with SDK level 21 + window.getClass().getDeclaredMethod("setNavigationBarColor", int.class).invoke(window, Color.parseColor(color)); + } 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.setNavigationBarColor not found for SDK level " + Build.VERSION.SDK_INT); + } + } + }); } } diff --git a/cordova-plugin-moodleapp/src/ts/plugins/SystemUI.ts b/cordova-plugin-moodleapp/src/ts/plugins/SystemUI.ts index 86e24235f..28494b2b1 100644 --- a/cordova-plugin-moodleapp/src/ts/plugins/SystemUI.ts +++ b/cordova-plugin-moodleapp/src/ts/plugins/SystemUI.ts @@ -19,10 +19,12 @@ export class SystemUI { /** * Set navigation bar color. + * + * @param color Color. */ - async setNavigationBarColor(): Promise { + async setNavigationBarColor(color: string): Promise { await new Promise((resolve, reject) => { - cordova.exec(resolve, reject, 'SystemUI', 'setNavigationBarColor', []); + cordova.exec(resolve, reject, 'SystemUI', 'setNavigationBarColor', [color]); }); }