From f205209613a18195f43c012608383e60c0df1606 Mon Sep 17 00:00:00 2001 From: Mathias-Boulay Date: Mon, 16 Dec 2024 16:04:59 +0100 Subject: [PATCH] Qol(settings): use energy saving settings for powerful devices Less boiling pocket heaters. May contain a small few refactor --- .../customcontrols/gamepad/Gamepad.java | 12 ++-- .../prefs/LauncherPreferences.java | 62 ++++++++++++------- .../prefs/QuickSettingSideDialog.java | 8 +-- .../LauncherPreferenceVideoFragment.java | 21 ++++--- .../kdt/pojavlaunch/utils/LocaleUtils.java | 3 +- .../src/main/res/values/values.xml | 11 ++++ .../src/main/res/xml/pref_control.xml | 12 ++-- .../src/main/res/xml/pref_java.xml | 2 +- .../src/main/res/xml/pref_video.xml | 2 +- 9 files changed, 81 insertions(+), 52 deletions(-) create mode 100644 app_pojavlauncher/src/main/res/values/values.xml diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/gamepad/Gamepad.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/gamepad/Gamepad.java index 4d3a7b135a..49fccca082 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/gamepad/Gamepad.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/gamepad/Gamepad.java @@ -43,6 +43,7 @@ import static net.kdt.pojavlaunch.customcontrols.gamepad.GamepadJoystick.DIRECTION_WEST; import static net.kdt.pojavlaunch.customcontrols.gamepad.GamepadJoystick.isJoystickEvent; import static net.kdt.pojavlaunch.prefs.LauncherPreferences.PREF_DEADZONE_SCALE; +import static net.kdt.pojavlaunch.prefs.LauncherPreferences.PREF_SCALE_FACTOR; import static net.kdt.pojavlaunch.utils.MCOptionUtils.getMcScale; import static org.lwjgl.glfw.CallbackBridge.sendKeyPress; import static org.lwjgl.glfw.CallbackBridge.sendMouseButton; @@ -52,9 +53,6 @@ public class Gamepad implements GrabListener, GamepadHandler { - /* Resolution scaler option, allow downsizing a window */ - private final float mScaleFactor = LauncherPreferences.DEFAULT_PREF.getInt("resolutionRatio",100)/100f; - /* Sensitivity, adjusted according to screen size */ private final double mSensitivityFactor = (1.4 * (1080f/ currentDisplayMetrics.heightPixels)); @@ -116,7 +114,7 @@ public void doFrame(long frameTimeNanos) { mPointerImageView.setImageDrawable(ResourcesCompat.getDrawable(ctx.getResources(), R.drawable.ic_gamepad_pointer, ctx.getTheme())); mPointerImageView.getDrawable().setFilterBitmap(false); - int size = (int) ((22 * getMcScale()) / mScaleFactor); + int size = (int) ((22 * getMcScale()) / PREF_SCALE_FACTOR); mPointerImageView.setLayoutParams(new FrameLayout.LayoutParams(size, size)); mMapProvider = mapProvider; @@ -155,7 +153,7 @@ public void updateJoysticks(){ public void notifyGUISizeChange(int newSize){ //Change the pointer size to match UI - int size = (int) ((22 * newSize) / mScaleFactor); + int size = (int) ((22 * newSize) / PREF_SCALE_FACTOR); mPointerImageView.post(() -> mPointerImageView.setLayoutParams(new FrameLayout.LayoutParams(size, size))); } @@ -228,7 +226,7 @@ private void tick(long frameTimeNanos){ if(!isGrabbing){ CallbackBridge.mouseX = MathUtils.clamp(CallbackBridge.mouseX, 0, CallbackBridge.windowWidth); CallbackBridge.mouseY = MathUtils.clamp(CallbackBridge.mouseY, 0, CallbackBridge.windowHeight); - placePointerView((int) (CallbackBridge.mouseX / mScaleFactor), (int) (CallbackBridge.mouseY/ mScaleFactor)); + placePointerView((int) (CallbackBridge.mouseX / PREF_SCALE_FACTOR), (int) (CallbackBridge.mouseY/ PREF_SCALE_FACTOR)); } //Send the mouse to the game @@ -340,7 +338,7 @@ public void onGrabState(boolean isGrabbing) { placePointerView(CallbackBridge.physicalWidth/2, CallbackBridge.physicalHeight/2); mPointerImageView.setVisibility(View.VISIBLE); // Sensitivity in menu is MC and HARDWARE resolution dependent - mMouseSensitivity = 19 * mScaleFactor / mSensitivityFactor; + mMouseSensitivity = 19 * PREF_SCALE_FACTOR / mSensitivityFactor; } @Override diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/prefs/LauncherPreferences.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/prefs/LauncherPreferences.java index 5e73275034..9eba503313 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/prefs/LauncherPreferences.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/prefs/LauncherPreferences.java @@ -10,13 +10,9 @@ import android.content.res.Configuration; import android.graphics.Rect; import android.os.Build; +import android.util.DisplayMetrics; import android.util.Log; -import androidx.core.view.DisplayCutoutCompat; -import androidx.core.view.WindowCompat; -import androidx.core.view.WindowInsetsCompat; -import androidx.core.view.WindowInsetsControllerCompat; - import net.kdt.pojavlaunch.*; import net.kdt.pojavlaunch.multirt.MultiRTUtils; import net.kdt.pojavlaunch.utils.JREUtils; @@ -28,11 +24,6 @@ public class LauncherPreferences { public static SharedPreferences DEFAULT_PREF; public static String PREF_RENDERER = "opengles2"; - public static boolean PREF_VERTYPE_RELEASE = true; - public static boolean PREF_VERTYPE_SNAPSHOT = false; - public static boolean PREF_VERTYPE_OLDALPHA = false; - public static boolean PREF_VERTYPE_OLDBETA = false; - public static boolean PREF_HIDE_SIDEBAR = false; public static boolean PREF_IGNORE_NOTCH = false; public static int PREF_NOTCH_SIZE = 0; public static float PREF_BUTTONSIZE = 100f; @@ -54,14 +45,14 @@ public class LauncherPreferences { public static boolean PREF_USE_ALTERNATE_SURFACE = true; public static boolean PREF_JAVA_SANDBOX = true; public static float PREF_SCALE_FACTOR = 1f; + public static boolean PREF_ENABLE_GYRO = false; public static float PREF_GYRO_SENSITIVITY = 1f; public static int PREF_GYRO_SAMPLE_RATE = 16; public static boolean PREF_GYRO_SMOOTHING = true; - public static boolean PREF_GYRO_INVERT_X = false; - public static boolean PREF_GYRO_INVERT_Y = false; + public static boolean PREF_FORCE_VSYNC = false; public static boolean PREF_BUTTON_ALL_CAPS = true; @@ -79,17 +70,13 @@ public class LauncherPreferences { public static void loadPreferences(Context ctx) { //Required for the data folder. Tools.initContextConstants(ctx); + boolean isDevicePowerful = isDevicePowerful(ctx); PREF_RENDERER = DEFAULT_PREF.getString("renderer", "opengles2"); PREF_BUTTONSIZE = DEFAULT_PREF.getInt("buttonscale", 100); PREF_MOUSESCALE = DEFAULT_PREF.getInt("mousescale", 100)/100f; - PREF_MOUSESPEED = ((float)DEFAULT_PREF.getInt("mousespeed",100))/100f; - PREF_HIDE_SIDEBAR = DEFAULT_PREF.getBoolean("hideSidebar", false); - PREF_IGNORE_NOTCH = DEFAULT_PREF.getBoolean("ignoreNotch", false); - PREF_VERTYPE_RELEASE = DEFAULT_PREF.getBoolean("vertype_release", true); - PREF_VERTYPE_SNAPSHOT = DEFAULT_PREF.getBoolean("vertype_snapshot", false); - PREF_VERTYPE_OLDALPHA = DEFAULT_PREF.getBoolean("vertype_oldalpha", false); - PREF_VERTYPE_OLDBETA = DEFAULT_PREF.getBoolean("vertype_oldbeta", false); + PREF_MOUSESPEED = ((float)DEFAULT_PREF.getInt("mousespeed",100))/100f; + PREF_IGNORE_NOTCH = DEFAULT_PREF.getBoolean("ignoreNotch", false); PREF_LONGPRESS_TRIGGER = DEFAULT_PREF.getInt("timeLongPressTrigger", 300); PREF_DEFAULTCTRL_PATH = DEFAULT_PREF.getString("defaultCtrl", Tools.CTRLDEF_FILE); PREF_FORCE_ENGLISH = DEFAULT_PREF.getBoolean("force_english", false); @@ -98,19 +85,19 @@ public static void loadPreferences(Context ctx) { PREF_DISABLE_SWAP_HAND = DEFAULT_PREF.getBoolean("disableDoubleTap", false); PREF_RAM_ALLOCATION = DEFAULT_PREF.getInt("allocation", findBestRAMAllocation(ctx)); PREF_CUSTOM_JAVA_ARGS = DEFAULT_PREF.getString("javaArgs", ""); - PREF_SUSTAINED_PERFORMANCE = DEFAULT_PREF.getBoolean("sustainedPerformance", false); + PREF_SUSTAINED_PERFORMANCE = DEFAULT_PREF.getBoolean("sustainedPerformance", isDevicePowerful); PREF_VIRTUAL_MOUSE_START = DEFAULT_PREF.getBoolean("mouse_start", false); PREF_ARC_CAPES = DEFAULT_PREF.getBoolean("arc_capes",false); - PREF_USE_ALTERNATE_SURFACE = DEFAULT_PREF.getBoolean("alternate_surface", false); + PREF_USE_ALTERNATE_SURFACE = DEFAULT_PREF.getBoolean("alternate_surface", isDevicePowerful); PREF_JAVA_SANDBOX = DEFAULT_PREF.getBoolean("java_sandbox", true); - PREF_SCALE_FACTOR = DEFAULT_PREF.getInt("resolutionRatio", 100)/100f; + PREF_SCALE_FACTOR = DEFAULT_PREF.getInt("resolutionRatio", findBestResolution(ctx, isDevicePowerful))/100f; PREF_ENABLE_GYRO = DEFAULT_PREF.getBoolean("enableGyro", false); PREF_GYRO_SENSITIVITY = ((float)DEFAULT_PREF.getInt("gyroSensitivity", 100))/100f; PREF_GYRO_SAMPLE_RATE = DEFAULT_PREF.getInt("gyroSampleRate", 16); PREF_GYRO_SMOOTHING = DEFAULT_PREF.getBoolean("gyroSmoothing", true); PREF_GYRO_INVERT_X = DEFAULT_PREF.getBoolean("gyroInvertX", false); PREF_GYRO_INVERT_Y = DEFAULT_PREF.getBoolean("gyroInvertY", false); - PREF_FORCE_VSYNC = DEFAULT_PREF.getBoolean("force_vsync", false); + PREF_FORCE_VSYNC = DEFAULT_PREF.getBoolean("force_vsync", isDevicePowerful); PREF_BUTTON_ALL_CAPS = DEFAULT_PREF.getBoolean("buttonAllCaps", true); PREF_DUMP_SHADERS = DEFAULT_PREF.getBoolean("dump_shaders", false); PREF_DEADZONE_SCALE = ((float) DEFAULT_PREF.getInt("gamepad_deadzone_scale", 100))/100f; @@ -132,7 +119,7 @@ public static void loadPreferences(Context ctx) { if(DEFAULT_PREF.contains("defaultRuntime")) { PREF_DEFAULT_RUNTIME = DEFAULT_PREF.getString("defaultRuntime",""); }else{ - if(MultiRTUtils.getRuntimes().size() < 1) { + if(MultiRTUtils.getRuntimes().isEmpty()) { PREF_DEFAULT_RUNTIME = ""; return; } @@ -164,6 +151,33 @@ private static int findBestRAMAllocation(Context ctx){ return 2048; //Default RAM allocation for 64 bits } + /// Find a correct resolution for the device + /// + /// Some devices are shipped with ridiculously high resolution, which can cause performance issues + /// This function will try to find a resolution that is good enough for the device + private static int findBestResolution(Context context, boolean isDevicePowerful) { + DisplayMetrics metrics = context.getResources().getDisplayMetrics(); + int minSide = Math.min(metrics.widthPixels, metrics.heightPixels); + int targetSide = isDevicePowerful ? 1080 : 720; + if (minSide <= targetSide) return 100; // No need to scale down + + float ratio = (100f * targetSide / minSide); + // The value must match the seekbar values + int increment = context.getResources().getInteger(R.integer.resolution_seekbar_increment); + return (int) (Math.ceil(ratio / increment) * increment); + } + + /// Check if the device is considered powerful. + /// Powerful devices will have some energy saving tweaks enabled by default + private static boolean isDevicePowerful(Context context) { + if (SDK_INT < Build.VERSION_CODES.Q) return false; + if (Tools.getTotalDeviceMemory(context) <= 4096) return false; + DisplayMetrics metrics = context.getResources().getDisplayMetrics(); + if (Math.min(metrics.widthPixels, metrics.heightPixels) < 1080) return false; + if (Runtime.getRuntime().availableProcessors() <= 4) return false; + return true; + } + /** Compute the notch size to avoid being out of bounds */ public static void computeNotchSize(Activity activity) { if (Build.VERSION.SDK_INT < P) return; diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/prefs/QuickSettingSideDialog.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/prefs/QuickSettingSideDialog.java index 47ebb7a809..2da6d8047b 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/prefs/QuickSettingSideDialog.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/prefs/QuickSettingSideDialog.java @@ -123,7 +123,7 @@ private void setupListeners() { }); mGyroSensitivityBar.setRange(25, 300); - mGyroSensitivityBar.setIncrement(5); + mGyroSensitivityBar.setIncrement(mGyroSensitivityBar.getContext().getResources().getInteger(R.integer.gyro_speed_seekbar_increment)); mGyroSensitivityBar.setOnSeekBarChangeListener((SimpleSeekBarListener) (seekBar, progress, fromUser) -> { PREF_GYRO_SENSITIVITY = progress / 100f; mEditor.putInt("gyroSensitivity", progress); @@ -133,7 +133,7 @@ private void setupListeners() { setSeekTextPercent(mGyroSensitivityText, mGyroSensitivityBar.getProgress()); mMouseSpeedBar.setRange(25, 300); - mMouseSpeedBar.setIncrement(5); + mMouseSpeedBar.setIncrement(mMouseSpeedBar.getContext().getResources().getInteger(R.integer.mouse_speed_seekbar_increment)); mMouseSpeedBar.setOnSeekBarChangeListener((SimpleSeekBarListener) (seekBar, progress, fromUser) -> { PREF_MOUSESPEED = progress / 100f; mEditor.putInt("mousespeed", progress); @@ -143,7 +143,7 @@ private void setupListeners() { setSeekTextPercent(mMouseSpeedText, mMouseSpeedBar.getProgress()); mGestureDelayBar.setRange(100, 1000); - mGestureDelayBar.setIncrement(10); + mGestureDelayBar.setIncrement(mGestureDelayBar.getContext().getResources().getInteger(R.integer.gesture_delay_seekbar_increment)); mGestureDelayBar.setOnSeekBarChangeListener((SimpleSeekBarListener) (seekBar, progress, fromUser) -> { PREF_LONGPRESS_TRIGGER = progress; mEditor.putInt("timeLongPressTrigger", progress); @@ -153,7 +153,7 @@ private void setupListeners() { setSeekTextMillisecond(mGestureDelayText, mGestureDelayBar.getProgress()); mResolutionBar.setRange(25, 100); - mResolutionBar.setIncrement(5); + mResolutionBar.setIncrement(mResolutionBar.getContext().getResources().getInteger(R.integer.resolution_seekbar_increment)); mResolutionBar.setOnSeekBarChangeListener((SimpleSeekBarListener) (seekBar, progress, fromUser) -> { PREF_SCALE_FACTOR = progress/100f; mEditor.putInt("resolutionRatio", progress); diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/prefs/screens/LauncherPreferenceVideoFragment.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/prefs/screens/LauncherPreferenceVideoFragment.java index 6876f2aa45..4b2bc5279b 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/prefs/screens/LauncherPreferenceVideoFragment.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/prefs/screens/LauncherPreferenceVideoFragment.java @@ -1,7 +1,5 @@ package net.kdt.pojavlaunch.prefs.screens; -import static net.kdt.pojavlaunch.prefs.LauncherPreferences.PREF_NOTCH_SIZE; - import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle; @@ -22,24 +20,31 @@ public class LauncherPreferenceVideoFragment extends LauncherPreferenceFragment @Override public void onCreatePreferences(Bundle b, String str) { addPreferencesFromResource(R.xml.pref_video); + int resolution = (int) (LauncherPreferences.PREF_SCALE_FACTOR * 100); //Disable notch checking behavior on android 8.1 and below. - requirePreference("ignoreNotch").setVisible(Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && PREF_NOTCH_SIZE > 0); + requirePreference("ignoreNotch").setVisible(Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && LauncherPreferences.PREF_NOTCH_SIZE > 0); - CustomSeekBarPreference seek5 = requirePreference("resolutionRatio", + CustomSeekBarPreference resolutionSeekbar = requirePreference("resolutionRatio", CustomSeekBarPreference.class); - seek5.setMin(25); - seek5.setSuffix(" %"); + resolutionSeekbar.setMin(25); + resolutionSeekbar.setSuffix(" %"); // #724 bug fix - if (seek5.getValue() < 25) { - seek5.setValue(100); + if (resolution < 25) { + resolutionSeekbar.setValue(100); + } else { + resolutionSeekbar.setValue(resolution); } // Sustained performance is only available since Nougat SwitchPreference sustainedPerfSwitch = requirePreference("sustainedPerformance", SwitchPreference.class); sustainedPerfSwitch.setVisible(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N); + sustainedPerfSwitch.setChecked(LauncherPreferences.PREF_SUSTAINED_PERFORMANCE); + + requirePreference("alternate_surface", SwitchPreferenceCompat.class).setChecked(LauncherPreferences.PREF_USE_ALTERNATE_SURFACE); + requirePreference("force_vsync", SwitchPreferenceCompat.class).setChecked(LauncherPreferences.PREF_FORCE_VSYNC); ListPreference rendererListPreference = requirePreference("renderer", ListPreference.class); diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/utils/LocaleUtils.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/utils/LocaleUtils.java index 82ea22cddb..fb7b53d44d 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/utils/LocaleUtils.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/utils/LocaleUtils.java @@ -2,6 +2,7 @@ import static net.kdt.pojavlaunch.prefs.LauncherPreferences.DEFAULT_PREF; +import static net.kdt.pojavlaunch.prefs.LauncherPreferences.PREF_FORCE_ENGLISH; import android.content.*; import android.content.res.*; @@ -24,7 +25,7 @@ public static ContextWrapper setLocale(Context context) { LauncherPreferences.loadPreferences(context); } - if(DEFAULT_PREF.getBoolean("force_english", false)){ + if(PREF_FORCE_ENGLISH){ Resources resources = context.getResources(); Configuration configuration = resources.getConfiguration(); diff --git a/app_pojavlauncher/src/main/res/values/values.xml b/app_pojavlauncher/src/main/res/values/values.xml new file mode 100644 index 0000000000..af6989ff31 --- /dev/null +++ b/app_pojavlauncher/src/main/res/values/values.xml @@ -0,0 +1,11 @@ + + + 5 + 10 + 5 + 5 + 5 + 5 + 5 + 8 + \ No newline at end of file diff --git a/app_pojavlauncher/src/main/res/xml/pref_control.xml b/app_pojavlauncher/src/main/res/xml/pref_control.xml index f6300e7c32..e892adcfad 100644 --- a/app_pojavlauncher/src/main/res/xml/pref_control.xml +++ b/app_pojavlauncher/src/main/res/xml/pref_control.xml @@ -35,7 +35,7 @@ android:title="@string/mcl_setting_title_longpresstrigger" app2:showSeekBarValue="true" app2:selectable="false" - app2:seekBarIncrement="10" + app2:seekBarIncrement="@integer/gesture_delay_seekbar_increment" android:icon="@drawable/ic_setting_gesture_time" /> @@ -50,7 +50,7 @@ android:summary="@string/mcl_setting_subtitle_buttonscale" app2:showSeekBarValue="true" app2:selectable="false" - app2:seekBarIncrement="5" + app2:seekBarIncrement="@integer/button_scale_seekbar_increment" android:icon="@drawable/ic_setting_control_scale" /> @@ -82,7 +82,7 @@ android:title="@string/mcl_setting_title_mousespeed" android:icon="@drawable/ic_setting_mouse_speed" app2:selectable="false" - app2:seekBarIncrement="5" + app2:seekBarIncrement="@integer/mouse_speed_seekbar_increment" app2:showSeekBarValue="true" /> diff --git a/app_pojavlauncher/src/main/res/xml/pref_java.xml b/app_pojavlauncher/src/main/res/xml/pref_java.xml index 46616ae4d8..239836cd7d 100644 --- a/app_pojavlauncher/src/main/res/xml/pref_java.xml +++ b/app_pojavlauncher/src/main/res/xml/pref_java.xml @@ -25,7 +25,7 @@ android:summary="@string/mcl_memory_allocation_subtitle" android:title="@string/mcl_memory_allocation" app2:showSeekBarValue="true" - app2:seekBarIncrement="8" + app2:seekBarIncrement="@integer/memory_seekbar_increment" app2:selectable="false"/>