Resolve trust agents on USER_STARTED in addition to USER_ADDED.

ACTION_USER_ADDED is a broadcast that is sent once when the user is
created.  TrustManagerService resolves the enabled trust agents for an
user only when the user is created.  However, if there is a reboot or
power loss before the broadcast is received, the trust agents are never
resolved for that user.

This change also registers for ACTION_USER_STARTED, so the service
checks for enabled trust agents on every boot.  If the trust agents have
been already resolved and initialized for that users, there is already a
Secure Settings key (TRUST_AGENTS_INITIALIZED) that is turned on, so we
wouldn't be doing this more than once per user.

Bug: 150145767
Test: TrustManagerService resolves trust agents only once - either on
USER_ADDED or USER_STARTED.

Change-Id: I83c2dd02d4b476f8b85af1aa4d9d0c77095207ce
diff --git a/services/core/java/com/android/server/trust/TrustManagerService.java b/services/core/java/com/android/server/trust/TrustManagerService.java
index 7408dd4..ef54d04 100644
--- a/services/core/java/com/android/server/trust/TrustManagerService.java
+++ b/services/core/java/com/android/server/trust/TrustManagerService.java
@@ -1389,7 +1389,8 @@
             if (DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED.equals(action)) {
                 refreshAgentList(getSendingUserId());
                 updateDevicePolicyFeatures();
-            } else if (Intent.ACTION_USER_ADDED.equals(action)) {
+            } else if (Intent.ACTION_USER_ADDED.equals(action) || Intent.ACTION_USER_STARTED.equals(
+                    action)) {
                 int userId = getUserId(intent);
                 if (userId > 0) {
                     maybeEnableFactoryTrustAgents(mLockPatternUtils, userId);
@@ -1430,6 +1431,7 @@
             filter.addAction(DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED);
             filter.addAction(Intent.ACTION_USER_ADDED);
             filter.addAction(Intent.ACTION_USER_REMOVED);
+            filter.addAction(Intent.ACTION_USER_STARTED);
             context.registerReceiverAsUser(this,
                     UserHandle.ALL,
                     filter,