|
| 1 | +package com.sparkleseditor.components.virtualkeys; |
| 2 | + |
| 3 | +import android.text.TextUtils; |
| 4 | +import androidx.annotation.NonNull; |
| 5 | +import androidx.annotation.Nullable; |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.stream.Collectors; |
| 8 | +import org.json.JSONException; |
| 9 | +import org.json.JSONObject; |
| 10 | + |
| 11 | +public class VirtualKeyButton { |
| 12 | + |
| 13 | + /** |
| 14 | + * The key name for the name of the extra key if using a dict to define the extra key. {key: name, |
| 15 | + * ...} |
| 16 | + */ |
| 17 | + public static final String KEY_KEY_NAME = "key"; |
| 18 | + |
| 19 | + /** |
| 20 | + * The key name for the macro value of the extra key if using a dict to define the extra key. |
| 21 | + * {macro: value, ...} |
| 22 | + */ |
| 23 | + public static final String KEY_MACRO = "macro"; |
| 24 | + |
| 25 | + /** |
| 26 | + * The key name for the alternate display name of the extra key if using a dict to define the |
| 27 | + * extra key. {display: name, ...} |
| 28 | + */ |
| 29 | + public static final String KEY_DISPLAY_NAME = "display"; |
| 30 | + |
| 31 | + /** |
| 32 | + * The key name for the nested dict to define popup extra key info if using a dict to define the |
| 33 | + * extra key. {popup: {key: name, ...}, ...} |
| 34 | + */ |
| 35 | + public static final String KEY_POPUP = "popup"; |
| 36 | + |
| 37 | + /** |
| 38 | + * The key that will be sent to the terminal, either a control character, like defined in {@link |
| 39 | + * VirtualKeysConstants#PRIMARY_KEY_CODES_FOR_STRINGS} (LEFT, RIGHT, PGUP...) or some text. |
| 40 | + */ |
| 41 | + private final String key; |
| 42 | + |
| 43 | + /** If the key is a macro, i.e. a sequence of keys separated by space. */ |
| 44 | + private final boolean macro; |
| 45 | + |
| 46 | + /** The text that will be displayed on the button. */ |
| 47 | + private final String display; |
| 48 | + |
| 49 | + /** |
| 50 | + * The {@link VirtualKeyButton} containing the information of the popup button (triggered by swipe |
| 51 | + * up). |
| 52 | + */ |
| 53 | + @Nullable private final VirtualKeyButton popup; |
| 54 | + |
| 55 | + /** |
| 56 | + * Initialize a {@link VirtualKeyButton}. |
| 57 | + * |
| 58 | + * @param config The {@link JSONObject} containing the info to create the {@link |
| 59 | + * VirtualKeyButton}. |
| 60 | + * @param extraKeyDisplayMap The {@link VirtualKeysConstants.VirtualKeyDisplayMap} that defines |
| 61 | + * the display text mapping for the keys if a custom value is not defined by {@link |
| 62 | + * #KEY_DISPLAY_NAME}. |
| 63 | + * @param extraKeyAliasMap The {@link VirtualKeysConstants.VirtualKeyDisplayMap} that defines the |
| 64 | + * aliases for the actual key names. |
| 65 | + */ |
| 66 | + public VirtualKeyButton( |
| 67 | + @NonNull JSONObject config, |
| 68 | + @NonNull VirtualKeysConstants.VirtualKeyDisplayMap extraKeyDisplayMap, |
| 69 | + @NonNull VirtualKeysConstants.VirtualKeyDisplayMap extraKeyAliasMap) |
| 70 | + throws JSONException { |
| 71 | + this(config, null, extraKeyDisplayMap, extraKeyAliasMap); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Initialize a {@link VirtualKeyButton}. |
| 76 | + * |
| 77 | + * @param config The {@link JSONObject} containing the info to create the {@link |
| 78 | + * VirtualKeyButton}. |
| 79 | + * @param popup The {@link VirtualKeyButton} optional {@link #popup} button. |
| 80 | + * @param extraKeyDisplayMap The {@link VirtualKeysConstants.VirtualKeyDisplayMap} that defines |
| 81 | + * the display text mapping for the keys if a custom value is not defined by {@link |
| 82 | + * #KEY_DISPLAY_NAME}. |
| 83 | + * @param extraKeyAliasMap The {@link VirtualKeysConstants.VirtualKeyDisplayMap} that defines the |
| 84 | + * aliases for the actual key names. |
| 85 | + */ |
| 86 | + public VirtualKeyButton( |
| 87 | + @NonNull JSONObject config, |
| 88 | + @Nullable VirtualKeyButton popup, |
| 89 | + @NonNull VirtualKeysConstants.VirtualKeyDisplayMap extraKeyDisplayMap, |
| 90 | + @NonNull VirtualKeysConstants.VirtualKeyDisplayMap extraKeyAliasMap) |
| 91 | + throws JSONException { |
| 92 | + String keyFromConfig = getStringFromJson(config, KEY_KEY_NAME); |
| 93 | + String macroFromConfig = getStringFromJson(config, KEY_MACRO); |
| 94 | + String[] keys; |
| 95 | + if (keyFromConfig != null && macroFromConfig != null) { |
| 96 | + throw new JSONException( |
| 97 | + "Both key and macro can't be set for the same key. key: \"" |
| 98 | + + keyFromConfig |
| 99 | + + "\", macro: \"" |
| 100 | + + macroFromConfig |
| 101 | + + "\""); |
| 102 | + } else if (keyFromConfig != null) { |
| 103 | + keys = new String[] {keyFromConfig}; |
| 104 | + this.macro = false; |
| 105 | + } else if (macroFromConfig != null) { |
| 106 | + keys = macroFromConfig.split(" "); |
| 107 | + this.macro = true; |
| 108 | + } else { |
| 109 | + throw new JSONException("All keys have to specify either key or macro"); |
| 110 | + } |
| 111 | + |
| 112 | + for (int i = 0; i < keys.length; i++) { |
| 113 | + keys[i] = replaceAlias(extraKeyAliasMap, keys[i]); |
| 114 | + } |
| 115 | + |
| 116 | + this.key = TextUtils.join(" ", keys); |
| 117 | + |
| 118 | + String displayFromConfig = getStringFromJson(config, KEY_DISPLAY_NAME); |
| 119 | + if (displayFromConfig != null) { |
| 120 | + this.display = displayFromConfig; |
| 121 | + } else { |
| 122 | + this.display = |
| 123 | + Arrays.stream(keys) |
| 124 | + .map(key -> extraKeyDisplayMap.get(key, key)) |
| 125 | + .collect(Collectors.joining(" ")); |
| 126 | + } |
| 127 | + |
| 128 | + this.popup = popup; |
| 129 | + } |
| 130 | + |
| 131 | + public String getStringFromJson(@NonNull JSONObject config, @NonNull String key) { |
| 132 | + try { |
| 133 | + return config.getString(key); |
| 134 | + } catch (JSONException e) { |
| 135 | + return null; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + /** Replace the alias with its actual key name if found in extraKeyAliasMap. */ |
| 140 | + public static String replaceAlias( |
| 141 | + @NonNull VirtualKeysConstants.VirtualKeyDisplayMap extraKeyAliasMap, String key) { |
| 142 | + return extraKeyAliasMap.get(key, key); |
| 143 | + } |
| 144 | + |
| 145 | + /** Get {@link #key}. */ |
| 146 | + public String getKey() { |
| 147 | + return key; |
| 148 | + } |
| 149 | + |
| 150 | + /** Check whether a {@link #macro} is defined or not. */ |
| 151 | + public boolean isMacro() { |
| 152 | + return macro; |
| 153 | + } |
| 154 | + |
| 155 | + /** Get {@link #display}. */ |
| 156 | + public String getDisplay() { |
| 157 | + return display; |
| 158 | + } |
| 159 | + |
| 160 | + /** Get {@link #popup}. */ |
| 161 | + @Nullable |
| 162 | + public VirtualKeyButton getPopup() { |
| 163 | + return popup; |
| 164 | + } |
| 165 | +} |
0 commit comments