make long-press toolbar pinning optional

This commit is contained in:
Helium314 2024-05-27 20:34:26 +02:00
parent 0f503389b3
commit 388366c242
10 changed files with 98 additions and 58 deletions

View file

@ -10,7 +10,7 @@ android {
applicationId "helium314.keyboard"
minSdkVersion 21
targetSdkVersion 34
versionCode 2000
versionCode 2001
versionName '2.0-alpha1'
ndk {
abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'

View file

@ -11,6 +11,8 @@ import helium314.keyboard.latin.settings.USER_DICTIONARY_SUFFIX
import helium314.keyboard.latin.utils.CUSTOM_LAYOUT_PREFIX
import helium314.keyboard.latin.utils.DeviceProtectedUtils
import helium314.keyboard.latin.utils.DictionaryInfoUtils
import helium314.keyboard.latin.utils.ToolbarKey
import helium314.keyboard.latin.utils.defaultPinnedToolbarPref
import helium314.keyboard.latin.utils.getCustomLayoutFile
import helium314.keyboard.latin.utils.onCustomLayoutFileListChanged
import helium314.keyboard.latin.utils.upgradeToolbarPrefs
@ -39,7 +41,6 @@ fun checkVersionUpgrade(context: Context) {
val oldVersion = prefs.getInt(Settings.PREF_VERSION_CODE, 0)
if (oldVersion == BuildConfig.VERSION_CODE)
return
upgradeToolbarPrefs(prefs)
// clear extracted dictionaries, in case updated version contains newer ones
DictionaryInfoUtils.getCachedDirectoryList(context)?.forEach {
if (!it.isDirectory) return@forEach
@ -73,6 +74,22 @@ fun checkVersionUpgrade(context: Context) {
putString(Settings.PREF_SELECTED_SUBTYPE, selectedSubtype)
}
}
if (oldVersion <= 2000) {
// upgrade pinned toolbar keys pref
val oldPinnedKeysPref = prefs.getString(Settings.PREF_PINNED_TOOLBAR_KEYS, "")!!
val pinnedKeys = oldPinnedKeysPref.split(";").mapNotNull {
try {
ToolbarKey.valueOf(it)
} catch (_: IllegalArgumentException) {
null
}
}
val newPinnedKeysPref = (pinnedKeys.map { "${it.name},true" } + defaultPinnedToolbarPref.split(";"))
.distinctBy { it.split(",").first() }
.joinToString(";")
prefs.edit { putString(Settings.PREF_PINNED_TOOLBAR_KEYS, newPinnedKeysPref) }
}
upgradeToolbarPrefs(prefs)
onCustomLayoutFileListChanged() // just to be sure
prefs.edit { putInt(Settings.PREF_VERSION_CODE, BuildConfig.VERSION_CODE) }
}

View file

@ -71,9 +71,14 @@ public final class PreferencesSettingsFragment extends SubScreenFragment {
R.string.toolbar_keys, (name) -> ToolbarUtilsKt.getToolbarIconByName(name, requireContext()));
return true;
});
findPreference(Settings.PREF_PINNED_TOOLBAR_KEYS).setOnPreferenceClickListener((pref) -> {
DialogUtilsKt.reorderDialog(requireContext(), Settings.PREF_PINNED_TOOLBAR_KEYS, ToolbarUtilsKt.getDefaultPinnedToolbarPref(),
R.string.pinned_toolbar_keys, (name) -> ToolbarUtilsKt.getToolbarIconByName(name, requireContext()));
return true;
});
findPreference(Settings.PREF_CLIPBOARD_TOOLBAR_KEYS).setOnPreferenceClickListener((pref) -> {
DialogUtilsKt.reorderDialog(requireContext(), Settings.PREF_CLIPBOARD_TOOLBAR_KEYS, ToolbarUtilsKt.getDefaultClipboardToolbarPref(),
R.string.toolbar_keys, (name) -> ToolbarUtilsKt.getToolbarIconByName(name, requireContext()));
R.string.clipboard_toolbar_keys, (name) -> ToolbarUtilsKt.getToolbarIconByName(name, requireContext()));
return true;
});
}
@ -89,7 +94,8 @@ public final class PreferencesSettingsFragment extends SubScreenFragment {
if (key == null) return;
switch (key) {
case Settings.PREF_POPUP_KEYS_ORDER, Settings.PREF_SHOW_POPUP_HINTS, Settings.PREF_SHOW_NUMBER_ROW,
Settings.PREF_POPUP_KEYS_LABELS_ORDER, Settings.PREF_TOOLBAR_KEYS, Settings.PREF_CLIPBOARD_TOOLBAR_KEYS
Settings.PREF_POPUP_KEYS_LABELS_ORDER, Settings.PREF_TOOLBAR_KEYS, Settings.PREF_CLIPBOARD_TOOLBAR_KEYS,
Settings.PREF_PINNED_TOOLBAR_KEYS, Settings.PREF_QUICK_PIN_TOOLBAR_KEYS
-> mReloadKeyboard = true;
case Settings.PREF_LOCALIZED_NUMBER_ROW -> KeyboardLayoutSet.onSystemLocaleChanged();
case Settings.PREF_SHOW_HINTS

View file

@ -44,8 +44,6 @@ import helium314.keyboard.latin.utils.ResourceUtils;
import helium314.keyboard.latin.utils.RunInLocaleKt;
import helium314.keyboard.latin.utils.StatsUtils;
import helium314.keyboard.latin.utils.SubtypeSettingsKt;
import helium314.keyboard.latin.utils.ToolbarKey;
import helium314.keyboard.latin.utils.ToolbarUtilsKt;
import java.io.File;
import java.util.ArrayList;
@ -148,6 +146,7 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang
public static final String PREF_USE_SYSTEM_LOCALES = "use_system_locales";
public static final String PREF_URL_DETECTION = "url_detection";
public static final String PREF_DONT_SHOW_MISSING_DICTIONARY_DIALOG = "dont_show_missing_dict_dialog";
public static final String PREF_QUICK_PIN_TOOLBAR_KEYS = "quick_pin_toolbar_keys";
public static final String PREF_PINNED_TOOLBAR_KEYS = "pinned_toolbar_keys";
public static final String PREF_TOOLBAR_KEYS = "toolbar_keys";
public static final String PREF_CLIPBOARD_TOOLBAR_KEYS = "clipboard_toolbar_keys";
@ -486,29 +485,6 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang
}
}
public static ArrayList<ToolbarKey> readPinnedKeys(final SharedPreferences prefs) {
final ArrayList<ToolbarKey> list = new ArrayList<>();
for (final String key : prefs.getString(Settings.PREF_PINNED_TOOLBAR_KEYS, "").split(";")) {
try {
list.add(ToolbarKey.valueOf(key));
} catch (IllegalArgumentException ignored) { } // may happen if toolbar key is removed from app
}
return list;
}
public static void addPinnedKey(final SharedPreferences prefs, final ToolbarKey key) {
final ArrayList<ToolbarKey> keys = readPinnedKeys(prefs);
if (keys.contains(key)) return;
keys.add(key);
prefs.edit().putString(Settings.PREF_PINNED_TOOLBAR_KEYS, ToolbarUtilsKt.toToolbarKeyString(keys)).apply();
}
public static void removePinnedKey(final SharedPreferences prefs, final ToolbarKey key) {
final ArrayList<ToolbarKey> keys = readPinnedKeys(prefs);
keys.remove(key);
prefs.edit().putString(Settings.PREF_PINNED_TOOLBAR_KEYS, ToolbarUtilsKt.toToolbarKeyString(keys)).apply();
}
public static int readMorePopupKeysPref(final SharedPreferences prefs) {
return switch (prefs.getString(Settings.PREF_MORE_POPUP_KEYS, "normal")) {
case "all" -> LocaleKeyboardInfosKt.POPUP_KEYS_ALL;

View file

@ -99,6 +99,7 @@ public class SettingsValues {
public final boolean mEnableEmojiAltPhysicalKey;
public final boolean mIsSplitKeyboardEnabled;
public final float mSplitKeyboardSpacerRelativeWidth;
public final boolean mQuickPinToolbarKeys;
public final int mScreenMetrics;
public final boolean mAddToPersonalDictionary;
public final boolean mUseContactsDictionary;
@ -181,6 +182,7 @@ public class SettingsValues {
mSplitKeyboardSpacerRelativeWidth = mIsSplitKeyboardEnabled
? Math.min(Math.max((displayWidthDp - 600) / 6000f + 0.15f, 0.15f), 0.25f) * prefs.getFloat(Settings.PREF_SPLIT_SPACER_SCALE, DEFAULT_SIZE_SCALE)
: 0f;
mQuickPinToolbarKeys = prefs.getBoolean(Settings.PREF_QUICK_PIN_TOOLBAR_KEYS, false);
mScreenMetrics = Settings.readScreenMetrics(res);
// Compute other readable settings

View file

@ -217,13 +217,13 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
mToolbarExpandKey.getLayoutParams().height *= 0.82; // shrink the whole key a little (drawable not affected)
mToolbarExpandKey.getLayoutParams().width *= 0.82;
for (final ToolbarKey pinnedKey : Settings.readPinnedKeys(prefs)) {
for (final ToolbarKey pinnedKey : ToolbarUtilsKt.getPinnedToolbarKeys(prefs)) {
final ImageButton button = createToolbarKey(context, keyboardAttr, pinnedKey);
button.setLayoutParams(toolbarKeyLayoutParams);
setupKey(button, colors);
mPinnedKeys.addView(button);
final View pinnedKeyInToolbar = mToolbar.findViewWithTag(pinnedKey);
if (pinnedKeyInToolbar != null)
if (pinnedKeyInToolbar != null && Settings.getInstance().getCurrent().mQuickPinToolbarKeys)
pinnedKeyInToolbar.setBackground(mEnabledToolKeyBackground);
}
@ -374,7 +374,7 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
private void onLongClickToolKey(final View view) {
if (!(view.getTag() instanceof ToolbarKey tag)) return;
if (view.getParent() == mPinnedKeys) {
if (view.getParent() == mPinnedKeys || !Settings.getInstance().getCurrent().mQuickPinToolbarKeys) {
final int longClickCode = getCodeForToolbarKeyLongClick(tag);
if (longClickCode != KeyCode.UNSPECIFIED) {
mListener.onCodeInput(longClickCode, Constants.SUGGESTION_STRIP_COORDINATE, Constants.SUGGESTION_STRIP_COORDINATE, false);
@ -384,9 +384,9 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
if (pinnedKeyView == null) {
addKeyToPinnedKeys(tag);
mToolbar.findViewWithTag(tag).setBackground(mEnabledToolKeyBackground);
Settings.addPinnedKey(DeviceProtectedUtils.getSharedPreferences(getContext()), tag);
ToolbarUtilsKt.addPinnedKey(DeviceProtectedUtils.getSharedPreferences(getContext()), tag);
} else {
Settings.removePinnedKey(DeviceProtectedUtils.getSharedPreferences(getContext()), tag);
ToolbarUtilsKt.removePinnedKey(DeviceProtectedUtils.getSharedPreferences(getContext()), tag);
mToolbar.findViewWithTag(tag).setBackground(mDefaultBackground.getConstantState().newDrawable(getResources()));
mPinnedKeys.removeView(pinnedKeyView);
}

View file

@ -114,8 +114,6 @@ enum class ToolbarKey {
val toolbarKeyStrings: Set<String> = entries.mapTo(HashSet()) { it.toString().lowercase(Locale.US) }
fun toToolbarKeyString(keys: Collection<ToolbarKey>) = keys.joinToString(";") { it.name }
val defaultToolbarPref = entries.filterNot { it == CLOSE_HISTORY }.joinToString(";") {
when (it) {
INCOGNITO, AUTOCORRECT, UP, DOWN, ONE_HANDED, FULL_LEFT, FULL_RIGHT, CUT, CLEAR_CLIPBOARD -> "${it.name},false"
@ -123,6 +121,10 @@ val defaultToolbarPref = entries.filterNot { it == CLOSE_HISTORY }.joinToString(
}
}
val defaultPinnedToolbarPref = entries.filterNot { it == CLOSE_HISTORY }.joinToString(";") {
"${it.name},false"
}
val defaultClipboardToolbarPref by lazy {
val default = listOf(ONE_HANDED, UNDO, UP, DOWN, LEFT, RIGHT, CLEAR_CLIPBOARD, COPY, CUT, SELECT_WORD, CLOSE_HISTORY)
val others = entries.filterNot { it in default }
@ -132,6 +134,7 @@ val defaultClipboardToolbarPref by lazy {
/** add missing keys, typically because a new key has been added */
fun upgradeToolbarPrefs(prefs: SharedPreferences) {
upgradeToolbarPref(prefs, Settings.PREF_TOOLBAR_KEYS, defaultToolbarPref)
upgradeToolbarPref(prefs, Settings.PREF_PINNED_TOOLBAR_KEYS, defaultPinnedToolbarPref)
upgradeToolbarPref(prefs, Settings.PREF_CLIPBOARD_TOOLBAR_KEYS, defaultClipboardToolbarPref)
}
@ -159,8 +162,31 @@ private fun upgradeToolbarPref(prefs: SharedPreferences, pref: String, default:
fun getEnabledToolbarKeys(prefs: SharedPreferences) = getEnabledToolbarKeys(prefs, Settings.PREF_TOOLBAR_KEYS, defaultToolbarPref)
fun getPinnedToolbarKeys(prefs: SharedPreferences) = getEnabledToolbarKeys(prefs, Settings.PREF_PINNED_TOOLBAR_KEYS, defaultPinnedToolbarPref)
fun getEnabledClipboardToolbarKeys(prefs: SharedPreferences) = getEnabledToolbarKeys(prefs, Settings.PREF_CLIPBOARD_TOOLBAR_KEYS, defaultClipboardToolbarPref)
fun addPinnedKey(prefs: SharedPreferences, key: ToolbarKey) {
// remove the existing version of this key and add the enabled one after the last currently enabled key
val string = prefs.getString(Settings.PREF_PINNED_TOOLBAR_KEYS, defaultPinnedToolbarPref)!!
val keys = string.split(";").toMutableList()
keys.removeAll { it.startsWith(key.name + ",") }
val lastEnabledIndex = keys.indexOfLast { it.endsWith("true") }
keys.add(lastEnabledIndex + 1, key.name + ",true")
prefs.edit { putString(Settings.PREF_PINNED_TOOLBAR_KEYS, keys.joinToString(";")) }
}
fun removePinnedKey(prefs: SharedPreferences, key: ToolbarKey) {
// just set it to disabled
val string = prefs.getString(Settings.PREF_PINNED_TOOLBAR_KEYS, defaultPinnedToolbarPref)!!
val result = string.split(";").joinToString(";") {
if (it.startsWith(key.name + ","))
key.name + ",false"
else it
}
prefs.edit { putString(Settings.PREF_PINNED_TOOLBAR_KEYS, result) }
}
private fun getEnabledToolbarKeys(prefs: SharedPreferences, pref: String, default: String): List<ToolbarKey> {
val string = prefs.getString(pref, default)!!
return string.split(";").mapNotNull {

View file

@ -207,21 +207,21 @@
<string name="more_keys_strip_description">More keys</string>
<!-- Title for input language & layout selection screen -->
<string name="language_and_layouts_title">Languages &amp; Layouts</string>
<!-- Title of the settings to enable number row -->
<!-- Title of the setting to enable number row -->
<string name="number_row">Number row</string>
<!-- Description of the settings to enable number row -->
<string name="number_row_summary">Always show number row</string>
<!-- Title of the settings to localize number row -->
<!-- Title of the setting to localize number row -->
<string name="localized_number_row">Localize number row</string>
<!-- Description of the settings to localize number row -->
<string name="localized_number_row_summary">Prefer localized over latin numbers</string>
<!-- Title of the settings to show key hints -->
<!-- Title of the setting to show key hints -->
<string name="show_hints">Show key hints</string>
<!-- Description of the settings to show hints -->
<string name="show_hints_summary">Show long-press hints</string>
<!-- Title of the settings to select key hints source -->
<!-- Title of the setting to select key hints source -->
<string name="hint_source">Select hint source</string>
<!-- Title of the settings to set popup key order -->
<!-- Title of the setting to set popup key order -->
<string name="popup_order">Select popup key order</string>
<!-- Names of the popup key classes -->
<string name="popup_keys_number" tools:keep="@string/popup_keys_number">Number row</string>
@ -229,7 +229,7 @@
<string name="popup_keys_language_priority" tools:keep="@string/popup_keys_language_priority">Language (priority)</string>
<string name="popup_keys_layout" tools:keep="@string/popup_keys_layout">Layout</string>
<string name="popup_keys_symbols" tools:keep="@string/popup_keys_symbols">Symbols</string>
<!-- Title of the settings to set toolbar keys -->
<!-- Title of the setting to set toolbar keys -->
<string name="toolbar_keys">Select toolbar keys</string>
<!-- Names of the toolbar keys-->
<string name="copy" tools:keep="@string/copy" translatable="false">@android:string/copy</string>
@ -250,9 +250,15 @@
<string name="undo" tools:keep="@string/undo">Undo</string>
<string name="redo" tools:keep="@string/redo">Redo</string>
<string name="close_history" tools:keep="@string/close_history">Close clipboard history</string>
<!-- Title of the settings to set clipboard toolbar keys -->
<!-- Title of the setting to set clipboard toolbar keys -->
<string name="clipboard_toolbar_keys">Select clipboard toolbar keys</string>
<!-- Title of the settings to show "..." as hints for more functionality on long-press -->
<!-- Title of the setting to set pinned toolbar keys -->
<string name="pinned_toolbar_keys">Select pinned toolbar keys</string>
<!-- Title of the setting to quick-pin toolbar keys -->
<string name="quick_pin_toolbar_keys">Pin toolbar key on long press</string>
<!-- Description of the setting to quick-pin toolbar keys -->
<string name="quick_pin_toolbar_keys_summary">This will disable other long press actions for toolbar keys that are not pinned</string>
<!-- Title of the setting to show "..." as hints for more functionality on long-press -->
<string name="show_popup_hints">Show functional hints</string>
<!-- Description of the show_popup_hints setting -->
<string name="show_popup_hints_summary">Show hints if long-pressing a key triggers additional functionality</string>
@ -264,9 +270,9 @@
<string name="prefs_long_press_symbol_for_numpad">Long press symbols key for numpad</string>
<!-- Title of the setting for reduced distance between keys -->
<string name="prefs_narrow_key_gaps">Narrow key gaps</string>
<!-- Title of the settings for setting keyboard height -->
<!-- Title of the setting for setting keyboard height -->
<string name="prefs_keyboard_height_scale">Keyboard height scale</string>
<!-- Title of the settings for setting bottom padding height -->
<!-- Title of the setting for setting bottom padding height -->
<string name="prefs_bottom_padding_scale">Bottom padding scale</string>
<!-- Description for English (UK) keyboard subtype [CHAR LIMIT=25]
(UK) should be an abbreviation of United Kingdom to fit in the CHAR LIMIT. -->
@ -494,13 +500,13 @@ disposition rather than other common dispositions for Latin languages. [CHAR LIM
<string name="day_or_night_day">Day</string>
<!-- Button for selecting night -->
<string name="day_or_night_night">Night</string>
<!-- Title of the settings for keypress vibration duration [CHAR LIMIT=35] -->
<!-- Title of the setting for keypress vibration duration [CHAR LIMIT=35] -->
<string name="prefs_keypress_vibration_duration_settings">Keypress vibration duration</string>
<!-- Title of the settings for keypress sound volume [CHAR LIMIT=35] -->
<!-- Title of the setting for keypress sound volume [CHAR LIMIT=35] -->
<string name="prefs_keypress_sound_volume_settings">Keypress sound volume</string>
<!-- Title of the settings for key long press delay [CHAR LIMIT=35] -->
<!-- Title of the setting for key long press delay [CHAR LIMIT=35] -->
<string name="prefs_key_longpress_timeout_settings">Key long press delay</string>
<!-- Title of the settings for enabling Emoji palette triggered by the Alt key on physical keyboards [CHAR LIMIT=35] -->
<!-- Title of the setting for enabling Emoji palette triggered by the Alt key on physical keyboards [CHAR LIMIT=35] -->
<string name="prefs_enable_emoji_alt_physical_key">Emoji for physical keyboard</string>
<!-- Description of the settings for enabling Emoji palette triggered by the Alt key on physical keyboards [CHAR LIMIT=64] -->
<string name="prefs_enable_emoji_alt_physical_key_summary">Physical Alt key shows the emoji palette</string>
@ -810,15 +816,15 @@ New dictionary:
<string name="label_pause_key" tools:keep="@string/label_pause_key">Pause</string>
<!-- Label for "Wait" key of phone number keyboard. Must be short to fit on key. 5 chars or less is preferable. [CHAR LIMIT=7]-->
<string name="label_wait_key" tools:keep="@string/label_wait_key">Wait</string>
<!-- Title of the settings for horizontal spacebar swipe gesture -->
<!-- Title of the setting for horizontal spacebar swipe gesture -->
<string name="show_horizontal_space_swipe">Horizontal spacebar swipe gesture</string>
<!-- Title of the settings for vertical spacebar swipe gesture -->
<!-- Title of the setting for vertical spacebar swipe gesture -->
<string name="show_vertical_space_swipe">Vertical spacebar swipe gesture</string>
<!-- Option for no action when (currently only used for swiping the spacebar) -->
<string name="action_none">None</string>
<!-- Option to move the cursor when swiping the spacebar -->
<string name="space_swipe_move_cursor_entry">Move Cursor</string>
<!-- Title of the settings for variable toolbar direction -->
<!-- Title of the setting for variable toolbar direction -->
<string name="var_toolbar_direction">Variable toolbar direction</string>
<!-- Description of the variable toolbar direction setting -->
<string name="var_toolbar_direction_summary">Reverse direction when a right-to-left keyboard subtype is selected</string>

View file

@ -99,10 +99,21 @@
android:key="toolbar_keys"
android:title="@string/toolbar_keys" />
<Preference
android:key="pinned_toolbar_keys"
android:title="@string/pinned_toolbar_keys" />
<Preference
android:key="clipboard_toolbar_keys"
android:title="@string/clipboard_toolbar_keys" />
<SwitchPreference
android:key="quick_pin_toolbar_keys"
android:title="@string/quick_pin_toolbar_keys"
android:summary="@string/quick_pin_toolbar_keys_summary"
android:defaultValue="false"
android:persistent="true" />
<SwitchPreference
android:key="var_toolbar_direction"
android:title="@string/var_toolbar_direction"