Skip to content

Commit

Permalink
refactor: Use string arrays for drop-down menus to support locales
Browse files Browse the repository at this point in the history
  • Loading branch information
Xtr126 committed Aug 1, 2024
1 parent cbaa13c commit a4a4293
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 40 deletions.
52 changes: 38 additions & 14 deletions app/src/main/java/xtr/keymapper/fragment/SettingsFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.google.android.material.textfield.MaterialAutoCompleteTextView;

import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import xtr.keymapper.keymap.KeymapConfig;
import xtr.keymapper.R;
import xtr.keymapper.Utils;
Expand All @@ -27,6 +31,9 @@
public class SettingsFragment extends BottomSheetDialogFragment {
private final KeymapConfig keymapConfig;
private FragmentSettingsDialogBinding binding;
private Map<String, Integer> mouseAimActionsMap;
private Map<String, Integer> pointerModeMap;
private Map<String, Integer> touchpadInputModeMap;

public SettingsFragment(Context context) {
keymapConfig = new KeymapConfig(context);
Expand Down Expand Up @@ -83,9 +90,18 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
mouseAimActions();
loadTouchpadInputSettings();

binding.pointerMode.setText(keymapConfig.pointerMode);
final String[] pointerModes = {KeymapConfig.POINTER_COMBINED, KeymapConfig.POINTER_OVERLAY, KeymapConfig.POINTER_SYSTEM};
((MaterialAutoCompleteTextView)binding.pointerMode).setSimpleItems(pointerModes);
final int[] pointerModeCodes = {KeymapConfig.POINTER_COMBINED, KeymapConfig.POINTER_OVERLAY, KeymapConfig.POINTER_SYSTEM};
String[] pointerModeNames = getResources().getStringArray(R.array.pointer_modes);
pointerModeMap = IntStream.range(0, pointerModeCodes.length)
.boxed()
.collect(Collectors.toMap(k -> pointerModeNames[k], v -> pointerModeCodes[v]));

for (Map.Entry<String, Integer> entry : pointerModeMap.entrySet()) {
if (entry.getValue().equals(keymapConfig.pointerMode)) {
binding.pointerMode.setText(entry.getKey());
}
}
((MaterialAutoCompleteTextView)binding.pointerMode).setSimpleItems(pointerModeNames);

setDefaultVisibilty();
binding.sliders.setVisibility(View.VISIBLE);
Expand Down Expand Up @@ -138,18 +154,26 @@ private void loadModifierKeys() {
}

private void mouseAimActions() {
if (keymapConfig.mouseAimToggle) binding.mouseAimAction.setText(KeymapConfig.TOGGLE);
else binding.mouseAimAction.setText(KeymapConfig.HOLD);
if (keymapConfig.mouseAimToggle) binding.mouseAimAction.setText(R.string.toggle);
else binding.mouseAimAction.setText(R.string.hold);

final String[] mouseAimActions = {KeymapConfig.HOLD, KeymapConfig.TOGGLE};
((MaterialAutoCompleteTextView)binding.mouseAimAction).setSimpleItems(mouseAimActions);
String[] mouseAimActionNames = getResources().getStringArray(R.array.mouse_aim_actions);
((MaterialAutoCompleteTextView)binding.mouseAimAction).setSimpleItems(mouseAimActionNames);
}

private void loadTouchpadInputSettings() {
binding.touchpadInputMode.setText(keymapConfig.touchpadInputMode);

final String[] touchpadInputModes = {KeymapConfig.TOUCHPAD_DIRECT, KeymapConfig.TOUCHPAD_RELATIVE, KeymapConfig.TOUCHPAD_DISABLED};
((MaterialAutoCompleteTextView)binding.touchpadInputMode).setSimpleItems(touchpadInputModes);
final int[] touchpadInputModeCodes = {KeymapConfig.TOUCHPAD_DIRECT, KeymapConfig.TOUCHPAD_RELATIVE, KeymapConfig.TOUCHPAD_DISABLED};
String[] touchpadInputModeNames = getResources().getStringArray(R.array.touchpad_input_modes);
touchpadInputModeMap = IntStream.range(0, touchpadInputModeCodes.length)
.boxed()
.collect(Collectors.toMap(k -> touchpadInputModeNames[k], v -> touchpadInputModeCodes[v]));

for (Map.Entry<String, Integer> entry : touchpadInputModeMap.entrySet()) {
if (entry.getValue().equals(keymapConfig.touchpadInputMode)) {
binding.touchpadInputMode.setText(entry.getKey());
}
}
((MaterialAutoCompleteTextView)binding.touchpadInputMode).setSimpleItems(touchpadInputModeNames);
}

public boolean onKey(View view, int keyCode, KeyEvent event) {
Expand Down Expand Up @@ -191,9 +215,9 @@ private void saveKeyboardShortcuts() {
@Override
public void onDestroyView() {
saveKeyboardShortcuts();
keymapConfig.mouseAimToggle = binding.mouseAimAction.getText().toString().equals(KeymapConfig.TOGGLE);
keymapConfig.touchpadInputMode = binding.touchpadInputMode.getText().toString();
keymapConfig.pointerMode = binding.pointerMode.getText().toString();
keymapConfig.mouseAimToggle = binding.mouseAimAction.getText().toString().equals(getResources().getString(R.string.toggle));
keymapConfig.touchpadInputMode = touchpadInputModeMap.get(binding.touchpadInputMode.getText().toString());
keymapConfig.pointerMode = pointerModeMap.get(binding.pointerMode.getText().toString());

keymapConfig.mouseSensitivity = binding.sliderMouse.getValue();
keymapConfig.scrollSpeed = binding.sliderScrollSpeed.getValue();
Expand Down
43 changes: 24 additions & 19 deletions app/src/main/java/xtr/keymapper/keymap/KeymapConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,31 @@ public class KeymapConfig implements Parcelable {
public int pauseResumeShortcutKey, launchEditorShortcutKey, switchProfileShortcutKey;
public int swipeDelayMs;
public String pauseResumeShortcutKeyModifier, launchEditorShortcutKeyModifier, switchProfileShortcutKeyModifier;
public String pointerMode;
public int pointerMode;

public static final String KEY_CTRL = "Ctrl", KEY_ALT = "Alt";
public static final String TOGGLE = "Toggle", HOLD = "Hold";
public static final String TOUCHPAD_DIRECT = "Direct";
public static final String TOUCHPAD_RELATIVE = "Relative";
public static final String TOUCHPAD_DISABLED = "Disabled";
public static final int TOGGLE = 1, HOLD = 2;
public static final int TOUCHPAD_DIRECT = 3;
public static final int TOUCHPAD_RELATIVE = 4;
public static final int TOUCHPAD_DISABLED = 5;

public static final String POINTER_SYSTEM = "System";
public static final String POINTER_OVERLAY = "Overlay";
public static final String POINTER_COMBINED = "Combined";
public static final int POINTER_SYSTEM = 6;
public static final int POINTER_OVERLAY = 7;
public static final int POINTER_COMBINED = 8;

public int mouseAimShortcutKey;
public boolean mouseAimToggle;
public String touchpadInputMode = TOUCHPAD_DISABLED;
public int touchpadInputMode = TOUCHPAD_DISABLED;

public KeymapConfig(Context context) {
if (context != null) {
sharedPref = context.getSharedPreferences("settings", MODE_PRIVATE);
loadSharedPrefs();
try {
loadSharedPrefs();
} catch (ClassCastException e) {
sharedPref.edit().clear().apply();
loadSharedPrefs();
}
}
}

Expand Down Expand Up @@ -72,10 +77,10 @@ protected KeymapConfig(Parcel in) {
mouseAimShortcutKey = in.readInt();
mouseAimToggle = in.readByte() != 0;
disableAutoProfiling = in.readByte() != 0;
touchpadInputMode = in.readString();
touchpadInputMode = in.readInt();
useShizuku = in.readByte() != 0;
editorOverlay = in.readByte() != 0;
pointerMode = in.readString();
pointerMode = in.readInt();
}

public static final Creator<KeymapConfig> CREATOR = new Creator<>() {
Expand All @@ -90,7 +95,7 @@ public KeymapConfig[] newArray(int size) {
}
};

private void loadSharedPrefs() {
private void loadSharedPrefs() throws ClassCastException {
mouseSensitivity = sharedPref.getFloat("mouse_sensitivity_multiplier", 1);
scrollSpeed = sharedPref.getFloat("scroll_speed_multiplier", 1);
ctrlMouseWheelZoom = sharedPref.getBoolean("ctrl_mouse_wheel_zoom", false);
Expand All @@ -115,8 +120,8 @@ private void loadSharedPrefs() {
swipeDelayMs = sharedPref.getInt("swipe_delay_ms", 10);
dpadRadiusMultiplier = sharedPref.getFloat("dpad_radius", 1f);

touchpadInputMode = sharedPref.getString("touchpad_input_mode", TOUCHPAD_DISABLED);
pointerMode = sharedPref.getString("pointer_mode", POINTER_COMBINED);
touchpadInputMode = sharedPref.getInt("touchpad_input_mode", TOUCHPAD_DISABLED);
pointerMode = sharedPref.getInt("pointer_mode", POINTER_COMBINED);
}

public void applySharedPrefs() {
Expand All @@ -138,9 +143,9 @@ public void applySharedPrefs() {
.putString("pause_resume_shortcut_modifier", pauseResumeShortcutKeyModifier)
.putString("launch_editor_shortcut_modifier", launchEditorShortcutKeyModifier)
.putString("switch_profile_shortcut_modifier", switchProfileShortcutKeyModifier)
.putString("touchpad_input_mode", touchpadInputMode)
.putInt("touchpad_input_mode", touchpadInputMode)
.putInt("swipe_delay_ms", swipeDelayMs)
.putString("pointer_mode", pointerMode)
.putInt("pointer_mode", pointerMode)
.apply();
}

Expand Down Expand Up @@ -183,9 +188,9 @@ public void writeToParcel(@NonNull Parcel dest, int flags) {
dest.writeInt(mouseAimShortcutKey);
dest.writeByte((byte) (mouseAimToggle ? 1 : 0));
dest.writeByte((byte) (disableAutoProfiling ? 1 : 0));
dest.writeString(touchpadInputMode);
dest.writeInt(touchpadInputMode);
dest.writeByte((byte) (useShizuku ? 1 : 0));
dest.writeByte((byte) (editorOverlay ? 1 : 0));
dest.writeString(pointerMode);
dest.writeInt(pointerMode);
}
}
12 changes: 6 additions & 6 deletions app/src/main/java/xtr/keymapper/server/InputService.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class InputService implements IInputInterface {
boolean stopEvents = false;
boolean pointerUp = false;
private final boolean isWaylandClient;
private final String touchpadInputMode;
private final int touchpadInputMode;
private final View cursorView;

private final Handler mHandler = new Handler(Looper.getMainLooper());
Expand All @@ -42,17 +42,17 @@ public InputService(KeymapProfile profile, KeymapConfig keymapConfig, IRemoteSer
this.isWaylandClient = isWaylandClient;
this.cursorView = cursorView;

if (cursorView == null || !keymapConfig.pointerMode.equals(KeymapConfig.POINTER_OVERLAY)) {
if (cursorView == null || (keymapConfig.pointerMode != KeymapConfig.POINTER_OVERLAY)) {
initMouseCursor(screenWidth, screenHeight);
// Reduce visibility of system pointer
cursorSetX(0);
cursorSetY(0);
}

this.touchpadInputMode = keymapConfig.touchpadInputMode;
if (touchpadInputMode.equals(KeymapConfig.TOUCHPAD_DIRECT))
if (touchpadInputMode == KeymapConfig.TOUCHPAD_DIRECT)
startTouchpadDirect();
else if (touchpadInputMode.equals(KeymapConfig.TOUCHPAD_RELATIVE))
else if (touchpadInputMode == KeymapConfig.TOUCHPAD_RELATIVE)
startTouchpadRelative();

mouseEventHandler = new MouseEventHandler(this);
Expand Down Expand Up @@ -143,9 +143,9 @@ public void stop() {
}

public void stopTouchpad() {
if (touchpadInputMode.equals(KeymapConfig.TOUCHPAD_DIRECT))
if (touchpadInputMode == KeymapConfig.TOUCHPAD_DIRECT)
stopTouchpadDirect();
else if (touchpadInputMode.equals(KeymapConfig.TOUCHPAD_RELATIVE))
else if (touchpadInputMode == KeymapConfig.TOUCHPAD_RELATIVE)
stopTouchpadRelative();
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/xtr/keymapper/server/RemoteService.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ private boolean addNewDevices(String[] data) {
public void startServer(KeymapProfile profile, KeymapConfig keymapConfig, IRemoteServiceCallback cb, int screenWidth, int screenHeight) throws RemoteException {
if (inputService != null) stopServer();
cb.asBinder().linkToDeath(this::stopServer, 0);
if (!keymapConfig.pointerMode.equals(KeymapConfig.POINTER_SYSTEM)) {
if (keymapConfig.pointerMode != KeymapConfig.POINTER_SYSTEM) {
try {
prepareCursorOverlayWindow();
} catch (Exception e) {
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,18 @@
<string name="add_key">添加键</string>
<string name="save">保存</string>

<!-- Mouse Aim -->
<string name="toggle">Toggle</string>
<string name="hold">Hold</string>

<!-- Pointer Mode -->
<string name="combined">Combined</string>
<string name="overlay">Overlay</string>
<string name="system">System</string>

<!-- Touchpad -->
<string name="relative">Relative</string>
<string name="disabled">Disabled</string>
<string name="direct">Direct</string>

</resources>
17 changes: 17 additions & 0 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="pointer_modes">
<item>@string/combined</item>
<item>@string/overlay</item>
<item>@string/system</item>
</string-array>
<string-array name="touchpad_input_modes">
<item>@string/direct</item>
<item>@string/relative</item>
<item>@string/disabled</item>
</string-array>
<string-array name="mouse_aim_actions">
<item>@string/toggle</item>
<item>@string/hold</item>
</string-array>
</resources>
16 changes: 16 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,20 @@
<string name="add_key">Add Key</string>
<string name="save">Save</string>

<!-- Mouse Aim -->
<string name="toggle">Toggle</string>
<string name="hold">Hold</string>

<!-- Pointer Mode -->
<string name="combined">Combined</string>
<string name="overlay">Overlay</string>
<string name="system">System</string>

<!-- Touchpad -->
<string name="relative">Relative</string>
<string name="disabled">Disabled</string>
<string name="direct">Direct</string>



</resources>

0 comments on commit a4a4293

Please sign in to comment.