Skip to content

Commit

Permalink
feat: Redesigned new profiles and user interface
Browse files Browse the repository at this point in the history
load profiles using SharedPreference and StringSet instead of file i/o
#4
  • Loading branch information
Xtr126 committed Feb 10, 2023
1 parent 95cf0bb commit c1b4306
Show file tree
Hide file tree
Showing 18 changed files with 547 additions and 175 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/xtr/keymapper/EditorService.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {

if (editor != null) editor.hideView();

Context context = new ContextThemeWrapper(this, R.style.Theme_MaterialComponents);
Context context = new ContextThemeWrapper(this, R.style.Theme_AppCompat_Dialog_Alert);
editor = new EditorUI(context, this);
editor.open();

Expand Down
71 changes: 31 additions & 40 deletions app/src/main/java/xtr/keymapper/EditorUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class EditorUI extends OnKeyEventListener.Stub {

private MovableFloatingActionKey keyInFocus;
// Keyboard keys
private final List<MovableFloatingActionKey> Keys = new ArrayList<>();
private final List<MovableFloatingActionKey> keyList = new ArrayList<>();
private MovableFloatingActionKey leftClick;

private MovableFrameLayout dpad1, dpad2, crosshair;
Expand Down Expand Up @@ -143,63 +143,54 @@ public void hideView() {
}

private void loadKeymap() throws IOException {
keymapConfig.loadConfig();
KeymapProfiles.Profile profile = new KeymapProfiles(context).getProfile(keymapConfig.profile);
// Add Keyboard keys as Views
profile.keys.forEach(this::addKey);

for (int n = 0; n < keymapConfig.getKeys().size(); n++) {
KeymapConfig.Key key = keymapConfig.getKeys().get(n);
addKey(key);
}

Dpad dpad1 = keymapConfig.dpad1;
Dpad dpad2 = keymapConfig.dpad2;
mouseAimConfig = keymapConfig.mouseAimConfig;

if (dpad1 != null) addDpad1(dpad1.getX(), dpad1.getY());
if (profile.dpad1 != null) addDpad1(profile.dpad1.getX(), profile.dpad1.getY());

if (dpad2 != null) addDpad2(dpad2.getX(), dpad2.getY());
if (profile.dpad2 != null) addDpad2(profile.dpad2.getX(), profile.dpad2.getY());

if (mouseAimConfig != null)
addCrosshair(mouseAimConfig.xCenter, mouseAimConfig.yCenter);
mouseAimConfig = profile.mouseAimConfig;
if (mouseAimConfig != null) addCrosshair(mouseAimConfig.xCenter, mouseAimConfig.yCenter);
}

private void saveKeymap() throws IOException {
StringBuilder linesToWrite = new StringBuilder();
private void saveKeymap() {
ArrayList<String> linesToWrite = new ArrayList<>();

if (dpad1 != null) {
Dpad dpad = new Dpad(dpad1, DpadType.UDLR);
linesToWrite.append(dpad.getData());
linesToWrite.add(dpad.getData());
}

if (dpad2 != null) {
Dpad dpad = new Dpad(dpad2, DpadType.WASD);
linesToWrite.append(dpad.getData());
linesToWrite.add(dpad.getData());

for (int i = 0; i < Keys.size(); i++) {
String key = Keys.get(i).getText();
if(key.matches("[WASD]")) {
Keys.get(i).key = null; // If WASD keys already added, remove them
}
}
// If WASD keys already added, remove them
for (int i = 0; i < keyList.size(); i++)
if (keyList.get(i).getText().matches("[WASD]"))
keyList.get(i).key = null;
}

if (crosshair != null) {
// get x and y coordinates from view
// Get x and y coordinates from view
mouseAimConfig.setCenterXY(crosshair);
mouseAimConfig.setLeftClickXY(leftClick);
linesToWrite.append(mouseAimConfig.getData());
linesToWrite.add(mouseAimConfig.getData());
}

// Keyboard keys
for (int i = 0; i < Keys.size(); i++) {
if(Keys.get(i).key != null) {
linesToWrite.append(Keys.get(i).getData());
}
}

// Save Config to file
keymapConfig.writeConfig(linesToWrite);
keyList.forEach(movableFloatingActionKey -> {
if(movableFloatingActionKey != null)
linesToWrite.add(movableFloatingActionKey.getData());
});

// Save Config
KeymapProfiles profiles = new KeymapProfiles(context);
profiles.saveProfile(keymapConfig.profile, linesToWrite, context.getPackageName());

// reload keymap if service running
// Reload keymap if service running
InputService.reloadKeymap();
}

Expand Down Expand Up @@ -257,7 +248,7 @@ private void addDpad2(float x, float y) {
.start();
}

private void addKey(KeymapConfig.Key key) {
private void addKey(KeymapProfiles.Key key) {
MovableFloatingActionKey floatingKey = new MovableFloatingActionKey(context);

floatingKey.setText(key.code.substring(4));
Expand All @@ -270,12 +261,12 @@ private void addKey(KeymapConfig.Key key) {

mainView.addView(floatingKey);

Keys.add(floatingKey);
keyList.add(floatingKey);
}

private void addKey() {
final KeymapConfig.Key key = new KeymapConfig.Key();
key.code = "X";
final KeymapProfiles.Key key = new KeymapProfiles.Key();
key.code = "KEY_X";
key.x = DEFAULT_X;
key.y = DEFAULT_Y;
addKey(key);
Expand Down
77 changes: 3 additions & 74 deletions app/src/main/java/xtr/keymapper/KeymapConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,22 @@
import android.content.Context;
import android.content.SharedPreferences;

import xtr.keymapper.mouse.MouseAimConfig;
import xtr.keymapper.dpad.Dpad;
import xtr.keymapper.fragment.Profiles;

import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import xtr.keymapper.fragment.ProfilesApps;

public class KeymapConfig {
private final Context context;
public static final String config_name = "/config.";
public Dpad dpad1 = null;
public Dpad dpad2 = null;
public MouseAimConfig mouseAimConfig = null;
private final SharedPreferences sharedPref;
private final SharedPreferences.Editor sharedPrefEditor;

public String profile, device;
public Float mouseSensitivity, scrollSpeed;
public int stopServiceShortcutKey, launchEditorShortcutKey;
public boolean ctrlMouseWheelZoom, ctrlDragMouseGesture;

private List<Key> keys;

public KeymapConfig(Context context) {
this.context = context;
sharedPref = context.getSharedPreferences("settings", MODE_PRIVATE);
sharedPrefEditor = sharedPref.edit();
loadSharedPrefs();
}

private void loadSharedPrefs() {
profile = sharedPref.getString("profile", Profiles.defaultProfile);
profile = sharedPref.getString("profile", ProfilesApps.defaultProfile);
device = sharedPref.getString("device", "null");
mouseSensitivity = sharedPref.getFloat("mouse_sensitivity_multiplier", 1);
scrollSpeed = sharedPref.getFloat("scroll_speed_multiplier", 1);
Expand All @@ -51,7 +31,7 @@ private void loadSharedPrefs() {
}

public void applySharedPrefs() {
sharedPrefEditor.putString("profile", profile)
sharedPref.edit().putString("profile", profile)
.putString("device", device)
.putFloat("mouse_sensitivity_multiplier", mouseSensitivity)
.putFloat("scroll_speed_multiplier", scrollSpeed)
Expand All @@ -61,55 +41,4 @@ public void applySharedPrefs() {
.putBoolean("ctrl_drag_mouse_gesture", ctrlDragMouseGesture)
.apply();
}

static final class Key {
String code;
float x;
float y;
}

public String getConfigPath() {
return context.getFilesDir() + config_name + profile;
}

public void writeConfig(StringBuilder linesToWrite) throws IOException {
FileWriter fileWriter = new FileWriter(getConfigPath());
fileWriter.append(linesToWrite);
fileWriter.close();
}

public List<Key> getKeys() {
return keys;
}

public void loadConfig() throws IOException {
keys = new ArrayList<>();

List<String> stream = Files.readAllLines(Paths.get(getConfigPath()));
stream.forEach(s -> {
String[] data = s.split("\\s+"); // Split a String like KEY_G 760.86346 426.18607
switch (data[0]){
case "UDLR_DPAD": {
dpad1 = new Dpad(data);
break;
}
case "WASD_DPAD": {
dpad2 = new Dpad(data);
break;
}
case "MOUSE_AIM": {
mouseAimConfig = new MouseAimConfig().parse(data);
break;
}
default: {
final Key key = new Key();
key.code = data[0];
key.x = Float.parseFloat(data[1]);
key.y = Float.parseFloat(data[2]);
keys.add(key);
break;
}
}
});
}
}
101 changes: 101 additions & 0 deletions app/src/main/java/xtr/keymapper/KeymapProfiles.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package xtr.keymapper;

import static android.content.Context.MODE_PRIVATE;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;

import xtr.keymapper.dpad.Dpad;
import xtr.keymapper.mouse.MouseAimConfig;

public class KeymapProfiles {
private final SharedPreferences sharedPref;

public KeymapProfiles(Context context) {
sharedPref = context.getSharedPreferences("profiles", MODE_PRIVATE);
}

public Map<String, Profile> getAllProfiles(){
Map<String, Profile> profiles = new HashMap<>();
sharedPref.getAll().forEach((BiConsumer<String, Object>) (key, o) -> profiles.put(key, getProfile(key)));
return profiles;
}

public void renameProfile(String profileName, String newProfile) {
Set<String> stringSet = sharedPref.getStringSet(profileName, null);
deleteProfile(profileName);
sharedPref.edit()
.putStringSet(newProfile, stringSet)
.apply();
}

static final class Key {

String code;
float x;
float y;
}
public static final class Profile {

public String packageName = "xtr.keymapper";
Dpad dpad1 = null;
Dpad dpad2 = null;
MouseAimConfig mouseAimConfig = null;
ArrayList<Key> keys = new ArrayList<>();
}
public void saveProfile(String profile, ArrayList<String> lines, String packageName) {
lines.add("APPLICATION " + packageName);
Set<String> stringSet = new HashSet<>(lines);
sharedPref.edit()
.putStringSet(profile, stringSet)
.apply();
}

public void deleteProfile(String profileName){
sharedPref.edit().remove(profileName).apply();
}

public Profile getProfile(String profileName) {
Set<String> stream = sharedPref.getStringSet(profileName, null);

Profile profile = new Profile();
if (stream != null) stream.forEach(s -> {

String[] data = s.split("\\s+"); // Split a String like KEY_G 760.86346 426.18607
switch (data[0]){
case "UDLR_DPAD":
profile.dpad1 = new Dpad(data);
break;

case "WASD_DPAD":
profile.dpad2 = new Dpad(data);
break;

case "MOUSE_AIM":
profile.mouseAimConfig = new MouseAimConfig().parse(data);
break;

case "APPLICATION":
profile.packageName = data[1];
break;

default: {
final Key key = new Key();
key.code = data[0];
key.x = Float.parseFloat(data[1]);
key.y = Float.parseFloat(data[2]);
profile.keys.add(key);
break;
}
}
});
return profile;
}
}
Loading

0 comments on commit c1b4306

Please sign in to comment.