diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml index ce1fc4f..ce889bd 100644 --- a/.idea/codeStyles/Project.xml +++ b/.idea/codeStyles/Project.xml @@ -1,22 +1,6 @@ - - diff --git a/localehelper/src/main/java/androidx/appcompat/app/LocaleHelperAppCompatDelegate.kt b/localehelper/src/main/java/androidx/appcompat/app/LocaleHelperAppCompatDelegate.kt index 78b053c..4353560 100644 --- a/localehelper/src/main/java/androidx/appcompat/app/LocaleHelperAppCompatDelegate.kt +++ b/localehelper/src/main/java/androidx/appcompat/app/LocaleHelperAppCompatDelegate.kt @@ -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) : @@ -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) @@ -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) } diff --git a/localehelper/src/main/java/com/zeugmasolutions/localehelper/Debug.kt b/localehelper/src/main/java/com/zeugmasolutions/localehelper/Debug.kt index 92bff14..d4b6324 100644 --- a/localehelper/src/main/java/com/zeugmasolutions/localehelper/Debug.kt +++ b/localehelper/src/main/java/com/zeugmasolutions/localehelper/Debug.kt @@ -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() + "]" @@ -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()) + } + } } diff --git a/localehelper/src/main/java/com/zeugmasolutions/localehelper/LocaleHelper.kt b/localehelper/src/main/java/com/zeugmasolutions/localehelper/LocaleHelper.kt index cfb6acf..ab19af5 100644 --- a/localehelper/src/main/java/com/zeugmasolutions/localehelper/LocaleHelper.kt +++ b/localehelper/src/main/java/com/zeugmasolutions/localehelper/LocaleHelper.kt @@ -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 { @@ -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) @@ -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") diff --git a/localehelper/src/main/java/com/zeugmasolutions/localehelper/LocaleHelperActivities.kt b/localehelper/src/main/java/com/zeugmasolutions/localehelper/LocaleHelperActivities.kt index 851a45f..5558eb3 100644 --- a/localehelper/src/main/java/com/zeugmasolutions/localehelper/LocaleHelperActivities.kt +++ b/localehelper/src/main/java/com/zeugmasolutions/localehelper/LocaleHelperActivities.kt @@ -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) diff --git a/localehelper/src/main/java/com/zeugmasolutions/localehelper/LocaleHelperDelegates.kt b/localehelper/src/main/java/com/zeugmasolutions/localehelper/LocaleHelperDelegates.kt index 2e942a4..d05ceaf 100644 --- a/localehelper/src/main/java/com/zeugmasolutions/localehelper/LocaleHelperDelegates.kt +++ b/localehelper/src/main/java/com/zeugmasolutions/localehelper/LocaleHelperDelegates.kt @@ -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) @@ -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) } @@ -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() } }