Skip to content

Commit

Permalink
refactor(seekbars):migrate range values into xml files
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathias-Boulay committed Dec 25, 2024
1 parent 736ee68 commit 9840632
Show file tree
Hide file tree
Showing 13 changed files with 172 additions and 93 deletions.
125 changes: 71 additions & 54 deletions app_pojavlauncher/src/main/java/com/kdt/CustomSeekbar.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.SeekBar;

Expand All @@ -19,8 +20,53 @@ public class CustomSeekbar extends SeekBar {
private int mIncrement = 1;
private SeekBar.OnSeekBarChangeListener mListener;

/** When using increments, this flag is used to prevent double calls to the listener */
private boolean mInternalChanges = false;
private final OnSeekBarChangeListener mInternalListener = new OnSeekBarChangeListener() {
/** When using increments, this flag is used to prevent double calls to the listener */
private boolean internalChanges = false;
/** Store the previous progress to prevent double calls with increments */
private int previousProgress = 0;
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (internalChanges) return;
internalChanges = true;

progress += mMin;
progress = applyIncrement(progress);

if (progress != previousProgress) {
if (mListener != null) {
previousProgress = progress;
mListener.onProgressChanged(seekBar, progress, fromUser);
}
}

// Forces the thumb to snap to the increment
setProgress(progress);
internalChanges = false;
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
if (internalChanges) return;

if (mListener != null) {
mListener.onStartTrackingTouch(seekBar);
}
}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
if (internalChanges) return;
internalChanges = true;

setProgress(seekBar.getProgress());

if (mListener != null) {
mListener.onStopTrackingTouch(seekBar);
}
internalChanges = false;
}
};

public CustomSeekbar(Context context) {
super(context);
Expand All @@ -37,11 +83,6 @@ public CustomSeekbar(Context context, AttributeSet attrs, int defStyleAttr) {
setup(attrs);
}

public CustomSeekbar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
setup(attrs);
}

public void setIncrement(int increment) {
mIncrement = increment;
}
Expand All @@ -68,10 +109,15 @@ public synchronized int getProgress() {

@Override
public synchronized void setMin(int min) {
super.setMin(min);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
super.setMin(0);
}
mMin = min;
//todo perform something to update the progress ?
}



/**
* Wrapper to allow for a listener to be set around the internal listener
*/
Expand All @@ -82,54 +128,25 @@ public void setOnSeekBarChangeListener(OnSeekBarChangeListener l) {

public void setup(@Nullable AttributeSet attrs) {
try (TypedArray attributes = getContext().obtainStyledAttributes(attrs, R.styleable.CustomSeekbar)) {
mIncrement = attributes.getInt(R.styleable.CustomSeekbar_seekBarIncrement, 1);
}

super.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
/** Store the previous progress to prevent double calls with increments */
private int previousProgress = 0;
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (mInternalChanges) return;
mInternalChanges = true;

progress += mMin;
progress = applyIncrement(progress);

if (progress != previousProgress) {
if (mListener != null) {
previousProgress = progress;
mListener.onProgressChanged(seekBar, progress, fromUser);
}
}

// Forces the thumb to snap to the increment
setProgress(progress);
mInternalChanges = false;
setIncrement(attributes.getInt(R.styleable.CustomSeekbar_seekBarIncrement, 1));
int min = attributes.getInt(R.styleable.CustomSeekbar_android_min, 0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
super.setMin(0);
}
setRange(min, super.getMax());
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
if (mInternalChanges) return;

if (mListener != null) {
mListener.onStartTrackingTouch(seekBar);
}
}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
if (mInternalChanges) return;
mInternalChanges = true;

setProgress(seekBar.getProgress());

if (mListener != null) {
mListener.onStopTrackingTouch(seekBar);
}
mInternalChanges = false;
}
});
// Due to issues with negative progress when setting up the seekbar
// We need to set a random progress to force the refresh of the thumb
if(super.getProgress() == 0) {
super.setProgress(super.getProgress() + 1);
post(() -> {
super.setProgress(super.getProgress() - 1);
post(() -> super.setOnSeekBarChangeListener(mInternalListener));
});
} else {
super.setOnSeekBarChangeListener(mInternalListener);
}
}

/**
Expand Down
39 changes: 32 additions & 7 deletions app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/Tools.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Process;
import android.provider.DocumentsContract;
import android.provider.OpenableColumns;
import android.util.ArrayMap;
Expand All @@ -40,7 +41,6 @@

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationManagerCompat;
Expand Down Expand Up @@ -538,7 +538,7 @@ private static void setFullscreenLegacy(Activity activity, boolean fullscreen) {
visibilityChangeListener.onSystemUiVisibilityChange(decorView.getSystemUiVisibility()); //call it once since the UI state may not change after the call, so the activity wont become fullscreen
}

@RequiresApi(Build.VERSION_CODES.R)

private static void setFullscreenSdk30(Activity activity, boolean fullscreen) {
WindowInsetsControllerCompat windowInsetsController =
WindowCompat.getInsetsController(activity.getWindow(), activity.getWindow().getDecorView());
Expand All @@ -555,25 +555,31 @@ private static void setFullscreenSdk30(Activity activity, boolean fullscreen) {
ViewCompat.setOnApplyWindowInsetsListener(
activity.getWindow().getDecorView(),
(view, windowInsets) -> {
if (fullscreen && !activity.isInMultiWindowMode()) {
boolean fullscreenImpl = fullscreen;
if (SDK_INT >= Build.VERSION_CODES.N && activity.isInMultiWindowMode())
fullscreenImpl = false;

if (fullscreenImpl) {
windowInsetsController.hide(WindowInsetsCompat.Type.systemBars());
activity.getWindow().setDecorFitsSystemWindows(false);
} else {
windowInsetsController.show(WindowInsetsCompat.Type.systemBars());
activity.getWindow().setDecorFitsSystemWindows(true);
}

if(SDK_INT >= Build.VERSION_CODES.R)
activity.getWindow().setDecorFitsSystemWindows(!fullscreenImpl);

return ViewCompat.onApplyWindowInsets(view, windowInsets);
});

}

public static void setFullscreen(Activity activity, boolean fullscreen) {
setFullscreenSdk30(activity, fullscreen);
/*
if (SDK_INT >= Build.VERSION_CODES.R) {
setFullscreenSdk30(activity, fullscreen);
}else {
setFullscreenLegacy(activity, fullscreen);
}
}*/
}

public static DisplayMetrics currentDisplayMetrics;
Expand Down Expand Up @@ -1343,4 +1349,23 @@ public static void dialogForceClose(Context ctx) {
}
}).show();
}

public static void setThreadsPriority(int priority) {
Process.getThreadPriority(Process.myTid());



Map<Thread, StackTraceElement[]> threads = Thread.getAllStackTraces();
for (Thread thread : threads.keySet()) {
//Log.d("Tools, thread: ", thread.getName());
Log.d("Tools, thread: ", thread + " group: " + thread.getThreadGroup());
Log.d("Tools, thread: ", Arrays.toString(thread.getStackTrace()));
Log.d("Tools, thread: ", String.valueOf(thread.getState()));
try {
thread.setPriority(priority);
}catch (Exception e) {
Log.e("Tools: thread", "Failed to set priority", e);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ public class CustomSeekBarPreference extends SeekBarPreference {
@SuppressLint("PrivateResource")
public CustomSeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
TypedArray a = context.obtainStyledAttributes(
attrs, R.styleable.SeekBarPreference, defStyleAttr, defStyleRes);
mMin = a.getInt(R.styleable.SeekBarPreference_min, 0);
a.recycle();
try (TypedArray a = context.obtainStyledAttributes(
attrs, R.styleable.SeekBarPreference, defStyleAttr, defStyleRes)) {
mMin = a.getInt(R.styleable.SeekBarPreference_min, 0);
}
}

public CustomSeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ private void setupListeners() {
mEditor.putBoolean("disableGestures", isChecked);
});

mGyroSensitivityBar.setRange(25, 300);
mGyroSensitivityBar.setOnSeekBarChangeListener((SimpleSeekBarListener) (seekBar, progress, fromUser) -> {
PREF_GYRO_SENSITIVITY = progress / 100f;
mEditor.putInt("gyroSensitivity", progress);
Expand All @@ -131,7 +130,6 @@ private void setupListeners() {
mGyroSensitivityBar.setProgress((int) (mOriginalGyroSensitivity * 100f));
setSeekTextPercent(mGyroSensitivityText, mGyroSensitivityBar.getProgress());

mMouseSpeedBar.setRange(25, 300);
mMouseSpeedBar.setOnSeekBarChangeListener((SimpleSeekBarListener) (seekBar, progress, fromUser) -> {
PREF_MOUSESPEED = progress / 100f;
mEditor.putInt("mousespeed", progress);
Expand All @@ -140,7 +138,6 @@ private void setupListeners() {
mMouseSpeedBar.setProgress((int) (mOriginalMouseSpeed * 100f));
setSeekTextPercent(mMouseSpeedText, mMouseSpeedBar.getProgress());

mGestureDelayBar.setRange(100, 1000);
mGestureDelayBar.setOnSeekBarChangeListener((SimpleSeekBarListener) (seekBar, progress, fromUser) -> {
PREF_LONGPRESS_TRIGGER = progress;
mEditor.putInt("timeLongPressTrigger", progress);
Expand All @@ -149,7 +146,6 @@ private void setupListeners() {
mGestureDelayBar.setProgress(mOriginalGestureDelay);
setSeekTextMillisecond(mGestureDelayText, mGestureDelayBar.getProgress());

mResolutionBar.setRange(25, 100);
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
Expand Up @@ -32,31 +32,26 @@ public void onCreatePreferences(Bundle b, String str) {

CustomSeekBarPreference seek2 = requirePreference("timeLongPressTrigger",
CustomSeekBarPreference.class);
seek2.setRange(100, 1000);
seek2.setValue(longPressTrigger);
seek2.setSuffix(" ms");

CustomSeekBarPreference seek3 = requirePreference("buttonscale",
CustomSeekBarPreference.class);
seek3.setRange(80, 250);
seek3.setValue(prefButtonSize);
seek3.setSuffix(" %");

CustomSeekBarPreference seek4 = requirePreference("mousescale",
CustomSeekBarPreference.class);
seek4.setRange(25, 300);
seek4.setValue(mouseScale);
seek4.setSuffix(" %");

CustomSeekBarPreference seek6 = requirePreference("mousespeed",
CustomSeekBarPreference.class);
seek6.setRange(25, 300);
seek6.setValue((int)(mouseSpeed *100f));
seek6.setSuffix(" %");

CustomSeekBarPreference deadzoneSeek = requirePreference("gamepad_deadzone_scale",
CustomSeekBarPreference.class);
deadzoneSeek.setRange(50, 200);
deadzoneSeek.setValue((int) (joystickDeadzone * 100f));
deadzoneSeek.setSuffix(" %");

Expand All @@ -71,13 +66,11 @@ public void onCreatePreferences(Bundle b, String str) {

CustomSeekBarPreference gyroSensitivitySeek = requirePreference("gyroSensitivity",
CustomSeekBarPreference.class);
gyroSensitivitySeek.setRange(25, 300);
gyroSensitivitySeek.setValue((int) (gyroSpeed*100f));
gyroSensitivitySeek.setSuffix(" %");

CustomSeekBarPreference gyroSampleRateSeek = requirePreference("gyroSampleRate",
CustomSeekBarPreference.class);
gyroSampleRateSeek.setRange(5, 50);
gyroSampleRateSeek.setValue(gyroSampleRate);
gyroSampleRateSeek.setSuffix(" ms");
computeVisibility();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,18 @@ public void onCreatePreferences(Bundle b, String str) {
// Triggers a write for some reason
addPreferencesFromResource(R.xml.pref_java);

CustomSeekBarPreference seek7 = requirePreference("allocation",
CustomSeekBarPreference memorySeekbar = requirePreference("allocation",
CustomSeekBarPreference.class);

int maxRAM;
int deviceRam = getTotalDeviceMemory(seek7.getContext());
int deviceRam = getTotalDeviceMemory(memorySeekbar.getContext());

if(is32BitsDevice() || deviceRam < 2048) maxRAM = Math.min(1024, deviceRam);
else maxRAM = deviceRam - (deviceRam < 3064 ? 800 : 1024); //To have a minimum for the device to breathe

seek7.setMin(256);
seek7.setMax(maxRAM);
seek7.setValue(ramAllocation);
seek7.setSuffix(" MB");
memorySeekbar.setMax(maxRAM);
memorySeekbar.setValue(ramAllocation);
memorySeekbar.setSuffix(" MB");

EditTextPreference editJVMArgs = findPreference("javaArgs");
if (editJVMArgs != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public void onCreatePreferences(Bundle b, String str) {

CustomSeekBarPreference resolutionSeekbar = requirePreference("resolutionRatio",
CustomSeekBarPreference.class);
resolutionSeekbar.setMin(25);
resolutionSeekbar.setSuffix(" %");

// #724 bug fix
Expand Down
Loading

0 comments on commit 9840632

Please sign in to comment.