add incogito and autocorrect toggle keys to toolbar

rename strings for the new keys
fix layout issue in toolbar/morekey dialog
adjust colors so disabled toolbar keys are a little darker
This commit is contained in:
Helium314 2024-01-01 13:16:49 +01:00
parent f093319436
commit 3bee2261f1
98 changed files with 205 additions and 148 deletions

View file

@ -22,7 +22,6 @@ import android.os.IBinder;
import android.os.Message;
import android.os.Process;
import android.text.InputType;
import org.dslul.openboard.inputmethod.latin.utils.Log;
import android.util.PrintWriterPrinter;
import android.util.Printer;
import android.util.SparseArray;
@ -81,6 +80,7 @@ import org.dslul.openboard.inputmethod.latin.utils.InlineAutofillUtils;
import org.dslul.openboard.inputmethod.latin.utils.InputMethodPickerKt;
import org.dslul.openboard.inputmethod.latin.utils.JniUtils;
import org.dslul.openboard.inputmethod.latin.utils.LeakGuardHandlerWrapper;
import org.dslul.openboard.inputmethod.latin.utils.Log;
import org.dslul.openboard.inputmethod.latin.utils.StatsUtils;
import org.dslul.openboard.inputmethod.latin.utils.StatsUtilsManager;
import org.dslul.openboard.inputmethod.latin.utils.SubtypeLocaleUtils;
@ -1592,6 +1592,12 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
// Implementation of {@link KeyboardActionListener}.
@Override
public void onCodeInput(final int codePoint, final int x, final int y, final boolean isKeyRepeat) {
if (codePoint < 0) {
switch (codePoint) {
case Constants.CODE_TOGGLE_AUTOCORRECT -> {mSettings.toggleAutoCorrect(); return; }
case Constants.CODE_TOGGLE_INCOGNITO -> {mSettings.toggleAlwaysIncognitoMode(); return; }
}
}
// TODO: this processing does not belong inside LatinIME, the caller should be doing this.
final MainKeyboardView mainKeyboardView = mKeyboardSwitcher.getMainKeyboardView();
// x and y include some padding, but everything down the line (especially native

View file

@ -124,6 +124,7 @@ class DynamicColors(context: Context, override val themeStyle: String, override
private val spaceBarStateList: ColorStateList
private val adjustedBackgroundStateList: ColorStateList
private val suggestionBackgroundList: ColorStateList
private val toolbarKeyStateList = activatedStateList(keyText, darken(darken(keyText)))
/** custom drawable used for keyboard background */
private val keyboardBackground: Drawable?
@ -244,7 +245,7 @@ class DynamicColors(context: Context, override val themeStyle: String, override
override fun get(color: ColorType): Int = when (color) {
TOOL_BAR_KEY_ENABLED_BACKGROUND, EMOJI_CATEGORY_SELECTED, ACTION_KEY_BACKGROUND,
CLIPBOARD_PIN, SHIFT_KEY_ICON -> accent
CHIP, EMOJI_CATEGORY_BACKGROUND, GESTURE_PREVIEW, MORE_KEYS_BACKGROUND, MORE_SUGGESTIONS_BACKGROUND, KEY_PREVIEW -> adjustedBackground
AUTOFILL_BACKGROUND_CHIP, EMOJI_CATEGORY_BACKGROUND, GESTURE_PREVIEW, MORE_KEYS_BACKGROUND, MORE_SUGGESTIONS_BACKGROUND, KEY_PREVIEW -> adjustedBackground
TOOL_BAR_EXPAND_KEY_BACKGROUND -> if (!isNight) accent else doubleAdjustedBackground
GESTURE_TRAIL -> gesture
KEY_TEXT, SUGGESTION_AUTO_CORRECT, REMOVE_SUGGESTION_ICON,
@ -271,9 +272,10 @@ class DynamicColors(context: Context, override val themeStyle: String, override
SPACE_BAR_BACKGROUND -> spaceBarStateList
MORE_KEYS_BACKGROUND -> adjustedBackgroundStateList
SUGGESTION_BACKGROUND -> if (!hasKeyBorders && themeStyle == STYLE_MATERIAL) adjustedBackgroundStateList
else backgroundStateList
else backgroundStateList
ACTION_KEY_MORE_KEYS_BACKGROUND -> if (themeStyle == STYLE_HOLO) adjustedBackgroundStateList
else actionKeyStateList
else actionKeyStateList
TOOL_BAR_KEY -> toolbarKeyStateList
else -> null // use color filter
}
if (colorStateList == null) {
@ -285,6 +287,10 @@ class DynamicColors(context: Context, override val themeStyle: String, override
}
override fun setColor(view: ImageView, color: ColorType) {
if (color == TOOL_BAR_KEY) {
setColor(view.drawable, color)
return
}
view.colorFilter = getColorFilter(color)
}
@ -379,6 +385,7 @@ class DefaultColors (
private val spaceBarStateList: ColorStateList
private val adjustedBackgroundStateList: ColorStateList
private val suggestionBackgroundList: ColorStateList
private val toolbarKeyStateList = activatedStateList(suggestionText, darken(darken(suggestionText)))
/** custom drawable used for keyboard background */
private val keyboardBackground: Drawable?
@ -439,7 +446,7 @@ class DefaultColors (
override fun get(color: ColorType): Int = when (color) {
TOOL_BAR_KEY_ENABLED_BACKGROUND, EMOJI_CATEGORY_SELECTED, ACTION_KEY_BACKGROUND,
CLIPBOARD_PIN, SHIFT_KEY_ICON -> accent
CHIP, EMOJI_CATEGORY_BACKGROUND, GESTURE_PREVIEW, MORE_KEYS_BACKGROUND, MORE_SUGGESTIONS_BACKGROUND, KEY_PREVIEW -> adjustedBackground
AUTOFILL_BACKGROUND_CHIP, EMOJI_CATEGORY_BACKGROUND, GESTURE_PREVIEW, MORE_KEYS_BACKGROUND, MORE_SUGGESTIONS_BACKGROUND, KEY_PREVIEW -> adjustedBackground
TOOL_BAR_EXPAND_KEY_BACKGROUND -> doubleAdjustedBackground
GESTURE_TRAIL -> gesture
KEY_TEXT, REMOVE_SUGGESTION_ICON, FUNCTIONAL_KEY_TEXT, KEY_ICON -> keyText
@ -469,6 +476,7 @@ class DefaultColors (
else backgroundStateList
ACTION_KEY_MORE_KEYS_BACKGROUND -> if (themeStyle == STYLE_HOLO) adjustedBackgroundStateList
else actionKeyStateList
TOOL_BAR_KEY -> toolbarKeyStateList
else -> null // use color filter
}
if (colorStateList == null) {
@ -480,6 +488,10 @@ class DefaultColors (
}
override fun setColor(view: ImageView, color: ColorType) {
if (color == TOOL_BAR_KEY) {
setColor(view.drawable, color)
return
}
view.colorFilter = getColorFilter(color)
}
@ -542,12 +554,17 @@ private fun stateList(pressed: Int, normal: Int): ColorStateList {
return ColorStateList(states, intArrayOf(pressed, normal))
}
private fun activatedStateList(activated: Int, normal: Int): ColorStateList {
val states = arrayOf(intArrayOf(android.R.attr.state_activated), intArrayOf(-android.R.attr.state_activated))
return ColorStateList(states, intArrayOf(activated, normal))
}
enum class ColorType {
ACTION_KEY_ICON,
ACTION_KEY_BACKGROUND,
ACTION_KEY_MORE_KEYS_BACKGROUND,
AUTOFILL_BACKGROUND_CHIP,
BACKGROUND,
CHIP,
CLIPBOARD_PIN,
CLIPBOARD_BACKGROUND,
EMOJI_BACKGROUND,

View file

@ -246,6 +246,8 @@ public final class Constants {
public static final int CODE_DOWN = -28;
public static final int CODE_UNDO = -29;
public static final int CODE_REDO = -30;
public static final int CODE_TOGGLE_AUTOCORRECT = -31;
public static final int CODE_TOGGLE_INCOGNITO = -32;
// Code value representing the code is not specified.
public static final int CODE_UNSPECIFIED = -200;

View file

@ -261,6 +261,10 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang
return prefs.getBoolean(PREF_AUTO_CORRECTION, true);
}
public void toggleAutoCorrect() {
mPrefs.edit().putBoolean(Settings.PREF_AUTO_CORRECTION, !readAutoCorrectEnabled(mPrefs)).apply();
}
public static String readAutoCorrectConfidence(final SharedPreferences prefs,
final Resources res) {
return prefs.getString(PREF_AUTO_CORRECTION_CONFIDENCE,
@ -295,6 +299,11 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang
return prefs.getBoolean(PREF_ALWAYS_INCOGNITO_MODE, false);
}
public void toggleAlwaysIncognitoMode() {
mPrefs.edit().putBoolean(Settings.PREF_ALWAYS_INCOGNITO_MODE, !readAlwaysIncognitoMode(mPrefs)).apply();
}
public static String readPrefAdditionalSubtypes(final SharedPreferences prefs,
final Resources res) {
final String predefinedPrefSubtypes = AdditionalSubtypeUtils.createPrefSubtypes(

View file

@ -29,7 +29,6 @@ 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;
@ -191,7 +190,7 @@ public class SettingsValues {
mGestureFloatingPreviewTextEnabled = !mInputAttributes.mDisableGestureFloatingPreviewText
&& prefs.getBoolean(Settings.PREF_GESTURE_FLOATING_PREVIEW_TEXT, true);
mAutoCorrectionEnabledPerUserSettings = mAutoCorrectEnabled;
//&& !mInputAttributes.mInputTypeNoAutoCorrect;
//&& !mInputAttributes.mInputTypeNoAutoCorrect; // follow that request or not?
mSuggestionsEnabledPerUserSettings = !mInputAttributes.mIsPasswordField &&
readSuggestionsEnabled(prefs);
mIncognitoModeEnabled = Settings.readAlwaysIncognitoMode(prefs) || mInputAttributes.mNoLearning

View file

@ -623,6 +623,11 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
final Integer code = getCodeForToolbarKey((ToolbarKey) tag);
if (code != null) {
mListener.onCodeInput(code, Constants.SUGGESTION_STRIP_COORDINATE, Constants.SUGGESTION_STRIP_COORDINATE, false);
if (tag == ToolbarKey.INCOGNITO || tag == ToolbarKey.AUTOCORRECT || tag == ToolbarKey.ONE_HANDED) {
if (tag == ToolbarKey.INCOGNITO)
updateKeys(); // update icon
view.setActivated(!view.isActivated());
}
return;
}
}
@ -680,6 +685,7 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
copy.setContentDescription(original.getContentDescription());
copy.setImageDrawable(original.getDrawable());
copy.setLayoutParams(original.getLayoutParams());
copy.setActivated(original.isActivated());
setupKey(copy, Settings.getInstance().getCurrent().mColors);
mPinnedKeys.addView(copy);
}

View file

@ -61,7 +61,7 @@ public class InlineAutofillUtils {
.setBackground(
Icon.createWithResource(context,
androidx.autofill.R.drawable.autofill_inline_suggestion_chip_background)
.setTint(colors.get(ColorType.CHIP)))
.setTint(colors.get(ColorType.AUTOFILL_BACKGROUND_CHIP)))
.setPadding(0, 0, 0, 0)
.build())
.setChipStyle(
@ -69,7 +69,7 @@ public class InlineAutofillUtils {
.setBackground(
Icon.createWithResource(context,
androidx.autofill.R.drawable.autofill_inline_suggestion_chip_background)
.setTint(colors.get(ColorType.CHIP)))
.setTint(colors.get(ColorType.AUTOFILL_BACKGROUND_CHIP)))
.build())
.setStartIconStyle(new ImageViewStyle.Builder().setLayoutMargin(0, 0, 0, 0).build())
.setTitleStyle(

View file

@ -26,6 +26,12 @@ fun createToolbarKey(context: Context, keyboardAttr: TypedArray, key: ToolbarKey
button.scaleX = 1.2f
button.scaleY = 1.2f
}
button.isActivated = when (key) {
INCOGNITO -> Settings.readAlwaysIncognitoMode(DeviceProtectedUtils.getSharedPreferences(context))
ONE_HANDED -> Settings.getInstance().current.mOneHandedModeEnabled
AUTOCORRECT -> Settings.getInstance().current.mAutoCorrectionEnabledPerUserSettings
else -> true
}
button.setImageDrawable(icon)
return button
}
@ -43,6 +49,8 @@ fun getCodeForToolbarKey(key: ToolbarKey) = when (key) {
DOWN -> CODE_DOWN
UNDO -> CODE_UNDO
REDO -> CODE_REDO
INCOGNITO -> CODE_TOGGLE_INCOGNITO
AUTOCORRECT -> CODE_TOGGLE_AUTOCORRECT
CLEAR_CLIPBOARD -> null // not managed via code input
}
@ -59,25 +67,34 @@ private fun getStyleableIconId(key: ToolbarKey) = when (key) {
DOWN -> R.styleable.Keyboard_iconArrowDown
UNDO -> R.styleable.Keyboard_iconUndo
REDO -> R.styleable.Keyboard_iconRedo
INCOGNITO -> R.styleable.Keyboard_iconIncognitoKey
AUTOCORRECT -> R.styleable.Keyboard_iconLanguageSwitchKey
CLEAR_CLIPBOARD -> R.styleable.Keyboard_iconClearClipboardKey
}
// names need to be aligned with resources strings (using lowercase of key.name)
enum class ToolbarKey {
VOICE, CLIPBOARD, CLEAR_CLIPBOARD, SETTINGS, SELECT_ALL, COPY, ONE_HANDED, LEFT, RIGHT, UP, DOWN, UNDO, REDO
VOICE, CLIPBOARD, UNDO, REDO, SETTINGS, SELECT_ALL, COPY, ONE_HANDED, LEFT, RIGHT, UP, DOWN, INCOGNITO, AUTOCORRECT, CLEAR_CLIPBOARD
}
fun toToolbarKeyString(keys: Collection<ToolbarKey>) = keys.joinToString(";") { it.name }
val defaultToolbarPref = entries.joinToString(";") { if (it != CLEAR_CLIPBOARD) "${it.name},true" else "${it.name},false" }
val defaultToolbarPref = entries.filterNot { it == CLEAR_CLIPBOARD }.joinToString(";") {
when (it) {
INCOGNITO, AUTOCORRECT, UP, DOWN, ONE_HANDED -> "${it.name},false"
else -> "${it.name},true"
}
}
/** add missing keys, typically because a new key has been added */
fun upgradeToolbarPref(prefs: SharedPreferences) {
val list = prefs.getString(Settings.PREF_TOOLBAR_KEYS, defaultToolbarPref)!!.split(";").toMutableList()
if (list.size == ToolbarKey.entries.size) return
ToolbarKey.entries.forEach { key ->
if (list.none { it.startsWith("${key.name},") })
list.add("${key.name},true")
val splitDefault = defaultToolbarPref.split(";")
if (list.size == splitDefault.size) return
splitDefault.forEach { entry ->
val keyWithComma = entry.substringBefore(",") + ","
if (list.none { it.startsWith(keyWithComma) })
list.add("${keyWithComma}true")
}
// likely not needed, but better prepare for possibility of key removal
list.removeAll {