MOBILE-4331 native: Impl setNavigationBarColor

Based on https://github.com/fagundes/cordova-plugin-navigationbar/blob/master/src/android/NavigationBar.java

Co-authored-by: Vinícius Fagundes <mvlacerda@gmail.com>
main
Noel De Martin 2023-07-05 12:00:51 +02:00
parent f202bcbb4a
commit ea40a685f5
2 changed files with 42 additions and 6 deletions

View File

@ -14,7 +14,10 @@
package com.moodle.moodlemobile; package com.moodle.moodlemobile;
import android.graphics.Color;
import android.os.Build;
import android.util.Log; import android.util.Log;
import android.view.Window;
import org.apache.cordova.CordovaPlugin; import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext; import org.apache.cordova.CallbackContext;
@ -30,7 +33,7 @@ public class SystemUI extends CordovaPlugin {
try { try {
switch (action) { switch (action) {
case "setNavigationBarColor": case "setNavigationBarColor":
this.setNavigationBarColor(); this.setNavigationBarColor(args.getString(0));
callbackContext.success(); callbackContext.success();
return true; return true;
@ -42,10 +45,41 @@ public class SystemUI extends CordovaPlugin {
return false; return false;
} }
private void setNavigationBarColor() { private void setNavigationBarColor(String color) {
Log.e(TAG, "Setting navigation bar 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);
}
}
});
} }
} }

View File

@ -19,10 +19,12 @@ export class SystemUI {
/** /**
* Set navigation bar color. * Set navigation bar color.
*
* @param color Color.
*/ */
async setNavigationBarColor(): Promise<void> { async setNavigationBarColor(color: string): Promise<void> {
await new Promise((resolve, reject) => { await new Promise((resolve, reject) => {
cordova.exec(resolve, reject, 'SystemUI', 'setNavigationBarColor', []); cordova.exec(resolve, reject, 'SystemUI', 'setNavigationBarColor', [color]);
}); });
} }