Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
thubalek committed Dec 7, 2021
1 parent 6938a4f commit 0dd7aa6
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 40 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 @@ -3,17 +3,15 @@ package com.zeugmasolutions.localeexample
import android.content.Context
import androidx.appcompat.app.AppCompatDelegate
import androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
import androidx.multidex.MultiDex
import com.zeugmasolutions.localehelper.LocaleAwareApplication

class Application : LocaleAwareApplication() {
override fun onCreate() {
super.onCreate()
AppCompatDelegate.setDefaultNightMode(MODE_NIGHT_FOLLOW_SYSTEM)
super.onCreate()
}

override fun attachBaseContext(base: Context) {
super.attachBaseContext(base)
MultiDex.install(this)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ import android.content.res.Configuration
import android.os.Build
import android.os.Bundle
import android.util.AttributeSet
import android.util.Log
import android.view.MenuInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.RequiresApi
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) :
AppCompatDelegate() {
Expand Down Expand Up @@ -54,8 +58,16 @@ class LocaleHelperAppCompatDelegate(private val superDelegate: AppCompatDelegate
override fun addContentView(v: View?, lp: ViewGroup.LayoutParams?) =
superDelegate.addContentView(v, lp)

override fun attachBaseContext2(context: Context) =
wrap(superDelegate.attachBaseContext2(super.attachBaseContext2(context)))
override fun attachBaseContext2(originalContext: Context): Context {
val superDelegateContext = super.attachBaseContext2(originalContext)
val wrappedContext = wrap(superDelegateContext)
log {
"\n -> " + originalContext.toDebugString() +
"\n -> appCompatDelegateContext (AppCompatDelegate): " + superDelegateContext.toDebugString() +
"\n -> wrappedContext: " + wrappedContext.toDebugString() + "\n"
}
return wrappedContext
}

override fun setTitle(title: CharSequence?) = superDelegate.setTitle(title)

Expand Down Expand Up @@ -104,4 +116,6 @@ class LocaleHelperAppCompatDelegate(private val superDelegate: AppCompatDelegate
override fun getLocalNightMode() = superDelegate.localNightMode

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

override fun attachBaseContext(context: Context?) = superDelegate.attachBaseContext(context)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Debug support.
*/
package com.zeugmasolutions.localehelper

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

const val debuggingIsEnabled = true

fun Configuration.toDebugString(): String {
return "Configuration[" + this.locales.toLanguageTags() + ",uiMode=" + this.uiMode.toDebugString() + "]"
}

private fun Int.toDebugString(): String {
return when (this and Configuration.UI_MODE_NIGHT_MASK) {
Configuration.UI_MODE_NIGHT_NO -> "Day"
Configuration.UI_MODE_NIGHT_YES -> "Night"
else -> {
"Unknown"
}
}
}

fun Context.toDebugString(): String {
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"
}
}

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
@@ -1,13 +1,13 @@
package com.zeugmasolutions.localehelper

import android.annotation.SuppressLint
import android.app.Application
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.util.*
import java.lang.Exception
import java.util.Locale

object LocaleHelper {
Expand All @@ -20,12 +20,6 @@ object LocaleHelper {
initialized = false
}

/**
* Returns the system [Locale]
*/
@SuppressLint("ConstantLocale")
val systemLocale: Locale = Locale.getDefault()

/**
* Attach the selected or default [Locale] to the [context]
*/
Expand All @@ -38,7 +32,7 @@ object LocaleHelper {
return updateContextResources(context, Locale.getDefault())
}

fun getSystemLocale(): Locale {
private fun getSystemLocale(): Locale {
val locales = ConfigurationCompat.getLocales(Resources.getSystem().configuration)
return if (locales.isEmpty) {
Locale.US
Expand Down Expand Up @@ -94,6 +88,10 @@ object LocaleHelper {
}

return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
log {
"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 @@ -3,14 +3,15 @@ package com.zeugmasolutions.localehelper
import android.app.Activity
import android.content.Context
import android.os.Build
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatDelegate
import androidx.appcompat.app.LocaleHelperAppCompatDelegate
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 @@ -37,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 @@ -53,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 0dd7aa6

Please sign in to comment.