Skip to content

Commit

Permalink
MOBILE-4331 native: Impl setNavigationBarColor
Browse files Browse the repository at this point in the history
  • Loading branch information
NoelDeMartin and fagundes committed Jul 5, 2023
1 parent e3f8d96 commit 0edbfdf
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
42 changes: 38 additions & 4 deletions cordova-plugin-moodleapp/src/android/SystemUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,7 +33,7 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
try {
switch (action) {
case "setNavigationBarColor":
this.setNavigationBarColor();
this.setNavigationBarColor(args.getString(0));
callbackContext.success();

return true;
Expand All @@ -42,10 +45,41 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
return false;
}

private void setNavigationBarColor() {
Log.e(TAG, "Setting navigation bar color");
private void setNavigationBarColor(String color) {
if (Build.VERSION.SDK_INT < 21) {
return;
}

if (color == null || color.isEmpty()) {
return;
}

Log.d(TAG, "Setting navigation bar color to " + color);

// TODO
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);
}
}
});
}

}
6 changes: 4 additions & 2 deletions cordova-plugin-moodleapp/src/ts/plugins/SystemUI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ export class SystemUI {

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

Expand Down

0 comments on commit 0edbfdf

Please sign in to comment.