Skip to content

Commit

Permalink
Qol(settings): use energy saving settings for powerful devices
Browse files Browse the repository at this point in the history
Less boiling pocket heaters.
May contain a small few refactor
  • Loading branch information
Mathias-Boulay committed Dec 16, 2024
1 parent 011139d commit f205209
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)));

}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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();

Expand Down
11 changes: 11 additions & 0 deletions app_pojavlauncher/src/main/res/values/values.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="resolution_seekbar_increment">5</integer>
<integer name="gesture_delay_seekbar_increment">10</integer>
<integer name="button_scale_seekbar_increment">5</integer>
<integer name="mouse_scale_seekbar_increment">5</integer>
<integer name="mouse_speed_seekbar_increment">5</integer>
<integer name="gyro_speed_seekbar_increment">5</integer>
<integer name="gamepad_deadzone_seekbar_increment">5</integer>
<integer name="memory_seekbar_increment">8</integer>
</resources>
12 changes: 6 additions & 6 deletions app_pojavlauncher/src/main/res/xml/pref_control.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
/>

Expand All @@ -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"
/>
<SwitchPreference
Expand All @@ -71,7 +71,7 @@
android:title="@string/mcl_setting_title_mousescale"

app2:selectable="false"
app2:seekBarIncrement="5"
app2:seekBarIncrement="@integer/mouse_scale_seekbar_increment"
app2:showSeekBarValue="true"
android:icon="@drawable/ic_setting_pointer_scale"
/>
Expand All @@ -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" />
<SwitchPreference
android:key="mouse_start"
Expand All @@ -104,7 +104,7 @@
android:title="@string/preference_gyro_sensitivity_title"
android:summary="@string/preference_gyro_sensitivity_description"
app2:selectable="false"
app2:seekBarIncrement="5"
app2:seekBarIncrement="@integer/gyro_speed_seekbar_increment"
app2:showSeekBarValue="true"/>
<net.kdt.pojavlaunch.prefs.CustomSeekBarPreference
android:key="gyroSampleRate"
Expand Down Expand Up @@ -145,7 +145,7 @@
android:title="@string/preference_deadzone_scale_title"
android:summary="@string/preference_deadzone_scale_description"
app2:showSeekBarValue="true"
app2:seekBarIncrement="5"
app2:seekBarIncrement="@integer/gamepad_deadzone_seekbar_increment"
/>

</PreferenceCategory>
Expand Down
2 changes: 1 addition & 1 deletion app_pojavlauncher/src/main/res/xml/pref_java.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"/>

<SwitchPreference
Expand Down
2 changes: 1 addition & 1 deletion app_pojavlauncher/src/main/res/xml/pref_video.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
android:title="@string/mcl_setting_title_resolution_scaler"
app2:showSeekBarValue="true"
app2:selectable="false"
app2:seekBarIncrement="5"
app2:seekBarIncrement="@integer/resolution_seekbar_increment"
android:icon="@drawable/ic_setting_screen_resolution"
/>

Expand Down

0 comments on commit f205209

Please sign in to comment.