From dc0a66c70051a5b5aa8de808a6ad58cff4fd923e Mon Sep 17 00:00:00 2001 From: X1nto Date: Sun, 14 Feb 2021 13:57:48 +0400 Subject: [PATCH] prevent activity recreation on screen rotate --- app/src/main/AndroidManifest.xml | 2 +- .../java/com/vanced/manager/ui/MainActivity.kt | 17 ++++++++++++++++- .../java/com/vanced/manager/utils/AppUtils.kt | 3 +++ .../com/vanced/manager/utils/DownloadHelper.kt | 4 +++- .../manager/utils/LanguageContextWrapper.kt | 18 ++++++++---------- 5 files changed, 31 insertions(+), 13 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index fdfb1687..f6b5ed9b 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -56,7 +56,7 @@ diff --git a/app/src/main/java/com/vanced/manager/ui/MainActivity.kt b/app/src/main/java/com/vanced/manager/ui/MainActivity.kt index c5df5b46..f14c8342 100644 --- a/app/src/main/java/com/vanced/manager/ui/MainActivity.kt +++ b/app/src/main/java/com/vanced/manager/ui/MainActivity.kt @@ -28,6 +28,7 @@ import com.vanced.manager.ui.dialogs.URLChangeDialog import com.vanced.manager.ui.fragments.HomeFragmentDirections import com.vanced.manager.ui.fragments.SettingsFragmentDirections import com.vanced.manager.utils.* +import com.vanced.manager.utils.AppUtils.currentLocale import com.vanced.manager.utils.AppUtils.faqpkg import com.vanced.manager.utils.AppUtils.log import com.vanced.manager.utils.AppUtils.playStorePkg @@ -154,6 +155,7 @@ class MainActivity : AppCompatActivity() { super.attachBaseContext(Crowdin.wrapContext(LanguageContextWrapper.wrap(newBase))) } + @Suppress("DEPRECATION") override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) onActivityResult(requestCode) @@ -161,7 +163,20 @@ class MainActivity : AppCompatActivity() { override fun onConfigurationChanged(newConfig: Configuration) { super.onConfigurationChanged(newConfig) - recreate() //restarting activity to recreate viewmodels, otherwise some text won't update + + //update manager language when system language is changed + @Suppress("DEPRECATION") + if (newConfig.locale != currentLocale) { + recreate() //restarting activity in order to recreate viewmodels, otherwise some text won't update + return + } + + when (newConfig.orientation) { + Configuration.ORIENTATION_PORTRAIT -> log("VMUI", "screen orientation changed to portrait") + Configuration.ORIENTATION_LANDSCAPE -> log("VMUI", "screen orientation changed to landscape") + else -> log("VMUI", "screen orientation changed to [REDACTED]") + } + } override fun recreate() { diff --git a/app/src/main/java/com/vanced/manager/utils/AppUtils.kt b/app/src/main/java/com/vanced/manager/utils/AppUtils.kt index 518aead1..1ebb4637 100644 --- a/app/src/main/java/com/vanced/manager/utils/AppUtils.kt +++ b/app/src/main/java/com/vanced/manager/utils/AppUtils.kt @@ -19,6 +19,7 @@ import kotlinx.coroutines.* import java.io.File import java.io.IOException import java.security.MessageDigest +import java.util.* object AppUtils: CoroutineScope by CoroutineScope(Dispatchers.IO) { @@ -33,6 +34,8 @@ object AppUtils: CoroutineScope by CoroutineScope(Dispatchers.IO) { val logs = mutableListOf() + var currentLocale: Locale? = null + fun log(tag: String, message: String) { logs.add( SpannableString("$tag: $message\n").apply { diff --git a/app/src/main/java/com/vanced/manager/utils/DownloadHelper.kt b/app/src/main/java/com/vanced/manager/utils/DownloadHelper.kt index 188c8662..008e98c9 100644 --- a/app/src/main/java/com/vanced/manager/utils/DownloadHelper.kt +++ b/app/src/main/java/com/vanced/manager/utils/DownloadHelper.kt @@ -5,6 +5,7 @@ import android.content.Context import android.content.Intent import android.net.Uri import android.os.Build +import android.util.Log import androidx.core.content.FileProvider import androidx.lifecycle.MutableLiveData import com.vanced.manager.R @@ -50,7 +51,7 @@ object DownloadHelper : CoroutineScope by CoroutineScope(Dispatchers.IO) { download.enqueue(object : Callback { override fun onResponse(call: Call, response: Response) { if (response.isSuccessful) { - CoroutineScope(Dispatchers.IO).launch { + launch { if (response.body()?.let { writeFile(it, context.getExternalFilesDir(fileFolder)?.path + "/" + fileName) } == true) { onDownloadComplete() } else { @@ -96,6 +97,7 @@ object DownloadHelper : CoroutineScope by CoroutineScope(Dispatchers.IO) { outputStream.write(fileReader, 0, read) downloadedBytes += read.toLong() downloadProgress.value?.downloadProgress?.postValue((downloadedBytes * 100 / totalBytes).toInt()) + Log.d("test", (downloadedBytes * 100 / totalBytes).toString()) } outputStream.flush() true diff --git a/app/src/main/java/com/vanced/manager/utils/LanguageContextWrapper.kt b/app/src/main/java/com/vanced/manager/utils/LanguageContextWrapper.kt index 9a70f2ee..321b0aa8 100644 --- a/app/src/main/java/com/vanced/manager/utils/LanguageContextWrapper.kt +++ b/app/src/main/java/com/vanced/manager/utils/LanguageContextWrapper.kt @@ -4,8 +4,7 @@ import android.content.Context import android.content.ContextWrapper import android.content.res.Configuration import android.content.res.Resources -import androidx.preference.PreferenceManager -import com.crowdin.platform.Crowdin +import com.vanced.manager.utils.AppUtils.currentLocale import java.util.* class LanguageContextWrapper(base: Context?) : ContextWrapper(base) { @@ -15,20 +14,19 @@ class LanguageContextWrapper(base: Context?) : ContextWrapper(base) { fun wrap(context: Context): ContextWrapper { val config: Configuration = context.resources.configuration context.createConfigurationContext(setLocale(config, context)) - Crowdin.wrapContext(context) return LanguageContextWrapper(context) } @Suppress("DEPRECATION") private fun setLocale(config: Configuration, context: Context): Configuration { - val pref = PreferenceManager.getDefaultSharedPreferences(context).getString("manager_lang", "System Default") + val pref = context.defPrefs.managerLang val sysLocale = Resources.getSystem().configuration.locale - val locale = - when { - pref == "System Default" -> Locale(sysLocale.language, sysLocale.country) - pref?.length!! > 2 -> Locale(pref.substring(0, pref.length - 3), pref.substring(pref.length - 2)) - else -> Locale(pref) - } + val locale = when { + pref == "System Default" -> Locale(sysLocale.language, sysLocale.country) + pref?.length!! > 2 -> Locale(pref.substring(0, pref.length - 3), pref.substring(pref.length - 2)) + else -> Locale(pref) + } + currentLocale = locale Locale.setDefault(locale) config.setLocale(locale) return config