use enum class for toolbar keys

This commit is contained in:
Helium314 2023-12-28 20:32:29 +01:00
parent 1fd0ed4464
commit 26168b0e09
5 changed files with 101 additions and 106 deletions

View file

@ -24,13 +24,9 @@ import org.dslul.openboard.inputmethod.latin.common.ColorType
import org.dslul.openboard.inputmethod.latin.common.Constants
import org.dslul.openboard.inputmethod.latin.settings.Settings
import org.dslul.openboard.inputmethod.latin.utils.ResourceUtils
import org.dslul.openboard.inputmethod.latin.utils.TAG_CLEAR_CLIPBOARD
import org.dslul.openboard.inputmethod.latin.utils.TAG_COPY
import org.dslul.openboard.inputmethod.latin.utils.TAG_LEFT
import org.dslul.openboard.inputmethod.latin.utils.TAG_RIGHT
import org.dslul.openboard.inputmethod.latin.utils.TAG_SELECT_ALL
import org.dslul.openboard.inputmethod.latin.utils.ToolbarKey
import org.dslul.openboard.inputmethod.latin.utils.createToolbarKey
import org.dslul.openboard.inputmethod.latin.utils.getCodeForTag
import org.dslul.openboard.inputmethod.latin.utils.getCodeForToolbarKey
class ClipboardHistoryView @JvmOverloads constructor(
context: Context,
@ -67,8 +63,8 @@ class ClipboardHistoryView @JvmOverloads constructor(
spacebarBackground = Settings.getInstance().current.mColors.selectAndColorDrawable(keyboardViewAttr, ColorType.SPACE_BAR_BACKGROUND)
keyboardViewAttr.recycle()
val keyboardAttr = context.obtainStyledAttributes(attrs, R.styleable.Keyboard, defStyle, R.style.SuggestionStripView)
val toolbarKeyTags = listOf(TAG_LEFT, TAG_RIGHT, TAG_COPY, TAG_SELECT_ALL, TAG_CLEAR_CLIPBOARD)
toolbarKeyTags.forEach { toolbarKeys.add(createToolbarKey(context, keyboardAttr, it)) }
listOf(ToolbarKey.LEFT, ToolbarKey.RIGHT, ToolbarKey.COPY, ToolbarKey.SELECT_ALL, ToolbarKey.CLEAR_CLIPBOARD)
.forEach { toolbarKeys.add(createToolbarKey(context, keyboardAttr, it)) }
keyboardAttr.recycle()
}
@ -226,13 +222,13 @@ class ClipboardHistoryView @JvmOverloads constructor(
}
}
val tag = view.tag
if (tag is String) {
val code = getCodeForTag(tag)
if (tag is ToolbarKey) {
val code = getCodeForToolbarKey(tag)
if (code != null) {
keyboardActionListener?.onCodeInput(code, Constants.NOT_A_COORDINATE, Constants.NOT_A_COORDINATE, false)
return
}
if (tag == TAG_CLEAR_CLIPBOARD)
if (tag == ToolbarKey.CLEAR_CLIPBOARD)
clipboardHistoryManager?.clearHistory()
}
}

View file

@ -37,11 +37,11 @@ import org.dslul.openboard.inputmethod.latin.utils.JniUtils;
import org.dslul.openboard.inputmethod.latin.utils.ResourceUtils;
import org.dslul.openboard.inputmethod.latin.utils.RunInLocale;
import org.dslul.openboard.inputmethod.latin.utils.StatsUtils;
import org.dslul.openboard.inputmethod.latin.utils.ToolbarKey;
import org.dslul.openboard.inputmethod.latin.utils.ToolbarUtilsKt;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.locks.ReentrantLock;
@ -464,23 +464,27 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang
prefs.edit().putString(PREF_PINNED_CLIPS, clips).apply();
}
public static List<String> readPinnedKeys(final SharedPreferences prefs) {
final String pinnedKeysString = prefs.getString(Settings.PREF_PINNED_KEYS, "");
if (pinnedKeysString.isEmpty())
return new ArrayList<>();
return Arrays.asList(pinnedKeysString.split(";"));
public static ArrayList<ToolbarKey> readPinnedKeys(final SharedPreferences prefs) {
final ArrayList<ToolbarKey> list = new ArrayList<>();
for (final String key : prefs.getString(Settings.PREF_PINNED_KEYS, "").split(";")) {
try {
list.add(ToolbarKey.valueOf(key));
} catch (IllegalArgumentException e) { } // may happen if toolbar key is removed from app
}
return list;
}
public static void addPinnedKey(final SharedPreferences prefs, final String key) {
final LinkedHashSet<String> keys = new LinkedHashSet<>(readPinnedKeys(prefs));
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_KEYS, String.join(";", keys)).apply();
prefs.edit().putString(Settings.PREF_PINNED_KEYS, ToolbarUtilsKt.toToolbarKeyString(keys)).apply();
}
public static void removePinnedKey(final SharedPreferences prefs, final String key) {
final LinkedHashSet<String> keys = new LinkedHashSet<>(readPinnedKeys(prefs));
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_KEYS, String.join(";", keys)).apply();
prefs.edit().putString(Settings.PREF_PINNED_KEYS, ToolbarUtilsKt.toToolbarKeyString(keys)).apply();
}
public static int readMoreMoreKeysPref(final SharedPreferences prefs) {

View file

@ -29,6 +29,7 @@ import org.dslul.openboard.inputmethod.latin.utils.AsyncResultHolder;
import org.dslul.openboard.inputmethod.latin.utils.MoreKeysUtilsKt;
import org.dslul.openboard.inputmethod.latin.utils.ScriptUtils;
import org.dslul.openboard.inputmethod.latin.utils.TargetPackageInfoGetterTask;
import org.dslul.openboard.inputmethod.latin.utils.ToolbarKey;
import java.util.Arrays;
import java.util.List;
@ -108,7 +109,7 @@ public class SettingsValues {
public final boolean mCustomNavBarColor;
public final float mKeyboardHeightScale;
public final boolean mUrlDetectionEnabled;
public final List<String> mPinnedKeys;
public final List<ToolbarKey> mPinnedKeys;
public final float mBottomPaddingScale;
// From the input box

View file

@ -58,6 +58,7 @@ import org.dslul.openboard.inputmethod.latin.settings.SettingsValues;
import org.dslul.openboard.inputmethod.latin.suggestions.MoreSuggestionsView.MoreSuggestionsListener;
import org.dslul.openboard.inputmethod.latin.utils.DeviceProtectedUtils;
import org.dslul.openboard.inputmethod.latin.utils.DialogUtils;
import org.dslul.openboard.inputmethod.latin.utils.ToolbarKey;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
@ -78,12 +79,12 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
public static boolean DEBUG_SUGGESTIONS;
private static final float DEBUG_INFO_TEXT_SIZE_IN_DIP = 6.5f;
// tags of keys to be added to toolbar, in order (all tags must be considered in getStyleableIconId)
private static final String[] toolbarKeyTags = new String[] {TAG_VOICE, TAG_CLIPBOARD,
TAG_SELECT_ALL, TAG_COPY, TAG_UNDO, TAG_REDO, TAG_ONE_HANDED, TAG_SETTINGS,
TAG_LEFT, TAG_RIGHT, TAG_UP, TAG_DOWN};
private static final ToolbarKey[] toolbarKeys = new ToolbarKey[] {ToolbarKey.VOICE, ToolbarKey.CLIPBOARD,
ToolbarKey.SELECT_ALL, ToolbarKey.COPY, ToolbarKey.UNDO, ToolbarKey.REDO, ToolbarKey.ONE_HANDED,
ToolbarKey.SETTINGS, ToolbarKey.LEFT, ToolbarKey.RIGHT, ToolbarKey.UP, ToolbarKey.DOWN};
private final ViewGroup mSuggestionsStrip;
private final ImageButton mToolbarKey;
private final ImageButton mToolbarExpandKey;
private final Drawable mIncognitoIcon;
private final Drawable mToolbarArrowIcon;
private final Drawable mBinIcon;
@ -149,7 +150,7 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
inflater.inflate(R.layout.suggestions_strip, this);
mSuggestionsStrip = findViewById(R.id.suggestions_strip);
mToolbarKey = findViewById(R.id.suggestions_strip_toolbar_key);
mToolbarExpandKey = findViewById(R.id.suggestions_strip_toolbar_key);
mStripVisibilityGroup = new StripVisibilityGroup(this, mSuggestionsStrip);
mPinnedKeys = findViewById(R.id.pinned_keys);
mToolbar = findViewById(R.id.toolbar);
@ -190,31 +191,32 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
getResources().getDimensionPixelSize(R.dimen.config_suggestions_strip_edge_key_width),
LinearLayout.LayoutParams.MATCH_PARENT
);
for (final String tag : toolbarKeyTags) {
final ImageButton button = createToolbarKey(context, keyboardAttr, tag);
for (final ToolbarKey key : toolbarKeys) {
final ImageButton button = createToolbarKey(context, keyboardAttr, key);
button.setLayoutParams(toolbarKeyLayoutParams);
setupKey(button, colors);
mToolbar.addView(button);
}
keyboardAttr.recycle();
final int toolbarHeight = Math.min(mToolbarKey.getLayoutParams().height, (int) getResources().getDimension(R.dimen.config_suggestions_strip_height));
mToolbarKey.getLayoutParams().height = toolbarHeight;
mToolbarKey.getLayoutParams().width = toolbarHeight; // we want it square
colors.setBackground(mToolbarKey, ColorType.SUGGESTION_BACKGROUND);
mDefaultBackground = mToolbarKey.getBackground();
final int toolbarHeight = Math.min(mToolbarExpandKey.getLayoutParams().height, (int) getResources().getDimension(R.dimen.config_suggestions_strip_height));
mToolbarExpandKey.getLayoutParams().height = toolbarHeight;
mToolbarExpandKey.getLayoutParams().width = toolbarHeight; // we want it square
colors.setBackground(mToolbarExpandKey, ColorType.SUGGESTION_BACKGROUND);
mDefaultBackground = mToolbarExpandKey.getBackground();
mEnabledToolKeyBackground.setColors(new int[] {colors.get(ColorType.TOOL_BAR_KEY_ENABLED_BACKGROUND) | 0xFF000000, Color.TRANSPARENT}); // ignore alpha on accent color
mEnabledToolKeyBackground.setGradientType(GradientDrawable.RADIAL_GRADIENT);
mEnabledToolKeyBackground.setGradientRadius(mToolbarKey.getLayoutParams().height / 2f); // nothing else has a usable height at this state
mEnabledToolKeyBackground.setGradientRadius(mToolbarExpandKey.getLayoutParams().height / 2f); // nothing else has a usable height at this state
mToolbarKey.setOnClickListener(this);
mToolbarKey.setImageDrawable(Settings.getInstance().getCurrent().mIncognitoModeEnabled ? mIncognitoIcon : mToolbarArrowIcon);
colors.setColor(mToolbarKey, ColorType.TOOL_BAR_EXPAND_KEY);
mToolbarKey.setBackground(new ShapeDrawable(new OvalShape())); // ShapeDrawable color is black, need src_atop filter
mToolbarKey.getBackground().setColorFilter(colors.get(ColorType.TOOL_BAR_EXPAND_KEY_BACKGROUND), PorterDuff.Mode.SRC_ATOP);
mToolbarKey.getLayoutParams().height *= 0.82; // shrink the whole key a little (drawable not affected)
mToolbarKey.getLayoutParams().width *= 0.82;
mToolbarExpandKey.setOnClickListener(this);
mToolbarExpandKey.setImageDrawable(Settings.getInstance().getCurrent().mIncognitoModeEnabled ? mIncognitoIcon : mToolbarArrowIcon);
colors.setColor(mToolbarExpandKey, ColorType.TOOL_BAR_EXPAND_KEY);
mToolbarExpandKey.setBackground(new ShapeDrawable(new OvalShape())); // ShapeDrawable color is black, need src_atop filter
mToolbarExpandKey.getBackground().setColorFilter(colors.get(ColorType.TOOL_BAR_EXPAND_KEY_BACKGROUND), PorterDuff.Mode.SRC_ATOP);
mToolbarExpandKey.getLayoutParams().height *= 0.82; // shrink the whole key a little (drawable not affected)
mToolbarExpandKey.getLayoutParams().width *= 0.82;
for (final String pinnedKey : Settings.getInstance().getCurrent().mPinnedKeys) {
for (final ToolbarKey pinnedKey : Settings.getInstance().getCurrent().mPinnedKeys) {
mToolbar.findViewWithTag(pinnedKey).setBackground(mEnabledToolKeyBackground);
addKeyToPinnedKeys(pinnedKey);
}
@ -234,19 +236,19 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
final int visibility = shouldBeVisible ? VISIBLE : (isFullscreenMode ? GONE : INVISIBLE);
setVisibility(visibility);
final SettingsValues currentSettingsValues = Settings.getInstance().getCurrent();
mToolbar.findViewWithTag(TAG_VOICE).setVisibility(currentSettingsValues.mShowsVoiceInputKey ? VISIBLE : GONE);
final View pinnedVoiceKey = mPinnedKeys.findViewWithTag(TAG_VOICE);
mToolbar.findViewWithTag(ToolbarKey.VOICE).setVisibility(currentSettingsValues.mShowsVoiceInputKey ? VISIBLE : GONE);
final View pinnedVoiceKey = mPinnedKeys.findViewWithTag(ToolbarKey.VOICE);
if (pinnedVoiceKey != null)
pinnedVoiceKey.setVisibility(currentSettingsValues.mShowsVoiceInputKey ? VISIBLE : GONE);
mToolbarKey.setImageDrawable(currentSettingsValues.mIncognitoModeEnabled ? mIncognitoIcon : mToolbarArrowIcon);
mToolbarKey.setScaleX(mToolbarContainer.getVisibility() != VISIBLE ? 1f : -1f);
mToolbarExpandKey.setImageDrawable(currentSettingsValues.mIncognitoModeEnabled ? mIncognitoIcon : mToolbarArrowIcon);
mToolbarExpandKey.setScaleX(mToolbarContainer.getVisibility() != VISIBLE ? 1f : -1f);
// hide toolbar and pinned keys if device is locked
final KeyguardManager km = (KeyguardManager) getContext().getSystemService(Context.KEYGUARD_SERVICE);
final boolean hideClipboard = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1
? km.isDeviceLocked()
: km.isKeyguardLocked();
mToolbarKey.setVisibility(hideClipboard ? GONE : VISIBLE);
mToolbarExpandKey.setVisibility(hideClipboard ? GONE : VISIBLE);
mPinnedKeys.setVisibility(hideClipboard ? GONE : VISIBLE);
}
@ -340,10 +342,10 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
}
private void onLongClickToolKey(final View view) {
if (TAG_CLIPBOARD.equals(view.getTag()) && view.getParent() == mPinnedKeys) {
if (ToolbarKey.CLIPBOARD == view.getTag() && view.getParent() == mPinnedKeys) {
onLongClickClipboardKey(); // long click pinned clipboard key
} else if (view.getParent() == mToolbar) {
final String tag = (String) view.getTag();
final ToolbarKey tag = (ToolbarKey) view.getTag();
final View pinnedKeyView = mPinnedKeys.findViewWithTag(tag);
if (pinnedKeyView == null) {
addKeyToPinnedKeys(tag);
@ -602,14 +604,14 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
public void onClick(final View view) {
AudioAndHapticFeedbackManager.getInstance().performHapticAndAudioFeedback(Constants.CODE_UNSPECIFIED, this);
final Object tag = view.getTag();
if (tag instanceof String) {
final Integer code = getCodeForTag((String) tag);
if (tag instanceof ToolbarKey) {
final Integer code = getCodeForToolbarKey((ToolbarKey) tag);
if (code != null) {
mListener.onCodeInput(code, Constants.SUGGESTION_STRIP_COORDINATE, Constants.SUGGESTION_STRIP_COORDINATE, false);
return;
}
}
if (view == mToolbarKey) {
if (view == mToolbarExpandKey) {
setToolbarVisibility(mToolbarContainer.getVisibility() != VISIBLE);
}
@ -649,11 +651,11 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
mSuggestionsStrip.setVisibility(VISIBLE);
mPinnedKeys.setVisibility(VISIBLE);
}
mToolbarKey.setScaleX(visible ? -1f : 1f);
mToolbarExpandKey.setScaleX(visible ? -1f : 1f);
}
private void addKeyToPinnedKeys(final String pinnedKey) {
final ImageButton original = (ImageButton) mToolbar.findViewWithTag(pinnedKey);
private void addKeyToPinnedKeys(final ToolbarKey pinnedKey) {
final ImageButton original = mToolbar.findViewWithTag(pinnedKey);
if (original == null) return;
final ImageButton copy = new ImageButton(getContext(), null, R.attr.suggestionWordStyle);
copy.setTag(pinnedKey);

View file

@ -7,13 +7,15 @@ import android.widget.ImageView
import org.dslul.openboard.inputmethod.latin.R
import org.dslul.openboard.inputmethod.latin.common.Constants.*
import org.dslul.openboard.inputmethod.latin.settings.Settings
import org.dslul.openboard.inputmethod.latin.utils.ToolbarKey.*
import java.util.EnumSet
fun createToolbarKey(context: Context, keyboardAttr: TypedArray, tag: String): ImageButton {
fun createToolbarKey(context: Context, keyboardAttr: TypedArray, key: ToolbarKey): ImageButton {
val button = ImageButton(context, null, R.attr.suggestionWordStyle)
button.scaleType = ImageView.ScaleType.CENTER
button.tag = tag
val icon = keyboardAttr.getDrawable(getStyleableIconId(tag))
if (tag == TAG_LEFT || tag == TAG_RIGHT || tag == TAG_UP || tag == TAG_DOWN) {
button.tag = key
val icon = keyboardAttr.getDrawable(getStyleableIconId(key))
if (key == LEFT || key == RIGHT || key == UP || key == DOWN) {
// arrows look a little awkward when not scaled
button.scaleX = 1.2f
button.scaleY = 1.2f
@ -22,50 +24,40 @@ fun createToolbarKey(context: Context, keyboardAttr: TypedArray, tag: String): I
return button
}
fun getCodeForTag(tag: String) = when (tag) {
TAG_VOICE -> CODE_SHORTCUT
TAG_SETTINGS -> CODE_SETTINGS
TAG_CLIPBOARD -> CODE_CLIPBOARD
TAG_SELECT_ALL -> CODE_SELECT_ALL
TAG_COPY -> CODE_COPY
TAG_ONE_HANDED -> if (Settings.getInstance().current.mOneHandedModeEnabled) CODE_START_ONE_HANDED_MODE else CODE_STOP_ONE_HANDED_MODE
TAG_LEFT -> CODE_LEFT
TAG_RIGHT -> CODE_RIGHT
TAG_UP -> CODE_UP
TAG_DOWN -> CODE_DOWN
TAG_UNDO -> CODE_UNDO
TAG_REDO -> CODE_REDO
TAG_CLEAR_CLIPBOARD -> null // not managed via code input
else -> null
fun getCodeForToolbarKey(key: ToolbarKey) = when (key) {
VOICE -> CODE_SHORTCUT
SETTINGS -> CODE_SETTINGS
CLIPBOARD -> CODE_CLIPBOARD
SELECT_ALL -> CODE_SELECT_ALL
COPY -> CODE_COPY
ONE_HANDED -> if (Settings.getInstance().current.mOneHandedModeEnabled) CODE_START_ONE_HANDED_MODE else CODE_STOP_ONE_HANDED_MODE
LEFT -> CODE_LEFT
RIGHT -> CODE_RIGHT
UP -> CODE_UP
DOWN -> CODE_DOWN
UNDO -> CODE_UNDO
REDO -> CODE_REDO
CLEAR_CLIPBOARD -> null // not managed via code input
}
private fun getStyleableIconId(tag: String) = when (tag) {
TAG_VOICE -> R.styleable.Keyboard_iconShortcutKey
TAG_SETTINGS -> R.styleable.Keyboard_iconSettingsKey
TAG_CLIPBOARD -> R.styleable.Keyboard_iconClipboardNormalKey
TAG_SELECT_ALL -> R.styleable.Keyboard_iconSelectAll
TAG_COPY -> R.styleable.Keyboard_iconCopyKey
TAG_ONE_HANDED -> R.styleable.Keyboard_iconStartOneHandedMode
TAG_LEFT -> R.styleable.Keyboard_iconArrowLeft
TAG_RIGHT -> R.styleable.Keyboard_iconArrowRight
TAG_UP -> R.styleable.Keyboard_iconArrowUp
TAG_DOWN -> R.styleable.Keyboard_iconArrowDown
TAG_UNDO -> R.styleable.Keyboard_iconUndo
TAG_REDO -> R.styleable.Keyboard_iconRedo
TAG_CLEAR_CLIPBOARD -> R.styleable.Keyboard_iconClearClipboardKey
else -> throw IllegalArgumentException("no styleable id for $tag")
private fun getStyleableIconId(key: ToolbarKey) = when (key) {
VOICE -> R.styleable.Keyboard_iconShortcutKey
SETTINGS -> R.styleable.Keyboard_iconSettingsKey
CLIPBOARD -> R.styleable.Keyboard_iconClipboardNormalKey
SELECT_ALL -> R.styleable.Keyboard_iconSelectAll
COPY -> R.styleable.Keyboard_iconCopyKey
ONE_HANDED -> R.styleable.Keyboard_iconStartOneHandedMode
LEFT -> R.styleable.Keyboard_iconArrowLeft
RIGHT -> R.styleable.Keyboard_iconArrowRight
UP -> R.styleable.Keyboard_iconArrowUp
DOWN -> R.styleable.Keyboard_iconArrowDown
UNDO -> R.styleable.Keyboard_iconUndo
REDO -> R.styleable.Keyboard_iconRedo
CLEAR_CLIPBOARD -> R.styleable.Keyboard_iconClearClipboardKey
}
const val TAG_VOICE = "voice_key"
const val TAG_CLIPBOARD = "clipboard_key"
const val TAG_CLEAR_CLIPBOARD = "clear_clipboard"
const val TAG_SETTINGS = "settings_key"
const val TAG_SELECT_ALL = "select_all_key"
const val TAG_COPY = "copy_key"
const val TAG_ONE_HANDED = "one_handed_key"
const val TAG_LEFT = "left_key"
const val TAG_RIGHT = "right_key"
const val TAG_UP = "up_key"
const val TAG_DOWN = "down_key"
const val TAG_REDO = "undo"
const val TAG_UNDO = "redo"
enum class ToolbarKey {
VOICE, CLIPBOARD, CLEAR_CLIPBOARD, SETTINGS, SELECT_ALL, COPY, ONE_HANDED, LEFT, RIGHT, UP, DOWN, UNDO, REDO
}
fun toToolbarKeyString(keys: Collection<ToolbarKey>) = keys.joinToString(";") { it.name }