Skip to content

Commit

Permalink
Adding an option to ignore media keys and let android conume them.
Browse files Browse the repository at this point in the history
  • Loading branch information
twaik committed Jan 13, 2025
1 parent 6299636 commit fc18466
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 24 deletions.
1 change: 1 addition & 0 deletions app/src/main/java/com/termux/x11/LoriePreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ public void onCreatePreferences(@Nullable Bundle savedInstanceState, @Nullable S

setNoActionOptionText(findPreference("volumeDownAction"), "android volume control");
setNoActionOptionText(findPreference("volumeUpAction"), "android volume control");
setNoActionOptionText(findPreference("mediaKeysAction"), "android media control");
}

private void setSummary(CharSequence key, int disabled) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/termux/x11/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public void onReceive(Context context, Intent intent) {
onPreferencesChanged("");
} else if (ACTION_CUSTOM.equals(intent.getAction())) {
android.util.Log.d("ACTION_CUSTOM", "action " + intent.getStringExtra("what"));
mInputHandler.extractUserActionFromPreferences(prefs, intent.getStringExtra("what")).accept(true);
mInputHandler.extractUserActionFromPreferences(prefs, intent.getStringExtra("what")).accept(0, true);
}
}
};
Expand Down
74 changes: 51 additions & 23 deletions app/src/main/java/com/termux/x11/input/TouchInputHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
import java.util.Arrays;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import java.util.function.BiConsumer;

/**
* This class is responsible for handling Touch input from the user. Touch events which manipulate
Expand Down Expand Up @@ -93,12 +93,10 @@ public class TouchInputHandler {
private final MainActivity mActivity;
private final DisplayMetrics mMetrics = new DisplayMetrics();

private final Consumer<Boolean> noAction = (down) -> {};
private Consumer<Boolean> swipeUpAction = noAction;
private Consumer<Boolean> swipeDownAction = noAction;
private Consumer<Boolean> volumeUpAction = noAction;
private Consumer<Boolean> volumeDownAction = noAction;
private Consumer<Boolean> backButtonAction = noAction;
private final BiConsumer<Integer, Boolean> noAction = (key, down) -> {};
private BiConsumer<Integer, Boolean> swipeUpAction = noAction, swipeDownAction = noAction,
volumeUpAction = noAction, volumeDownAction = noAction, backButtonAction = noAction,
mediaKeysAction = noAction;

private static final int KEY_BACK = 158;

Expand Down Expand Up @@ -208,7 +206,6 @@ private TouchInputHandler(MainActivity activity, RenderData renderData,

refreshInputDevices();
((InputManager) mActivity.getSystemService(Context.INPUT_SERVICE)).registerInputDeviceListener(new InputManager.InputDeviceListener() {
/** @noinspection DataFlowIssue*/
@Override
public void onInputDeviceAdded(int deviceId) {
InputDevice dev = InputDevice.getDevice(deviceId);
Expand All @@ -223,7 +220,6 @@ public void onInputDeviceRemoved(int deviceId) {
refreshInputDevices();
}

/** @noinspection DataFlowIssue*/
@Override
public void onInputDeviceChanged(int deviceId) {
InputDevice dev = InputDevice.getDevice(deviceId);
Expand Down Expand Up @@ -468,25 +464,27 @@ public void reloadPreferences(Prefs p) {
volumeUpAction = extractUserActionFromPreferences(p, "volumeUp");
volumeDownAction = extractUserActionFromPreferences(p, "volumeDown");
backButtonAction = extractUserActionFromPreferences(p, "backButton");
mediaKeysAction = extractUserActionFromPreferences(p, "mediaKeys");

if(mTouchpadHandler != null)
mTouchpadHandler.reloadPreferences(p);
}

public Consumer<Boolean> extractUserActionFromPreferences(Prefs p, String name) {
public BiConsumer<Integer, Boolean> extractUserActionFromPreferences(Prefs p, String name) {
LoriePreferences.PrefsProto.Preference pref = p.keys.get(name + "Action");
if (pref == null)
return noAction;

switch(pref.asList().get()) {
case "toggle soft keyboard": return (down) -> { if (down) MainActivity.toggleKeyboardVisibility(mActivity); };
case "toggle additional key bar": return (down) -> { if (down) mActivity.toggleExtraKeys(); };
case "open preferences": return (down) -> { if (down) mActivity.startActivity(new Intent(mActivity, LoriePreferences.class) {{ setAction(Intent.ACTION_MAIN); }}); };
case "release pointer and keyboard capture": return (down) -> { if (down) setCapturingEnabled(false); };
case "toggle fullscreen": return (down) -> { if (down) MainActivity.prefs.fullscreen.put(!MainActivity.prefs.fullscreen.get()); };
case "exit": return (down) -> { if (down) mActivity.finish(); };
case "send volume up": return (down) -> mActivity.getLorieView().sendKeyEvent(0, KEYCODE_VOLUME_UP, down);
case "send volume down": return (down) -> mActivity.getLorieView().sendKeyEvent(0, KEYCODE_VOLUME_DOWN, down);
case "toggle soft keyboard": return (key, down) -> { if (down) MainActivity.toggleKeyboardVisibility(mActivity); };
case "toggle additional key bar": return (key, down) -> { if (down) mActivity.toggleExtraKeys(); };
case "open preferences": return (key, down) -> { if (down) mActivity.startActivity(new Intent(mActivity, LoriePreferences.class) {{ setAction(Intent.ACTION_MAIN); }}); };
case "release pointer and keyboard capture": return (key, down) -> { if (down) setCapturingEnabled(false); };
case "toggle fullscreen": return (key, down) -> { if (down) MainActivity.prefs.fullscreen.put(!MainActivity.prefs.fullscreen.get()); };
case "exit": return (key, down) -> { if (down) mActivity.finish(); };
case "send volume up": return (key, down) -> mActivity.getLorieView().sendKeyEvent(0, KEYCODE_VOLUME_UP, down);
case "send volume down": return (key, down) -> mActivity.getLorieView().sendKeyEvent(0, KEYCODE_VOLUME_DOWN, down);
case "send media action": return (key, down) -> mActivity.getLorieView().sendKeyEvent(0, key, down);
default: return noAction;
}
}
Expand Down Expand Up @@ -572,9 +570,9 @@ private void moveCursorToScreenPoint(float screenX, float screenY) {
/** Processes a (multi-finger) swipe gesture. */
private boolean onSwipe() {
if (mTotalMotionY > mSwipeThreshold)
swipeDownAction.accept(true);
swipeDownAction.accept(0, true);
else if (mTotalMotionY < -mSwipeThreshold)
swipeUpAction.accept(true);
swipeUpAction.accept(0, true);
else
return false;

Expand Down Expand Up @@ -759,6 +757,28 @@ private boolean screenPointLiesOutsideImageBoundary(float screenX, float screenY
}
}

/**
* It is a copy of {@link android.view.KeyEvent#isMediaSessionKey} to be used on Android 30 and below.
* Returns whether this key will be sent to the
* {@link android.media.session.MediaSession.Callback} if not handled.
*/
public static boolean isMediaSessionKey(int keyCode) {
switch (keyCode) {
case KeyEvent.KEYCODE_MEDIA_PLAY:
case KeyEvent.KEYCODE_MEDIA_PAUSE:
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
case KeyEvent.KEYCODE_HEADSETHOOK:
case KeyEvent.KEYCODE_MEDIA_STOP:
case KeyEvent.KEYCODE_MEDIA_NEXT:
case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
case KeyEvent.KEYCODE_MEDIA_REWIND:
case KeyEvent.KEYCODE_MEDIA_RECORD:
case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
return true;
}
return false;
}

public boolean sendKeyEvent(KeyEvent e) {
int k = e.getKeyCode();

Expand All @@ -769,19 +789,27 @@ public boolean sendKeyEvent(KeyEvent e) {
return false;
}

if (isMediaSessionKey(k)) {
if (mediaKeysAction == noAction)
return false;

mediaKeysAction.accept(k, e.getAction() == KeyEvent.ACTION_DOWN);
return true;
}

if (k == KEYCODE_VOLUME_DOWN) {
if (volumeDownAction == noAction)
return false;

volumeDownAction.accept(e.getAction() == KeyEvent.ACTION_DOWN);
volumeDownAction.accept(k, e.getAction() == KeyEvent.ACTION_DOWN);
return true;
}

if (k == KEYCODE_VOLUME_UP) {
if (volumeUpAction == noAction)
return false;

volumeUpAction.accept(e.getAction() == KeyEvent.ACTION_DOWN);
volumeUpAction.accept(k, e.getAction() == KeyEvent.ACTION_DOWN);
return true;
}

Expand All @@ -795,7 +823,7 @@ public boolean sendKeyEvent(KeyEvent e) {
}

if (e.getScanCode() == KEY_BACK && e.getDevice().getKeyboardType() != KEYBOARD_TYPE_ALPHABETIC || e.getScanCode() == 0) {
backButtonAction.accept(e.getAction() == KeyEvent.ACTION_DOWN);
backButtonAction.accept(k, e.getAction() == KeyEvent.ACTION_DOWN);
return true;
}
}
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
<item>@array/userActionsValues</item>
<item>send volume up</item>
</string-array>
<string-array name="userActionsMediaKeyValues">
<item>@array/userActionsValues</item>
<item>send media action</item>
</string-array>
<string-array name="forceOrientationVariants">
<item>auto</item>
<item>portrait</item>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ E.g. <b>META</b> for <i>META key</i>, \n
<string name="pref_notificationTapAction">Notification tap</string>
<string name="pref_notificationButton0Action">Notification first button</string>
<string name="pref_notificationButton1Action">Notification second button</string>
<string name="pref_mediaKeysAction">Media keys</string>

<string name="pref_summary_requiresExactOrCustom">Requires "display resolution mode" to be "exact" or "custom"</string>
<string name="pref_summary_requiresIntercepting">Requires intercepting system shortcuts with Dex mode or with Accessibility service</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,6 @@
<ListPreference app:key="notificationTapAction" app:defaultValue="open preferences" app:entries="@array/userActionsValues" app:entryValues="@array/userActionsValues" />
<ListPreference app:key="notificationButton0Action" app:defaultValue="open preferences" app:entries="@array/userActionsValues" app:entryValues="@array/userActionsValues" />
<ListPreference app:key="notificationButton1Action" app:defaultValue="exit" app:entries="@array/userActionsValues" app:entryValues="@array/userActionsValues" />
<ListPreference app:key="mediaKeysAction" app:defaultValue="no action" app:entries="@array/userActionsMediaKeyValues" app:entryValues="@array/userActionsMediaKeyValues" />
</PreferenceScreen>
</PreferenceScreen>

1 comment on commit fc18466

@twaik
Copy link
Member Author

@twaik twaik commented on fc18466 Jan 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

conume... It is consume... Damn...

Please sign in to comment.