Fix attention service resolution timing

Test: manually tested the lifecycle is as expected
Bug: 111939367
Change-Id: Idbc8c2975a9da5a0f871971c973b3a64960e9e3c
diff --git a/services/core/java/com/android/server/attention/AttentionManagerService.java b/services/core/java/com/android/server/attention/AttentionManagerService.java
index 5b469fe..bfc4c72 100644
--- a/services/core/java/com/android/server/attention/AttentionManagerService.java
+++ b/services/core/java/com/android/server/attention/AttentionManagerService.java
@@ -108,24 +108,23 @@
                 AttentionService.ATTENTION_FAILURE_UNKNOWN);
     }
 
-    @Override
-    public void onBootPhase(int phase) {
-        super.onBootPhase(phase);
-        if (phase == SystemService.PHASE_THIRD_PARTY_APPS_CAN_START) {
+    /** Resolves and sets up the attention service if it had not been done yet. */
+    private boolean isServiceAvailable() {
+        if (mComponentName == null) {
             mComponentName = resolveAttentionService(mContext);
-            if (isAttentionServiceSupported()) {
-                // If the service is supported we want to keep receiving the screen off events.
+            if (mComponentName != null) {
                 mContext.registerReceiver(new ScreenStateReceiver(),
                         new IntentFilter(Intent.ACTION_SCREEN_OFF));
             }
         }
+        return mComponentName != null;
     }
 
     /**
      * Returns {@code true} if attention service is supported on this device.
      */
     public boolean isAttentionServiceSupported() {
-        return mComponentName != null && isServiceEnabled();
+        return isServiceEnabled() && isServiceAvailable();
     }
 
     private boolean isServiceEnabled() {
@@ -258,7 +257,7 @@
     private UserState getOrCreateUserStateLocked(int userId) {
         UserState result = mUserStates.get(userId);
         if (result == null) {
-            result = new UserState(userId, mContext, mLock);
+            result = new UserState(userId, mContext, mLock, mComponentName);
             mUserStates.put(userId, result);
         }
         return result;
@@ -397,6 +396,7 @@
     }
 
     private static final class UserState {
+        final ComponentName mComponentName;
         final AttentionServiceConnection mConnection = new AttentionServiceConnection();
 
         @GuardedBy("mLock")
@@ -416,10 +416,11 @@
         final Context mContext;
         final Object mLock;
 
-        private UserState(int userId, Context context, Object lock) {
+        private UserState(int userId, Context context, Object lock, ComponentName componentName) {
             mUserId = userId;
             mContext = Preconditions.checkNotNull(context);
             mLock = Preconditions.checkNotNull(lock);
+            mComponentName = Preconditions.checkNotNull(componentName);
         }
 
 
@@ -443,15 +444,9 @@
             final long identity = Binder.clearCallingIdentity();
 
             try {
-                final ComponentName componentName =
-                        resolveAttentionService(mContext);
-                if (componentName == null) {
-                    // Might happen if the storage is encrypted and the user is not unlocked
-                    return false;
-                }
                 final Intent mServiceIntent = new Intent(
                         AttentionService.SERVICE_INTERFACE).setComponent(
-                        componentName);
+                        mComponentName);
                 willBind = mContext.bindServiceAsUser(mServiceIntent, mConnection,
                         Context.BIND_AUTO_CREATE, UserHandle.CURRENT);
                 mBinding = willBind;