prevent activity recreation on screen rotate

This commit is contained in:
X1nto 2021-02-14 13:57:48 +04:00
parent f6790ff22c
commit dc0a66c700
5 changed files with 31 additions and 13 deletions

View File

@ -56,7 +56,7 @@
<activity <activity
android:name=".ui.MainActivity" android:name=".ui.MainActivity"
android:configChanges="layoutDirection|locale" android:configChanges="layoutDirection|locale|keyboardHidden|orientation|screenSize"
android:exported="true" android:exported="true"
android:label="@string/app_name" android:label="@string/app_name"
android:theme="@style/DarkTheme"> android:theme="@style/DarkTheme">

View File

@ -28,6 +28,7 @@ import com.vanced.manager.ui.dialogs.URLChangeDialog
import com.vanced.manager.ui.fragments.HomeFragmentDirections import com.vanced.manager.ui.fragments.HomeFragmentDirections
import com.vanced.manager.ui.fragments.SettingsFragmentDirections import com.vanced.manager.ui.fragments.SettingsFragmentDirections
import com.vanced.manager.utils.* 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.faqpkg
import com.vanced.manager.utils.AppUtils.log import com.vanced.manager.utils.AppUtils.log
import com.vanced.manager.utils.AppUtils.playStorePkg import com.vanced.manager.utils.AppUtils.playStorePkg
@ -154,6 +155,7 @@ class MainActivity : AppCompatActivity() {
super.attachBaseContext(Crowdin.wrapContext(LanguageContextWrapper.wrap(newBase))) super.attachBaseContext(Crowdin.wrapContext(LanguageContextWrapper.wrap(newBase)))
} }
@Suppress("DEPRECATION")
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data) super.onActivityResult(requestCode, resultCode, data)
onActivityResult(requestCode) onActivityResult(requestCode)
@ -161,7 +163,20 @@ class MainActivity : AppCompatActivity() {
override fun onConfigurationChanged(newConfig: Configuration) { override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig) 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() { override fun recreate() {

View File

@ -19,6 +19,7 @@ import kotlinx.coroutines.*
import java.io.File import java.io.File
import java.io.IOException import java.io.IOException
import java.security.MessageDigest import java.security.MessageDigest
import java.util.*
object AppUtils: CoroutineScope by CoroutineScope(Dispatchers.IO) { object AppUtils: CoroutineScope by CoroutineScope(Dispatchers.IO) {
@ -33,6 +34,8 @@ object AppUtils: CoroutineScope by CoroutineScope(Dispatchers.IO) {
val logs = mutableListOf<Spannable>() val logs = mutableListOf<Spannable>()
var currentLocale: Locale? = null
fun log(tag: String, message: String) { fun log(tag: String, message: String) {
logs.add( logs.add(
SpannableString("$tag: $message\n").apply { SpannableString("$tag: $message\n").apply {

View File

@ -5,6 +5,7 @@ import android.content.Context
import android.content.Intent import android.content.Intent
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Build
import android.util.Log
import androidx.core.content.FileProvider import androidx.core.content.FileProvider
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import com.vanced.manager.R import com.vanced.manager.R
@ -50,7 +51,7 @@ object DownloadHelper : CoroutineScope by CoroutineScope(Dispatchers.IO) {
download.enqueue(object : Callback<ResponseBody> { download.enqueue(object : Callback<ResponseBody> {
override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) { override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
if (response.isSuccessful) { if (response.isSuccessful) {
CoroutineScope(Dispatchers.IO).launch { launch {
if (response.body()?.let { writeFile(it, context.getExternalFilesDir(fileFolder)?.path + "/" + fileName) } == true) { if (response.body()?.let { writeFile(it, context.getExternalFilesDir(fileFolder)?.path + "/" + fileName) } == true) {
onDownloadComplete() onDownloadComplete()
} else { } else {
@ -96,6 +97,7 @@ object DownloadHelper : CoroutineScope by CoroutineScope(Dispatchers.IO) {
outputStream.write(fileReader, 0, read) outputStream.write(fileReader, 0, read)
downloadedBytes += read.toLong() downloadedBytes += read.toLong()
downloadProgress.value?.downloadProgress?.postValue((downloadedBytes * 100 / totalBytes).toInt()) downloadProgress.value?.downloadProgress?.postValue((downloadedBytes * 100 / totalBytes).toInt())
Log.d("test", (downloadedBytes * 100 / totalBytes).toString())
} }
outputStream.flush() outputStream.flush()
true true

View File

@ -4,8 +4,7 @@ import android.content.Context
import android.content.ContextWrapper import android.content.ContextWrapper
import android.content.res.Configuration import android.content.res.Configuration
import android.content.res.Resources import android.content.res.Resources
import androidx.preference.PreferenceManager import com.vanced.manager.utils.AppUtils.currentLocale
import com.crowdin.platform.Crowdin
import java.util.* import java.util.*
class LanguageContextWrapper(base: Context?) : ContextWrapper(base) { class LanguageContextWrapper(base: Context?) : ContextWrapper(base) {
@ -15,20 +14,19 @@ class LanguageContextWrapper(base: Context?) : ContextWrapper(base) {
fun wrap(context: Context): ContextWrapper { fun wrap(context: Context): ContextWrapper {
val config: Configuration = context.resources.configuration val config: Configuration = context.resources.configuration
context.createConfigurationContext(setLocale(config, context)) context.createConfigurationContext(setLocale(config, context))
Crowdin.wrapContext(context)
return LanguageContextWrapper(context) return LanguageContextWrapper(context)
} }
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
private fun setLocale(config: Configuration, context: Context): Configuration { 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 sysLocale = Resources.getSystem().configuration.locale
val locale = val locale = when {
when { pref == "System Default" -> Locale(sysLocale.language, sysLocale.country)
pref == "System Default" -> Locale(sysLocale.language, sysLocale.country) pref?.length!! > 2 -> Locale(pref.substring(0, pref.length - 3), pref.substring(pref.length - 2))
pref?.length!! > 2 -> Locale(pref.substring(0, pref.length - 3), pref.substring(pref.length - 2)) else -> Locale(pref)
else -> Locale(pref) }
} currentLocale = locale
Locale.setDefault(locale) Locale.setDefault(locale)
config.setLocale(locale) config.setLocale(locale)
return config return config