Files
Lawnchair/src/com/android/launcher3/util/LockedUserState.kt
T
Stefan Andonian 5bd9a225a8 Preload expensive objects before user is unlocked.
This change includes migrating data required for starting the launcher
from encrypted storage into device protected storage. All of the data
being moved has already been approved by the correct authorities.

Bug: 251502424
Test: Performed latency testing using logs from user unlock until first
workspace screen loaded.

Change-Id: I58b0cd1c7bad260c2252f9e172ef85ab885c7fe9
2023-03-01 23:09:07 +00:00

60 lines
1.8 KiB
Kotlin

package com.android.launcher3.util
import android.content.Context
import android.content.Intent
import android.os.Process
import android.os.UserManager
import androidx.annotation.VisibleForTesting
class LockedUserState(private val mContext: Context) : SafeCloseable {
var isUserUnlocked: Boolean
private set
private val mUserUnlockedActions: RunnableList = RunnableList()
@VisibleForTesting
val mUserUnlockedReceiver = SimpleBroadcastReceiver {
if (Intent.ACTION_USER_UNLOCKED == it.action) {
isUserUnlocked = true
notifyUserUnlocked()
}
}
init {
isUserUnlocked =
mContext
.getSystemService(UserManager::class.java)!!
.isUserUnlocked(Process.myUserHandle())
if (isUserUnlocked) {
notifyUserUnlocked()
} else {
mUserUnlockedReceiver.register(mContext, Intent.ACTION_USER_UNLOCKED)
}
}
private fun notifyUserUnlocked() {
mUserUnlockedActions.executeAllAndDestroy()
mUserUnlockedReceiver.unregisterReceiverSafely(mContext)
}
/** Stops the receiver from listening for ACTION_USER_UNLOCK broadcasts. */
override fun close() {
mUserUnlockedReceiver.unregisterReceiverSafely(mContext)
}
/**
* Adds a `Runnable` to be executed when a user is unlocked. If the user is already unlocked,
* this runnable will run immediately because RunnableList will already have been destroyed.
*/
fun runOnUserUnlocked(action: Runnable) {
mUserUnlockedActions.add(action)
}
companion object {
@VisibleForTesting
@JvmField
val INSTANCE = MainThreadInitializedObject { LockedUserState(it) }
@JvmStatic fun get(context: Context): LockedUserState = INSTANCE.get(context)
}
}