From 20ebbf1feb7605a82675c7fbf6113d1212a452a9 Mon Sep 17 00:00:00 2001 From: Xtr126 <80520774+Xtr126@users.noreply.github.com> Date: Fri, 11 Oct 2024 20:52:32 +0530 Subject: [PATCH] feat: Integrate new layout to editor --- .../xtr/keymapper/activity/MainActivity.java | 5 +- .../xtr/keymapper/editor/EditorActivity.java | 2 + .../java/xtr/keymapper/editor/EditorUI.java | 21 +- .../SettingsFragment.java | 106 ++--- .../res/layout/fragment_settings_dialog.xml | 389 ------------------ .../main/res/layout/keymap_editor_layout.xml | 29 +- 6 files changed, 77 insertions(+), 475 deletions(-) rename app/src/main/java/xtr/keymapper/{fragment => editor}/SettingsFragment.java (69%) delete mode 100644 app/src/main/res/layout/fragment_settings_dialog.xml diff --git a/app/src/main/java/xtr/keymapper/activity/MainActivity.java b/app/src/main/java/xtr/keymapper/activity/MainActivity.java index 034b230f..426bb785 100644 --- a/app/src/main/java/xtr/keymapper/activity/MainActivity.java +++ b/app/src/main/java/xtr/keymapper/activity/MainActivity.java @@ -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; @@ -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 diff --git a/app/src/main/java/xtr/keymapper/editor/EditorActivity.java b/app/src/main/java/xtr/keymapper/editor/EditorActivity.java index c64422fe..2b44ec17 100644 --- a/app/src/main/java/xtr/keymapper/editor/EditorActivity.java +++ b/app/src/main/java/xtr/keymapper/editor/EditorActivity.java @@ -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; @@ -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(); } } diff --git a/app/src/main/java/xtr/keymapper/editor/EditorUI.java b/app/src/main/java/xtr/keymapper/editor/EditorUI.java index 0f3cf1e6..3260cb69 100644 --- a/app/src/main/java/xtr/keymapper/editor/EditorUI.java +++ b/app/src/main/java/xtr/keymapper/editor/EditorUI.java @@ -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); @@ -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(); @@ -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); @@ -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, @@ -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; } @@ -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(); } diff --git a/app/src/main/java/xtr/keymapper/fragment/SettingsFragment.java b/app/src/main/java/xtr/keymapper/editor/SettingsFragment.java similarity index 69% rename from app/src/main/java/xtr/keymapper/fragment/SettingsFragment.java rename to app/src/main/java/xtr/keymapper/editor/SettingsFragment.java index 23ba5cb5..b6401327 100644 --- a/app/src/main/java/xtr/keymapper/fragment/SettingsFragment.java +++ b/app/src/main/java/xtr/keymapper/editor/SettingsFragment.java @@ -1,10 +1,6 @@ -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; @@ -12,49 +8,59 @@ 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 pointerModeMap; private Map 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); @@ -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])); @@ -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; @@ -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])); @@ -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) { @@ -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(" "); @@ -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()); @@ -234,8 +211,7 @@ public void onDestroyView() { keymapConfig.dpadRadiusMultiplier = binding.sliderDpad.getValue(); keymapConfig.applySharedPrefs(); - RemoteServiceHelper.reloadKeymap(getContext()); + RemoteServiceHelper.reloadKeymap(context); binding = null; - super.onDestroyView(); } } diff --git a/app/src/main/res/layout/fragment_settings_dialog.xml b/app/src/main/res/layout/fragment_settings_dialog.xml deleted file mode 100644 index 29255521..00000000 --- a/app/src/main/res/layout/fragment_settings_dialog.xml +++ /dev/null @@ -1,389 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/app/src/main/res/layout/keymap_editor_layout.xml b/app/src/main/res/layout/keymap_editor_layout.xml index e9a97e17..a1e2674b 100644 --- a/app/src/main/res/layout/keymap_editor_layout.xml +++ b/app/src/main/res/layout/keymap_editor_layout.xml @@ -2,11 +2,10 @@ + app:layout_constraintTop_toTopOf="parent"> + app:layout_constraintLeft_toLeftOf="parent" + app:layout_constraintRight_toRightOf="parent"> - - + app:layout_constraintTop_toTopOf="parent"> - - - -