Fixed language won't change in certain views unless app restarts

This commit is contained in:
Viktor De Pasquale 2019-05-10 16:22:03 +02:00
parent ff7a0ba599
commit 67c3f40adb
7 changed files with 50 additions and 44 deletions

View File

@ -9,12 +9,11 @@ import org.koin.dsl.module
val applicationModule = module {
single { RxBus() }
single { get<Context>().resources }
single { get<Context>() as App }
single { get<Context>().packageManager }
factory { get<Context>().resources }
factory { get<Context>() as App }
factory { get<Context>().packageManager }
single(SUTimeout) {
get<App>().protectedContext
.getSharedPreferences("su_timeout", 0)
get<App>().protectedContext.getSharedPreferences("su_timeout", 0)
}
single { PreferenceManager.getDefaultSharedPreferences(get<App>().protectedContext) }
}

View File

@ -15,7 +15,7 @@ import org.koin.dsl.module
val viewModelModules = module {
viewModel { MainViewModel() }
viewModel { HomeViewModel(get(), get()) }
viewModel { HomeViewModel(get()) }
viewModel { SuperuserViewModel(get(), get(), get(), get()) }
viewModel { HideViewModel(get(), get()) }
viewModel { ModuleViewModel(get(), get()) }

View File

@ -62,7 +62,7 @@ abstract class MagiskActivity<ViewModel : MagiskViewModel, Binding : ViewDataBin
override fun applyOverrideConfiguration(config: Configuration?) {
// Force applying our preferred local
config!!.setLocale(LocaleManager.locale)
config?.setLocale(LocaleManager.locale)
super.applyOverrideConfiguration(config)
}

View File

@ -1,23 +1,23 @@
package com.topjohnwu.magisk.ui.home
import android.content.res.Resources
import android.content.Context
import com.skoumal.teanity.extensions.addOnPropertyChangedCallback
import com.skoumal.teanity.util.KObservableField
import com.topjohnwu.magisk.*
import com.topjohnwu.magisk.BuildConfig
import com.topjohnwu.magisk.Config
import com.topjohnwu.magisk.Const
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.model.events.*
import com.topjohnwu.magisk.model.observer.Observer
import com.topjohnwu.magisk.tasks.CheckUpdates
import com.topjohnwu.magisk.ui.base.MagiskViewModel
import com.topjohnwu.magisk.utils.Event
import com.topjohnwu.magisk.utils.ISafetyNetHelper
import com.topjohnwu.magisk.utils.toggle
import com.topjohnwu.magisk.utils.*
import com.topjohnwu.net.Networking
import com.topjohnwu.superuser.Shell
class HomeViewModel(
private val resources: Resources,
private val app: App
private val context: Context
) : MagiskViewModel() {
val isAdvancedExpanded = KObservableField(false)
@ -29,17 +29,17 @@ class HomeViewModel(
val magiskStateText = Observer(magiskState) {
when (magiskState.value) {
MagiskState.NO_ROOT -> TODO()
MagiskState.NOT_INSTALLED -> resources.getString(R.string.magisk_version_error)
MagiskState.UP_TO_DATE -> resources.getString(R.string.magisk_up_to_date)
MagiskState.LOADING -> resources.getString(R.string.checking_for_updates)
MagiskState.OBSOLETE -> resources.getString(R.string.magisk_update_title)
MagiskState.NOT_INSTALLED -> R.string.magisk_version_error.res()
MagiskState.UP_TO_DATE -> R.string.magisk_up_to_date.res()
MagiskState.LOADING -> R.string.checking_for_updates.res()
MagiskState.OBSOLETE -> R.string.magisk_update_title.res()
}
}
val magiskCurrentVersion = KObservableField("")
val magiskLatestVersion = KObservableField("")
val magiskAdditionalInfo = Observer(magiskState) {
if (Config.get<Boolean>(Config.Key.COREONLY))
resources.getString(R.string.core_only_enabled)
R.string.core_only_enabled.res()
else
""
}
@ -48,22 +48,22 @@ class HomeViewModel(
val managerStateText = Observer(managerState) {
when (managerState.value) {
MagiskState.NO_ROOT -> "wtf"
MagiskState.NOT_INSTALLED -> resources.getString(R.string.invalid_update_channel)
MagiskState.UP_TO_DATE -> resources.getString(R.string.manager_up_to_date)
MagiskState.LOADING -> resources.getString(R.string.checking_for_updates)
MagiskState.OBSOLETE -> resources.getString(R.string.manager_update_title)
MagiskState.NOT_INSTALLED -> R.string.invalid_update_channel.res()
MagiskState.UP_TO_DATE -> R.string.manager_up_to_date.res()
MagiskState.LOADING -> R.string.checking_for_updates.res()
MagiskState.OBSOLETE -> R.string.manager_update_title.res()
}
}
val managerCurrentVersion = KObservableField("")
val managerLatestVersion = KObservableField("")
val managerAdditionalInfo = Observer(managerState) {
if (app.packageName != BuildConfig.APPLICATION_ID)
"(${app.packageName})"
if (packageName != BuildConfig.APPLICATION_ID)
"($packageName)"
else
""
}
val safetyNetTitle = KObservableField(resources.getString(R.string.safetyNet_check_text))
val safetyNetTitle = KObservableField(R.string.safetyNet_check_text.res())
val ctsState = KObservableField(SafetyNetState.IDLE)
val basicIntegrityState = KObservableField(SafetyNetState.IDLE)
val safetyNetState = Observer(ctsState, basicIntegrityState) {
@ -81,8 +81,6 @@ class HomeViewModel(
val hasRoot = KObservableField(false)
private var shownDialog = false
private val current = resources.getString(R.string.current_installed)
private val latest = resources.getString(R.string.latest_version)
init {
Event.register(this)
@ -126,7 +124,7 @@ class HomeViewModel(
fun safetyNetPressed() {
ctsState.value = SafetyNetState.LOADING
basicIntegrityState.value = SafetyNetState.LOADING
safetyNetTitle.value = resources.getString(R.string.checking_safetyNet_status)
safetyNetTitle.value = R.string.checking_safetyNet_status.res()
UpdateSafetyNetEvent().publish()
}
@ -135,7 +133,7 @@ class HomeViewModel(
response and 0x0F == 0 -> {
val hasCtsPassed = response and ISafetyNetHelper.CTS_PASS != 0
val hasBasicIntegrityPassed = response and ISafetyNetHelper.BASIC_PASS != 0
safetyNetTitle.value = resources.getString(R.string.safetyNet_check_success)
safetyNetTitle.value = R.string.safetyNet_check_success.res()
ctsState.value = if (hasCtsPassed) {
SafetyNetState.PASS
} else {
@ -154,11 +152,10 @@ class HomeViewModel(
else -> {
ctsState.value = SafetyNetState.IDLE
basicIntegrityState.value = SafetyNetState.IDLE
val errorString = when (response) {
safetyNetTitle.value = when (response) {
ISafetyNetHelper.RESPONSE_ERR -> R.string.safetyNet_res_invalid
else -> R.string.safetyNet_api_error
}
safetyNetTitle.value = resources.getString(errorString)
}.res()
}
}
@ -172,7 +169,7 @@ class HomeViewModel(
hasRoot.value = Shell.rootAccess()
if (Networking.checkNetworkStatus(app)) {
if (Networking.checkNetworkStatus(context)) {
CheckUpdates.check()
} else {
state = State.LOADING_FAILED
@ -187,16 +184,14 @@ class HomeViewModel(
else -> MagiskState.UP_TO_DATE
}
if (magiskState.value != MagiskState.NOT_INSTALLED) {
magiskCurrentVersion.value = version
.format(Config.magiskVersionString, Config.magiskVersionCode)
.let { current.format(it) }
magiskCurrentVersion.value = if (magiskState.value != MagiskState.NOT_INSTALLED) {
version.format(Config.magiskVersionString, Config.magiskVersionCode)
} else {
magiskCurrentVersion.value = ""
""
}
magiskLatestVersion.value = version
.format(Config.remoteMagiskVersionString, Config.remoteMagiskVersionCode)
.let { latest.format(it) }
managerState.value = when (Config.remoteManagerVersionCode) {
in Int.MIN_VALUE until 0 -> MagiskState.NOT_INSTALLED //wrong update channel
@ -206,10 +201,9 @@ class HomeViewModel(
managerCurrentVersion.value = version
.format(BuildConfig.VERSION_NAME, BuildConfig.VERSION_CODE)
.let { current.format(it) }
managerLatestVersion.value = version
.format(Config.remoteManagerVersionString, Config.remoteManagerVersionCode)
.let { latest.format(it) }
}
private fun ensureEnv() {

View File

@ -11,6 +11,12 @@ import android.provider.OpenableColumns
import com.topjohnwu.magisk.App
import java.io.FileNotFoundException
val packageName: String
get() {
val app: App by inject()
return app.packageName
}
val PackageInfo.processes
get() = activities?.processNames.orEmpty() +
services?.processNames.orEmpty() +

View File

@ -1,5 +1,7 @@
package com.topjohnwu.magisk.utils
import android.content.res.Resources
val specialChars = arrayOf('!', '@', '#', '$', '%', '&', '?')
fun String.replaceRandomWithSpecial(): String {
@ -8,4 +10,9 @@ fun String.replaceRandomWithSpecial(): String {
random = random()
} while (random == '.')
return replace(random, specialChars.random())
}
fun Int.res(vararg args: Any): String {
val resources: Resources by inject()
return resources.getString(this, *args)
}

View File

@ -110,7 +110,7 @@
android:layout_width="0dp"
android:layout_height="wrap_content"
android:maxLines="1"
android:text="@{state != MagiskState.LOADING ? currentVersion : @string/checking_for_updates}"
android:text="@{state != MagiskState.LOADING ? @string/current_installed(currentVersion) : @string/checking_for_updates}"
app:autoSizeMinTextSize="1sp"
app:autoSizeTextType="uniform"
app:layout_constraintBottom_toTopOf="@+id/latest_version"
@ -125,7 +125,7 @@
android:layout_width="0dp"
android:layout_height="wrap_content"
android:maxLines="1"
android:text="@{state != MagiskState.LOADING ? latestVersion : @string/checking_for_updates}"
android:text="@{state != MagiskState.LOADING ? @string/latest_version(latestVersion) : @string/checking_for_updates}"
app:autoSizeMinTextSize="1sp"
app:autoSizeTextType="uniform"
app:layout_constraintBottom_toTopOf="@+id/additional"