Skip to content

Commit

Permalink
refactor: use ArrayList in KeymapConfig to load other keys #3
Browse files Browse the repository at this point in the history
  • Loading branch information
Xtr126 committed Feb 2, 2023
1 parent 5e930ce commit c04892f
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 77 deletions.
43 changes: 21 additions & 22 deletions app/src/main/java/xtr/keymapper/EditorUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,34 +132,25 @@ public void hideView() {

private void loadKeymap() throws IOException {

KeymapConfig keymapConfig = new KeymapConfig(context);
KeymapConfig keymapConfig = new KeymapConfig(context).loadSharedPrefs();
keymapConfig.loadConfig();

String[] keys = keymapConfig.getKeys();
Float[] keysX = keymapConfig.getX();
Float[] keysY = keymapConfig.getY();

for (int n = 0; n < keys.length; n++) {
if (keys[n] != null) {
addKey(keys[n], keysX[n], keysY[n]);
}
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 (dpad1 != null) addDpad1(dpad1.getX(), dpad1.getY());

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

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

private void saveKeymap() throws IOException {
Expand Down Expand Up @@ -206,7 +197,7 @@ private void saveKeymap() throws IOException {

public void setupButtons() {
binding.saveButton.setOnClickListener(v -> hideView());
binding.addButton.setOnClickListener(v -> addKey("A", DEFAULT_X, DEFAULT_Y));
binding.addButton.setOnClickListener(v -> addKey());
binding.mouseLeft.setOnClickListener(v -> addleftClick(DEFAULT_X, DEFAULT_Y));
binding.crossHair.setOnClickListener(v -> {
mouseAimConfig = new MouseAimConfig();
Expand Down Expand Up @@ -258,13 +249,13 @@ private void addDpad2(float x, float y) {
.start();
}

private void addKey(String key, float x ,float y) {
private void addKey(KeymapConfig.Key key) {
MovableFloatingActionKey floatingKey = new MovableFloatingActionKey(context);

floatingKey.setText(key);
floatingKey.setText(key.code);
floatingKey.animate()
.x(x)
.y(y)
.x(key.x)
.y(key.y)
.setDuration(1000)
.start();
floatingKey.setOnClickListener(this::onClick);
Expand All @@ -274,6 +265,14 @@ private void addKey(String key, float x ,float y) {
Keys.add(floatingKey);
}

private void addKey() {
final KeymapConfig.Key key = new KeymapConfig.Key();
key.code = "X";
key.x = DEFAULT_X;
key.y = DEFAULT_Y;
addKey(key);
}

public void onClick(View view) {
keyInFocus = ((MovableFloatingActionKey)view);
}
Expand Down Expand Up @@ -308,8 +307,8 @@ private void addleftClick(float x, float y) {
.setDuration(500)
.start();
}

class ResizableLayout implements View.OnTouchListener {

private final View view;

@SuppressLint("ClickableViewAccessibility")
Expand Down
39 changes: 19 additions & 20 deletions app/src/main/java/xtr/keymapper/KeymapConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class KeymapConfig {
private final Context context;
public static final String config_name = "/config.";
private final String[] keys = new String[36]; // element 0 to 35 for A-Z 0-9
private final Float[] keyX = new Float[36];
private final Float[] keyY = new Float[36];
public Dpad dpad1 = null;
public Dpad dpad2 = null;
public MouseAimConfig mouseAimConfig = null;
Expand All @@ -32,6 +30,8 @@ public class KeymapConfig {
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);
Expand Down Expand Up @@ -62,19 +62,13 @@ public void applySharedPrefs() {
.apply();
}

public String[] getKeys() {
return keys;
}

public Float[] getX() {
return keyX;
}

public Float[] getY() {
return keyY;
static final class Key {
String code;
float x;
float y;
}

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

Expand All @@ -84,7 +78,13 @@ public void writeConfig(StringBuilder linesToWrite) throws IOException {
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
Expand All @@ -102,12 +102,11 @@ public void loadConfig() throws IOException {
break;
}
default: {
int i = Utils.obtainIndex(data[0]);
if ( i > -1 ) {
keys[i] = data[0].substring(4);
keyX[i] = Float.valueOf(data[1]);
keyY[i] = Float.valueOf(data[2]);
}
final Key key = new Key();
key.code = data[0].substring(4);
key.x = Float.parseFloat(data[1]);
key.y = Float.parseFloat(data[2]);
keys.add(key);
break;
}
}
Expand Down
68 changes: 35 additions & 33 deletions app/src/main/java/xtr/keymapper/TouchPointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import android.widget.Toast;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import xtr.keymapper.activity.InputDeviceSelector;
import xtr.keymapper.activity.MainActivity;
Expand All @@ -42,7 +44,6 @@ public class TouchPointer extends Service {
private View cursorView;
private WindowManager mWindowManager;
int x1 = 100, y1 = 100;
private Float[] keysX, keysY;
private DpadHandler dpad1Handler, dpad2Handler;
private MouseAimHandler mouseAimHandler;
private final Handler mHandler = new Handler(Looper.getMainLooper());
Expand All @@ -55,6 +56,8 @@ public class TouchPointer extends Service {
public MainActivity.Callback activityCallback;
int width; int height;

private List<KeymapConfig.Key> keys = new ArrayList<>();

private final IBinder binder = new TouchPointerBinder();

public class TouchPointerBinder extends Binder {
Expand Down Expand Up @@ -187,8 +190,7 @@ public void loadKeymap() {
Log.e("loadKeymap", e.toString());
}

keysX = keymapConfig.getX();
keysY = keymapConfig.getY();
keys = keymapConfig.getKeys();

if (keymapConfig.dpad1 != null)
dpad1Handler = new DpadHandler(this, keymapConfig.dpad1, dpad1pid.id);
Expand Down Expand Up @@ -289,19 +291,20 @@ private void init() {
}

private class KeyEvent {
String label;
String code;
int action;
}

private void handleEvent(String line) throws RemoteException {
// line: /dev/input/event3 EV_KEY KEY_X DOWN
String[] input_event = line.split("\\s+");
if(!input_event[1].equals("EV_KEY")) return;

if (activityCallback != null) activityCallback.updateCmdView2(line + "\n");
if (!input_event[1].equals("EV_KEY")) return;

KeyEvent event = new KeyEvent();
event.label = input_event[2];
event.code = input_event[2];
if (!event.code.contains("KEY_")) return;

if (activityCallback != null) activityCallback.updateCmdView2(line + "\n");

switch (input_event[3]) {
case "UP":
Expand All @@ -314,32 +317,31 @@ private void handleEvent(String line) throws RemoteException {
return;
}

int i = Utils.obtainIndex(event.label); // Strips off KEY_ from KEY_X and return the index of X in alphabet
if (i >= 0 && i <= 35) { // A-Z and 0-9 only in this range
if (keysX != null && keysX[i] != null) { // null if keymap not set
mService.injectEvent(keysX[i], keysY[i], event.action, i);
} else if (dpad2Handler != null) { // Dpad with WASD keys
dpad2Handler.handleEvent(event.label, event.action);
}
// Keyboard shortcuts
if (event.action == DOWN) {
if (i == stop_service) stopPointer();
if (i == launch_editor) startService(new Intent(TouchPointer.this, EditorService.class));
}
} else {
switch (event.label) {
default:
if (dpad1Handler != null) // Dpad with arrow keys
dpad1Handler.handleEvent(event.label, event.action);
break;
case "KEY_GRAVE":
if (event.action == DOWN) mouseEventHandler.triggerMouseAim();
break;
case "KEY_LEFTCTRL":
ctrlKeyPressed = event.action == DOWN;
break;
}
// Keyboard shortcuts
int i = Utils.obtainIndex(event.code);
if (event.action == DOWN ) if (i > 0) {
if (i == stop_service) stopPointer();
if (i == launch_editor)
startService(new Intent(TouchPointer.this, EditorService.class));
}

if ("KEY_GRAVE".equals(event.code)) {
if (event.action == DOWN) mouseEventHandler.triggerMouseAim();
}

if (event.code.contains("CTRL")) ctrlKeyPressed = event.action == DOWN;

for (i = 0; i < keys.size(); i++) {
KeymapConfig.Key key = keys.get(i);
if (event.code.equals(key.code))
mService.injectEvent(key.x, key.y, event.action, i);
}

if (dpad1Handler != null) // Dpad with arrow keys
dpad1Handler.handleEvent(event.code, event.action);

if (dpad2Handler != null) // Dpad with WASD keys
dpad2Handler.handleEvent(event.code, event.action);
}
}

Expand Down
8 changes: 6 additions & 2 deletions app/src/main/java/xtr/keymapper/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
public class Utils {
public static final String alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

public static int obtainIndex(String s) {
return alphabet.indexOf(s.substring(4));
/**
* @param key input key code KEY_X
* @return the index of X in alphabet
*/
public static int obtainIndex(String key) {
return alphabet.indexOf(key.substring(4));
}

public static BufferedReader geteventStream() throws IOException {
Expand Down

0 comments on commit c04892f

Please sign in to comment.