-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathLauncherPreferences.java
221 lines (196 loc) · 11.2 KB
/
LauncherPreferences.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package net.kdt.pojavlaunch.prefs;
import static android.os.Build.VERSION.SDK_INT;
import static android.os.Build.VERSION_CODES.P;
import static net.kdt.pojavlaunch.Architecture.is32BitsDevice;
import android.app.Activity;
import android.content.*;
import android.content.res.Configuration;
import android.graphics.Rect;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.Log;
import net.kdt.pojavlaunch.*;
import net.kdt.pojavlaunch.multirt.MultiRTUtils;
import net.kdt.pojavlaunch.utils.JREUtils;
import java.io.IOException;
public class LauncherPreferences {
public static final String PREF_KEY_CURRENT_PROFILE = "currentProfile";
public static final String PREF_KEY_SKIP_NOTIFICATION_CHECK = "skipNotificationPermissionCheck";
public static SharedPreferences DEFAULT_PREF;
public static String PREF_RENDERER = "opengles2";
public static boolean PREF_IGNORE_NOTCH = false;
public static int PREF_NOTCH_SIZE = 0;
public static float PREF_BUTTONSIZE = 100f;
public static float PREF_MOUSESCALE = 1f;
public static int PREF_LONGPRESS_TRIGGER = 300;
public static String PREF_DEFAULTCTRL_PATH = Tools.CTRLDEF_FILE;
public static String PREF_CUSTOM_JAVA_ARGS;
public static boolean PREF_FORCE_ENGLISH = false;
public static final String PREF_VERSION_REPOS = "https://piston-meta.mojang.com/mc/game/version_manifest_v2.json";
public static boolean PREF_CHECK_LIBRARY_SHA = true;
public static boolean PREF_DISABLE_GESTURES = false;
public static boolean PREF_DISABLE_SWAP_HAND = false;
public static float PREF_MOUSESPEED = 1f;
public static int PREF_RAM_ALLOCATION;
public static String PREF_DEFAULT_RUNTIME;
public static boolean PREF_SUSTAINED_PERFORMANCE = false;
public static boolean PREF_VIRTUAL_MOUSE_START = false;
public static boolean PREF_ARC_CAPES = false;
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;
public static boolean PREF_DUMP_SHADERS = false;
public static float PREF_DEADZONE_SCALE = 1f;
public static boolean PREF_BIG_CORE_AFFINITY = false;
public static boolean PREF_ZINK_PREFER_SYSTEM_DRIVER = false;
public static boolean PREF_VERIFY_MANIFEST = true;
public static String PREF_DOWNLOAD_SOURCE = "default";
public static boolean PREF_SKIP_NOTIFICATION_PERMISSION_CHECK = false;
public static boolean PREF_VSYNC_IN_ZINK = true;
public static boolean PREF_ROLLBACK_OFFICIAL_IF_FAIL = false;
public static void loadPreferences(Context ctx) {
//Required for CTRLDEF_FILE and MultiRT
Tools.initStorageConstants(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_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);
PREF_CHECK_LIBRARY_SHA = DEFAULT_PREF.getBoolean("checkLibraries",true);
PREF_DISABLE_GESTURES = DEFAULT_PREF.getBoolean("disableGestures",false);
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", 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", isDevicePowerful);
PREF_JAVA_SANDBOX = DEFAULT_PREF.getBoolean("java_sandbox", true);
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", 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;
PREF_BIG_CORE_AFFINITY = DEFAULT_PREF.getBoolean("bigCoreAffinity", false);
PREF_ZINK_PREFER_SYSTEM_DRIVER = DEFAULT_PREF.getBoolean("zinkPreferSystemDriver", false);
PREF_DOWNLOAD_SOURCE = DEFAULT_PREF.getString("downloadSource", "default");
PREF_VERIFY_MANIFEST = DEFAULT_PREF.getBoolean("verifyManifest", true);
PREF_SKIP_NOTIFICATION_PERMISSION_CHECK = DEFAULT_PREF.getBoolean(PREF_KEY_SKIP_NOTIFICATION_CHECK, false);
PREF_VSYNC_IN_ZINK = DEFAULT_PREF.getBoolean("vsync_in_zink", true);
PREF_ROLLBACK_OFFICIAL_IF_FAIL = DEFAULT_PREF.getBoolean("downloadRollback", false);
String argLwjglLibname = "-Dorg.lwjgl.opengl.libname=";
for (String arg : JREUtils.parseJavaArguments(PREF_CUSTOM_JAVA_ARGS)) {
if (arg.startsWith(argLwjglLibname)) {
// purge arg
DEFAULT_PREF.edit().putString("javaArgs",
PREF_CUSTOM_JAVA_ARGS.replace(arg, "")).apply();
}
}
if(DEFAULT_PREF.contains("defaultRuntime")) {
PREF_DEFAULT_RUNTIME = DEFAULT_PREF.getString("defaultRuntime","");
}else{
if(MultiRTUtils.getRuntimes().isEmpty()) {
PREF_DEFAULT_RUNTIME = "";
return;
}
PREF_DEFAULT_RUNTIME = MultiRTUtils.getRuntimes().get(0).name;
LauncherPreferences.DEFAULT_PREF.edit().putString("defaultRuntime",LauncherPreferences.PREF_DEFAULT_RUNTIME).apply();
}
}
/**
* This functions aims at finding the best default RAM amount,
* according to the RAM amount of the physical device.
* Put not enough RAM ? Minecraft will lag and crash.
* Put too much RAM ?
* The GC will lag, android won't be able to breathe properly.
* @param ctx Context needed to get the total memory of the device.
* @return The best default value found.
*/
private static int findBestRAMAllocation(Context ctx){
int deviceRam = Tools.getTotalDeviceMemory(ctx);
if (deviceRam < 1024) return 296;
if (deviceRam < 1536) return 448;
if (deviceRam < 2048) return 656;
// Limit the max for 32 bits devices more harshly
if (is32BitsDevice()) return 696;
if (deviceRam < 3064) return 936;
if (deviceRam < 4096) return 1144;
if (deviceRam < 6144) return 1536;
return 2048; //Default RAM allocation for 64 bits
}
/// Find a correct resolution for the device
///
/// Some devices are shipped with a 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;
if (hasAllCoreSameFreq()) return false;
return true;
}
private static boolean hasAllCoreSameFreq() {
int coreCount = Runtime.getRuntime().availableProcessors();
try {
String freq0 = Tools.read("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq");
String freqX = Tools.read("/sys/devices/system/cpu/cpu" + (coreCount - 1) + "/cpufreq/cpuinfo_max_freq");
if(freq0.equals(freqX)) return true;
} catch (IOException e) {
Log.e("LauncherPreferences", "Failed to read CPU frequencies", e);
}
return false;
}
/** Compute the notch size to avoid being out of bounds */
public static void computeNotchSize(Activity activity) {
if (Build.VERSION.SDK_INT < P) return;
try {
final Rect cutout;
if(SDK_INT >= Build.VERSION_CODES.S){
cutout = activity.getWindowManager().getCurrentWindowMetrics().getWindowInsets().getDisplayCutout().getBoundingRects().get(0);
} else {
cutout = activity.getWindow().getDecorView().getRootWindowInsets().getDisplayCutout().getBoundingRects().get(0);
}
// Notch values are rotation sensitive, handle all cases
int orientation = activity.getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_PORTRAIT) LauncherPreferences.PREF_NOTCH_SIZE = cutout.height();
else if (orientation == Configuration.ORIENTATION_LANDSCAPE) LauncherPreferences.PREF_NOTCH_SIZE = cutout.width();
else LauncherPreferences.PREF_NOTCH_SIZE = Math.min(cutout.width(), cutout.height());
}catch (Exception e){
Log.i("NOTCH DETECTION", "No notch detected, or the device if in split screen mode");
LauncherPreferences.PREF_NOTCH_SIZE = -1;
}
Tools.updateWindowSize(activity);
}
}