|
|
@ -8,16 +8,15 @@ import android.os.LocaleList |
|
|
|
import io.legado.app.constant.PreferKey |
|
|
|
import io.legado.app.constant.PreferKey |
|
|
|
import io.legado.app.utils.getPrefInt |
|
|
|
import io.legado.app.utils.getPrefInt |
|
|
|
import io.legado.app.utils.getPrefString |
|
|
|
import io.legado.app.utils.getPrefString |
|
|
|
|
|
|
|
import io.legado.app.utils.sysConfiguration |
|
|
|
import java.util.* |
|
|
|
import java.util.* |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Suppress("unused") |
|
|
|
object AppContextWrapper { |
|
|
|
object AppContextWrapper { |
|
|
|
|
|
|
|
|
|
|
|
fun wrap(context: Context): Context { |
|
|
|
fun wrap(context: Context): Context { |
|
|
|
var fontScale = context.getPrefInt(PreferKey.fontScale) / 10f |
|
|
|
|
|
|
|
if (fontScale !in 0.8f..1.6f) { |
|
|
|
|
|
|
|
fontScale = 0f |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
val resources: Resources = context.resources |
|
|
|
val resources: Resources = context.resources |
|
|
|
val configuration: Configuration = resources.configuration |
|
|
|
val configuration: Configuration = resources.configuration |
|
|
|
val targetLocale = getSetLocale(context) |
|
|
|
val targetLocale = getSetLocale(context) |
|
|
@ -28,20 +27,28 @@ object AppContextWrapper { |
|
|
|
@Suppress("DEPRECATION") |
|
|
|
@Suppress("DEPRECATION") |
|
|
|
configuration.locale = targetLocale |
|
|
|
configuration.locale = targetLocale |
|
|
|
} |
|
|
|
} |
|
|
|
configuration.fontScale = fontScale |
|
|
|
configuration.fontScale = getFontScale(context) |
|
|
|
return context.createConfigurationContext(configuration) |
|
|
|
return context.createConfigurationContext(configuration) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun getFontScale(context: Context): Float { |
|
|
|
|
|
|
|
var fontScale = context.getPrefInt(PreferKey.fontScale) / 10f |
|
|
|
|
|
|
|
if (fontScale !in 0.8f..1.6f) { |
|
|
|
|
|
|
|
fontScale = sysConfiguration.fontScale |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return fontScale |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 当前系统语言 |
|
|
|
* 当前系统语言 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private fun getSystemLocale(): Locale { |
|
|
|
private fun getSystemLocale(): Locale { |
|
|
|
val locale: Locale |
|
|
|
val locale: Locale |
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //7.0有多语言设置获取顶部的语言 |
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //7.0有多语言设置获取顶部的语言 |
|
|
|
locale = Resources.getSystem().configuration.locales.get(0) |
|
|
|
locale = sysConfiguration.locales.get(0) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
@Suppress("DEPRECATION") |
|
|
|
@Suppress("DEPRECATION") |
|
|
|
locale = Resources.getSystem().configuration.locale |
|
|
|
locale = sysConfiguration.locale |
|
|
|
} |
|
|
|
} |
|
|
|
return locale |
|
|
|
return locale |
|
|
|
} |
|
|
|
} |
|
|
|