• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.android.launcher3.util
2 
3 import android.content.Context
4 import android.content.Intent
5 import android.os.Process
6 import android.os.UserManager
7 import androidx.annotation.VisibleForTesting
8 
9 class LockedUserState(private val mContext: Context) : SafeCloseable {
10     var isUserUnlocked: Boolean
11         private set
12     private val mUserUnlockedActions: RunnableList = RunnableList()
13 
14     @VisibleForTesting
<lambda>null15     val mUserUnlockedReceiver = SimpleBroadcastReceiver {
16         if (Intent.ACTION_USER_UNLOCKED == it.action) {
17             isUserUnlocked = true
18             notifyUserUnlocked()
19         }
20     }
21 
22     init {
23         isUserUnlocked =
24             mContext
25                 .getSystemService(UserManager::class.java)!!
26                 .isUserUnlocked(Process.myUserHandle())
27         if (isUserUnlocked) {
28             notifyUserUnlocked()
29         } else {
30             mUserUnlockedReceiver.register(mContext, Intent.ACTION_USER_UNLOCKED)
31         }
32     }
33 
notifyUserUnlockednull34     private fun notifyUserUnlocked() {
35         mUserUnlockedActions.executeAllAndDestroy()
36         mUserUnlockedReceiver.unregisterReceiverSafely(mContext)
37     }
38 
39     /** Stops the receiver from listening for ACTION_USER_UNLOCK broadcasts. */
closenull40     override fun close() {
41         mUserUnlockedReceiver.unregisterReceiverSafely(mContext)
42     }
43 
44     /**
45      * Adds a `Runnable` to be executed when a user is unlocked. If the user is already unlocked,
46      * this runnable will run immediately because RunnableList will already have been destroyed.
47      */
runOnUserUnlockednull48     fun runOnUserUnlocked(action: Runnable) {
49         mUserUnlockedActions.add(action)
50     }
51 
52     companion object {
53         @VisibleForTesting
54         @JvmField
<lambda>null55         val INSTANCE = MainThreadInitializedObject { LockedUserState(it) }
56 
getnull57         @JvmStatic fun get(context: Context): LockedUserState = INSTANCE.get(context)
58     }
59 }
60