Twidere-App-Android-Twitter.../twidere/src/main/kotlin/org/mariotaku/twidere/constant/PreferenceKeys.kt

310 lines
14 KiB
Kotlin
Raw Normal View History

2016-08-25 08:14:29 +02:00
package org.mariotaku.twidere.constant
import android.content.SharedPreferences
import android.os.Build
import android.text.TextUtils
2020-05-12 09:20:12 +02:00
import androidx.collection.ArraySet
import androidx.core.os.LocaleHelperAccessor
2016-08-25 08:14:29 +02:00
import org.mariotaku.kpreferences.*
2017-03-23 03:41:04 +01:00
import org.mariotaku.ktextension.bcp47Tag
2017-04-07 06:12:34 +02:00
import org.mariotaku.ktextension.toLongOr
2016-12-14 09:02:50 +01:00
import org.mariotaku.twidere.BuildConfig
2017-01-26 16:15:05 +01:00
import org.mariotaku.twidere.Constants.*
2020-05-13 10:36:15 +02:00
import org.mariotaku.twidere.TwidereConstants
2017-02-02 11:18:34 +01:00
import org.mariotaku.twidere.TwidereConstants.KEY_MEDIA_PRELOAD
2017-01-03 16:37:24 +01:00
import org.mariotaku.twidere.annotation.AccountType
2017-03-02 09:26:12 +01:00
import org.mariotaku.twidere.annotation.ImageShapeStyle
2017-06-25 17:01:33 +02:00
import org.mariotaku.twidere.annotation.NavbarStyle
import org.mariotaku.twidere.annotation.PreviewStyle
2016-08-25 08:14:29 +02:00
import org.mariotaku.twidere.extension.getNonEmptyString
import org.mariotaku.twidere.model.CustomAPIConfig
2017-01-21 05:08:47 +01:00
import org.mariotaku.twidere.model.UserKey
import org.mariotaku.twidere.model.account.cred.Credentials
import org.mariotaku.twidere.model.timeline.UserTimelineFilter
2017-01-03 14:22:20 +01:00
import org.mariotaku.twidere.preference.ThemeBackgroundPreference
2017-04-13 18:57:14 +02:00
import org.mariotaku.twidere.util.sync.DataSyncProvider
2017-03-23 03:41:04 +01:00
import java.util.*
2016-08-25 08:14:29 +02:00
/**
* Created by mariotaku on 16/8/25.
*/
val textSizeKey = KIntKey(KEY_TEXT_SIZE, 15)
val nameFirstKey = KBooleanKey(KEY_NAME_FIRST, true)
val displayProfileImageKey = KBooleanKey(KEY_DISPLAY_PROFILE_IMAGE, true)
val mediaPreviewKey = KBooleanKey(KEY_MEDIA_PREVIEW, true)
val bandwidthSavingModeKey = KBooleanKey(KEY_BANDWIDTH_SAVING_MODE, false)
val displaySensitiveContentsKey = KBooleanKey(KEY_DISPLAY_SENSITIVE_CONTENTS, false)
val hideCardActionsKey = KBooleanKey(KEY_HIDE_CARD_ACTIONS, false)
val iWantMyStarsBackKey = KBooleanKey(KEY_I_WANT_MY_STARS_BACK, false)
2017-04-25 05:36:37 +02:00
val favoriteConfirmationKey = KBooleanKey(KEY_FAVORITE_CONFIRMATION, false)
2016-08-25 08:14:29 +02:00
val showAbsoluteTimeKey = KBooleanKey(KEY_SHOW_ABSOLUTE_TIME, false)
val statusShortenerKey = KNullableStringKey(KEY_STATUS_SHORTENER, null)
val mediaUploaderKey = KNullableStringKey(KEY_MEDIA_UPLOADER, null)
val newDocumentApiKey = KBooleanKey(KEY_NEW_DOCUMENT_API, Build.VERSION.SDK_INT == Build.VERSION_CODES.M)
val rememberPositionKey = KBooleanKey(KEY_REMEMBER_POSITION, true)
2016-12-13 04:06:07 +01:00
val attachLocationKey = KBooleanKey(KEY_ATTACH_LOCATION, false)
val attachPreciseLocationKey = KBooleanKey(KEY_ATTACH_PRECISE_LOCATION, false)
val noCloseAfterTweetSentKey = KBooleanKey(KEY_NO_CLOSE_AFTER_TWEET_SENT, false)
2017-04-19 08:50:47 +02:00
val loadItemLimitKey = KIntKey(KEY_LOAD_ITEM_LIMIT, DEFAULT_LOAD_ITEM_LIMIT, 10..200)
2017-02-10 16:38:59 +01:00
val databaseItemLimitKey = KIntKey(KEY_DATABASE_ITEM_LIMIT, DEFAULT_DATABASE_ITEM_LIMIT)
val defaultFeatureLastUpdated = KLongKey("default_feature_last_updated", -1)
2016-12-13 12:35:25 +01:00
val drawerTutorialCompleted = KBooleanKey(KEY_SETTINGS_WIZARD_COMPLETED, false)
2016-12-14 09:02:50 +01:00
val stopAutoRefreshWhenBatteryLowKey = KBooleanKey(KEY_STOP_AUTO_REFRESH_WHEN_BATTERY_LOW, true)
val apiLastChangeKey = KLongKey(KEY_API_LAST_CHANGE, -1)
val bugReportsKey = KBooleanKey(KEY_BUG_REPORTS, BuildConfig.DEBUG)
2016-12-16 01:38:37 +01:00
val readFromBottomKey = KBooleanKey(KEY_READ_FROM_BOTTOM, false)
val randomizeAccountNameKey = KBooleanKey(KEY_RANDOMIZE_ACCOUNT_NAME, false)
2016-12-17 05:10:24 +01:00
val defaultAutoRefreshKey = KBooleanKey(KEY_DEFAULT_AUTO_REFRESH, false)
2016-12-23 02:26:52 +01:00
val defaultAutoRefreshAskedKey = KBooleanKey("default_auto_refresh_asked", true)
val unreadCountKey = KBooleanKey(KEY_UNREAD_COUNT, true)
val drawerToggleKey = KBooleanKey(KEY_DRAWER_TOGGLE, false)
val fabVisibleKey = KBooleanKey(KEY_FAB_VISIBLE, true)
2016-12-23 15:46:59 +01:00
val themeKey = KStringKey(KEY_THEME, VALUE_THEME_NAME_LIGHT)
val themeColorKey = KIntKey(KEY_THEME_COLOR, 0)
2017-04-25 05:36:37 +02:00
val filterUnavailableQuoteStatusesKey = KBooleanKey(KEY_FILTER_UNAVAILABLE_QUOTE_STATUSES, false)
val filterPossibilitySensitiveStatusesKey = KBooleanKey(KEY_FILTER_POSSIBILITY_SENSITIVE_STATUSES, false)
val chromeCustomTabKey = KBooleanKey(KEY_CHROME_CUSTOM_TAB, true)
2017-01-26 16:15:05 +01:00
val usageStatisticsKey = KBooleanKey(KEY_USAGE_STATISTICS, false)
2017-04-25 05:36:37 +02:00
val lightFontKey = KBooleanKey(KEY_LIGHT_FONT, false)
2017-02-01 07:04:14 +01:00
val extraFeaturesNoticeVersionKey = KIntKey("extra_features_notice_version", 0)
2017-02-02 11:18:34 +01:00
val mediaPreloadKey = KBooleanKey(KEY_MEDIA_PRELOAD, false)
val mediaPreloadOnWifiOnlyKey = KBooleanKey(KEY_PRELOAD_WIFI_ONLY, true)
2017-04-25 05:36:37 +02:00
val autoRefreshCompatibilityModeKey = KBooleanKey(KEY_AUTO_REFRESH_COMPATIBILITY_MODE,
Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
val floatingDetailedContentsKey = KBooleanKey(KEY_FLOATING_DETAILED_CONTENTS, true)
2017-02-07 18:17:52 +01:00
val localTrendsWoeIdKey = KIntKey(KEY_LOCAL_TRENDS_WOEID, 1)
val phishingLinksWaringKey = KBooleanKey(KEY_PHISHING_LINK_WARNING, true)
2017-04-25 05:36:37 +02:00
val multiColumnWidthKey = KStringKey(KEY_MULTI_COLUMN_TAB_WIDTH, "normal")
2017-04-17 15:10:14 +02:00
val quickSendKey = KBooleanKey(KEY_QUICK_SEND, false)
val refreshAfterTweetKey = KBooleanKey(KEY_REFRESH_AFTER_TWEET, false)
2017-06-25 17:01:33 +02:00
val refreshOnStartKey = KBooleanKey(KEY_REFRESH_ON_START, false)
2017-04-17 15:10:14 +02:00
val homeRefreshMentionsKey = KBooleanKey(KEY_HOME_REFRESH_MENTIONS, true)
val homeRefreshDirectMessagesKey = KBooleanKey(KEY_HOME_REFRESH_DIRECT_MESSAGES, true)
val homeRefreshSavedSearchesKey = KBooleanKey(KEY_HOME_REFRESH_SAVED_SEARCHES, true)
2017-04-27 07:32:32 +02:00
val composeStatusVisibilityKey = KNullableStringKey("compose_status_visibility", null)
2017-06-25 17:01:33 +02:00
val navbarStyleKey = KStringKey(KEY_NAVBAR_STYLE, NavbarStyle.DEFAULT)
val lastLaunchTimeKey = KLongKey("last_launch_time", -1)
2017-08-25 05:34:24 +02:00
val promotionsEnabledKey = KBooleanKey("promotions_enabled", false)
2017-08-28 11:56:09 +02:00
val translationDestinationKey = KNullableStringKey(KEY_TRANSLATION_DESTINATION, null)
2020-05-12 09:20:12 +02:00
val tabPositionKey = KStringKey(KEY_TAB_POSITION, SharedPreferenceConstants.DEFAULT_TAB_POSITION)
2020-05-13 10:36:15 +02:00
val yandexKeyKey = KStringKey(SharedPreferenceConstants.KEY_YANDEX_KEY, TwidereConstants.YANDEX_KEY)
2020-05-14 08:34:03 +02:00
val autoHideTabs = KBooleanKey(SharedPreferenceConstants.KEY_AUTO_HIDE_TABS, true)
2020-05-14 09:47:36 +02:00
val hideCardNumbersKey = KBooleanKey(KEY_HIDE_CARD_NUMBERS, false)
2020-06-02 11:15:34 +02:00
val showLinkPreviewKey = KBooleanKey(KEY_SHOW_LINK_PREVIEW, false)
2020-05-14 09:47:36 +02:00
2017-04-17 15:10:14 +02:00
object cacheSizeLimitKey : KSimpleKey<Int>(KEY_CACHE_SIZE_LIMIT, 300) {
override fun read(preferences: SharedPreferences) = preferences.getInt(key, def).coerceIn(100,
500)
override fun write(editor: SharedPreferences.Editor, value: Int): Boolean {
editor.putInt(key, value)
return true
}
}
2017-01-16 17:38:50 +01:00
2017-09-04 18:21:41 +02:00
object overrideLanguageKey : KSimpleKey<Locale?>(KEY_OVERRIDE_LANGUAGE, null) {
2017-03-23 03:41:04 +01:00
override fun read(preferences: SharedPreferences): Locale? {
2017-09-25 08:04:37 +02:00
return preferences.getString(key, null)?.takeIf(String::isNotEmpty)
?.let(LocaleHelperAccessor::forLanguageTag)
2017-03-23 03:41:04 +01:00
}
override fun write(editor: SharedPreferences.Editor, value: Locale?): Boolean {
editor.putString(key, value?.bcp47Tag)
return true
}
}
2017-03-21 03:33:27 +01:00
val themeBackgroundOptionKey = KStringKey(KEY_THEME_BACKGROUND, VALUE_THEME_BACKGROUND_DEFAULT)
2017-01-03 14:22:20 +01:00
object themeBackgroundAlphaKey : KSimpleKey<Int>(KEY_THEME_BACKGROUND_ALPHA, 0xFF) {
override fun read(preferences: SharedPreferences): Int {
return preferences.getInt(KEY_THEME_BACKGROUND_ALPHA, DEFAULT_THEME_BACKGROUND_ALPHA)
.coerceIn(ThemeBackgroundPreference.MIN_ALPHA, ThemeBackgroundPreference.MAX_ALPHA)
}
override fun write(editor: SharedPreferences.Editor, value: Int): Boolean {
editor.putInt(key, value.coerceIn(ThemeBackgroundPreference.MIN_ALPHA,
ThemeBackgroundPreference.MAX_ALPHA))
return true
}
}
2017-03-02 09:26:12 +01:00
object profileImageStyleKey : KSimpleKey<Int>(KEY_PROFILE_IMAGE_STYLE, ImageShapeStyle.SHAPE_CIRCLE) {
override fun read(preferences: SharedPreferences): Int {
2017-03-02 09:26:12 +01:00
if (preferences.getString(key, null) == VALUE_PROFILE_IMAGE_STYLE_SQUARE) return ImageShapeStyle.SHAPE_RECTANGLE
return ImageShapeStyle.SHAPE_CIRCLE
}
override fun write(editor: SharedPreferences.Editor, value: Int): Boolean {
2017-03-02 09:26:12 +01:00
editor.putString(key, if (value == ImageShapeStyle.SHAPE_CIRCLE) VALUE_PROFILE_IMAGE_STYLE_ROUND else VALUE_PROFILE_IMAGE_STYLE_SQUARE)
return true
}
}
object mediaPreviewStyleKey : KSimpleKey<Int>(KEY_MEDIA_PREVIEW_STYLE, PreviewStyle.CROP) {
2017-01-07 07:16:02 +01:00
override fun read(preferences: SharedPreferences): Int {
2020-06-08 23:19:10 +02:00
return when (preferences.getString(key, null)) {
VALUE_MEDIA_PREVIEW_STYLE_SCALE -> PreviewStyle.SCALE
VALUE_MEDIA_PREVIEW_STYLE_REAL_SIZE -> PreviewStyle.ACTUAL_SIZE
else -> PreviewStyle.CROP
}
2017-01-07 07:16:02 +01:00
}
override fun write(editor: SharedPreferences.Editor, value: Int): Boolean {
editor.putString(key, when (value) {
PreviewStyle.SCALE -> VALUE_MEDIA_PREVIEW_STYLE_SCALE
2017-05-18 05:25:28 +02:00
PreviewStyle.ACTUAL_SIZE -> VALUE_MEDIA_PREVIEW_STYLE_REAL_SIZE
else -> VALUE_MEDIA_PREVIEW_STYLE_CROP
})
2017-01-07 07:16:02 +01:00
return true
}
}
2017-01-16 17:38:50 +01:00
object linkHighlightOptionKey : KSimpleKey<Int>(KEY_LINK_HIGHLIGHT_OPTION, VALUE_LINK_HIGHLIGHT_OPTION_CODE_NONE) {
override fun read(preferences: SharedPreferences): Int = when (preferences.getString(key, null)) {
VALUE_LINK_HIGHLIGHT_OPTION_BOTH -> VALUE_LINK_HIGHLIGHT_OPTION_CODE_BOTH
VALUE_LINK_HIGHLIGHT_OPTION_UNDERLINE -> VALUE_LINK_HIGHLIGHT_OPTION_CODE_UNDERLINE
VALUE_LINK_HIGHLIGHT_OPTION_HIGHLIGHT -> VALUE_LINK_HIGHLIGHT_OPTION_CODE_HIGHLIGHT
else -> VALUE_LINK_HIGHLIGHT_OPTION_CODE_NONE
}
override fun write(editor: SharedPreferences.Editor, value: Int): Boolean {
editor.putString(key, when (value) {
VALUE_LINK_HIGHLIGHT_OPTION_CODE_BOTH -> VALUE_LINK_HIGHLIGHT_OPTION_BOTH
VALUE_LINK_HIGHLIGHT_OPTION_CODE_UNDERLINE -> VALUE_LINK_HIGHLIGHT_OPTION_UNDERLINE
VALUE_LINK_HIGHLIGHT_OPTION_CODE_HIGHLIGHT -> VALUE_LINK_HIGHLIGHT_OPTION_HIGHLIGHT
else -> VALUE_LINK_HIGHLIGHT_OPTION_NONE
})
return true
}
}
2016-12-17 16:05:02 +01:00
object refreshIntervalKey : KSimpleKey<Long>(KEY_REFRESH_INTERVAL, 15) {
override fun read(preferences: SharedPreferences): Long {
2017-04-07 06:12:34 +02:00
return preferences.getString(key, null).toLongOr(def)
2016-12-17 16:05:02 +01:00
}
override fun write(editor: SharedPreferences.Editor, value: Long): Boolean {
editor.putString(key, value.toString())
return true
}
}
2016-08-25 08:14:29 +02:00
object defaultAPIConfigKey : KPreferenceKey<CustomAPIConfig> {
override fun contains(preferences: SharedPreferences): Boolean {
if (preferences.getString(KEY_API_URL_FORMAT, null) == null) return false
return true
}
override fun read(preferences: SharedPreferences): CustomAPIConfig {
val apiUrlFormat = preferences.getNonEmptyString(KEY_API_URL_FORMAT, DEFAULT_TWITTER_API_URL_FORMAT)
val authType = preferences.getString(KEY_CREDENTIALS_TYPE, Credentials.Type.OAUTH)
2017-01-03 16:37:24 +01:00
val customApiType = preferences.getString(KEY_CUSTOM_API_TYPE, null) ?: AccountType.TWITTER
2016-08-25 08:14:29 +02:00
val sameOAuthSigningUrl = preferences.getBoolean(KEY_SAME_OAUTH_SIGNING_URL, false)
val noVersionSuffix = preferences.getBoolean(KEY_NO_VERSION_SUFFIX, false)
val consumerKey = preferences.getNonEmptyString(KEY_CONSUMER_KEY, TWITTER_CONSUMER_KEY).trim()
val consumerSecret = preferences.getNonEmptyString(KEY_CONSUMER_SECRET, TWITTER_CONSUMER_SECRET).trim()
2017-01-03 16:37:24 +01:00
return CustomAPIConfig("Default", customApiType, apiUrlFormat, authType, sameOAuthSigningUrl,
noVersionSuffix, consumerKey, consumerSecret).apply {
isDefault = true
}
2016-08-25 08:14:29 +02:00
}
override fun write(editor: SharedPreferences.Editor, value: CustomAPIConfig): Boolean {
if (!TextUtils.isEmpty(value.consumerKey) && !TextUtils.isEmpty(value.consumerSecret)) {
editor.putString(KEY_CONSUMER_KEY, value.consumerKey)
editor.putString(KEY_CONSUMER_SECRET, value.consumerSecret)
} else {
editor.remove(KEY_CONSUMER_KEY)
editor.remove(KEY_CONSUMER_SECRET)
}
editor.putString(KEY_API_URL_FORMAT, value.apiUrlFormat)
2017-01-03 16:37:24 +01:00
editor.putString(KEY_CUSTOM_API_TYPE, value.type)
editor.putString(KEY_CREDENTIALS_TYPE, value.credentialsType)
2016-08-25 08:14:29 +02:00
editor.putBoolean(KEY_SAME_OAUTH_SIGNING_URL, value.isSameOAuthUrl)
editor.putBoolean(KEY_NO_VERSION_SUFFIX, value.isNoVersionSuffix)
return true
}
2017-01-03 12:59:38 +01:00
}
2017-04-13 18:57:14 +02:00
object dataSyncProviderInfoKey : KPreferenceKey<DataSyncProvider?> {
2017-01-03 12:59:38 +01:00
private const val PROVIDER_TYPE_KEY = "sync_provider_type"
override fun contains(preferences: SharedPreferences): Boolean {
return read(preferences) != null
}
2017-04-13 18:57:14 +02:00
override fun read(preferences: SharedPreferences): DataSyncProvider? {
2017-01-03 12:59:38 +01:00
val type = preferences.getString(PROVIDER_TYPE_KEY, null) ?: return null
2017-04-13 18:57:14 +02:00
return DataSyncProvider.Factory.createForType(type, preferences)
2017-01-03 12:59:38 +01:00
}
2017-04-13 18:57:14 +02:00
override fun write(editor: SharedPreferences.Editor, value: DataSyncProvider?): Boolean {
2017-01-03 12:59:38 +01:00
if (value == null) {
editor.remove(PROVIDER_TYPE_KEY)
} else {
editor.putString(PROVIDER_TYPE_KEY, value.type)
value.writeToPreferences(editor)
}
return true
}
2017-01-21 05:08:47 +01:00
}
object composeAccountsKey : KSimpleKey<Array<UserKey>?>(KEY_COMPOSE_ACCOUNTS, null) {
override fun read(preferences: SharedPreferences): Array<UserKey>? {
val string = preferences.getString(key, null) ?: return null
return UserKey.arrayOf(string)
}
override fun write(editor: SharedPreferences.Editor, value: Array<UserKey>?): Boolean {
editor.putString(key, value?.joinToString(","))
return true
}
}
2017-09-04 18:21:41 +02:00
object defaultAccountKey : KSimpleKey<UserKey?>(KEY_DEFAULT_ACCOUNT_KEY, null) {
2017-04-20 14:04:37 +02:00
override fun read(preferences: SharedPreferences): UserKey? {
return preferences.getString(key, null)?.let(UserKey::valueOf)
}
override fun write(editor: SharedPreferences.Editor, value: UserKey?): Boolean {
editor.putString(key, value?.toString())
return true
}
}
object userTimelineFilterKey : KSimpleKey<UserTimelineFilter>("user_timeline_filter", UserTimelineFilter()) {
override fun read(preferences: SharedPreferences): UserTimelineFilter {
val rawString = preferences.getString(key, null) ?: return def
val options = rawString.split(",")
return UserTimelineFilter().apply {
isIncludeReplies = "replies" in options
isIncludeRetweets = "retweets" in options
}
}
override fun write(editor: SharedPreferences.Editor, value: UserTimelineFilter): Boolean {
val options = ArraySet<String>().apply {
if (value.isIncludeReplies) {
add("replies")
}
if (value.isIncludeRetweets) {
add("retweets")
}
}.joinToString(",")
editor.putString(key, options)
return true
}
2016-08-25 08:14:29 +02:00
}