Merge "Add user-switching observer to AttentionDetector" into qt-dev
diff --git a/services/core/java/com/android/server/power/AttentionDetector.java b/services/core/java/com/android/server/power/AttentionDetector.java
index 14f1196..ed11fd4 100644
--- a/services/core/java/com/android/server/power/AttentionDetector.java
+++ b/services/core/java/com/android/server/power/AttentionDetector.java
@@ -19,6 +19,8 @@
 import static android.provider.Settings.System.ADAPTIVE_SLEEP;
 
 import android.Manifest;
+import android.app.ActivityManager;
+import android.app.SynchronousUserSwitchObserver;
 import android.attention.AttentionManagerInternal;
 import android.attention.AttentionManagerInternal.AttentionCallbackInternal;
 import android.content.ContentResolver;
@@ -28,6 +30,7 @@
 import android.os.Handler;
 import android.os.PowerManager;
 import android.os.PowerManagerInternal;
+import android.os.RemoteException;
 import android.os.SystemClock;
 import android.os.UserHandle;
 import android.provider.Settings;
@@ -54,6 +57,8 @@
     private static final String TAG = "AttentionDetector";
     private static final boolean DEBUG = false;
 
+    private Context mContext;
+
     private boolean mIsSettingEnabled;
 
     /**
@@ -132,6 +137,7 @@
     }
 
     public void systemReady(Context context) {
+        mContext = context;
         updateEnabledFromSettings(context);
         mPackageManager = context.getPackageManager();
         mContentResolver = context.getContentResolver();
@@ -141,6 +147,13 @@
         mMaxAttentionApiTimeoutMillis = context.getResources().getInteger(
                 com.android.internal.R.integer.config_attentionApiTimeout);
 
+        try {
+            final UserSwitchObserver observer = new UserSwitchObserver();
+            ActivityManager.getService().registerUserSwitchObserver(observer, TAG);
+        } catch (RemoteException e) {
+             // Shouldn't happen since in-process.
+        }
+
         context.getContentResolver().registerContentObserver(Settings.System.getUriFor(
                 Settings.System.ADAPTIVE_SLEEP),
                 false, new ContentObserver(new Handler()) {
@@ -326,4 +339,11 @@
             mRequested.set(false);
         }
     }
+
+    private final class UserSwitchObserver extends SynchronousUserSwitchObserver {
+        @Override
+        public void onUserSwitching(int newUserId) throws RemoteException {
+            updateEnabledFromSettings(mContext);
+        }
+    }
 }