Skip to content

Commit

Permalink
Most likely fixed zeugma-solutions#32
Browse files Browse the repository at this point in the history
Code needs polishing
  • Loading branch information
thubalek committed Dec 6, 2021
1 parent 9160671 commit c3bc0d7
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 45 deletions.
16 changes: 0 additions & 16 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import androidx.appcompat.view.ActionMode
import androidx.appcompat.widget.Toolbar
import com.zeugmasolutions.localehelper.LOG_TAG
import com.zeugmasolutions.localehelper.LocaleHelper
import com.zeugmasolutions.localehelper.log
import com.zeugmasolutions.localehelper.toDebugString

class LocaleHelperAppCompatDelegate(private val superDelegate: AppCompatDelegate) :
Expand Down Expand Up @@ -59,23 +60,15 @@ class LocaleHelperAppCompatDelegate(private val superDelegate: AppCompatDelegate

override fun attachBaseContext2(originalContext: Context): Context {
val originalUiMode = originalContext.resources.configuration.uiMode
val appCompatDelegateContext = super.attachBaseContext2(originalContext)
val superDelegateContext = superDelegate.attachBaseContext2(appCompatDelegateContext)
val superDelegateContext = super.attachBaseContext2(originalContext)
val wrappedContext = wrap(superDelegateContext)
val fixedWrappedContext = wrappedContext.createConfigurationContext(wrappedContext.resources.configuration.apply {
this.uiMode = originalUiMode
})
if (isLoggingEnabled) {
Log.d(
LOG_TAG,
originalContext.toDebugString() + " -> appCompatDelegateContext: "
+ appCompatDelegateContext.toDebugString() + "-> superDelegateContext: "
+ superDelegateContext.toDebugString() + "-> wrappedContext: "
+ wrappedContext.toDebugString() + "-> fixedWrappedContext:"
+ fixedWrappedContext
)
log {
"\n -> " + originalContext.toDebugString() + "\n -> appCompatDelegateContext (AppCompatDelegate): " +
// appCompatDelegateContext.toDebugString() + "\n -> superDelegateContext (${superDelegate.javaClass.simpleName}): " +
superDelegateContext.toDebugString() + "\n -> wrappedContext: " +
wrappedContext.toDebugString() + "\n"
}
return fixedWrappedContext
return wrappedContext
}

override fun setTitle(title: CharSequence?) = superDelegate.setTitle(title)
Expand Down Expand Up @@ -126,7 +119,5 @@ class LocaleHelperAppCompatDelegate(private val superDelegate: AppCompatDelegate

private fun wrap(context: Context): Context = LocaleHelper.onAttach(context)

companion object {
const val isLoggingEnabled = false
}
override fun attachBaseContext(context: Context?) = superDelegate.attachBaseContext(context)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.zeugmasolutions.localehelper

import android.content.Context
import android.content.res.Configuration
import android.util.Log

fun Configuration.toDebugString(): String {
return "Configuration[" + this.locales.toLanguageTags() + ",uiMode=" + this.uiMode.toDebugString() + "]"
Expand All @@ -19,5 +20,26 @@ private fun Int.toDebugString(): String {


fun Context.toDebugString(): String {
return "[Context: " + this.resources.configuration.toDebugString() + "]"
return "[Context: " + this.javaClass.simpleName + "@" + this.hashCode() + ", " + this.resources.configuration.toDebugString() + ", theme: " +
this.safeTheme() + "]"
}

private fun Context.safeTheme(): String {
return try {
this.theme.toString()
} catch (e: Throwable) {
"null"
}
}

const val debuggingIsEnabled = true

fun log(throwable: Throwable? = null, block: () -> String) {
if (debuggingIsEnabled) {
if (throwable != null) {
Log.d(LOG_TAG, block(), throwable)
} else {
Log.d(LOG_TAG, block())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import android.content.Context
import android.content.SharedPreferences
import android.content.res.Resources
import android.os.Build
import android.view.ContextThemeWrapper
import androidx.core.os.ConfigurationCompat
import java.lang.Exception
import java.util.Locale

object LocaleHelper {
Expand Down Expand Up @@ -77,6 +79,12 @@ object LocaleHelper {
return context
}

if (context is Application) {
log {
"context ${context.toDebugString()} is Application"
}
}

val resources = context.resources
val configuration = resources.configuration
configuration.setCurrentLocale(locale)
Expand All @@ -86,6 +94,10 @@ object LocaleHelper {
}

return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
log(Exception()) {
"Calling createConfigurationContext for configuration: ${configuration.toDebugString()} " +
"and context ${context.toDebugString()}"
}
context.createConfigurationContext(configuration)
} else {
@Suppress("DEPRECATION")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ open class LocaleAwareCompatActivity : AppCompatActivity() {

override fun getDelegate() = localeDelegate.getAppCompatDelegate(super.getDelegate())

override fun attachBaseContext(newBase: Context) {
super.attachBaseContext(localeDelegate.attachBaseContext(newBase))
}

override fun onCreate(savedInstanceState: Bundle?) {
localeDelegate.onCreate(this)
super.onCreate(savedInstanceState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import java.util.Locale

interface LocaleHelperActivityDelegate {
fun setLocale(activity: Activity, newLocale: Locale?)
fun attachBaseContext(newBase: Context): Context
fun attachBaseContext2(newBase: Context): Context
fun onPaused(activity: Activity)
fun onResumed(activity: Activity)
fun onCreate(activity: Activity)
Expand All @@ -38,13 +38,16 @@ class LocaleHelperActivityDelegateImpl : LocaleHelperActivityDelegate {
}

override fun setLocale(activity: Activity, newLocale: Locale?) {
Log.d(LOG_TAG, "Setting new locale `${newLocale}` and recreating activity `${activity.javaClass.name}`")
Log.d(
LOG_TAG,
"Setting new locale `${newLocale}` and recreating activity `${activity.javaClass.name}`"
)
LocaleHelper.setLocale(activity, newLocale)
locale = LocaleHelper.getLocale(activity)
activity.recreate()
}

override fun attachBaseContext(newBase: Context): Context {
override fun attachBaseContext2(newBase: Context): Context {
return LocaleHelper.onAttach(newBase)
}

Expand All @@ -54,15 +57,15 @@ class LocaleHelperActivityDelegateImpl : LocaleHelperActivityDelegate {

override fun onPaused(activity: Activity) {
val localeFromHelper = LocaleHelper.getLocale(activity)
Log.d(LOG_TAG, "Remembering locale `$localeFromHelper` in `${activity.javaClass.name}`")
log { "Remembering locale `$localeFromHelper` in `${activity.javaClass.name}`" }
locale = localeFromHelper
}

override fun onResumed(activity: Activity) {
val localeFromHelper = LocaleHelper.getLocale(activity)
Log.d(LOG_TAG, "onResume (Comparing `$locale` vs `$localeFromHelper`)")
log { "onResume (Comparing `$locale` vs `$localeFromHelper`)" }
if (locale == localeFromHelper) return
Log.d(LOG_TAG, "Calling `${activity.javaClass.name}`.recreate()")
log { "Calling `${activity.javaClass.name}`.recreate()" }
activity.recreate()
}
}
Expand Down

0 comments on commit c3bc0d7

Please sign in to comment.