11package io.github.sds100.keymapper
22
3+ import android.annotation.SuppressLint
34import android.content.Intent
45import android.os.Build
6+ import android.os.UserManager
7+ import android.util.Log
58import androidx.appcompat.app.AppCompatDelegate
9+ import androidx.core.content.getSystemService
610import androidx.lifecycle.Lifecycle
711import androidx.lifecycle.LifecycleObserver
812import androidx.lifecycle.OnLifecycleEvent
@@ -64,7 +68,10 @@ import java.util.Calendar
6468/* *
6569 * Created by sds100 on 19/05/2020.
6670 */
71+ @SuppressLint(" LogNotTimber" )
6772class KeyMapperApp : MultiDexApplication () {
73+ private val tag = KeyMapperApp ::class .simpleName
74+
6875 val appCoroutineScope = MainScope ()
6976
7077 val notificationAdapter by lazy { AndroidNotificationAdapter (this , appCoroutineScope) }
@@ -167,9 +174,16 @@ class KeyMapperApp : MultiDexApplication() {
167174
168175 private val processLifecycleOwner by lazy { ProcessLifecycleOwner .get() }
169176
177+ private val userManager: UserManager ? by lazy { getSystemService<UserManager >() }
178+
179+ private val initLock: Any = Any ()
180+ private var initialized = false
181+
170182 override fun onCreate () {
171183 val priorExceptionHandler = Thread .getDefaultUncaughtExceptionHandler()
172184
185+ Log .i(tag, " KeyMapperApp: OnCreate" )
186+
173187 Thread .setDefaultUncaughtExceptionHandler { thread, exception ->
174188 // log in a blocking manner and always log regardless of whether the setting is turned on
175189 val entry = LogEntryEntity (
@@ -188,9 +202,30 @@ class KeyMapperApp : MultiDexApplication() {
188202
189203 super .onCreate()
190204
191- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .S ) {
192- // DynamicColors.applyToActivitiesIfAvailable(this)
205+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .N && userManager?.isUserUnlocked == false ) {
206+ Log .i(tag, " KeyMapperApp: Delay init because locked." )
207+ // If the device is still encrypted and locked do not initialize anything that
208+ // may potentially need the encrypted app storage like databases.
209+ return
210+ }
211+
212+ synchronized(initLock) {
213+ init ()
214+ initialized = true
193215 }
216+ }
217+
218+ fun onBootUnlocked () {
219+ synchronized(initLock) {
220+ if (! initialized) {
221+ init ()
222+ }
223+ initialized = true
224+ }
225+ }
226+
227+ private fun init () {
228+ Log .i(tag, " KeyMapperApp: Init" )
194229
195230 ServiceLocator .settingsRepository(this ).get(Keys .darkTheme)
196231 .map { it?.toIntOrNull() }
0 commit comments