improved language settings

This commit is contained in:
Mariotaku Lee 2017-09-05 00:21:41 +08:00
parent 053b36c6cc
commit e238a0ae5c
No known key found for this signature in database
GPG Key ID: 15C10F89D7C33535
9 changed files with 76 additions and 22 deletions

View File

@ -284,6 +284,8 @@ public interface SharedPreferenceConstants {
String KEY_STREAMING_POWER_SAVING = "streaming_power_saving";
@ExportablePreference(STRING)
String KEY_NAVBAR_STYLE = "navbar_style";
@ExportablePreference(STRING)
String KEY_OVERRIDE_LANGUAGE = "override_language";
// Internal preferences

View File

@ -0,0 +1,50 @@
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2017 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.ktextension
import android.app.Activity
import android.content.ComponentName
import android.content.pm.ActivityInfo
import android.graphics.drawable.Drawable
import android.support.v4.content.ContextCompat
val Activity.activityIcon: Drawable?
get() {
val info = activityInfo
val activityLabelRes = info.icon
if (activityLabelRes != 0) return ContextCompat.getDrawable(this, activityLabelRes)
val appLabelRes = applicationInfo.icon
if (appLabelRes != 0) return ContextCompat.getDrawable(this, appLabelRes)
return info.loadIcon(packageManager)
}
val Activity.activityLabel: CharSequence?
get() {
val info = activityInfo
val activityLabelRes = info.labelRes
if (activityLabelRes != 0) return getText(activityLabelRes)
val appLabelRes = applicationInfo.labelRes
if (appLabelRes != 0) return getText(appLabelRes)
return info.loadLabel(packageManager)
}
val Activity.activityInfo: ActivityInfo
get() = packageManager.getActivityInfo(ComponentName(this, javaClass), 0)

View File

@ -1,10 +1,8 @@
package org.mariotaku.ktextension
import android.content.BroadcastReceiver
import android.content.ComponentName
import android.content.Context
import android.content.pm.PackageManager
import android.graphics.drawable.Drawable
import android.support.v4.content.ContextCompat
/**
@ -27,10 +25,4 @@ fun Context.unregisterReceiverSafe(receiver: BroadcastReceiver?): Boolean {
} catch (e: IllegalArgumentException) {
return false
}
}
val Context.componentIcon: Drawable?
get() {
val info = packageManager.getActivityInfo(ComponentName(this, javaClass), 0)
return info.loadIcon(packageManager)
}
}

View File

@ -51,6 +51,7 @@ import org.mariotaku.chameleon.Chameleon
import org.mariotaku.chameleon.ChameleonActivity
import org.mariotaku.kpreferences.KPreferences
import org.mariotaku.kpreferences.get
import org.mariotaku.ktextension.activityLabel
import org.mariotaku.ktextension.getSystemWindowInsets
import org.mariotaku.ktextension.systemWindowInsets
import org.mariotaku.ktextension.unregisterReceiverSafe
@ -64,6 +65,7 @@ import org.mariotaku.twidere.activity.iface.IThemedActivity
import org.mariotaku.twidere.annotation.NavbarStyle
import org.mariotaku.twidere.constant.*
import org.mariotaku.twidere.extension.defaultSharedPreferences
import org.mariotaku.twidere.extension.firstLanguage
import org.mariotaku.twidere.extension.overriding
import org.mariotaku.twidere.fragment.iface.IBaseFragment.SystemWindowInsetsCallback
import org.mariotaku.twidere.model.DefaultFeatures
@ -256,6 +258,7 @@ open class BaseActivity : ChameleonActivity(), IBaseActivity<BaseActivity>, IThe
}
onApplyNavigationStyle(themeNavigationStyle, themeColor)
super.onCreate(savedInstanceState)
title = activityLabel
requestManager = Glide.with(this)
ActivitySupport.setTaskDescription(this, TaskDescriptionCompat(title.toString(), null,
ColorUtils.setAlphaComponent(overrideTheme.colorToolbar, 0xFF)))
@ -348,12 +351,9 @@ open class BaseActivity : ChameleonActivity(), IBaseActivity<BaseActivity>, IThe
actionHelper.dispatchOnResumeFragments(this)
}
override fun attachBaseContext(newBase: Context?) {
if (newBase == null) {
super.attachBaseContext(null)
return
}
val locale = newBase.defaultSharedPreferences[overrideLanguageKey]
override fun attachBaseContext(newBase: Context) {
val locale = newBase.defaultSharedPreferences[overrideLanguageKey] ?: Resources.getSystem()
.firstLanguage
if (locale == null) {
super.attachBaseContext(newBase)
return

View File

@ -42,7 +42,7 @@ import org.mariotaku.chameleon.Chameleon
import org.mariotaku.chameleon.ChameleonActivity
import org.mariotaku.kpreferences.get
import org.mariotaku.kpreferences.set
import org.mariotaku.ktextension.componentIcon
import org.mariotaku.ktextension.activityIcon
import org.mariotaku.ktextension.contains
import org.mariotaku.restfu.http.RestHttpClient
import org.mariotaku.twidere.BuildConfig
@ -121,7 +121,7 @@ open class MainActivity : ChameleonActivity(), IBaseActivity<MainActivity> {
}
main.visibility = View.VISIBLE
appIcon.setImageDrawable(componentIcon)
appIcon.setImageDrawable(activityIcon)
skipPresentation.setOnClickListener {
launchDirectly()
}

View File

@ -26,6 +26,7 @@ import android.content.*
import android.content.SharedPreferences.OnSharedPreferenceChangeListener
import android.content.pm.PackageManager
import android.content.res.Configuration
import android.content.res.Resources
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
import android.net.ConnectivityManager
@ -43,6 +44,7 @@ import org.mariotaku.kpreferences.get
import org.mariotaku.kpreferences.set
import org.mariotaku.ktextension.addOnAccountsUpdatedListenerSafe
import org.mariotaku.ktextension.isCurrentThreadCompat
import org.mariotaku.ktextension.localesCompat
import org.mariotaku.mediaviewer.library.MediaDownloader
import org.mariotaku.restfu.http.RestHttpClient
import org.mariotaku.twidere.BuildConfig
@ -52,6 +54,7 @@ import org.mariotaku.twidere.activity.AssistLauncherActivity
import org.mariotaku.twidere.activity.MainActivity
import org.mariotaku.twidere.activity.MainHondaJOJOActivity
import org.mariotaku.twidere.constant.*
import org.mariotaku.twidere.extension.firstLanguage
import org.mariotaku.twidere.extension.model.loadRemoteSettings
import org.mariotaku.twidere.extension.model.save
import org.mariotaku.twidere.extension.setLocale
@ -224,6 +227,9 @@ class TwidereApplication : Application(), OnSharedPreferenceChangeListener {
stopService(streamingIntent)
}
}
KEY_OVERRIDE_LANGUAGE -> {
applyLanguageSettings()
}
}
Analyzer.preferencesChanged(preferences)
}
@ -234,7 +240,8 @@ class TwidereApplication : Application(), OnSharedPreferenceChangeListener {
}
private fun applyLanguageSettings() {
val locale = sharedPreferences[overrideLanguageKey] ?: return
val locale = sharedPreferences[overrideLanguageKey] ?: Resources.getSystem().
firstLanguage ?: return
resources.setLocale(locale)
}

View File

@ -100,7 +100,7 @@ object cacheSizeLimitKey : KSimpleKey<Int>(KEY_CACHE_SIZE_LIMIT, 300) {
}
object overrideLanguageKey : KSimpleKey<Locale?>("override_language", null) {
object overrideLanguageKey : KSimpleKey<Locale?>(KEY_OVERRIDE_LANGUAGE, null) {
override fun read(preferences: SharedPreferences): Locale? {
return preferences.getString(key, null)?.takeIf(String::isNotEmpty)?.replace('-', '_')
?.let(LocaleUtils::toLocale)
@ -269,7 +269,7 @@ object composeAccountsKey : KSimpleKey<Array<UserKey>?>(KEY_COMPOSE_ACCOUNTS, nu
}
object defaultAccountKey: KSimpleKey<UserKey?>(KEY_DEFAULT_ACCOUNT_KEY, null) {
object defaultAccountKey : KSimpleKey<UserKey?>(KEY_DEFAULT_ACCOUNT_KEY, null) {
override fun read(preferences: SharedPreferences): UserKey? {
return preferences.getString(key, null)?.let(UserKey::valueOf)
}

View File

@ -30,4 +30,7 @@ fun Resources.setLocale(locale: Locale) {
config.localesCompat = LocaleListCompat.create(locale)
@Suppress("DEPRECATION")
updateConfiguration(config, displayMetrics)
}
}
val Resources.firstLanguage: Locale?
get() = configuration.localesCompat.takeIf { !it.isEmpty }?.get(0)

View File

@ -39,7 +39,7 @@
android:key="override_language"
android:title="@string/preference_title_override_language">
<extra
android:name="should_recreate"
android:name="should_restart"
android:value="true"/>
</org.mariotaku.twidere.preference.EntrySummaryListPreference>