Skip to content

Commit

Permalink
refactor: Remove all usages of speedDial and setup CardViews
Browse files Browse the repository at this point in the history
  • Loading branch information
Xtr126 committed Oct 15, 2024
1 parent 433969e commit d281a95
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 94 deletions.
1 change: 0 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ dependencies {
implementation "androidx.constraintlayout:constraintlayout:2.1.4"
implementation "androidx.coordinatorlayout:coordinatorlayout:1.2.0"
implementation 'com.google.android.material:material:1.12.0'
implementation "com.leinardi.android:speed-dial:3.3.0"
implementation 'androidx.webkit:webkit:1.12.1'
implementation "com.github.topjohnwu.libsu:core:5.3.0"
implementation "com.github.topjohnwu.libsu:service:5.3.0"
Expand Down
117 changes: 50 additions & 67 deletions app/src/main/java/xtr/keymapper/editor/EditorUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import xtr.keymapper.databinding.CrosshairBinding;
import xtr.keymapper.databinding.DpadArrowsBinding;
import xtr.keymapper.databinding.DpadBinding;
import xtr.keymapper.databinding.KeymapEditorBinding;
import xtr.keymapper.databinding.MouseAimConfigBinding;
import xtr.keymapper.databinding.ResizableBinding;
import xtr.keymapper.dpad.Dpad;
Expand All @@ -45,15 +44,13 @@
import xtr.keymapper.keymap.KeymapProfileKey;
import xtr.keymapper.keymap.KeymapProfiles;
import xtr.keymapper.mouse.MouseAimConfig;
import xtr.keymapper.server.RemoteService;
import xtr.keymapper.server.RemoteServiceHelper;
import xtr.keymapper.swipekey.SwipeKey;
import xtr.keymapper.swipekey.SwipeKeyView;

public class EditorUI extends OnKeyEventListener.Stub {

private final LayoutInflater layoutInflater;
private final ViewGroup mainView;

private KeyInFocus keyInFocus;
// Keyboard keys
Expand All @@ -65,8 +62,6 @@ public class EditorUI extends OnKeyEventListener.Stub {
private MovableFrameLayout crosshair;
private final MovableFrameLayout[] dpadArray = new MovableFrameLayout[MAX_DPADS];
private final DpadBinding[] dpadBindingArray = new DpadBinding[MAX_DPADS];
// Default position of new views added
private final KeymapEditorBinding binding;
private final Context context;
private final OnHideListener onHideListener;
private final Handler mHandler = new Handler(Looper.getMainLooper());
Expand All @@ -75,7 +70,7 @@ public class EditorUI extends OnKeyEventListener.Stub {
private boolean overlayOpen = false;
private MovableFrameLayout dpadUdlr;
private final SettingsFragment settingsFragment;
private final ViewGroup settingsView;
private final ViewGroup mainView;
public static final int START_SETTINGS = 0;
public static final int START_EDITOR = 1;

Expand All @@ -90,18 +85,13 @@ public EditorUI (Context context, OnHideListener onHideListener, String profileN

layoutInflater = context.getSystemService(LayoutInflater.class);

binding = KeymapEditorBinding.inflate(layoutInflater);
mainView = binding.getRoot();

settingsFragment = new SettingsFragment(context);
settingsView = settingsFragment.createView(layoutInflater);
settingsFragment.init(startMode);
mainView = settingsFragment.createView(layoutInflater);

binding.speedDial.inflate(R.menu.keymap_editor_menu);
settingsFragment.inflate(R.menu.keymap_editor_menu, startMode, layoutInflater);

binding.speedDial.open();
setupButtons();
settingsFragment.init(startMode);
settingsFragment.inflateMenuResource(startMode, layoutInflater);
settingsFragment.setOnActionSelectedListener(this::onActionSelected);
}

public void open(boolean overlayWindow) {
Expand All @@ -111,10 +101,8 @@ public void open(boolean overlayWindow) {
else {
if (context instanceof EditorActivity)
((Activity)context).setContentView(mainView);
else
else // For MainActivity
((Activity)context).addContentView(mainView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

mainView.addView(settingsView,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
}

if (onHideListener != null && !onHideListener.getEvent()) {
Expand All @@ -126,7 +114,6 @@ public void open(boolean overlayWindow) {
public void openOverlayWindow() {
if (overlayOpen) {
removeView(mainView);
removeView(settingsView);
}
WindowManager mWindowManager = context.getSystemService(WindowManager.class);
WindowManager.LayoutParams mParams = new WindowManager.LayoutParams(
Expand All @@ -137,8 +124,6 @@ public void openOverlayWindow() {
WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR,
PixelFormat.TRANSLUCENT);
mWindowManager.addView(mainView, mParams);
mainView.addView(settingsView, mParams);
mainView.bringChildToFront(settingsView);
overlayOpen = true;
}

Expand Down Expand Up @@ -170,16 +155,58 @@ public void onKeyEvent(String event) {
}


/**
* Called when a CardView has been clicked.
*
* @param id the relevant id of the menu item for the card.
*/
public void onActionSelected(int id) {
// X y coordinates of center of root view
float defaultX = mainView.getPivotX();
float defaultY = mainView.getPivotY();

if (id == R.id.add) {
addKey(defaultX, defaultY);
}
else if (id == R.id.save) {
hideView();
}
else if (id == R.id.dpad) {
final CharSequence[] items = { "Arrow keys", "WASD Keys", "Custom"};
MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(context);
builder.setTitle("Select Dpad").setItems(items, (dialog, i) -> {
if (i == 0) addArrowKeysDpad(defaultX, defaultY);
else if (i == 1) addWasdDpad(defaultX, defaultY);
else addDpad(getNextDpadId(), defaultX, defaultY);
});
AlertDialog dialog = builder.create();
if (overlayOpen) dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
dialog.show();
}
else if (id == R.id.crosshair) {
profile.mouseAimConfig = new MouseAimConfig();
addCrosshair(defaultX, defaultY);
}
else if (id == R.id.mouse_left) {
addLeftClick(defaultX, defaultY);
}
else if (id == R.id.swipe_key) {
addSwipeKey();
}
else if (id == R.id.mouse_right) {
addRightClick(defaultX, defaultY);
}
}
public interface OnHideListener {
void onHideView();
boolean getEvent();

}

public void hideView() {
saveKeymap();
settingsFragment.onDestroyView();
removeView(mainView);
removeView(settingsView);
if (onHideListener != null) onHideListener.onHideView();
else RemoteServiceHelper.reloadKeymap(context);
}
Expand Down Expand Up @@ -242,7 +269,7 @@ private void saveKeymap() {
if (rightClick != null) {
linesToWrite.add(MOUSE_RIGHT + " " + rightClick.getX() + " " + rightClick.getY());
}

// Keyboard keys
floatingKeysMap.forEach((frameLayout, movableFloatingActionKey) -> linesToWrite.add(movableFloatingActionKey.getData()));
swipeKeyList.stream().map(swipeKeyView -> new SwipeKey(swipeKeyView).getData()).forEach(linesToWrite::add);
Expand All @@ -254,50 +281,6 @@ private void saveKeymap() {
// Reload keymap if service running
}


public void setupButtons() {
binding.speedDial.setOnActionSelectedListener(actionItem -> {
// X y coordinates of center of root view
float defaultX = mainView.getPivotX();
float defaultY = mainView.getPivotY();

int id = actionItem.getId();

if (id == R.id.add) {
addKey(defaultX, defaultY);
}
else if (id == R.id.save) {
hideView();
}
else if (id == R.id.dpad) {
final CharSequence[] items = { "Arrow keys", "WASD Keys", "Custom"};
MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(context);
builder.setTitle("Select Dpad").setItems(items, (dialog, i) -> {
if (i == 0) addArrowKeysDpad(defaultX, defaultY);
else if (i == 1) addWasdDpad(defaultX, defaultY);
else addDpad(getNextDpadId(), defaultX, defaultY);
});
AlertDialog dialog = builder.create();
if (overlayOpen) dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
dialog.show();
}
else if (id == R.id.crosshair) {
profile.mouseAimConfig = new MouseAimConfig();
addCrosshair(defaultX, defaultY);
}
else if (id == R.id.mouse_left) {
addLeftClick(defaultX, defaultY);
}
else if (id == R.id.swipe_key) {
addSwipeKey();
}
else if (id == R.id.mouse_right) {
addRightClick(defaultX, defaultY);
}
return true;
});
}

private void addWasdDpad(float defaultX, float defaultY) {
int i = getNextDpadId();
addDpad(i, defaultX, defaultY);
Expand Down
29 changes: 25 additions & 4 deletions app/src/main/java/xtr/keymapper/editor/SettingsFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import android.widget.LinearLayout;
import android.widget.PopupMenu;

import androidx.annotation.MenuRes;
import androidx.annotation.IdRes;
import androidx.annotation.NonNull;

import com.google.android.material.button.MaterialButtonToggleGroup;
Expand All @@ -25,14 +25,15 @@
import xtr.keymapper.databinding.KeymapEditorItemBinding;
import xtr.keymapper.databinding.KeymapEditorLayoutBinding;
import xtr.keymapper.keymap.KeymapConfig;
import xtr.keymapper.server.RemoteServiceHelper;

public class SettingsFragment {
private final KeymapConfig keymapConfig;
private KeymapEditorLayoutBinding binding;
private Map<String, Integer> pointerModeMap;
private Map<String, Integer> touchpadInputModeMap;
private final Context context;
private OnCardItemSelectedListener onCardItemSelectedListener;

public SettingsFragment(Context context) {
this.context = context;
keymapConfig = new KeymapConfig(context);
Expand Down Expand Up @@ -231,9 +232,9 @@ public void onDestroyView() {
binding = null;
}

public void inflate(@MenuRes int menuRes, int startMode, LayoutInflater layoutInflater) {
public void inflateMenuResource(int startMode, LayoutInflater layoutInflater) {
PopupMenu popupMenu = new PopupMenu(context, new View(context));
popupMenu.inflate(menuRes);
popupMenu.inflate(R.menu.keymap_editor_menu);
Menu menu = popupMenu.getMenu();

if (startMode == EditorUI.START_EDITOR) for (int n = 1; n <= 3; n++) { // n = { 1,2,3 } For dividing between three columns
Expand All @@ -253,12 +254,32 @@ public void inflate(@MenuRes int menuRes, int startMode, LayoutInflater layoutIn
itemBinding.imageView.setImageDrawable(menuItem.getIcon());
itemBinding.title.setText(menuItem.getTitle());
itemBinding.description.setText(menuItem.getContentDescription());
itemBinding.getRoot().setOnClickListener(v -> onCardItemSelected(menuItem.getItemId()));
}
} else if (startMode == EditorUI.START_SETTINGS) {
KeymapEditorItemBinding itemBinding = KeymapEditorItemBinding.inflate(layoutInflater, binding.L1, true);
itemBinding.imageView.setImageResource(R.drawable.ic_baseline_done_36);
itemBinding.title.setText(R.string.save);
itemBinding.getRoot().setOnClickListener(v -> onCardItemSelected(R.id.save));
}

}

private void onCardItemSelected(int itemId) {
if (onCardItemSelectedListener != null)
onCardItemSelectedListener.onActionSelected(itemId);
}

public void setOnActionSelectedListener(OnCardItemSelectedListener l) {
this.onCardItemSelectedListener = l;
}

public interface OnCardItemSelectedListener {
/**
* Called when a CardView has been clicked.
*
* @param menuItemId the relevant id of the menu item for the card.
*/
void onActionSelected(@IdRes int menuItemId);
}
}
22 changes: 0 additions & 22 deletions app/src/main/res/layout/keymap_editor.xml

This file was deleted.

0 comments on commit d281a95

Please sign in to comment.