The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 1 | /* |
Tadashi G. Takaoka | cbee8a3 | 2013-01-08 15:41:46 +0900 | [diff] [blame] | 2 | * Copyright (C) 2013 The Android Open Source Project |
Jean Chalard | 89764ad | 2011-01-19 17:44:39 +0900 | [diff] [blame] | 3 | * |
Tadashi G. Takaoka | 8aa9963 | 2013-01-21 21:52:57 +0900 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Doug Zongker | a16ff1b | 2010-01-28 10:09:44 -0800 | [diff] [blame] | 7 | * |
Tadashi G. Takaoka | 8aa9963 | 2013-01-21 21:52:57 +0900 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Doug Zongker | a16ff1b | 2010-01-28 10:09:44 -0800 | [diff] [blame] | 9 | * |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 10 | * Unless required by applicable law or agreed to in writing, software |
Tadashi G. Takaoka | 8aa9963 | 2013-01-21 21:52:57 +0900 | [diff] [blame] | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
Ken Wakasa | a7d2fc6 | 2013-07-22 12:43:37 +0900 | [diff] [blame] | 17 | package com.android.inputmethod.latin.settings; |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 18 | |
Tadashi G. Takaoka | ff0735a | 2013-01-08 16:35:21 +0900 | [diff] [blame] | 19 | import android.content.Context; |
| 20 | import android.content.SharedPreferences; |
Chieu Nguyen | a2376cd | 2015-04-22 14:41:45 -0700 | [diff] [blame] | 21 | import android.content.pm.ApplicationInfo; |
Jean Chalard | 6bb6d30 | 2014-09-03 19:36:35 +0900 | [diff] [blame] | 22 | import android.content.res.Configuration; |
Tadashi G. Takaoka | ff0735a | 2013-01-08 16:35:21 +0900 | [diff] [blame] | 23 | import android.content.res.Resources; |
Tadashi G. Takaoka | 7ed40a1 | 2014-05-24 00:41:11 +0900 | [diff] [blame] | 24 | import android.os.Build; |
Tadashi G. Takaoka | ff0735a | 2013-01-08 16:35:21 +0900 | [diff] [blame] | 25 | import android.preference.PreferenceManager; |
Satoshi Kataoka | 6f9c6f2 | 2013-06-06 10:57:47 +0900 | [diff] [blame] | 26 | import android.util.Log; |
Tadashi G. Takaoka | ff0735a | 2013-01-08 16:35:21 +0900 | [diff] [blame] | 27 | |
Tadashi G. Takaoka | 78cff10 | 2014-08-12 23:13:18 +0900 | [diff] [blame] | 28 | import com.android.inputmethod.compat.BuildCompatUtils; |
Ken Wakasa | a7d2fc6 | 2013-07-22 12:43:37 +0900 | [diff] [blame] | 29 | import com.android.inputmethod.latin.AudioAndHapticFeedbackManager; |
Ken Wakasa | a7d2fc6 | 2013-07-22 12:43:37 +0900 | [diff] [blame] | 30 | import com.android.inputmethod.latin.InputAttributes; |
| 31 | import com.android.inputmethod.latin.R; |
Jean Chalard | 4beeb92 | 2014-10-28 21:31:09 +0900 | [diff] [blame] | 32 | import com.android.inputmethod.latin.common.StringUtils; |
Tadashi G. Takaoka | a410cb4 | 2013-07-24 10:37:07 +0900 | [diff] [blame] | 33 | import com.android.inputmethod.latin.utils.AdditionalSubtypeUtils; |
Steve Kondik | 0b49592 | 2013-12-06 19:21:08 +0100 | [diff] [blame] | 34 | import com.android.inputmethod.latin.utils.JniUtils; |
Ken Wakasa | e28eba5 | 2013-06-24 01:11:32 +0900 | [diff] [blame] | 35 | import com.android.inputmethod.latin.utils.ResourceUtils; |
Tadashi G. Takaoka | 0c3a9b5 | 2013-07-24 15:58:11 +0900 | [diff] [blame] | 36 | import com.android.inputmethod.latin.utils.RunInLocale; |
Jatin Matani | cc7e44e | 2014-09-30 17:09:00 -0700 | [diff] [blame] | 37 | import com.android.inputmethod.latin.utils.StatsUtils; |
Tadashi G. Takaoka | ff0735a | 2013-01-08 16:35:21 +0900 | [diff] [blame] | 38 | |
Keisuke Kuroyanagi | eb28bac | 2014-01-10 20:28:30 +0900 | [diff] [blame] | 39 | import java.util.Collections; |
Tadashi G. Takaoka | ff0735a | 2013-01-08 16:35:21 +0900 | [diff] [blame] | 40 | import java.util.Locale; |
Keisuke Kuroyanagi | eb28bac | 2014-01-10 20:28:30 +0900 | [diff] [blame] | 41 | import java.util.Set; |
Jean Chalard | 10d6666 | 2013-08-02 20:22:00 +0900 | [diff] [blame] | 42 | import java.util.concurrent.locks.ReentrantLock; |
Tadashi G. Takaoka | ff0735a | 2013-01-08 16:35:21 +0900 | [diff] [blame] | 43 | |
Tadashi G. Takaoka | d3a4c51 | 2014-10-22 14:04:07 +0900 | [diff] [blame] | 44 | import javax.annotation.Nonnull; |
| 45 | |
Tadashi G. Takaoka | ff0735a | 2013-01-08 16:35:21 +0900 | [diff] [blame] | 46 | public final class Settings implements SharedPreferences.OnSharedPreferenceChangeListener { |
Satoshi Kataoka | 6f9c6f2 | 2013-06-06 10:57:47 +0900 | [diff] [blame] | 47 | private static final String TAG = Settings.class.getSimpleName(); |
Tadashi G. Takaoka | 058c6c6 | 2014-06-19 17:47:11 -0700 | [diff] [blame] | 48 | // Settings screens |
Sandeep Siddhartha | a4e0b6e | 2014-09-16 14:56:34 -0700 | [diff] [blame] | 49 | public static final String SCREEN_ACCOUNTS = "screen_accounts"; |
Tadashi G. Takaoka | a715d7f | 2014-07-29 15:33:40 +0900 | [diff] [blame] | 50 | public static final String SCREEN_THEME = "screen_theme"; |
Tadashi G. Takaoka | 058c6c6 | 2014-06-19 17:47:11 -0700 | [diff] [blame] | 51 | public static final String SCREEN_DEBUG = "screen_debug"; |
Steve Kondik | 0b49592 | 2013-12-06 19:21:08 +0100 | [diff] [blame] | 52 | public static final String SCREEN_GESTURE = "screen_gesture"; |
Jean Chalard | 837b109 | 2011-12-09 16:41:05 +0900 | [diff] [blame] | 53 | // In the same order as xml/prefs.xml |
Jean Chalard | 837b109 | 2011-12-09 16:41:05 +0900 | [diff] [blame] | 54 | public static final String PREF_AUTO_CAP = "auto_cap"; |
Tadashi G. Takaoka | 9502cc1 | 2010-12-09 21:06:26 +0900 | [diff] [blame] | 55 | public static final String PREF_VIBRATE_ON = "vibrate_on"; |
| 56 | public static final String PREF_SOUND_ON = "sound_on"; |
Jean Chalard | ba93dda | 2011-12-09 17:58:51 +0900 | [diff] [blame] | 57 | public static final String PREF_POPUP_ON = "popup_on"; |
Satoshi Kataoka | 21a89c1 | 2013-09-05 15:46:04 +0900 | [diff] [blame] | 58 | // PREF_VOICE_MODE_OBSOLETE is obsolete. Use PREF_VOICE_INPUT_KEY instead. |
| 59 | public static final String PREF_VOICE_MODE_OBSOLETE = "voice_mode"; |
| 60 | public static final String PREF_VOICE_INPUT_KEY = "pref_voice_input_key"; |
Dan Zivkovic | 764a18c | 2015-06-16 18:26:02 -0700 | [diff] [blame] | 61 | public static final String PREF_EDIT_PERSONAL_DICTIONARY = "edit_personal_dictionary"; |
Jean Chalard | 837b109 | 2011-12-09 16:41:05 +0900 | [diff] [blame] | 62 | public static final String PREF_CONFIGURE_DICTIONARIES_KEY = "configure_dictionaries_key"; |
Chieu Nguyen | 116e58c | 2015-03-12 19:39:06 -0700 | [diff] [blame] | 63 | // PREF_AUTO_CORRECTION_THRESHOLD_OBSOLETE is obsolete. Use PREF_AUTO_CORRECTION instead. |
| 64 | public static final String PREF_AUTO_CORRECTION_THRESHOLD_OBSOLETE = |
| 65 | "auto_correction_threshold"; |
| 66 | public static final String PREF_AUTO_CORRECTION = "pref_key_auto_correction"; |
Tadashi G. Takaoka | ab661e3 | 2014-07-22 10:18:38 -0700 | [diff] [blame] | 67 | // PREF_SHOW_SUGGESTIONS_SETTING_OBSOLETE is obsolete. Use PREF_SHOW_SUGGESTIONS instead. |
| 68 | public static final String PREF_SHOW_SUGGESTIONS_SETTING_OBSOLETE = "show_suggestions_setting"; |
| 69 | public static final String PREF_SHOW_SUGGESTIONS = "show_suggestions"; |
Tadashi G. Takaoka | 47e2bf3 | 2012-07-27 15:32:29 +0900 | [diff] [blame] | 70 | public static final String PREF_KEY_USE_CONTACTS_DICT = "pref_key_use_contacts_dict"; |
Tadashi G. Takaoka | c2ba73a | 2014-02-20 06:02:26 +0000 | [diff] [blame] | 71 | public static final String PREF_KEY_USE_PERSONALIZED_DICTS = "pref_key_use_personalized_dicts"; |
Ken Wakasa | 139bd83f | 2012-12-04 15:24:55 +0900 | [diff] [blame] | 72 | public static final String PREF_KEY_USE_DOUBLE_SPACE_PERIOD = |
| 73 | "pref_key_use_double_space_period"; |
Jean Chalard | bb61293 | 2013-04-23 12:19:31 +0900 | [diff] [blame] | 74 | public static final String PREF_BLOCK_POTENTIALLY_OFFENSIVE = |
| 75 | "pref_key_block_potentially_offensive"; |
Tadashi G. Takaoka | 7ed40a1 | 2014-05-24 00:41:11 +0900 | [diff] [blame] | 76 | public static final boolean ENABLE_SHOW_LANGUAGE_SWITCH_KEY_SETTINGS = |
Tadashi G. Takaoka | 78cff10 | 2014-08-12 23:13:18 +0900 | [diff] [blame] | 77 | BuildCompatUtils.EFFECTIVE_SDK_INT <= Build.VERSION_CODES.KITKAT; |
Tadashi G. Takaoka | 6fb586a | 2014-10-06 17:20:10 +0900 | [diff] [blame] | 78 | public static final boolean SHOULD_SHOW_LXX_SUGGESTION_UI = |
Yohei Yukawa | 66d30a4 | 2014-10-22 00:07:23 +0900 | [diff] [blame] | 79 | BuildCompatUtils.EFFECTIVE_SDK_INT >= Build.VERSION_CODES.LOLLIPOP; |
Tadashi G. Takaoka | 1a4aa7e | 2012-09-20 10:32:56 +0900 | [diff] [blame] | 80 | public static final String PREF_SHOW_LANGUAGE_SWITCH_KEY = |
| 81 | "pref_show_language_switch_key"; |
Tadashi G. Takaoka | 81d4e3c | 2012-02-17 17:46:01 -0800 | [diff] [blame] | 82 | public static final String PREF_INCLUDE_OTHER_IMES_IN_LANGUAGE_SWITCH_LIST = |
| 83 | "pref_include_other_imes_in_language_switch_list"; |
Tadashi G. Takaoka | f5a0bd2 | 2012-04-18 22:13:29 +0900 | [diff] [blame] | 84 | public static final String PREF_CUSTOM_INPUT_STYLES = "custom_input_styles"; |
Jatin Matani | a5d4cb7 | 2014-09-10 10:25:43 +0900 | [diff] [blame] | 85 | public static final String PREF_ENABLE_SPLIT_KEYBOARD = "pref_split_keyboard"; |
Tadashi G. Takaoka | c9072dc | 2014-02-17 12:10:09 +0900 | [diff] [blame] | 86 | // TODO: consolidate key preview dismiss delay with the key preview animation parameters. |
Jean Chalard | 240297d | 2011-05-11 20:51:07 +0900 | [diff] [blame] | 87 | public static final String PREF_KEY_PREVIEW_POPUP_DISMISS_DELAY = |
| 88 | "pref_key_preview_popup_dismiss_delay"; |
Dan Zivkovic | 5455179 | 2015-03-13 14:50:54 -0700 | [diff] [blame] | 89 | public static final String PREF_BIGRAM_PREDICTIONS = "next_word_prediction"; |
Tadashi G. Takaoka | 62b8ddd | 2012-07-20 17:49:06 +0900 | [diff] [blame] | 90 | public static final String PREF_GESTURE_INPUT = "gesture_input"; |
Jean Chalard | ba93dda | 2011-12-09 17:58:51 +0900 | [diff] [blame] | 91 | public static final String PREF_VIBRATION_DURATION_SETTINGS = |
satok | 4339657 | 2011-09-28 20:11:05 +0900 | [diff] [blame] | 92 | "pref_vibration_duration_settings"; |
Tadashi G. Takaoka | ad2f695 | 2014-10-17 16:07:50 +0900 | [diff] [blame] | 93 | public static final String PREF_KEYPRESS_SOUND_VOLUME = "pref_keypress_sound_volume"; |
| 94 | public static final String PREF_KEY_LONGPRESS_TIMEOUT = "pref_key_longpress_timeout"; |
Tadashi G. Takaoka | f7e01e8 | 2014-10-27 11:59:57 +0900 | [diff] [blame] | 95 | public static final String PREF_ENABLE_EMOJI_ALT_PHYSICAL_KEY = |
| 96 | "pref_enable_emoji_alt_physical_key"; |
Tadashi G. Takaoka | 47e2bf3 | 2012-07-27 15:32:29 +0900 | [diff] [blame] | 97 | public static final String PREF_GESTURE_PREVIEW_TRAIL = "pref_gesture_preview_trail"; |
Tadashi G. Takaoka | 15281ad | 2012-10-02 10:30:03 +0900 | [diff] [blame] | 98 | public static final String PREF_GESTURE_FLOATING_PREVIEW_TEXT = |
| 99 | "pref_gesture_floating_preview_text"; |
Chieu Nguyen | a2376cd | 2015-04-22 14:41:45 -0700 | [diff] [blame] | 100 | public static final String PREF_SHOW_SETUP_WIZARD_ICON = "pref_show_setup_wizard_icon"; |
Jean Chalard | 837b109 | 2011-12-09 16:41:05 +0900 | [diff] [blame] | 101 | |
Satoshi Kataoka | 756791e | 2013-03-13 13:08:34 +0900 | [diff] [blame] | 102 | public static final String PREF_KEY_IS_INTERNAL = "pref_key_is_internal"; |
Tadashi G. Takaoka | ff0735a | 2013-01-08 16:35:21 +0900 | [diff] [blame] | 103 | |
Yohei Yukawa | 9aa7f66 | 2014-05-14 14:26:29 +0900 | [diff] [blame] | 104 | public static final String PREF_ENABLE_METRICS_LOGGING = "pref_enable_metrics_logging"; |
Tadashi G. Takaoka | d3b0ece | 2013-01-08 18:20:31 +0900 | [diff] [blame] | 105 | // This preference key is deprecated. Use {@link #PREF_SHOW_LANGUAGE_SWITCH_KEY} instead. |
| 106 | // This is being used only for the backward compatibility. |
| 107 | private static final String PREF_SUPPRESS_LANGUAGE_SWITCH_KEY = |
| 108 | "pref_suppress_language_switch_key"; |
| 109 | |
Satoshi Kataoka | 283cf9c | 2013-08-16 14:35:56 +0900 | [diff] [blame] | 110 | private static final String PREF_LAST_USED_PERSONALIZATION_TOKEN = |
| 111 | "pref_last_used_personalization_token"; |
Keisuke Kuroyanagi | ba07df7 | 2014-01-10 16:30:59 +0900 | [diff] [blame] | 112 | private static final String PREF_LAST_PERSONALIZATION_DICT_WIPED_TIME = |
| 113 | "pref_last_used_personalization_dict_wiped_time"; |
Keisuke Kuroyanagi | eb28bac | 2014-01-10 20:28:30 +0900 | [diff] [blame] | 114 | private static final String PREF_CORPUS_HANDLES_FOR_PERSONALIZATION = |
| 115 | "pref_corpus_handles_for_personalization"; |
Satoshi Kataoka | a462cd8 | 2013-03-19 16:49:19 +0900 | [diff] [blame] | 116 | |
Satoshi Kataoka | f3f0000 | 2013-09-16 17:13:41 +0900 | [diff] [blame] | 117 | // Emoji |
| 118 | public static final String PREF_EMOJI_RECENT_KEYS = "emoji_recent_keys"; |
| 119 | public static final String PREF_EMOJI_CATEGORY_LAST_TYPED_ID = "emoji_category_last_typed_id"; |
Satoshi Kataoka | 6dc99dc | 2013-10-11 14:54:26 +0900 | [diff] [blame] | 120 | public static final String PREF_LAST_SHOWN_EMOJI_CATEGORY_ID = "last_shown_emoji_category_id"; |
Satoshi Kataoka | f3f0000 | 2013-09-16 17:13:41 +0900 | [diff] [blame] | 121 | |
Tadashi G. Takaoka | 276b1a2 | 2014-02-07 15:25:50 +0900 | [diff] [blame] | 122 | private static final float UNDEFINED_PREFERENCE_VALUE_FLOAT = -1.0f; |
| 123 | private static final int UNDEFINED_PREFERENCE_VALUE_INT = -1; |
| 124 | |
Jean Chalard | 1404c1a | 2013-12-20 15:00:44 +0900 | [diff] [blame] | 125 | private Context mContext; |
Tadashi G. Takaoka | ff0735a | 2013-01-08 16:35:21 +0900 | [diff] [blame] | 126 | private Resources mRes; |
| 127 | private SharedPreferences mPrefs; |
Tadashi G. Takaoka | ff0735a | 2013-01-08 16:35:21 +0900 | [diff] [blame] | 128 | private SettingsValues mSettingsValues; |
Jean Chalard | 10d6666 | 2013-08-02 20:22:00 +0900 | [diff] [blame] | 129 | private final ReentrantLock mSettingsValuesLock = new ReentrantLock(); |
Tadashi G. Takaoka | ff0735a | 2013-01-08 16:35:21 +0900 | [diff] [blame] | 130 | |
| 131 | private static final Settings sInstance = new Settings(); |
| 132 | |
| 133 | public static Settings getInstance() { |
| 134 | return sInstance; |
| 135 | } |
| 136 | |
| 137 | public static void init(final Context context) { |
| 138 | sInstance.onCreate(context); |
| 139 | } |
| 140 | |
| 141 | private Settings() { |
| 142 | // Intentional empty constructor for singleton. |
| 143 | } |
| 144 | |
| 145 | private void onCreate(final Context context) { |
Jean Chalard | 1404c1a | 2013-12-20 15:00:44 +0900 | [diff] [blame] | 146 | mContext = context; |
Tadashi G. Takaoka | ff0735a | 2013-01-08 16:35:21 +0900 | [diff] [blame] | 147 | mRes = context.getResources(); |
| 148 | mPrefs = PreferenceManager.getDefaultSharedPreferences(context); |
| 149 | mPrefs.registerOnSharedPreferenceChangeListener(this); |
Chieu Nguyen | 116e58c | 2015-03-12 19:39:06 -0700 | [diff] [blame] | 150 | upgradeAutocorrectionSettings(mPrefs, mRes); |
Tadashi G. Takaoka | ff0735a | 2013-01-08 16:35:21 +0900 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | public void onDestroy() { |
| 154 | mPrefs.unregisterOnSharedPreferenceChangeListener(this); |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public void onSharedPreferenceChanged(final SharedPreferences prefs, final String key) { |
Jean Chalard | 10d6666 | 2013-08-02 20:22:00 +0900 | [diff] [blame] | 159 | mSettingsValuesLock.lock(); |
| 160 | try { |
| 161 | if (mSettingsValues == null) { |
| 162 | // TODO: Introduce a static function to register this class and ensure that |
| 163 | // loadSettings must be called before "onSharedPreferenceChanged" is called. |
| 164 | Log.w(TAG, "onSharedPreferenceChanged called before loadSettings."); |
| 165 | return; |
| 166 | } |
Jean Chalard | 1404c1a | 2013-12-20 15:00:44 +0900 | [diff] [blame] | 167 | loadSettings(mContext, mSettingsValues.mLocale, mSettingsValues.mInputAttributes); |
Jatin Matani | cc7e44e | 2014-09-30 17:09:00 -0700 | [diff] [blame] | 168 | StatsUtils.onLoadSettings(mSettingsValues); |
Jean Chalard | 10d6666 | 2013-08-02 20:22:00 +0900 | [diff] [blame] | 169 | } finally { |
| 170 | mSettingsValuesLock.unlock(); |
Satoshi Kataoka | 6f9c6f2 | 2013-06-06 10:57:47 +0900 | [diff] [blame] | 171 | } |
Tadashi G. Takaoka | ff0735a | 2013-01-08 16:35:21 +0900 | [diff] [blame] | 172 | } |
| 173 | |
Jean Chalard | 1404c1a | 2013-12-20 15:00:44 +0900 | [diff] [blame] | 174 | public void loadSettings(final Context context, final Locale locale, |
Tadashi G. Takaoka | d3a4c51 | 2014-10-22 14:04:07 +0900 | [diff] [blame] | 175 | @Nonnull final InputAttributes inputAttributes) { |
Jean Chalard | 10d6666 | 2013-08-02 20:22:00 +0900 | [diff] [blame] | 176 | mSettingsValuesLock.lock(); |
Jean Chalard | 1404c1a | 2013-12-20 15:00:44 +0900 | [diff] [blame] | 177 | mContext = context; |
Jean Chalard | 10d6666 | 2013-08-02 20:22:00 +0900 | [diff] [blame] | 178 | try { |
| 179 | final SharedPreferences prefs = mPrefs; |
| 180 | final RunInLocale<SettingsValues> job = new RunInLocale<SettingsValues>() { |
| 181 | @Override |
| 182 | protected SettingsValues job(final Resources res) { |
Tadashi G. Takaoka | 860c3b8 | 2014-01-09 16:59:23 +0900 | [diff] [blame] | 183 | return new SettingsValues(context, prefs, res, inputAttributes); |
Jean Chalard | 10d6666 | 2013-08-02 20:22:00 +0900 | [diff] [blame] | 184 | } |
| 185 | }; |
| 186 | mSettingsValues = job.runInLocale(mRes, locale); |
| 187 | } finally { |
| 188 | mSettingsValuesLock.unlock(); |
| 189 | } |
Tadashi G. Takaoka | ff0735a | 2013-01-08 16:35:21 +0900 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | // TODO: Remove this method and add proxy method to SettingsValues. |
| 193 | public SettingsValues getCurrent() { |
| 194 | return mSettingsValues; |
| 195 | } |
Tadashi G. Takaoka | d3b0ece | 2013-01-08 18:20:31 +0900 | [diff] [blame] | 196 | |
Satoshi Kataoka | 756791e | 2013-03-13 13:08:34 +0900 | [diff] [blame] | 197 | public boolean isInternal() { |
| 198 | return mSettingsValues.mIsInternal; |
| 199 | } |
| 200 | |
Tadashi G. Takaoka | 395009c | 2014-10-28 14:40:06 +0900 | [diff] [blame] | 201 | public static int readScreenMetrics(final Resources res) { |
| 202 | return res.getInteger(R.integer.config_screen_metrics); |
| 203 | } |
| 204 | |
Tadashi G. Takaoka | d3b0ece | 2013-01-08 18:20:31 +0900 | [diff] [blame] | 205 | // Accessed from the settings interface, hence public |
Tadashi G. Takaoka | ce87566 | 2013-01-22 13:29:30 +0900 | [diff] [blame] | 206 | public static boolean readKeypressSoundEnabled(final SharedPreferences prefs, |
| 207 | final Resources res) { |
Tadashi G. Takaoka | a3920c7 | 2014-04-25 11:49:33 -0700 | [diff] [blame] | 208 | return prefs.getBoolean(PREF_SOUND_ON, |
Tadashi G. Takaoka | ce87566 | 2013-01-22 13:29:30 +0900 | [diff] [blame] | 209 | res.getBoolean(R.bool.config_default_sound_enabled)); |
| 210 | } |
| 211 | |
| 212 | public static boolean readVibrationEnabled(final SharedPreferences prefs, |
| 213 | final Resources res) { |
| 214 | final boolean hasVibrator = AudioAndHapticFeedbackManager.getInstance().hasVibrator(); |
| 215 | return hasVibrator && prefs.getBoolean(PREF_VIBRATE_ON, |
| 216 | res.getBoolean(R.bool.config_default_vibration_enabled)); |
| 217 | } |
| 218 | |
Chieu Nguyen | 116e58c | 2015-03-12 19:39:06 -0700 | [diff] [blame] | 219 | public static boolean readAutoCorrectEnabled(final SharedPreferences prefs, |
Tadashi G. Takaoka | ce87566 | 2013-01-22 13:29:30 +0900 | [diff] [blame] | 220 | final Resources res) { |
Chieu Nguyen | 116e58c | 2015-03-12 19:39:06 -0700 | [diff] [blame] | 221 | return prefs.getBoolean(PREF_AUTO_CORRECTION, true); |
Tadashi G. Takaoka | ce87566 | 2013-01-22 13:29:30 +0900 | [diff] [blame] | 222 | } |
| 223 | |
Jean Chalard | 5657746 | 2014-11-21 22:49:50 +0900 | [diff] [blame] | 224 | public static float readPlausibilityThreshold(final Resources res) { |
| 225 | return Float.parseFloat(res.getString(R.string.plausibility_threshold)); |
| 226 | } |
| 227 | |
Jean Chalard | bb61293 | 2013-04-23 12:19:31 +0900 | [diff] [blame] | 228 | public static boolean readBlockPotentiallyOffensive(final SharedPreferences prefs, |
| 229 | final Resources res) { |
Tadashi G. Takaoka | a3920c7 | 2014-04-25 11:49:33 -0700 | [diff] [blame] | 230 | return prefs.getBoolean(PREF_BLOCK_POTENTIALLY_OFFENSIVE, |
Jean Chalard | bb61293 | 2013-04-23 12:19:31 +0900 | [diff] [blame] | 231 | res.getBoolean(R.bool.config_block_potentially_offensive)); |
| 232 | } |
| 233 | |
Tadashi G. Takaoka | ce87566 | 2013-01-22 13:29:30 +0900 | [diff] [blame] | 234 | public static boolean readFromBuildConfigIfGestureInputEnabled(final Resources res) { |
Steve Kondik | 0b49592 | 2013-12-06 19:21:08 +0100 | [diff] [blame] | 235 | if (!JniUtils.sHaveGestureLib) { |
| 236 | return false; |
| 237 | } |
Tadashi G. Takaoka | ce87566 | 2013-01-22 13:29:30 +0900 | [diff] [blame] | 238 | return res.getBoolean(R.bool.config_gesture_input_enabled_by_build_config); |
| 239 | } |
| 240 | |
| 241 | public static boolean readGestureInputEnabled(final SharedPreferences prefs, |
| 242 | final Resources res) { |
| 243 | return readFromBuildConfigIfGestureInputEnabled(res) |
Tadashi G. Takaoka | a3920c7 | 2014-04-25 11:49:33 -0700 | [diff] [blame] | 244 | && prefs.getBoolean(PREF_GESTURE_INPUT, true); |
Tadashi G. Takaoka | ce87566 | 2013-01-22 13:29:30 +0900 | [diff] [blame] | 245 | } |
| 246 | |
Ken Wakasa | 2fa3693 | 2013-12-13 17:09:16 +0900 | [diff] [blame] | 247 | public static boolean readFromBuildConfigIfToShowKeyPreviewPopupOption(final Resources res) { |
| 248 | return res.getBoolean(R.bool.config_enable_show_key_preview_popup_option); |
Tadashi G. Takaoka | ce87566 | 2013-01-22 13:29:30 +0900 | [diff] [blame] | 249 | } |
| 250 | |
Tadashi G. Takaoka | d3b0ece | 2013-01-08 18:20:31 +0900 | [diff] [blame] | 251 | public static boolean readKeyPreviewPopupEnabled(final SharedPreferences prefs, |
| 252 | final Resources res) { |
Tadashi G. Takaoka | ce87566 | 2013-01-22 13:29:30 +0900 | [diff] [blame] | 253 | final boolean defaultKeyPreviewPopup = res.getBoolean( |
| 254 | R.bool.config_default_key_preview_popup); |
Ken Wakasa | 2fa3693 | 2013-12-13 17:09:16 +0900 | [diff] [blame] | 255 | if (!readFromBuildConfigIfToShowKeyPreviewPopupOption(res)) { |
Tadashi G. Takaoka | ce87566 | 2013-01-22 13:29:30 +0900 | [diff] [blame] | 256 | return defaultKeyPreviewPopup; |
| 257 | } |
| 258 | return prefs.getBoolean(PREF_POPUP_ON, defaultKeyPreviewPopup); |
Tadashi G. Takaoka | d3b0ece | 2013-01-08 18:20:31 +0900 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | public static int readKeyPreviewPopupDismissDelay(final SharedPreferences prefs, |
| 262 | final Resources res) { |
Tadashi G. Takaoka | d3b0ece | 2013-01-08 18:20:31 +0900 | [diff] [blame] | 263 | return Integer.parseInt(prefs.getString(PREF_KEY_PREVIEW_POPUP_DISMISS_DELAY, |
| 264 | Integer.toString(res.getInteger( |
| 265 | R.integer.config_key_preview_linger_timeout)))); |
| 266 | } |
| 267 | |
| 268 | public static boolean readShowsLanguageSwitchKey(final SharedPreferences prefs) { |
| 269 | if (prefs.contains(PREF_SUPPRESS_LANGUAGE_SWITCH_KEY)) { |
| 270 | final boolean suppressLanguageSwitchKey = prefs.getBoolean( |
| 271 | PREF_SUPPRESS_LANGUAGE_SWITCH_KEY, false); |
| 272 | final SharedPreferences.Editor editor = prefs.edit(); |
| 273 | editor.remove(PREF_SUPPRESS_LANGUAGE_SWITCH_KEY); |
| 274 | editor.putBoolean(PREF_SHOW_LANGUAGE_SWITCH_KEY, !suppressLanguageSwitchKey); |
| 275 | editor.apply(); |
| 276 | } |
| 277 | return prefs.getBoolean(PREF_SHOW_LANGUAGE_SWITCH_KEY, true); |
| 278 | } |
| 279 | |
| 280 | public static String readPrefAdditionalSubtypes(final SharedPreferences prefs, |
| 281 | final Resources res) { |
Tadashi G. Takaoka | a410cb4 | 2013-07-24 10:37:07 +0900 | [diff] [blame] | 282 | final String predefinedPrefSubtypes = AdditionalSubtypeUtils.createPrefSubtypes( |
Tadashi G. Takaoka | d3b0ece | 2013-01-08 18:20:31 +0900 | [diff] [blame] | 283 | res.getStringArray(R.array.predefined_subtypes)); |
| 284 | return prefs.getString(PREF_CUSTOM_INPUT_STYLES, predefinedPrefSubtypes); |
| 285 | } |
| 286 | |
| 287 | public static void writePrefAdditionalSubtypes(final SharedPreferences prefs, |
| 288 | final String prefSubtypes) { |
Tadashi G. Takaoka | a3920c7 | 2014-04-25 11:49:33 -0700 | [diff] [blame] | 289 | prefs.edit().putString(PREF_CUSTOM_INPUT_STYLES, prefSubtypes).apply(); |
Tadashi G. Takaoka | d3b0ece | 2013-01-08 18:20:31 +0900 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | public static float readKeypressSoundVolume(final SharedPreferences prefs, |
| 293 | final Resources res) { |
Tadashi G. Takaoka | 276b1a2 | 2014-02-07 15:25:50 +0900 | [diff] [blame] | 294 | final float volume = prefs.getFloat( |
| 295 | PREF_KEYPRESS_SOUND_VOLUME, UNDEFINED_PREFERENCE_VALUE_FLOAT); |
| 296 | return (volume != UNDEFINED_PREFERENCE_VALUE_FLOAT) ? volume |
| 297 | : readDefaultKeypressSoundVolume(res); |
Tadashi G. Takaoka | a7a7f4e | 2013-01-10 12:06:00 +0900 | [diff] [blame] | 298 | } |
| 299 | |
Tadashi G. Takaoka | bd9887a | 2014-02-10 12:54:45 +0900 | [diff] [blame] | 300 | // Default keypress sound volume for unknown devices. |
| 301 | // The negative value means system default. |
| 302 | private static final String DEFAULT_KEYPRESS_SOUND_VOLUME = Float.toString(-1.0f); |
| 303 | |
Tadashi G. Takaoka | a7a7f4e | 2013-01-10 12:06:00 +0900 | [diff] [blame] | 304 | public static float readDefaultKeypressSoundVolume(final Resources res) { |
Tadashi G. Takaoka | bd9887a | 2014-02-10 12:54:45 +0900 | [diff] [blame] | 305 | return Float.parseFloat(ResourceUtils.getDeviceOverrideValue(res, |
| 306 | R.array.keypress_volumes, DEFAULT_KEYPRESS_SOUND_VOLUME)); |
Tadashi G. Takaoka | d3b0ece | 2013-01-08 18:20:31 +0900 | [diff] [blame] | 307 | } |
| 308 | |
Tadashi G. Takaoka | cc5e4a4 | 2013-01-18 12:19:59 +0900 | [diff] [blame] | 309 | public static int readKeyLongpressTimeout(final SharedPreferences prefs, |
| 310 | final Resources res) { |
Tadashi G. Takaoka | 276b1a2 | 2014-02-07 15:25:50 +0900 | [diff] [blame] | 311 | final int milliseconds = prefs.getInt( |
Tadashi G. Takaoka | ad2f695 | 2014-10-17 16:07:50 +0900 | [diff] [blame] | 312 | PREF_KEY_LONGPRESS_TIMEOUT, UNDEFINED_PREFERENCE_VALUE_INT); |
Tadashi G. Takaoka | 276b1a2 | 2014-02-07 15:25:50 +0900 | [diff] [blame] | 313 | return (milliseconds != UNDEFINED_PREFERENCE_VALUE_INT) ? milliseconds |
| 314 | : readDefaultKeyLongpressTimeout(res); |
Tadashi G. Takaoka | cc5e4a4 | 2013-01-18 12:19:59 +0900 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | public static int readDefaultKeyLongpressTimeout(final Resources res) { |
| 318 | return res.getInteger(R.integer.config_default_longpress_key_timeout); |
| 319 | } |
| 320 | |
Tadashi G. Takaoka | a7a7f4e | 2013-01-10 12:06:00 +0900 | [diff] [blame] | 321 | public static int readKeypressVibrationDuration(final SharedPreferences prefs, |
Tadashi G. Takaoka | d3b0ece | 2013-01-08 18:20:31 +0900 | [diff] [blame] | 322 | final Resources res) { |
Tadashi G. Takaoka | 276b1a2 | 2014-02-07 15:25:50 +0900 | [diff] [blame] | 323 | final int milliseconds = prefs.getInt( |
| 324 | PREF_VIBRATION_DURATION_SETTINGS, UNDEFINED_PREFERENCE_VALUE_INT); |
| 325 | return (milliseconds != UNDEFINED_PREFERENCE_VALUE_INT) ? milliseconds |
| 326 | : readDefaultKeypressVibrationDuration(res); |
Tadashi G. Takaoka | a7a7f4e | 2013-01-10 12:06:00 +0900 | [diff] [blame] | 327 | } |
| 328 | |
Tadashi G. Takaoka | bd9887a | 2014-02-10 12:54:45 +0900 | [diff] [blame] | 329 | // Default keypress vibration duration for unknown devices. |
| 330 | // The negative value means system default. |
| 331 | private static final String DEFAULT_KEYPRESS_VIBRATION_DURATION = Integer.toString(-1); |
| 332 | |
Tadashi G. Takaoka | a7a7f4e | 2013-01-10 12:06:00 +0900 | [diff] [blame] | 333 | public static int readDefaultKeypressVibrationDuration(final Resources res) { |
Tadashi G. Takaoka | bd9887a | 2014-02-10 12:54:45 +0900 | [diff] [blame] | 334 | return Integer.parseInt(ResourceUtils.getDeviceOverrideValue(res, |
| 335 | R.array.keypress_vibration_durations, DEFAULT_KEYPRESS_VIBRATION_DURATION)); |
Tadashi G. Takaoka | d3b0ece | 2013-01-08 18:20:31 +0900 | [diff] [blame] | 336 | } |
| 337 | |
Tadashi G. Takaoka | 276b1a2 | 2014-02-07 15:25:50 +0900 | [diff] [blame] | 338 | public static float readKeyPreviewAnimationScale(final SharedPreferences prefs, |
| 339 | final String prefKey, final float defaultValue) { |
| 340 | final float fraction = prefs.getFloat(prefKey, UNDEFINED_PREFERENCE_VALUE_FLOAT); |
| 341 | return (fraction != UNDEFINED_PREFERENCE_VALUE_FLOAT) ? fraction : defaultValue; |
| 342 | } |
| 343 | |
| 344 | public static int readKeyPreviewAnimationDuration(final SharedPreferences prefs, |
| 345 | final String prefKey, final int defaultValue) { |
| 346 | final int milliseconds = prefs.getInt(prefKey, UNDEFINED_PREFERENCE_VALUE_INT); |
| 347 | return (milliseconds != UNDEFINED_PREFERENCE_VALUE_INT) ? milliseconds : defaultValue; |
| 348 | } |
| 349 | |
Tadashi G. Takaoka | be819db | 2014-12-09 17:53:24 +0900 | [diff] [blame] | 350 | public static float readKeyboardHeight(final SharedPreferences prefs, |
| 351 | final float defaultValue) { |
| 352 | final float percentage = prefs.getFloat( |
| 353 | DebugSettings.PREF_KEYBOARD_HEIGHT_SCALE, UNDEFINED_PREFERENCE_VALUE_FLOAT); |
Andy Wang | bb9400a | 2014-10-21 17:03:05 +0900 | [diff] [blame] | 354 | return (percentage != UNDEFINED_PREFERENCE_VALUE_FLOAT) ? percentage : defaultValue; |
| 355 | } |
| 356 | |
Tadashi G. Takaoka | d3b0ece | 2013-01-08 18:20:31 +0900 | [diff] [blame] | 357 | public static boolean readUseFullscreenMode(final Resources res) { |
| 358 | return res.getBoolean(R.bool.config_use_fullscreen_mode); |
| 359 | } |
Tadashi G. Takaoka | 2e1b55f | 2013-03-05 12:54:13 +0900 | [diff] [blame] | 360 | |
Chieu Nguyen | a2376cd | 2015-04-22 14:41:45 -0700 | [diff] [blame] | 361 | public static boolean readShowSetupWizardIcon(final SharedPreferences prefs, |
| 362 | final Context context) { |
| 363 | if (!prefs.contains(PREF_SHOW_SETUP_WIZARD_ICON)) { |
| 364 | final ApplicationInfo appInfo = context.getApplicationInfo(); |
| 365 | final boolean isApplicationInSystemImage = |
| 366 | (appInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0; |
| 367 | // Default value |
| 368 | return !isApplicationInSystemImage; |
| 369 | } |
| 370 | return prefs.getBoolean(PREF_SHOW_SETUP_WIZARD_ICON, false); |
| 371 | } |
| 372 | |
Jean Chalard | 6bb6d30 | 2014-09-03 19:36:35 +0900 | [diff] [blame] | 373 | public static boolean readHasHardwareKeyboard(final Configuration conf) { |
| 374 | // The standard way of finding out whether we have a hardware keyboard. This code is taken |
| 375 | // from InputMethodService#onEvaluateInputShown, which canonically determines this. |
| 376 | // In a nutshell, we have a keyboard if the configuration says the type of hardware keyboard |
| 377 | // is NOKEYS and if it's not hidden (e.g. folded inside the device). |
| 378 | return conf.keyboard != Configuration.KEYBOARD_NOKEYS |
| 379 | && conf.hardKeyboardHidden != Configuration.HARDKEYBOARDHIDDEN_YES; |
| 380 | } |
| 381 | |
Satoshi Kataoka | 756791e | 2013-03-13 13:08:34 +0900 | [diff] [blame] | 382 | public static boolean isInternal(final SharedPreferences prefs) { |
Tadashi G. Takaoka | a3920c7 | 2014-04-25 11:49:33 -0700 | [diff] [blame] | 383 | return prefs.getBoolean(PREF_KEY_IS_INTERNAL, false); |
Satoshi Kataoka | 756791e | 2013-03-13 13:08:34 +0900 | [diff] [blame] | 384 | } |
Satoshi Kataoka | d7491e6 | 2013-08-02 15:22:17 +0900 | [diff] [blame] | 385 | |
Satoshi Kataoka | 283cf9c | 2013-08-16 14:35:56 +0900 | [diff] [blame] | 386 | public void writeLastUsedPersonalizationToken(byte[] token) { |
Keisuke Kuroyanagi | f1457e7 | 2013-12-25 11:44:16 +0900 | [diff] [blame] | 387 | if (token == null) { |
| 388 | mPrefs.edit().remove(PREF_LAST_USED_PERSONALIZATION_TOKEN).apply(); |
| 389 | } else { |
| 390 | final String tokenStr = StringUtils.byteArrayToHexString(token); |
| 391 | mPrefs.edit().putString(PREF_LAST_USED_PERSONALIZATION_TOKEN, tokenStr).apply(); |
| 392 | } |
Satoshi Kataoka | 283cf9c | 2013-08-16 14:35:56 +0900 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | public byte[] readLastUsedPersonalizationToken() { |
| 396 | final String tokenStr = mPrefs.getString(PREF_LAST_USED_PERSONALIZATION_TOKEN, null); |
| 397 | return StringUtils.hexStringToByteArray(tokenStr); |
| 398 | } |
Satoshi Kataoka | f3f0000 | 2013-09-16 17:13:41 +0900 | [diff] [blame] | 399 | |
Keisuke Kuroyanagi | ba07df7 | 2014-01-10 16:30:59 +0900 | [diff] [blame] | 400 | public void writeLastPersonalizationDictWipedTime(final long timestamp) { |
| 401 | mPrefs.edit().putLong(PREF_LAST_PERSONALIZATION_DICT_WIPED_TIME, timestamp).apply(); |
| 402 | } |
| 403 | |
| 404 | public long readLastPersonalizationDictGeneratedTime() { |
| 405 | return mPrefs.getLong(PREF_LAST_PERSONALIZATION_DICT_WIPED_TIME, 0); |
| 406 | } |
| 407 | |
Keisuke Kuroyanagi | eb28bac | 2014-01-10 20:28:30 +0900 | [diff] [blame] | 408 | public void writeCorpusHandlesForPersonalization(final Set<String> corpusHandles) { |
| 409 | mPrefs.edit().putStringSet(PREF_CORPUS_HANDLES_FOR_PERSONALIZATION, corpusHandles).apply(); |
| 410 | } |
| 411 | |
| 412 | public Set<String> readCorpusHandlesForPersonalization() { |
| 413 | final Set<String> emptySet = Collections.emptySet(); |
| 414 | return mPrefs.getStringSet(PREF_CORPUS_HANDLES_FOR_PERSONALIZATION, emptySet); |
| 415 | } |
| 416 | |
Satoshi Kataoka | f3f0000 | 2013-09-16 17:13:41 +0900 | [diff] [blame] | 417 | public static void writeEmojiRecentKeys(final SharedPreferences prefs, String str) { |
| 418 | prefs.edit().putString(PREF_EMOJI_RECENT_KEYS, str).apply(); |
| 419 | } |
| 420 | |
| 421 | public static String readEmojiRecentKeys(final SharedPreferences prefs) { |
| 422 | return prefs.getString(PREF_EMOJI_RECENT_KEYS, ""); |
| 423 | } |
| 424 | |
Satoshi Kataoka | 6dc99dc | 2013-10-11 14:54:26 +0900 | [diff] [blame] | 425 | public static void writeLastTypedEmojiCategoryPageId( |
| 426 | final SharedPreferences prefs, final int categoryId, final int categoryPageId) { |
| 427 | final String key = PREF_EMOJI_CATEGORY_LAST_TYPED_ID + categoryId; |
| 428 | prefs.edit().putInt(key, categoryPageId).apply(); |
Satoshi Kataoka | f3f0000 | 2013-09-16 17:13:41 +0900 | [diff] [blame] | 429 | } |
| 430 | |
Satoshi Kataoka | 6dc99dc | 2013-10-11 14:54:26 +0900 | [diff] [blame] | 431 | public static int readLastTypedEmojiCategoryPageId( |
| 432 | final SharedPreferences prefs, final int categoryId) { |
| 433 | final String key = PREF_EMOJI_CATEGORY_LAST_TYPED_ID + categoryId; |
Satoshi Kataoka | f3f0000 | 2013-09-16 17:13:41 +0900 | [diff] [blame] | 434 | return prefs.getInt(key, 0); |
| 435 | } |
Satoshi Kataoka | 6dc99dc | 2013-10-11 14:54:26 +0900 | [diff] [blame] | 436 | |
| 437 | public static void writeLastShownEmojiCategoryId( |
| 438 | final SharedPreferences prefs, final int categoryId) { |
| 439 | prefs.edit().putInt(PREF_LAST_SHOWN_EMOJI_CATEGORY_ID, categoryId).apply(); |
| 440 | } |
| 441 | |
| 442 | public static int readLastShownEmojiCategoryId( |
| 443 | final SharedPreferences prefs, final int defValue) { |
| 444 | return prefs.getInt(PREF_LAST_SHOWN_EMOJI_CATEGORY_ID, defValue); |
| 445 | } |
Chieu Nguyen | 116e58c | 2015-03-12 19:39:06 -0700 | [diff] [blame] | 446 | |
| 447 | private void upgradeAutocorrectionSettings(final SharedPreferences prefs, final Resources res) { |
| 448 | final String thresholdSetting = |
| 449 | prefs.getString(PREF_AUTO_CORRECTION_THRESHOLD_OBSOLETE, null); |
| 450 | if (thresholdSetting != null) { |
| 451 | SharedPreferences.Editor editor = prefs.edit(); |
| 452 | editor.remove(PREF_AUTO_CORRECTION_THRESHOLD_OBSOLETE); |
| 453 | final String autoCorrectionOff = |
| 454 | res.getString(R.string.auto_correction_threshold_mode_index_off); |
| 455 | if (thresholdSetting.equals(autoCorrectionOff)) { |
| 456 | editor.putBoolean(PREF_AUTO_CORRECTION, false); |
| 457 | } else { |
| 458 | editor.putBoolean(PREF_AUTO_CORRECTION, true); |
| 459 | } |
| 460 | editor.commit(); |
| 461 | } |
| 462 | } |
Ken Wakasa | 644d33d | 2011-10-28 00:33:15 +0900 | [diff] [blame] | 463 | } |