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
android:name=".ui.MainActivity"
android:configChanges="layoutDirection|locale"
android:configChanges="layoutDirection|locale|keyboardHidden|orientation|screenSize"
android:exported="true"
android:label="@string/app_name"
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.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() {

View File

@ -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<Spannable>()
var currentLocale: Locale? = null
fun log(tag: String, message: String) {
logs.add(
SpannableString("$tag: $message\n").apply {

View File

@ -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<ResponseBody> {
override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
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

View File

@ -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