package metricrat.co.uk.hidebars; import android.app.*; import android.content.*; import android.view.*; import com.google.appinventor.components.annotations.*; import com.google.appinventor.components.runtime.*; import android.os.Build; import android.view.WindowInsets; import android.widget.LinearLayout.LayoutParams; import org.json.JSONArray; import org.json.JSONException; public class HideBars extends AndroidNonvisibleComponent { private ComponentContainer container; private static Context context; private final Activity activity; float density; public HideBars(ComponentContainer container) { super(container.$form()); this.container = container; context = container.$context(); this.activity = container.$context(); this.density = this.form.deviceDensity(); } @SimpleFunction(description = "hides nav bar - you can swipe it open") public void HideNavBar() { Window scrn = this.activity.getWindow(); scrn.getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES; scrn.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); scrn.getInsetsController().hide(WindowInsets.Type.navigationBars()); scrn.getInsetsController().setSystemBarsBehavior(WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE); scrn.setDecorFitsSystemWindows(false); } @SimpleFunction(description = "hides status bar - you can swipe it open") public void HideStatusBar() { Window scrn = this.activity.getWindow(); scrn.getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES; scrn.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); scrn.getInsetsController().hide(WindowInsets.Type.statusBars()); scrn.getInsetsController().setSystemBarsBehavior(WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE); scrn.setDecorFitsSystemWindows(false); } @SimpleFunction(description = "hides system bars - you can swipe them open, and makes app fullscreen") public void HideSystemUI() { Window scrn = this.activity.getWindow(); scrn.getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES; scrn.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); scrn.getInsetsController().hide(WindowInsets.Type.systemBars()); scrn.getInsetsController().setSystemBarsBehavior(WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE); scrn.setDecorFitsSystemWindows(false); } @SimpleFunction(description = "shows system bars and clears full screen settings") public void ShowSystemUI() { Window scrn = this.activity.getWindow(); scrn.getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT; scrn.clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); scrn.getInsetsController().show(WindowInsets.Type.systemBars()); scrn.getInsetsController().setSystemBarsBehavior(WindowInsetsController.BEHAVIOR_DEFAULT); scrn.setDecorFitsSystemWindows(true); } @SimpleFunction(description = "sets system bars colours") public void SetSystemBarsColours(int colour) { Window scrn = this.activity.getWindow(); if (Build.VERSION.SDK_INT >= 35) { WindowInsets windowInsets = scrn.getDecorView().getRootWindowInsets(); scrn.getDecorView().setOnApplyWindowInsetsListener((view, insets) -> { int NavHeight = windowInsets.getInsets(WindowInsets.Type.systemBars()).bottom; view.setPadding(0, 0, 0, NavHeight); view.setBackgroundColor(colour); return insets; }); } else { scrn.setStatusBarColor(colour); scrn.setNavigationBarColor(colour); } } @SimpleFunction(description = "sets the background colour of the Status Bar") public void SetStatusBarColourMethod(int colour) { Window scrn = this.activity.getWindow(); if (Build.VERSION.SDK_INT >= 35) { WindowInsets windowInsets = scrn.getDecorView().getRootWindowInsets(); scrn.getDecorView().setOnApplyWindowInsetsListener((view, insets) -> { int statusBarHeight = windowInsets.getInsets(WindowInsets.Type.statusBars()).top; view.setPadding(0, statusBarHeight, 0, 0); view.setBackgroundColor(colour); return insets; }); } else { scrn.setStatusBarColor(colour); } } @SimpleProperty(description = "sets the background colour of the Status Bar") public void SetStatusBarColour(int colour) { Window scrn = this.activity.getWindow(); if (Build.VERSION.SDK_INT >= 35) { WindowInsets windowInsets = scrn.getDecorView().getRootWindowInsets(); scrn.getDecorView().setOnApplyWindowInsetsListener((view, insets) -> { int statusBarHeight = windowInsets.getInsets(WindowInsets.Type.statusBars()).top; view.setPadding(0, statusBarHeight, 0, 0); view.setBackgroundColor(colour); return insets; }); } else { scrn.setStatusBarColor(colour); } } @SimpleProperty(description = "sets the background colour of the Nav Bar ") public void SetNavBarColour(int colour) { Window scrn = this.activity.getWindow(); if (Build.VERSION.SDK_INT >= 35) { WindowInsets windowInsets = scrn.getDecorView().getRootWindowInsets(); scrn.getDecorView().setOnApplyWindowInsetsListener((view, insets) -> { int navBarHeight = windowInsets.getInsets(WindowInsets.Type.navigationBars()).bottom; view.setPadding(0, 0, 0, navBarHeight); view.setBackgroundColor(colour); return insets; }); } else { scrn.setNavigationBarColor(colour); } } @SimpleFunction(description = "sets system bars text/icon colours to light mode") public void SetLightMode() { Window scrn = this.activity.getWindow(); scrn.getInsetsController().setSystemBarsAppearance(WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS, WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS); } @SimpleFunction(description = "sets system bars text/icon colours to dark mode") public void SetDarkMode() { Window scrn = this.activity.getWindow(); scrn.getInsetsController().setSystemBarsAppearance(0, WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS); } //credits @Kevinkun @SimpleFunction( description = "Set margins for a component.The margin value can be a single number (setting all four directions at once), or four numbers separated" + " by " + "commas (setting left, top, right, bottom separately). Does not work for Screen." ) public void SetMargin(AndroidViewComponent component, String margin) throws JSONException { View view = component.getView(); Object params = view.getLayoutParams(); if (params instanceof LayoutParams) { LayoutParams lp = (LayoutParams)params; if (!margin.startsWith("[")) { margin = "[" + margin + "]"; } JSONArray m = new JSONArray(margin); if (m.length() == 4) { lp.setMargins((int)((float)m.getInt(0) * this.density), (int)((float)m.getInt(1) * this.density), (int)((float)m.getInt(2) * this.density), (int)((float)m.getInt(3) * this.density)); } else if (m.length() == 1) { lp.setMargins((int)((float)m.getInt(0) * this.density), (int)((float)m.getInt(0) * this.density), (int)((float)m.getInt(0) * this.density), (int)((float)m.getInt(0) * this.density)); } view.setLayoutParams(lp); view.invalidate(); } } }