Skip to content

Commit

Permalink
#12 - allow to map VOLUME UP/DOWN
Browse files Browse the repository at this point in the history
  • Loading branch information
green-green-avk committed Sep 14, 2020
1 parent 8e40395 commit 49b3ace
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 26 deletions.
12 changes: 6 additions & 6 deletions app/src/main/java/green_green_avk/anotherterm/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ protected void onAfterChange(@NonNull final String key, @Nullable final Object v
}

public final Settings settings = new Settings();
private static Settings sSettings = null;
// private static Settings sSettings = null;

@Nullable
public static Settings getSettings() {
return sSettings;
}
// @Nullable
// public static Settings getSettings() {
// return sSettings;
// }

public ScratchpadManager scratchpadManager = null;

Expand All @@ -87,7 +87,7 @@ public void onCreate() {
super.onCreate();
PluginsManager.init(this);
settings.init(this, PreferenceManager.getDefaultSharedPreferences(this));
sSettings = settings;
// sSettings = settings;
FontsManager.init(this);
FontsManager.setFrom(settings.terminal_font_default_fromfiles);
TermKeyMapManager.init(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@
import green_green_avk.anotherterm.ui.HwKeyMap;

public final class HwKeyMapEditorFragment extends Fragment {
private static final int[] devIdsDict = new int[HwKeyMapTable.DEV_IDS_NUM];
private static final int[] devIdsDict = new int[]{
R.string.label_dev_built_in,
R.string.label_dev_external
};

static {
devIdsDict[HwKeyMapTable.BUILT_IN] = R.string.label_built_in;
devIdsDict[HwKeyMapTable.EXTERNAL] = R.string.label_external;
private int getDevIdLabel(final int devId) {
if (devId < 0 || devId >= devIdsDict.length) return R.string.label_dev_unknown;
return devIdsDict[devId];
}

private final class ToKeycode {
Expand Down Expand Up @@ -121,7 +124,7 @@ public View getView(final int position, final View convertView, final ViewGroup
.inflate(R.layout.hw_key_map_editor_entry, parent, false);
final HwKeyMapTable.Entry entry = keymap.getEntry(position);
v.<TextView>findViewById(R.id.keycode).setText(TermKeyMap.keyCodeToString(entry.keycode));
v.<TextView>findViewById(R.id.devId).setText(devIdsDict[entry.devId]);
v.<TextView>findViewById(R.id.devId).setText(getDevIdLabel(entry.devId));
final Spinner wToKeycode = v.findViewById(R.id.toKeycode);
final List<ToKeycode> al = KeyEvent.isModifierKey(entry.keycode) ?
toKeycodeListForModifiers : toKeycodeList;
Expand Down
20 changes: 15 additions & 5 deletions app/src/main/java/green_green_avk/anotherterm/HwKeyMapManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.util.SparseIntArray;
import android.view.InputDevice;
import android.view.KeyCharacterMap;
Expand Down Expand Up @@ -66,20 +67,29 @@ public static void set(@NonNull final HwKeyMap hwKeyMap) {
else throw new IllegalArgumentException("Unable to process this HwKeyMap type");
}

public static boolean isVirtual(@NonNull final KeyEvent event) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
return event.getDevice().isVirtual();
return event.getDeviceId() == KeyCharacterMap.VIRTUAL_KEYBOARD;
}

public static boolean isExternal(@NonNull final KeyEvent event) {
// We can only guess before API 29.
return event.getDeviceId() > KeyCharacterMap.SPECIAL_FUNCTION;
}

public static boolean isBypassKey(@NonNull final KeyEvent event) {
if ((event.getSource() & InputDevice.SOURCE_ANY & (
InputDevice.SOURCE_MOUSE
| InputDevice.SOURCE_STYLUS
| InputDevice.SOURCE_TRACKBALL
)) != 0) return true; // Mouse right & middle buttons...
if (event.getDeviceId() == KeyCharacterMap.VIRTUAL_KEYBOARD) return event.isSystem();
if (event.getDeviceId() <= KeyCharacterMap.SPECIAL_FUNCTION)
if (isVirtual(event)) return event.isSystem();
if (!isExternal(event))
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_HOME:
case KeyEvent.KEYCODE_VOLUME_UP: // Font size.
case KeyEvent.KEYCODE_VOLUME_DOWN: // Font size.
case KeyEvent.KEYCODE_POWER:
return true; // Don't prevent default behavior.
return true; // Just in case.
}
return false;
}
Expand Down
10 changes: 4 additions & 6 deletions app/src/main/java/green_green_avk/anotherterm/HwKeyMapTable.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package green_green_avk.anotherterm;

import android.util.SparseIntArray;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;

import androidx.annotation.NonNull;
Expand All @@ -11,18 +10,17 @@
public final class HwKeyMapTable extends HwKeyMap {
public static final int BUILT_IN = 0;
public static final int EXTERNAL = 1;
public static final int DEV_IDS_NUM = 2;

private final SparseIntArray map = new SparseIntArray();

private int key(final int keycode, final int devId) {
return (keycode & 0xFFFF) | ((devId & 0xFFFF) << 16);
return (keycode & 0xFFFF) | (devId << 16);
}

@Override
public int getDevId(@NonNull final KeyEvent event) {
if (event.getDeviceId() == KeyCharacterMap.VIRTUAL_KEYBOARD) return -1; // Of no use.
return event.getDeviceId() <= KeyCharacterMap.SPECIAL_FUNCTION ? BUILT_IN : EXTERNAL;
if (HwKeyMapManager.isVirtual(event)) return -1; // Of no use.
return HwKeyMapManager.isExternal(event) ? EXTERNAL : BUILT_IN;
}

@Override
Expand All @@ -44,7 +42,7 @@ static final class Entry {

private Entry(final int key, final int toKeycode) {
keycode = key & 0xFFFF;
devId = key >>> 16;
devId = key >> 16;
this.toKeycode = toKeycode;
}
}
Expand Down
9 changes: 5 additions & 4 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,6 @@
<string name="note_hw_key_map"><![CDATA[
<p>Please, don\'t hesitate report anything you have doubts about:
it\'s an experimental feature.</p>
<p><kbd>VOLUME_UP</kbd> and <kbd>VOLUME_DOWN</kbd> buttons
are already used for font size adjustment and thus cannot be mapped.</p>
<ul>
<li>\"Block\" means \"Completely turn off a key\"</li>
<li>\"Bypass\" means \"Bypass as is and use to determine the final key code only,
Expand All @@ -271,10 +269,13 @@
<p>Android own modifier flags are used by default
but in some cases they could be broken by IME.
Mapping a modifier key to itself could help in this case.</p>
<p><b>Note:</b> This application own screen keyboard is not affected by and does not affect
Android soft input / hardware keyboard modifier keys state.</p>
]]></string>
<string name="label_press_a_key_to_add">Press a key to add</string>
<string name="label_built_in">Built-in</string>
<string name="label_external">External</string>
<string name="label_dev_unknown">Unknown</string>
<string name="label_dev_built_in">Built-in</string>
<string name="label_dev_external">External</string>
<string name="label_key_block">Block</string>
<string name="label_key_bypass">Bypass</string>
</resources>

0 comments on commit 49b3ace

Please sign in to comment.