Skip to content

Commit

Permalink
feat: Integrate new layout to editor
Browse files Browse the repository at this point in the history
  • Loading branch information
Xtr126 committed Oct 11, 2024
1 parent 5d400ad commit 20ebbf1
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 475 deletions.
5 changes: 2 additions & 3 deletions app/src/main/java/xtr/keymapper/activity/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import xtr.keymapper.TouchPointer;
import xtr.keymapper.databinding.ActivityMainBinding;
import xtr.keymapper.editor.EditorActivity;
import xtr.keymapper.fragment.SettingsFragment;
import xtr.keymapper.keymap.KeymapConfig;
import xtr.keymapper.profiles.ProfilesViewAdapter;
import xtr.keymapper.server.RemoteServiceHelper;
Expand Down Expand Up @@ -100,8 +99,8 @@ private void setupButtons() {
binding.controls.launchApp.setOnClickListener(v -> launchApp());
binding.controls.startPointer.setOnClickListener(v -> startPointer());
binding.controls.startEditor.setOnClickListener(v -> startEditor());
binding.controls.configButton.setOnClickListener
(v -> new SettingsFragment(this).show(getSupportFragmentManager(), "dialog"));
// binding.controls.configButton.setOnClickListener
// (v -> new SettingsFragment(this).show(getSupportFragmentManager(), "dialog"));
binding.controls.aboutButton.setOnClickListener
(v -> startActivity(new Intent(this, InfoActivity.class)));
binding.controls.importExportButton.setOnClickListener
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/xtr/keymapper/editor/EditorActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.os.Bundle;
import android.os.RemoteException;
import android.util.Log;
import android.view.WindowManager;

import androidx.appcompat.app.AlertDialog;
import androidx.core.view.WindowCompat;
Expand Down Expand Up @@ -59,6 +60,7 @@ public void onCreate(Bundle savedInstanceState) {
.setPositiveButton(R.string.ok, (dialog, which) -> {})
.setTitle(R.string.dialog_alert_editor_title);
AlertDialog dialog = builder.create();
if (keymapConfig.editorOverlay) dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
dialog.show();
}
}
Expand Down
21 changes: 18 additions & 3 deletions app/src/main/java/xtr/keymapper/editor/EditorUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ public class EditorUI extends OnKeyEventListener.Stub {
private KeymapProfile profile;
private boolean overlayOpen = false;
private MovableFrameLayout dpadUdlr;
private final SettingsFragment settingsFragment;
private final ViewGroup settingsView;

interface KeyInFocus {
void setText(String key);
Expand All @@ -86,6 +88,9 @@ public EditorUI (Context context, OnHideListener onHideListener, String profileN
binding = KeymapEditorBinding.inflate(layoutInflater);
mainView = binding.getRoot();

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

binding.speedDial.inflate(R.menu.keymap_editor_menu);
binding.speedDial.open();
setupButtons();
Expand All @@ -95,7 +100,10 @@ public void open(boolean overlayWindow) {
loadKeymap();
if (mainView.getWindowToken() == null && mainView.getParent() == null)
if (overlayWindow) openOverlayWindow();
else ((EditorActivity)context).setContentView(mainView);
else {
((EditorActivity)context).setContentView(mainView);
mainView.addView(settingsView,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
}

if (!onHideListener.getEvent()) {
mainView.setOnKeyListener(this::onKey);
Expand All @@ -104,7 +112,10 @@ public void open(boolean overlayWindow) {
}

public void openOverlayWindow() {
if (overlayOpen) removeView(mainView);
if (overlayOpen) {
removeView(mainView);
removeView(settingsView);
}
WindowManager mWindowManager = context.getSystemService(WindowManager.class);
WindowManager.LayoutParams mParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT,
Expand All @@ -114,6 +125,8 @@ 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 @@ -152,12 +165,14 @@ public interface OnHideListener {

public void hideView() {
saveKeymap();
settingsFragment.onDestroyView();
removeView(mainView);
removeView(settingsView);
onHideListener.onHideView();
}

private void removeView(ViewGroup view) {
if (overlayOpen) context.getSystemService(WindowManager.class).removeView(view);
if (overlayOpen && view.isAttachedToWindow()) context.getSystemService(WindowManager.class).removeView(view);
view.removeAllViews();
view.invalidate();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,60 +1,66 @@
package xtr.keymapper.fragment;
package xtr.keymapper.editor;

import static com.google.android.material.bottomsheet.BottomSheetBehavior.STATE_EXPANDED;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.google.android.material.textfield.MaterialAutoCompleteTextView;
import com.google.android.material.button.MaterialButtonToggleGroup;

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;
import xtr.keymapper.databinding.FragmentSettingsDialogBinding;
import xtr.keymapper.databinding.KeymapEditorLayoutBinding;
import xtr.keymapper.keymap.KeymapConfig;
import xtr.keymapper.server.RemoteServiceHelper;

public class SettingsFragment extends BottomSheetDialogFragment {
public class SettingsFragment {
private final KeymapConfig keymapConfig;
private FragmentSettingsDialogBinding binding;
private KeymapEditorLayoutBinding binding;
private Map<String, Integer> pointerModeMap;
private Map<String, Integer> touchpadInputModeMap;

private final Context context;
public SettingsFragment(Context context) {
this.context = context;
keymapConfig = new KeymapConfig(context);
}

@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
public ViewGroup createView(@NonNull LayoutInflater inflater) {
// Inflate the layout for this fragment
binding = FragmentSettingsDialogBinding.inflate(inflater, container, false);
binding = KeymapEditorLayoutBinding.inflate(inflater);
init();
return binding.getRoot();
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {

private final MaterialButtonToggleGroup.OnButtonCheckedListener ON_BUTTON_CHECKED_LISTENER = new MaterialButtonToggleGroup.OnButtonCheckedListener() {
@Override
public void onButtonChecked(MaterialButtonToggleGroup group, int checkedId, boolean isChecked) {
if (checkedId == R.id.button_sliders) {
binding.sliders.setVisibility(isChecked ? View.VISIBLE : View.GONE);
} else if (checkedId == R.id.button_shortcuts) {
binding.shortcuts.setVisibility(isChecked ? View.VISIBLE : View.GONE);
} else if (checkedId == R.id.button_misc) {
binding.misc.setVisibility(isChecked ? View.VISIBLE : View.GONE);
}
}
};

public void init() {
binding.sliderDpad.setValue(keymapConfig.dpadRadiusMultiplier);
binding.sliderMouse.setValue(keymapConfig.mouseSensitivity);
binding.sliderScrollSpeed.setValue(keymapConfig.scrollSpeed);
binding.sliderSwipeDelay.setValue(keymapConfig.swipeDelayMs);

binding.swipeDelayText.setText(getString(R.string.swipe_delay_ms, keymapConfig.swipeDelayMs));
binding.sliderSwipeDelay.addOnChangeListener((slider, value, fromUser) -> binding.swipeDelayText.setText(getString(R.string.swipe_delay_ms, (int)value)));
binding.swipeDelayText.setText(context.getString(R.string.swipe_delay_ms, keymapConfig.swipeDelayMs));
binding.sliderSwipeDelay.addOnChangeListener((slider, value, fromUser) -> binding.swipeDelayText.setText(context.getString(R.string.swipe_delay_ms, (int)value)));

binding.mouseDragToggle.setChecked(keymapConfig.ctrlDragMouseGesture);
binding.mouseWheelToggle.setChecked(keymapConfig.ctrlMouseWheelZoom);
Expand All @@ -71,26 +77,13 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
binding.switchProfile.setOnKeyListener(this::onKey);
binding.mouseAimKey.setOnKeyListener(this::onKey);

binding.bottomNavigation.setOnItemSelectedListener(item -> {
setDefaultVisibilty();
int itemId = item.getItemId();
if (itemId == R.id.sliders) {
binding.sliders.setVisibility(View.VISIBLE);
return true;
} else if (itemId == R.id.shortcuts) {
binding.shortcuts.setVisibility(View.VISIBLE);
return true;
} else if (itemId == R.id.misc) {
binding.misc.setVisibility(View.VISIBLE);
return true;
}
return false;
});
binding.toggleButtonGroup.addOnButtonCheckedListener(ON_BUTTON_CHECKED_LISTENER);

mouseAimActions();
loadTouchpadInputSettings();

final int[] pointerModeCodes = {KeymapConfig.POINTER_COMBINED, KeymapConfig.POINTER_OVERLAY, KeymapConfig.POINTER_SYSTEM};
String[] pointerModeNames = getResources().getStringArray(R.array.pointer_modes);
String[] pointerModeNames = context.getResources().getStringArray(R.array.pointer_modes);
pointerModeMap = IntStream.range(0, pointerModeCodes.length)
.boxed()
.collect(Collectors.toMap(k -> pointerModeNames[k], v -> pointerModeCodes[v]));
Expand All @@ -100,17 +93,9 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
binding.pointerMode.setText(entry.getKey());
}
}
((MaterialAutoCompleteTextView)binding.pointerMode).setSimpleItems(pointerModeNames);

setDefaultVisibilty();
binding.sliders.setVisibility(View.VISIBLE);
binding.pointerMode.setSimpleItems(pointerModeNames);
}

private void setDefaultVisibilty() {
binding.sliders.setVisibility(View.GONE);
binding.misc.setVisibility(View.GONE);
binding.shortcuts.setVisibility(View.GONE);
}

private void loadKeyboardShortcuts(){
int pause_resume = keymapConfig.pauseResumeShortcutKey;
Expand Down Expand Up @@ -147,22 +132,22 @@ private void loadModifierKeys() {
binding.switchProfileModifier.setText(keymapConfig.switchProfileShortcutKeyModifier);

final String[] modifierKeys = {KeymapConfig.KEY_CTRL, KeymapConfig.KEY_ALT};
((MaterialAutoCompleteTextView)binding.launchEditorModifier).setSimpleItems(modifierKeys);
((MaterialAutoCompleteTextView)binding.pauseResumeModifier).setSimpleItems(modifierKeys);
((MaterialAutoCompleteTextView)binding.switchProfileModifier).setSimpleItems(modifierKeys);
binding.launchEditorModifier.setSimpleItems(modifierKeys);
binding.pauseResumeModifier.setSimpleItems(modifierKeys);
binding.switchProfileModifier.setSimpleItems(modifierKeys);
}

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

String[] mouseAimActionNames = getResources().getStringArray(R.array.mouse_aim_actions);
((MaterialAutoCompleteTextView)binding.mouseAimAction).setSimpleItems(mouseAimActionNames);
String[] mouseAimActionNames = context.getResources().getStringArray(R.array.mouse_aim_actions);
binding.mouseAimAction.setSimpleItems(mouseAimActionNames);
}

private void loadTouchpadInputSettings() {
final int[] touchpadInputModeCodes = {KeymapConfig.TOUCHPAD_DIRECT, KeymapConfig.TOUCHPAD_RELATIVE, KeymapConfig.TOUCHPAD_DISABLED};
String[] touchpadInputModeNames = getResources().getStringArray(R.array.touchpad_input_modes);
String[] touchpadInputModeNames = context.getResources().getStringArray(R.array.touchpad_input_modes);
touchpadInputModeMap = IntStream.range(0, touchpadInputModeCodes.length)
.boxed()
.collect(Collectors.toMap(k -> touchpadInputModeNames[k], v -> touchpadInputModeCodes[v]));
Expand All @@ -172,7 +157,7 @@ private void loadTouchpadInputSettings() {
binding.touchpadInputMode.setText(entry.getKey());
}
}
((MaterialAutoCompleteTextView)binding.touchpadInputMode).setSimpleItems(touchpadInputModeNames);
binding.touchpadInputMode.setSimpleItems(touchpadInputModeNames);
}

public boolean onKey(View view, int keyCode, KeyEvent event) {
Expand All @@ -182,13 +167,6 @@ public boolean onKey(View view, int keyCode, KeyEvent event) {
return true;
}

@NonNull @Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
BottomSheetDialog dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);
// Expanded bottom sheet dialog by default
dialog.setOnShowListener(d -> ((BottomSheetDialog) d).getBehavior().setState(STATE_EXPANDED));
return dialog;
}

private void saveKeyboardShortcuts() {
if(binding.launchEditor.getText().toString().isEmpty()) binding.launchEditor.setText(" ");
Expand All @@ -211,10 +189,9 @@ private void saveKeyboardShortcuts() {
keymapConfig.switchProfileShortcutKeyModifier = binding.switchProfileModifier.getText().toString();
}

@Override
public void onDestroyView() {
saveKeyboardShortcuts();
keymapConfig.mouseAimToggle = binding.mouseAimAction.getText().toString().equals(getResources().getString(R.string.toggle));
keymapConfig.mouseAimToggle = binding.mouseAimAction.getText().toString().equals(context.getResources().getString(R.string.toggle));
keymapConfig.touchpadInputMode = touchpadInputModeMap.get(binding.touchpadInputMode.getText().toString());
keymapConfig.pointerMode = pointerModeMap.get(binding.pointerMode.getText().toString());

Expand All @@ -234,8 +211,7 @@ public void onDestroyView() {
keymapConfig.dpadRadiusMultiplier = binding.sliderDpad.getValue();

keymapConfig.applySharedPrefs();
RemoteServiceHelper.reloadKeymap(getContext());
RemoteServiceHelper.reloadKeymap(context);
binding = null;
super.onDestroyView();
}
}
Loading

0 comments on commit 20ebbf1

Please sign in to comment.