Merge "Be cool in backup/restore of apps that can't touch external storage"
diff --git a/core/java/android/app/Notification.java b/core/java/android/app/Notification.java
index 4a0ee48..ebca041 100644
--- a/core/java/android/app/Notification.java
+++ b/core/java/android/app/Notification.java
@@ -448,7 +448,7 @@
      * Structure to encapsulate an "action", including title and icon, that can be attached to a Notification.
      * @hide
      */
-    private static class Action implements Parcelable {
+    public static class Action implements Parcelable {
         public int icon;
         public CharSequence title;
         public PendingIntent actionIntent;
@@ -500,7 +500,10 @@
         };
     }
 
-    private Action[] actions;
+    /**
+     * @hide
+     */
+    public Action[] actions;
 
     /**
      * Constructs a Notification object with default values.
diff --git a/core/java/android/app/PendingIntent.java b/core/java/android/app/PendingIntent.java
index 2897ee0..37804e9 100644
--- a/core/java/android/app/PendingIntent.java
+++ b/core/java/android/app/PendingIntent.java
@@ -41,7 +41,7 @@
  * you are granting it the right to perform the operation you have specified
  * as if the other application was yourself (with the same permissions and
  * identity).  As such, you should be careful about how you build the PendingIntent:
- * often, for example, the base Intent you supply will have the component
+ * almost always, for example, the base Intent you supply should have the component
  * name explicitly set to one of your own components, to ensure it is ultimately
  * sent there and nowhere else.
  *
@@ -200,6 +200,11 @@
      * existing activity, so you must use the {@link Intent#FLAG_ACTIVITY_NEW_TASK
      * Intent.FLAG_ACTIVITY_NEW_TASK} launch flag in the Intent.
      *
+     * <p class="note">For security reasons, the {@link android.content.Intent}
+     * you supply here should almost always be an <em>explicit intent</em>,
+     * that is specify an explicit component to be delivered to through
+     * {@link Intent#setClass(android.content.Context, Class)} Intent.setClass</p>
+     *
      * @param context The Context in which this PendingIntent should start
      * the activity.
      * @param requestCode Private request code for the sender (currently
@@ -227,6 +232,11 @@
      * existing activity, so you must use the {@link Intent#FLAG_ACTIVITY_NEW_TASK
      * Intent.FLAG_ACTIVITY_NEW_TASK} launch flag in the Intent.
      *
+     * <p class="note">For security reasons, the {@link android.content.Intent}
+     * you supply here should almost always be an <em>explicit intent</em>,
+     * that is specify an explicit component to be delivered to through
+     * {@link Intent#setClass(android.content.Context, Class)} Intent.setClass</p>
+     *
      * @param context The Context in which this PendingIntent should start
      * the activity.
      * @param requestCode Private request code for the sender (currently
@@ -313,6 +323,11 @@
      * UI the user actually sees when the intents are started.
      * </p>
      *
+     * <p class="note">For security reasons, the {@link android.content.Intent} objects
+     * you supply here should almost always be <em>explicit intents</em>,
+     * that is specify an explicit component to be delivered to through
+     * {@link Intent#setClass(android.content.Context, Class)} Intent.setClass</p>
+     *
      * @param context The Context in which this PendingIntent should start
      * the activity.
      * @param requestCode Private request code for the sender (currently
@@ -359,6 +374,11 @@
      * UI the user actually sees when the intents are started.
      * </p>
      *
+     * <p class="note">For security reasons, the {@link android.content.Intent} objects
+     * you supply here should almost always be <em>explicit intents</em>,
+     * that is specify an explicit component to be delivered to through
+     * {@link Intent#setClass(android.content.Context, Class)} Intent.setClass</p>
+     *
      * @param context The Context in which this PendingIntent should start
      * the activity.
      * @param requestCode Private request code for the sender (currently
@@ -423,6 +443,11 @@
      * Retrieve a PendingIntent that will perform a broadcast, like calling
      * {@link Context#sendBroadcast(Intent) Context.sendBroadcast()}.
      *
+     * <p class="note">For security reasons, the {@link android.content.Intent}
+     * you supply here should almost always be an <em>explicit intent</em>,
+     * that is specify an explicit component to be delivered to through
+     * {@link Intent#setClass(android.content.Context, Class)} Intent.setClass</p>
+     *
      * @param context The Context in which this PendingIntent should perform
      * the broadcast.
      * @param requestCode Private request code for the sender (currently
@@ -473,6 +498,11 @@
      * {@link Context#startService Context.startService()}.  The start
      * arguments given to the service will come from the extras of the Intent.
      *
+     * <p class="note">For security reasons, the {@link android.content.Intent}
+     * you supply here should almost always be an <em>explicit intent</em>,
+     * that is specify an explicit component to be delivered to through
+     * {@link Intent#setClass(android.content.Context, Class)} Intent.setClass</p>
+     *
      * @param context The Context in which this PendingIntent should start
      * the service.
      * @param requestCode Private request code for the sender (currently
@@ -707,6 +737,15 @@
      * sending the Intent.  The returned string is supplied by the system, so
      * that an application can not spoof its package.
      *
+     * <p class="note">Be careful about how you use this.  All this tells you is
+     * who created the PendingIntent.  It does <strong>not</strong> tell you who
+     * handed the PendingIntent to you: that is, PendingIntent objects are intended to be
+     * passed between applications, so the PendingIntent you receive from an application
+     * could actually be one it received from another application, meaning the result
+     * you get here will identify the original application.  Because of this, you should
+     * only use this information to identify who you expect to be interacting with
+     * through a {@link #send} call, not who gave you the PendingIntent.</p>
+     *
      * @return The package name of the PendingIntent, or null if there is
      * none associated with it.
      */
@@ -726,6 +765,15 @@
      * sending the Intent.  The returned integer is supplied by the system, so
      * that an application can not spoof its uid.
      *
+     * <p class="note">Be careful about how you use this.  All this tells you is
+     * who created the PendingIntent.  It does <strong>not</strong> tell you who
+     * handed the PendingIntent to you: that is, PendingIntent objects are intended to be
+     * passed between applications, so the PendingIntent you receive from an application
+     * could actually be one it received from another application, meaning the result
+     * you get here will identify the original application.  Because of this, you should
+     * only use this information to identify who you expect to be interacting with
+     * through a {@link #send} call, not who gave you the PendingIntent.</p>
+     *
      * @return The uid of the PendingIntent, or -1 if there is
      * none associated with it.
      */
@@ -747,6 +795,15 @@
      * {@link android.os.Process#myUserHandle() Process.myUserHandle()} for
      * more explanation of user handles.
      *
+     * <p class="note">Be careful about how you use this.  All this tells you is
+     * who created the PendingIntent.  It does <strong>not</strong> tell you who
+     * handed the PendingIntent to you: that is, PendingIntent objects are intended to be
+     * passed between applications, so the PendingIntent you receive from an application
+     * could actually be one it received from another application, meaning the result
+     * you get here will identify the original application.  Because of this, you should
+     * only use this information to identify who you expect to be interacting with
+     * through a {@link #send} call, not who gave you the PendingIntent.</p>
+     *
      * @return The user handle of the PendingIntent, or null if there is
      * none associated with it.
      */
diff --git a/core/java/android/appwidget/AppWidgetHost.java b/core/java/android/appwidget/AppWidgetHost.java
index fa3bf4d..a470e70 100644
--- a/core/java/android/appwidget/AppWidgetHost.java
+++ b/core/java/android/appwidget/AppWidgetHost.java
@@ -31,6 +31,7 @@
 import android.os.ServiceManager;
 import android.os.UserHandle;
 import android.util.DisplayMetrics;
+import android.util.Log;
 import android.util.TypedValue;
 import android.widget.RemoteViews;
 import android.widget.RemoteViews.OnClickHandler;
@@ -55,38 +56,39 @@
 
     Context mContext;
     String mPackageName;
+    Handler mHandler;
+    int mHostId;
+    Callbacks mCallbacks = new Callbacks();
+    final HashMap<Integer,AppWidgetHostView> mViews = new HashMap<Integer, AppWidgetHostView>();
+    private OnClickHandler mOnClickHandler;
 
     class Callbacks extends IAppWidgetHost.Stub {
-        public void updateAppWidget(int appWidgetId, RemoteViews views) {
+        public void updateAppWidget(int appWidgetId, RemoteViews views, int userId) {
             if (isLocalBinder() && views != null) {
                 views = views.clone();
-                views.setUser(mUser);
+                views.setUser(new UserHandle(userId));
             }
-            Message msg = mHandler.obtainMessage(HANDLE_UPDATE);
-            msg.arg1 = appWidgetId;
-            msg.obj = views;
+            Message msg = mHandler.obtainMessage(HANDLE_UPDATE, appWidgetId, userId, views);
             msg.sendToTarget();
         }
 
-        public void providerChanged(int appWidgetId, AppWidgetProviderInfo info) {
+        public void providerChanged(int appWidgetId, AppWidgetProviderInfo info, int userId) {
             if (isLocalBinder() && info != null) {
                 info = info.clone();
             }
-            Message msg = mHandler.obtainMessage(HANDLE_PROVIDER_CHANGED);
-            msg.arg1 = appWidgetId;
-            msg.obj = info;
+            Message msg = mHandler.obtainMessage(HANDLE_PROVIDER_CHANGED,
+                    appWidgetId, userId, info);
             msg.sendToTarget();
         }
 
-        public void providersChanged() {
-            Message msg = mHandler.obtainMessage(HANDLE_PROVIDERS_CHANGED);
+        public void providersChanged(int userId) {
+            Message msg = mHandler.obtainMessage(HANDLE_PROVIDERS_CHANGED, userId, 0);
             msg.sendToTarget();
         }
 
-        public void viewDataChanged(int appWidgetId, int viewId) {
-            Message msg = mHandler.obtainMessage(HANDLE_VIEW_DATA_CHANGED);
-            msg.arg1 = appWidgetId;
-            msg.arg2 = viewId;
+        public void viewDataChanged(int appWidgetId, int viewId, int userId) {
+            Message msg = mHandler.obtainMessage(HANDLE_VIEW_DATA_CHANGED,
+                    appWidgetId, viewId, userId);
             msg.sendToTarget();
         }
     }
@@ -99,7 +101,7 @@
         public void handleMessage(Message msg) {
             switch (msg.what) {
                 case HANDLE_UPDATE: {
-                    updateAppWidgetView(msg.arg1, (RemoteViews)msg.obj);
+                    updateAppWidgetView(msg.arg1, (RemoteViews)msg.obj, msg.arg2);
                     break;
                 }
                 case HANDLE_PROVIDER_CHANGED: {
@@ -107,26 +109,17 @@
                     break;
                 }
                 case HANDLE_PROVIDERS_CHANGED: {
-                    onProvidersChanged();
+                    onProvidersChanged(msg.arg1);
                     break;
                 }
                 case HANDLE_VIEW_DATA_CHANGED: {
-                    viewDataChanged(msg.arg1, msg.arg2);
+                    viewDataChanged(msg.arg1, msg.arg2, (Integer) msg.obj);
                     break;
                 }
             }
         }
     }
 
-    Handler mHandler;
-
-    int mHostId;
-    Callbacks mCallbacks = new Callbacks();
-    final HashMap<Integer,AppWidgetHostView> mViews = new HashMap<Integer, AppWidgetHostView>();
-    private OnClickHandler mOnClickHandler;
-    // Optionally set by lockscreen
-    private UserHandle mUser;
-
     public AppWidgetHost(Context context, int hostId) {
         this(context, hostId, null, context.getMainLooper());
     }
@@ -140,14 +133,9 @@
         mOnClickHandler = handler;
         mHandler = new UpdateHandler(looper);
         mDisplayMetrics = context.getResources().getDisplayMetrics();
-        mUser = Process.myUserHandle();
         bindService();
     }
 
-    /** @hide */
-    public void setUserId(int userId) {
-        mUser = new UserHandle(userId);
-    }
 
     private static void bindService() {
         synchronized (sServiceLock) {
@@ -163,23 +151,15 @@
      * becomes visible, i.e. from onStart() in your Activity.
      */
     public void startListening() {
-        startListeningAsUser(UserHandle.myUserId());
-    }
-
-    /**
-     * Start receiving onAppWidgetChanged calls for your AppWidgets.  Call this when your activity
-     * becomes visible, i.e. from onStart() in your Activity.
-     * @hide
-     */
-    public void startListeningAsUser(int userId) {
         int[] updatedIds;
         ArrayList<RemoteViews> updatedViews = new ArrayList<RemoteViews>();
 
+        final int userId = mContext.getUserId();
         try {
             if (mPackageName == null) {
                 mPackageName = mContext.getPackageName();
             }
-            updatedIds = sService.startListeningAsUser(
+            updatedIds = sService.startListening(
                     mCallbacks, mPackageName, mHostId, updatedViews, userId);
         }
         catch (RemoteException e) {
@@ -191,7 +171,7 @@
             if (updatedViews.get(i) != null) {
                 updatedViews.get(i).setUser(new UserHandle(userId));
             }
-            updateAppWidgetView(updatedIds[i], updatedViews.get(i));
+            updateAppWidgetView(updatedIds[i], updatedViews.get(i), userId);
         }
     }
 
@@ -201,26 +181,14 @@
      */
     public void stopListening() {
         try {
-            sService.stopListeningAsUser(mHostId, UserHandle.myUserId());
+            sService.stopListening(mHostId, mContext.getUserId());
         }
         catch (RemoteException e) {
             throw new RuntimeException("system server dead?", e);
         }
-    }
 
-    /**
-     * Stop receiving onAppWidgetChanged calls for your AppWidgets.  Call this when your activity is
-     * no longer visible, i.e. from onStop() in your Activity.
-     * @hide
-     */
-    public void stopListeningAsUser(int userId) {
-        try {
-            sService.stopListeningAsUser(mHostId, userId);
-        }
-        catch (RemoteException e) {
-            throw new RuntimeException("system server dead?", e);
-        }
-        // Also clear the views
+        // This is here because keyguard needs it since it'll be switching users after this call.
+        // If it turns out other apps need to call this often, we should re-think how this works.
         clearViews();
     }
 
@@ -230,11 +198,12 @@
      * @return a appWidgetId
      */
     public int allocateAppWidgetId() {
+
         try {
             if (mPackageName == null) {
                 mPackageName = mContext.getPackageName();
             }
-            return sService.allocateAppWidgetId(mPackageName, mHostId);
+            return sService.allocateAppWidgetId(mPackageName, mHostId, mContext.getUserId());
         }
         catch (RemoteException e) {
             throw new RuntimeException("system server dead?", e);
@@ -247,7 +216,7 @@
      * @return a appWidgetId
      * @hide
      */
-    public static int allocateAppWidgetIdForSystem(int hostId) {
+    public static int allocateAppWidgetIdForSystem(int hostId, int userId) {
         checkCallerIsSystem();
         try {
             if (sService == null) {
@@ -256,7 +225,7 @@
             Context systemContext =
                     (Context) ActivityThread.currentActivityThread().getSystemContext();
             String packageName = systemContext.getPackageName();
-            return sService.allocateAppWidgetId(packageName, hostId);
+            return sService.allocateAppWidgetId(packageName, hostId, userId);
         } catch (RemoteException e) {
             throw new RuntimeException("system server dead?", e);
         }
@@ -272,7 +241,7 @@
             if (sService == null) {
                 bindService();
             }
-            return sService.getAppWidgetIdsForHost(mHostId);
+            return sService.getAppWidgetIdsForHost(mHostId, mContext.getUserId());
         } catch (RemoteException e) {
             throw new RuntimeException("system server dead?", e);
         }
@@ -297,7 +266,7 @@
         synchronized (mViews) {
             mViews.remove(appWidgetId);
             try {
-                sService.deleteAppWidgetId(appWidgetId);
+                sService.deleteAppWidgetId(appWidgetId, mContext.getUserId());
             }
             catch (RemoteException e) {
                 throw new RuntimeException("system server dead?", e);
@@ -309,13 +278,13 @@
      * Stop listening to changes for this AppWidget.
      * @hide
      */
-    public static void deleteAppWidgetIdForSystem(int appWidgetId) {
+    public static void deleteAppWidgetIdForSystem(int appWidgetId, int userId) {
         checkCallerIsSystem();
         try {
             if (sService == null) {
                 bindService();
             }
-            sService.deleteAppWidgetId(appWidgetId);
+            sService.deleteAppWidgetId(appWidgetId, userId);
         } catch (RemoteException e) {
             throw new RuntimeException("system server dead?", e);
         }
@@ -331,7 +300,7 @@
      */
     public void deleteHost() {
         try {
-            sService.deleteHost(mHostId);
+            sService.deleteHost(mHostId, mContext.getUserId());
         }
         catch (RemoteException e) {
             throw new RuntimeException("system server dead?", e);
@@ -347,8 +316,16 @@
      * </ul>
      */
     public static void deleteAllHosts() {
+        deleteAllHosts(UserHandle.myUserId());
+    }
+
+    /**
+     * Private method containing a userId
+     * @hide
+     */
+    public static void deleteAllHosts(int userId) {
         try {
-            sService.deleteAllHosts();
+            sService.deleteAllHosts(userId);
         }
         catch (RemoteException e) {
             throw new RuntimeException("system server dead?", e);
@@ -361,8 +338,9 @@
      */
     public final AppWidgetHostView createView(Context context, int appWidgetId,
             AppWidgetProviderInfo appWidget) {
+        final int userId = context.getUserId();
         AppWidgetHostView view = onCreateView(context, appWidgetId, appWidget);
-        view.setUserId(mUser.getIdentifier());
+        view.setUserId(userId);
         view.setOnClickHandler(mOnClickHandler);
         view.setAppWidget(appWidgetId, appWidget);
         synchronized (mViews) {
@@ -370,9 +348,9 @@
         }
         RemoteViews views;
         try {
-            views = sService.getAppWidgetViews(appWidgetId);
+            views = sService.getAppWidgetViews(appWidgetId, userId);
             if (views != null) {
-                views.setUser(mUser);
+                views.setUser(new UserHandle(mContext.getUserId()));
             }
         } catch (RemoteException e) {
             throw new RuntimeException("system server dead?", e);
@@ -422,10 +400,20 @@
      * are added, updated or removed, or widget components are enabled or disabled.)
      */
     protected void onProvidersChanged() {
-        // Do nothing
+        onProvidersChanged(mContext.getUserId());
     }
 
-    void updateAppWidgetView(int appWidgetId, RemoteViews views) {
+    /**
+     * Private method containing a userId
+     * @hide
+     */
+    protected void onProvidersChanged(int userId) {
+        checkUserMatch(userId);
+        // Does nothing
+    }
+
+    void updateAppWidgetView(int appWidgetId, RemoteViews views, int userId) {
+        checkUserMatch(userId);
         AppWidgetHostView v;
         synchronized (mViews) {
             v = mViews.get(appWidgetId);
@@ -435,7 +423,8 @@
         }
     }
 
-    void viewDataChanged(int appWidgetId, int viewId) {
+    void viewDataChanged(int appWidgetId, int viewId, int userId) {
+        checkUserMatch(userId);
         AppWidgetHostView v;
         synchronized (mViews) {
             v = mViews.get(appWidgetId);
@@ -445,6 +434,16 @@
         }
     }
 
+    // Ensure that the userId passed to us agrees with the one associated with this instance
+    // of AppWidgetHost.
+    // TODO: This should be removed in production code.
+    private void checkUserMatch(int userId) {
+        if (userId != mContext.getUserId()) {
+            throw new IllegalStateException(
+                "User ids don't match, userId=" + userId + ", mUserId=" + mContext.getUserId());
+        }
+    }
+
     /**
      * Clear the list of Views that have been created by this AppWidgetHost.
      */
diff --git a/core/java/android/appwidget/AppWidgetManager.java b/core/java/android/appwidget/AppWidgetManager.java
index 6b1c3e2..e68d23a 100644
--- a/core/java/android/appwidget/AppWidgetManager.java
+++ b/core/java/android/appwidget/AppWidgetManager.java
@@ -16,6 +16,7 @@
 
 package android.appwidget;
 
+import android.app.ActivityManagerNative;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
@@ -268,8 +269,8 @@
     /**
      * Sent when the custom extras for an AppWidget change.
      *
-     * @see AppWidgetProvider#onAppWidgetOptionsChanged 
-     *      AppWidgetProvider.onAppWidgetOptionsChanged(Context context, 
+     * @see AppWidgetProvider#onAppWidgetOptionsChanged
+     *      AppWidgetProvider.onAppWidgetOptionsChanged(Context context,
      *      AppWidgetManager appWidgetManager, int appWidgetId, Bundle newExtras)
      */
     public static final String ACTION_APPWIDGET_OPTIONS_CHANGED = "android.appwidget.action.APPWIDGET_UPDATE_OPTIONS";
@@ -352,7 +353,7 @@
      * It is okay to call this method both inside an {@link #ACTION_APPWIDGET_UPDATE} broadcast,
      * and outside of the handler.
      * This method will only work when called from the uid that owns the AppWidget provider.
-     * 
+     *
      * <p>
      * The total Bitmap memory used by the RemoteViews object cannot exceed that required to
      * fill the screen 1.5 times, ie. (screen width x screen height x 4 x 1.5) bytes.
@@ -362,7 +363,7 @@
      */
     public void updateAppWidget(int[] appWidgetIds, RemoteViews views) {
         try {
-            sService.updateAppWidgetIds(appWidgetIds, views);
+            sService.updateAppWidgetIds(appWidgetIds, views, mContext.getUserId());
         }
         catch (RemoteException e) {
             throw new RuntimeException("system server dead?", e);
@@ -382,7 +383,7 @@
      */
     public void updateAppWidgetOptions(int appWidgetId, Bundle options) {
         try {
-            sService.updateAppWidgetOptions(appWidgetId, options);
+            sService.updateAppWidgetOptions(appWidgetId, options, mContext.getUserId());
         }
         catch (RemoteException e) {
             throw new RuntimeException("system server dead?", e);
@@ -402,7 +403,7 @@
      */
     public Bundle getAppWidgetOptions(int appWidgetId) {
         try {
-            return sService.getAppWidgetOptions(appWidgetId);
+            return sService.getAppWidgetOptions(appWidgetId, mContext.getUserId());
         }
         catch (RemoteException e) {
             throw new RuntimeException("system server dead?", e);
@@ -436,7 +437,7 @@
      * Perform an incremental update or command on the widget(s) specified by appWidgetIds.
      *
      * This update  differs from {@link #updateAppWidget(int[], RemoteViews)} in that the
-     * RemoteViews object which is passed is understood to be an incomplete representation of the 
+     * RemoteViews object which is passed is understood to be an incomplete representation of the
      * widget, and hence does not replace the cached representation of the widget. As of API
      * level 17, the new properties set within the views objects will be appended to the cached
      * representation of the widget, and hence will persist.
@@ -458,7 +459,7 @@
      */
     public void partiallyUpdateAppWidget(int[] appWidgetIds, RemoteViews views) {
         try {
-            sService.partiallyUpdateAppWidgetIds(appWidgetIds, views);
+            sService.partiallyUpdateAppWidgetIds(appWidgetIds, views, mContext.getUserId());
         } catch (RemoteException e) {
             throw new RuntimeException("system server dead?", e);
         }
@@ -507,7 +508,7 @@
      */
     public void updateAppWidget(ComponentName provider, RemoteViews views) {
         try {
-            sService.updateAppWidgetProvider(provider, views);
+            sService.updateAppWidgetProvider(provider, views, mContext.getUserId());
         }
         catch (RemoteException e) {
             throw new RuntimeException("system server dead?", e);
@@ -523,7 +524,7 @@
      */
     public void notifyAppWidgetViewDataChanged(int[] appWidgetIds, int viewId) {
         try {
-            sService.notifyAppWidgetViewDataChanged(appWidgetIds, viewId);
+            sService.notifyAppWidgetViewDataChanged(appWidgetIds, viewId, mContext.getUserId());
         }
         catch (RemoteException e) {
             throw new RuntimeException("system server dead?", e);
@@ -557,7 +558,8 @@
      */
     public List<AppWidgetProviderInfo> getInstalledProviders(int categoryFilter) {
         try {
-            List<AppWidgetProviderInfo> providers = sService.getInstalledProviders(categoryFilter);
+            List<AppWidgetProviderInfo> providers = sService.getInstalledProviders(categoryFilter,
+                    mContext.getUserId());
             for (AppWidgetProviderInfo info : providers) {
                 // Converting complex to dp.
                 info.minWidth =
@@ -584,7 +586,8 @@
      */
     public AppWidgetProviderInfo getAppWidgetInfo(int appWidgetId) {
         try {
-            AppWidgetProviderInfo info = sService.getAppWidgetInfo(appWidgetId);
+            AppWidgetProviderInfo info = sService.getAppWidgetInfo(appWidgetId,
+                    mContext.getUserId());
             if (info != null) {
                 // Converting complex to dp.
                 info.minWidth =
@@ -617,7 +620,7 @@
      */
     public void bindAppWidgetId(int appWidgetId, ComponentName provider) {
         try {
-            sService.bindAppWidgetId(appWidgetId, provider, null);
+            sService.bindAppWidgetId(appWidgetId, provider, null, mContext.getUserId());
         }
         catch (RemoteException e) {
             throw new RuntimeException("system server dead?", e);
@@ -641,7 +644,7 @@
      */
     public void bindAppWidgetId(int appWidgetId, ComponentName provider, Bundle options) {
         try {
-            sService.bindAppWidgetId(appWidgetId, provider, options);
+            sService.bindAppWidgetId(appWidgetId, provider, options, mContext.getUserId());
         }
         catch (RemoteException e) {
             throw new RuntimeException("system server dead?", e);
@@ -667,7 +670,7 @@
         }
         try {
             return sService.bindAppWidgetIdIfAllowed(
-                    mContext.getPackageName(), appWidgetId, provider, null);
+                    mContext.getPackageName(), appWidgetId, provider, null, mContext.getUserId());
         }
         catch (RemoteException e) {
             throw new RuntimeException("system server dead?", e);
@@ -696,8 +699,8 @@
             return false;
         }
         try {
-            return sService.bindAppWidgetIdIfAllowed(
-                    mContext.getPackageName(), appWidgetId, provider, options);
+            return sService.bindAppWidgetIdIfAllowed(mContext.getPackageName(), appWidgetId,
+                    provider, options, mContext.getUserId());
         }
         catch (RemoteException e) {
             throw new RuntimeException("system server dead?", e);
@@ -715,7 +718,7 @@
      */
     public boolean hasBindAppWidgetPermission(String packageName) {
         try {
-            return sService.hasBindAppWidgetPermission(packageName);
+            return sService.hasBindAppWidgetPermission(packageName, mContext.getUserId());
         }
         catch (RemoteException e) {
             throw new RuntimeException("system server dead?", e);
@@ -733,7 +736,7 @@
      */
     public void setBindAppWidgetPermission(String packageName, boolean permission) {
         try {
-            sService.setBindAppWidgetPermission(packageName, permission);
+            sService.setBindAppWidgetPermission(packageName, permission, mContext.getUserId());
         }
         catch (RemoteException e) {
             throw new RuntimeException("system server dead?", e);
@@ -794,7 +797,7 @@
      */
     public int[] getAppWidgetIds(ComponentName provider) {
         try {
-            return sService.getAppWidgetIds(provider);
+            return sService.getAppWidgetIds(provider, mContext.getUserId());
         }
         catch (RemoteException e) {
             throw new RuntimeException("system server dead?", e);
diff --git a/core/java/android/content/ContentProvider.java b/core/java/android/content/ContentProvider.java
index 90ee0ac..e9b800d 100644
--- a/core/java/android/content/ContentProvider.java
+++ b/core/java/android/content/ContentProvider.java
@@ -871,6 +871,18 @@
      * their responsibility to close it when done.  That is, the implementation
      * of this method should create a new ParcelFileDescriptor for each call.
      *
+     * <p class="note">For use in Intents, you will want to implement {@link #getType}
+     * to return the appropriate MIME type for the data returned here with
+     * the same URI.  This will allow intent resolution to automatically determine the data MIME
+     * type and select the appropriate matching targets as part of its operation.</p>
+     *
+     * <p class="note">For better interoperability with other applications, it is recommended
+     * that for any URIs that can be opened, you also support queries on them
+     * containing at least the columns specified by {@link android.provider.OpenableColumns}.
+     * You may also want to support other common columns if you have additional meta-data
+     * to supply, such as {@link android.provider.MediaStore.MediaColumns#DATE_ADDED}
+     * in {@link android.provider.MediaStore.MediaColumns}.</p>
+     *
      * @param uri The URI whose file is to be opened.
      * @param mode Access mode for the file.  May be "r" for read-only access,
      * "rw" for read and write access, or "rwt" for read and write access
@@ -886,6 +898,7 @@
      *
      * @see #openAssetFile(Uri, String)
      * @see #openFileHelper(Uri, String)
+     * @see #getType(android.net.Uri)
      */
     public ParcelFileDescriptor openFile(Uri uri, String mode)
             throws FileNotFoundException {
@@ -913,6 +926,15 @@
      * {@link AssetFileDescriptor#UNKNOWN_LENGTH} to be compatible with
      * applications that can not handle sub-sections of files.</p>
      *
+     * <p class="note">For use in Intents, you will want to implement {@link #getType}
+     * to return the appropriate MIME type for the data returned here with
+     * the same URI.  This will allow intent resolution to automatically determine the data MIME
+     * type and select the appropriate matching targets as part of its operation.</p>
+     *
+     * <p class="note">For better interoperability with other applications, it is recommended
+     * that for any URIs that can be opened, you also support queries on them
+     * containing at least the columns specified by {@link android.provider.OpenableColumns}.</p>
+     *
      * @param uri The URI whose file is to be opened.
      * @param mode Access mode for the file.  May be "r" for read-only access,
      * "w" for write-only access (erasing whatever data is currently in
@@ -930,6 +952,7 @@
      * 
      * @see #openFile(Uri, String)
      * @see #openFileHelper(Uri, String)
+     * @see #getType(android.net.Uri)
      */
     public AssetFileDescriptor openAssetFile(Uri uri, String mode)
             throws FileNotFoundException {
@@ -1009,12 +1032,19 @@
      * perform data conversions to generate data of the desired type.
      *
      * <p>The default implementation compares the given mimeType against the
-     * result of {@link #getType(Uri)} and, if the match, simple calls
+     * result of {@link #getType(Uri)} and, if they match, simply calls
      * {@link #openAssetFile(Uri, String)}.
      *
      * <p>See {@link ClipData} for examples of the use and implementation
      * of this method.
      *
+     * <p class="note">For better interoperability with other applications, it is recommended
+     * that for any URIs that can be opened, you also support queries on them
+     * containing at least the columns specified by {@link android.provider.OpenableColumns}.
+     * You may also want to support other common columns if you have additional meta-data
+     * to supply, such as {@link android.provider.MediaStore.MediaColumns#DATE_ADDED}
+     * in {@link android.provider.MediaStore.MediaColumns}.</p>
+     *
      * @param uri The data in the content provider being queried.
      * @param mimeTypeFilter The type of data the client desires.  May be
      * a pattern, such as *\/*, if the caller does not have specific type
diff --git a/core/java/android/content/Context.java b/core/java/android/content/Context.java
index c964af4..8a9eed2 100644
--- a/core/java/android/content/Context.java
+++ b/core/java/android/content/Context.java
@@ -2691,6 +2691,14 @@
             throws PackageManager.NameNotFoundException;
 
     /**
+     * Get the userId associated with this context
+     * @return user id
+     *
+     * @hide
+     */
+    public abstract int getUserId();
+
+    /**
      * Return a new Context object for the current Context but whose resources
      * are adjusted to match the given Configuration.  Each call to this method
      * returns a new instance of a Context object; Context objects are not
diff --git a/core/java/android/content/ContextWrapper.java b/core/java/android/content/ContextWrapper.java
index 736dd99..2f1bf8c 100644
--- a/core/java/android/content/ContextWrapper.java
+++ b/core/java/android/content/ContextWrapper.java
@@ -623,6 +623,12 @@
         return mBase.createPackageContextAsUser(packageName, flags, user);
     }
 
+    /** @hide */
+    @Override
+    public int getUserId() {
+        return mBase.getUserId();
+    }
+
     @Override
     public Context createConfigurationContext(Configuration overrideConfiguration) {
         return mBase.createConfigurationContext(overrideConfiguration);
diff --git a/core/java/android/content/Intent.java b/core/java/android/content/Intent.java
index bbf3b69..f8ff8d1 100644
--- a/core/java/android/content/Intent.java
+++ b/core/java/android/content/Intent.java
@@ -2544,6 +2544,14 @@
     public static final String ACTION_QUICK_CLOCK =
             "android.intent.action.QUICK_CLOCK";
 
+    /**
+     * Broadcast Action: This is broadcast when a user action should request the
+     * brightness setting dialog.
+     * @hide
+     */
+    public static final String ACTION_SHOW_BRIGHTNESS_DIALOG =
+            "android.intent.action.SHOW_BRIGHTNESS_DIALOG";
+
     // ---------------------------------------------------------------------
     // ---------------------------------------------------------------------
     // Standard intent categories (see addCategory()).
@@ -2665,7 +2673,8 @@
     public static final String CATEGORY_SAMPLE_CODE = "android.intent.category.SAMPLE_CODE";
     /**
      * Used to indicate that a GET_CONTENT intent only wants URIs that can be opened with
-     * ContentResolver.openInputStream. Openable URIs must support the columns in OpenableColumns
+     * ContentResolver.openInputStream. Openable URIs must support the columns in
+     * {@link android.provider.OpenableColumns}
      * when queried, though it is allowable for those columns to be blank.
      */
     @SdkConstant(SdkConstantType.INTENT_CATEGORY)
diff --git a/core/java/android/content/IntentFilter.java b/core/java/android/content/IntentFilter.java
index 3b0d846..5e65b59 100644
--- a/core/java/android/content/IntentFilter.java
+++ b/core/java/android/content/IntentFilter.java
@@ -86,7 +86,8 @@
  * <strong>data scheme+authority+path</strong> if specified) must match.
  *
  * <p><strong>Action</strong> matches if any of the given values match the
- * Intent action, <em>or</em> if no actions were specified in the filter.
+ * Intent action; if the filter specifies no actions, then it will only match
+ * Intents that do not contain an action.
  *
  * <p><strong>Data Type</strong> matches if any of the given values match the
  * Intent type.  The Intent
diff --git a/core/java/android/net/ConnectivityManager.java b/core/java/android/net/ConnectivityManager.java
index 000c56c..1f44e49 100644
--- a/core/java/android/net/ConnectivityManager.java
+++ b/core/java/android/net/ConnectivityManager.java
@@ -70,6 +70,7 @@
      * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY
      * is set to {@code true} if there are no connected networks at all.
      */
+    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
     public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
 
     /**
@@ -78,6 +79,7 @@
      *
      * @hide
      */
+    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
     public static final String CONNECTIVITY_ACTION_IMMEDIATE =
             "android.net.conn.CONNECTIVITY_CHANGE_IMMEDIATE";
 
@@ -198,6 +200,7 @@
      * the network and it's condition.
      * @hide
      */
+    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
     public static final String INET_CONDITION_ACTION =
             "android.net.conn.INET_CONDITION_ACTION";
 
@@ -206,6 +209,7 @@
      * TODO - finish the doc
      * @hide
      */
+    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
     public static final String ACTION_TETHER_STATE_CHANGED =
             "android.net.conn.TETHER_STATE_CHANGED";
 
@@ -233,6 +237,7 @@
      * notification.
      * @hide
      */
+    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
     public static final String ACTION_CAPTIVE_PORTAL_TEST_COMPLETED =
             "android.net.conn.CAPTIVE_PORTAL_TEST_COMPLETED";
     /**
diff --git a/core/java/android/util/Patterns.java b/core/java/android/util/Patterns.java
index 152827d..9522112 100644
--- a/core/java/android/util/Patterns.java
+++ b/core/java/android/util/Patterns.java
@@ -169,10 +169,10 @@
      * </ul>
      */
     public static final Pattern PHONE
-        = Pattern.compile(                                  // sdd = space, dot, or dash
-                "(\\+[0-9]+[\\- \\.]*)?"                    // +<digits><sdd>*
-                + "(\\([0-9]+\\)[\\- \\.]*)?"               // (<digits>)<sdd>*
-                + "([0-9][0-9\\- \\.][0-9\\- \\.]+[0-9])"); // <digit><digit|sdd>+<digit> 
+        = Pattern.compile(                      // sdd = space, dot, or dash
+                "(\\+[0-9]+[\\- \\.]*)?"        // +<digits><sdd>*
+                + "(\\([0-9]+\\)[\\- \\.]*)?"   // (<digits>)<sdd>*
+                + "([0-9][0-9\\- \\.]+[0-9])"); // <digit><digit|sdd>+<digit>
 
     /**
      *  Convenience method to take all of the non-null matching groups in a
diff --git a/core/java/com/android/internal/appwidget/IAppWidgetHost.aidl b/core/java/com/android/internal/appwidget/IAppWidgetHost.aidl
index 78b4466..6d51d38 100644
--- a/core/java/com/android/internal/appwidget/IAppWidgetHost.aidl
+++ b/core/java/com/android/internal/appwidget/IAppWidgetHost.aidl
@@ -22,9 +22,9 @@
 
 /** {@hide} */
 oneway interface IAppWidgetHost {
-    void updateAppWidget(int appWidgetId, in RemoteViews views);
-    void providerChanged(int appWidgetId, in AppWidgetProviderInfo info);
-    void providersChanged();
-    void viewDataChanged(int appWidgetId, int viewId);
+    void updateAppWidget(int appWidgetId, in RemoteViews views, int userId);
+    void providerChanged(int appWidgetId, in AppWidgetProviderInfo info, int userId);
+    void providersChanged(int userId);
+    void viewDataChanged(int appWidgetId, int viewId, int userId);
 }
 
diff --git a/core/java/com/android/internal/appwidget/IAppWidgetService.aidl b/core/java/com/android/internal/appwidget/IAppWidgetService.aidl
index e685e63..7ddd5d2 100644
--- a/core/java/com/android/internal/appwidget/IAppWidgetService.aidl
+++ b/core/java/com/android/internal/appwidget/IAppWidgetService.aidl
@@ -26,42 +26,39 @@
 
 /** {@hide} */
 interface IAppWidgetService {
-    
+
     //
     // for AppWidgetHost
     //
     int[] startListening(IAppWidgetHost host, String packageName, int hostId,
-            out List<RemoteViews> updatedViews);
-    int[] startListeningAsUser(IAppWidgetHost host, String packageName, int hostId,
             out List<RemoteViews> updatedViews, int userId);
-    void stopListening(int hostId);
-    void stopListeningAsUser(int hostId, int userId);
-    int allocateAppWidgetId(String packageName, int hostId);
-    void deleteAppWidgetId(int appWidgetId);
-    void deleteHost(int hostId);
-    void deleteAllHosts();
-    RemoteViews getAppWidgetViews(int appWidgetId);
-    int[] getAppWidgetIdsForHost(int hostId);
+    void stopListening(int hostId, int userId);
+    int allocateAppWidgetId(String packageName, int hostId, int userId);
+    void deleteAppWidgetId(int appWidgetId, int userId);
+    void deleteHost(int hostId, int userId);
+    void deleteAllHosts(int userId);
+    RemoteViews getAppWidgetViews(int appWidgetId, int userId);
+    int[] getAppWidgetIdsForHost(int hostId, int userId);
 
     //
     // for AppWidgetManager
     //
-    void updateAppWidgetIds(in int[] appWidgetIds, in RemoteViews views);
-    void updateAppWidgetOptions(int appWidgetId, in Bundle extras);
-    Bundle getAppWidgetOptions(int appWidgetId);
-    void partiallyUpdateAppWidgetIds(in int[] appWidgetIds, in RemoteViews views);
-    void updateAppWidgetProvider(in ComponentName provider, in RemoteViews views);
-    void notifyAppWidgetViewDataChanged(in int[] appWidgetIds, int viewId);
-    List<AppWidgetProviderInfo> getInstalledProviders(int categoryFilter);
-    AppWidgetProviderInfo getAppWidgetInfo(int appWidgetId);
-    boolean hasBindAppWidgetPermission(in String packageName);
-    void setBindAppWidgetPermission(in String packageName, in boolean permission);
-    void bindAppWidgetId(int appWidgetId, in ComponentName provider, in Bundle options);
-    boolean bindAppWidgetIdIfAllowed(
-            in String packageName, int appWidgetId, in ComponentName provider, in Bundle options);
+    void updateAppWidgetIds(in int[] appWidgetIds, in RemoteViews views, int userId);
+    void updateAppWidgetOptions(int appWidgetId, in Bundle extras, int userId);
+    Bundle getAppWidgetOptions(int appWidgetId, int userId);
+    void partiallyUpdateAppWidgetIds(in int[] appWidgetIds, in RemoteViews views, int userId);
+    void updateAppWidgetProvider(in ComponentName provider, in RemoteViews views, int userId);
+    void notifyAppWidgetViewDataChanged(in int[] appWidgetIds, int viewId, int userId);
+    List<AppWidgetProviderInfo> getInstalledProviders(int categoryFilter, int userId);
+    AppWidgetProviderInfo getAppWidgetInfo(int appWidgetId, int userId);
+    boolean hasBindAppWidgetPermission(in String packageName, int userId);
+    void setBindAppWidgetPermission(in String packageName, in boolean permission, int userId);
+    void bindAppWidgetId(int appWidgetId, in ComponentName provider, in Bundle options, int userId);
+    boolean bindAppWidgetIdIfAllowed(in String packageName, int appWidgetId,
+            in ComponentName provider, in Bundle options, int userId);
     void bindRemoteViewsService(int appWidgetId, in Intent intent, in IBinder connection, int userId);
     void unbindRemoteViewsService(int appWidgetId, in Intent intent, int userId);
-    int[] getAppWidgetIds(in ComponentName provider);
+    int[] getAppWidgetIds(in ComponentName provider, int userId);
 
 }
 
diff --git a/core/java/com/android/internal/widget/SizeAdaptiveLayout.java b/core/java/com/android/internal/widget/SizeAdaptiveLayout.java
index 0280a0b..ba113a3 100644
--- a/core/java/com/android/internal/widget/SizeAdaptiveLayout.java
+++ b/core/java/com/android/internal/widget/SizeAdaptiveLayout.java
@@ -121,12 +121,6 @@
         mTransitionAnimation.addListener(mAnimatorListener);
     }
 
-    @Override
-    public void setLayoutDirection(int layoutDirection) {
-        super.setLayoutDirection(layoutDirection);
-        mModestyPanel.setLayoutDirection(layoutDirection);
-    }
-
     /**
      * Visible for testing
      * @hide
@@ -246,7 +240,6 @@
                                                            View.MeasureSpec.EXACTLY);
         mActiveChild = selectActiveChild(measureSpec);
         mActiveChild.setVisibility(View.VISIBLE);
-        mActiveChild.setLayoutDirection(getLayoutDirection());
 
         if (mLastActive != mActiveChild && mLastActive != null) {
             if (DEBUG) Log.d(TAG, this + " changed children from: " + mLastActive +
diff --git a/core/jni/android_view_Surface.cpp b/core/jni/android_view_Surface.cpp
index ab0d38e..6cd1a5c 100644
--- a/core/jni/android_view_Surface.cpp
+++ b/core/jni/android_view_Surface.cpp
@@ -112,8 +112,8 @@
 
 // ----------------------------------------------------------------------------
 
-static bool isSurfaceValid(const sp<Surface>& sur) {
-    return sur != 0 && sur->getISurfaceTexture() != 0;
+static inline bool isSurfaceValid(const sp<Surface>& sur) {
+    return Surface::isValid(sur);
 }
 
 // ----------------------------------------------------------------------------
diff --git a/core/res/res/values/strings.xml b/core/res/res/values/strings.xml
index 00c6f6d..5fc26fc 100644
--- a/core/res/res/values/strings.xml
+++ b/core/res/res/values/strings.xml
@@ -3512,8 +3512,8 @@
     <string name="vpn_lockdown_connected">Always-on VPN connected</string>
     <!-- Notification title when error connecting to lockdown VPN. -->
     <string name="vpn_lockdown_error">Always-on VPN error</string>
-    <!-- Notification body that indicates user can touch to cycle lockdown VPN connection. -->
-    <string name="vpn_lockdown_reset">Touch to reset connection</string>
+    <!-- Notification body that indicates user can touch to configure lockdown VPN connection. -->
+    <string name="vpn_lockdown_config">Touch to configure</string>
 
     <!-- Localized strings for WebView -->
     <!-- Label for button in a WebView that will open a chooser to choose a file to upload -->
diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml
index 412d4b9..42ce336 100644
--- a/core/res/res/values/symbols.xml
+++ b/core/res/res/values/symbols.xml
@@ -1516,6 +1516,7 @@
   <java-symbol type="color" name="config_defaultNotificationColor" />
   <java-symbol type="drawable" name="ic_notification_ime_default" />
   <java-symbol type="drawable" name="ic_notify_wifidisplay" />
+  <java-symbol type="drawable" name="ic_menu_refresh" />
   <java-symbol type="drawable" name="stat_notify_car_mode" />
   <java-symbol type="drawable" name="stat_notify_disabled" />
   <java-symbol type="drawable" name="stat_notify_disk_full" />
@@ -1649,7 +1650,7 @@
   <java-symbol type="string" name="vpn_lockdown_connecting" />
   <java-symbol type="string" name="vpn_lockdown_connected" />
   <java-symbol type="string" name="vpn_lockdown_error" />
-  <java-symbol type="string" name="vpn_lockdown_reset" />
+  <java-symbol type="string" name="vpn_lockdown_config" />
   <java-symbol type="string" name="wallpaper_binding_label" />
   <java-symbol type="string" name="wifi_display_notification_title" />
   <java-symbol type="string" name="wifi_display_notification_message" />
diff --git a/core/tests/coretests/src/android/util/PatternsTest.java b/core/tests/coretests/src/android/util/PatternsTest.java
index 9519b9f..ebdbb0e 100644
--- a/core/tests/coretests/src/android/util/PatternsTest.java
+++ b/core/tests/coretests/src/android/util/PatternsTest.java
@@ -156,6 +156,8 @@
                 "Me: 16505551212 this\n",
                 "Me: 6505551212 this\n",
                 "Me: 5551212 this\n",
+                "Me: 2211 this\n",
+                "Me: 112 this\n",
 
                 "Me: 1-650-555-1212 this\n",
                 "Me: (650) 555-1212 this\n",
diff --git a/docs/html/about/versions/android-4.2.jd b/docs/html/about/versions/android-4.2.jd
index 13ee872..b02c1d1 100644
--- a/docs/html/about/versions/android-4.2.jd
+++ b/docs/html/about/versions/android-4.2.jd
@@ -111,6 +111,20 @@
 android:minSdkVersion}</a> is lower than 17, your app is not able to modify the settings that have
 moved to {@link android.provider.Settings.Global} when running on Android 4.2 and higher.</p>
   </li>
+
+  <li>If your app uses {@link android.webkit.WebView}, Android 4.2 adds an additional layer of
+  security so you can more safely <b>bind JavaScript to your
+  Android code</b>. If you set your
+  <a href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#target">{@code targetSdkVersion}</a>
+  to 17 or higher, you must now add the {@code @JavascriptInterface} annotation to any method that you
+  want available to your JavaScript (the method must also be public). If you do not provide the
+  annotation, the method is not accessible by a web page in your {@link android.webkit.WebView}
+  when running on Android 4.2 or higher. If you set the
+  <a href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#target">{@code targetSdkVersion}</a>
+  to 16 or lower, the annotation is not required, but we recommend that you update your target version
+  and add the annotation for additional security.
+    <p>Read more about <a href="{@docRoot}guide/webapps/webview.html#BindingJavaScript">binding
+    JavaScript code to Android code</a>.</p></li>
 </ul>
 
 
diff --git a/docs/html/distribute/googleplay/about/distribution.jd b/docs/html/distribute/googleplay/about/distribution.jd
index e09e300..78883b4 100644
--- a/docs/html/distribute/googleplay/about/distribution.jd
+++ b/docs/html/distribute/googleplay/about/distribution.jd
@@ -18,23 +18,24 @@
 binary.</p>
 
 <p>Later, as you add features or address code issues, you can publish an updated
-binary at any time. Google Play makes the new version available immediately and
+binary at any time. Google Play makes the new version available almost immediately and
 notifies existing customers that an update is ready for download. To streamline
 the rollout across your customer base, Google Play also lets users accept
 automatic updates of your app, so that your updates are delivered and installed
 as soon as you publish them.</p>
 
+
 <h2 id="targeting">Reaching the customers you want</h2>
 
+<div class="figure-right" style="width:400px;">
+<img src="{@docRoot}images/gp-dc-countries.png" class="frame">
+</div>
+
 <p>Google Play does more than connect your app with users&mdash;it helps you
 reach the broadest possible distribution across the Android ecosystem, while
 making sure that your app is only available to the audience that you want to
 reach.</p>
 
-<div style="float:right;margin-left:18px;border:1px solid #DDD;margin:1.5em;">
-<img src="{@docRoot}images/gp-dc-countries.png" style="width:400px;padding:4px;margin-bottom:0;">
-</div>
-
 <h3 id="geotargeting">Geographic targeting</h3>
 
 <p>You can use controls in the Google Play Developer Console to easily
@@ -47,16 +48,15 @@
 and carrier targeting at any time just by saving changes in the Google Play
 Developer Console.</p>
 
+<div class="figure-right" style="width:400px;">
+<img src="{@docRoot}images/gp-supported-dev-requirements.png" class="frame">
+</div>
+
 <p>To help you market to users around the world, you
 can <a href="{@docRoot}distribute/googleplay/publish/preparing.html#localize">localize
 your store listing</a>, including app details and description,
 promotional graphics, screenshots, and more.</p>
 
-<div style="float:right;margin-left:18px;border:1px solid #DDD;margin:1.5em;">
-<img src="{@docRoot}images/gp-supported-dev-requirements.png"
-style="width:400px;padding:4px;margin-bottom:0;">
-</div>
-
 <h3 id="captargeting">Capabilities targeting</h3>
 
 <p>Google Play also lets you control distribution according to device features
diff --git a/docs/html/distribute/googleplay/about/monetizing.jd b/docs/html/distribute/googleplay/about/monetizing.jd
index 8bafd53..8233a31 100644
--- a/docs/html/distribute/googleplay/about/monetizing.jd
+++ b/docs/html/distribute/googleplay/about/monetizing.jd
@@ -116,8 +116,8 @@
 
 <h2 id="buyer-currency" style="margin-top:1.5em;">Flexible pricing in the currencies of your customers</h2>
 
-<div style="float:right;margin-left:18px;border:1px solid #DDD;">
-<img src="{@docRoot}images/gp-buyer-currency.png" style="width:240px;padding:4px;margin-bottom:1em;">
+<div class="figure-right" style="width:250px;">
+<img src="{@docRoot}images/gp-buyer-currency.png" class="frame">
 </div>
 
 <p>Google Play gives you complete control over how you price your products. You
diff --git a/docs/html/distribute/googleplay/publish/console.jd b/docs/html/distribute/googleplay/publish/console.jd
index af2b7e8..069b2d2 100644
--- a/docs/html/distribute/googleplay/publish/console.jd
+++ b/docs/html/distribute/googleplay/publish/console.jd
@@ -1,4 +1,4 @@
-page.title=The Developer Console
+page.title=Developer Console
 @jd:body
 
 
@@ -9,41 +9,41 @@
 tools on Google Play. This sections below introduce a few of the key areas
 you'll find in the Developer Console.</p>
 
-<div style="width:610px;margin-left:">
-<div style="width:610px;border:1px solid #DDD;">
-<img src="{@docRoot}images/gp-dc-home.png" style="width:600px;padding:4px;margin-bottom:0em;">
-</div>
-<p class="image-caption" style="padding:.5em"><span
-style="font-weight:500;">Developer Console home page</span>: Gives you a quick
-overview of your apps, lets you jump to stats, reviews, or product details, or
+<div class="figure" style="width:756px;">
+<img src="{@docRoot}images/gp-dc-home.png" class="frame">
+<p class="img-caption"><strong>All applications page</strong>: Gives you a quick
+overview of your apps, lets you jump to stats, reviews, and product details, or
 upload a new app. </p>
 </div>
 
-<h3 id="profile">Your Developer Profile</h3>
-
-<div style="width:408px;float:right;margin:1.5em;">
-<div style="width:410px;border:1px solid #DDD;">
-<img src="{@docRoot}images/gp-dc-profile.png" style="width:400px;padding:4px;margin-bottom:0em;">
-</div>
-<p class="image-caption" style="padding:.5em"><span
-style="font-weight:500;">Developer profile</span>: Specifies your developer
-identity and contact information, stores your developer key, and more.</p>
+<div class="figure-right" style="width:450px;">
+<img src="{@docRoot}images/gp-dc-profile.png" class="frame">
+<p class="img-caption"><strong>Account details page</strong>: Specifies your developer
+identity and contact information, accounts for app testing, and more.</p>
 </div>
 
-<p>Your developer profile identifies you to Google Play and to your customers.
-During registration you can provide information for your profile, but you can go
-back at any time to edit the information and change your settings. </p>
+<h3 id="profile">Your account details</h3>
+
+<p>The account details page is where you specify basic information about yourself
+or your company in a developer profile. The information in your developer profile
+is important because it identifies you to Google Play and also to your customers.</p>
+
+<p>During registration you must provide the information for your profile, but you can
+go back at any time to edit the information and change your settings. </p>
 
 <p>Your developer profile contains:</p>
 <ul>
-<li>Your developer name &mdash; the name you want to show users on your product
-details page and elsewhere on Google Play. 
+<li>Your developer name &mdash; the name you want to show users on your store
+listing page and elsewhere on Google Play. </li>
 <li>Your developer contact information &mdash; how Google can contact you if
-needed (this information isn't exposed to users.
-<li>Merchant information, in-app billing information.</li>
-<li>Your developer public key for licensing and In-app Billing.</li>
+needed (this information isn't exposed to users).</li>
+<li>Your developer website URL &mdash; shown to users on your store listing page
+so they can learn more about your company or products.</li>
 </ul>
 
+<p>On the account details page you can also register for a merchant account, set
+up test accounts for Google Play licensing, and more. </p>
+
 <h3 id="user-accounts">Multiple user accounts</h3>
 
 <p>If you are working with a team, you can set up multiple user accounts to
@@ -53,6 +53,14 @@
 have access to. For example, an owner can grant users access to publishing and
 app configuration, but not access to financial reports. </p>
 
+
+<div class="figure-right" style="width:450px;">
+<img src="{@docRoot}images/gp-dc-details.png" class="frame">
+<p class="img-caption"><strong>Store listing page</strong>: Lets you upload your
+graphic assets, description, support information, and other information to
+create the store listing page for a specific app.</p>
+</div>
+
 <h3 id="merchant">Linking your Merchant Account</h3>
 
 <p>If you want to sell apps or in-app products, you can link your Google
@@ -60,20 +68,10 @@
 Checkout account for financial and tax identification and monthly payouts of
 sales. </p>
 
-<div style="width:410px;float:right;margin:1.5em;">
-<div style="width:410px;border:1px solid #DDD;">
-<img src="{@docRoot}images/gp-dc-details.png" style="width:400px;padding:4px;margin-bottom:0em;">
-</div>
-<p class="image-caption" style="padding:.5em"><span
-style="font-weight:500;">Product details page</span>: Lets you upload your
-graphic assets, description, support information, and other information to
-create the product details page for a specific app.</p>
-</div>
-
-<h3 id="details">Your product and listing details</h3>
+<h3 id="details">Your store listing details</h3>
 
 <p>The Developer Console lets you set up a colorful storefront page for your app
-called the <em>product details page</em>. Your product details page is the home
+called the <em>Store Listing page</em>. Your Store Listing page is the home
 for your app in Google Play &mdash; it's the page users see on their mobile
 phones or on the web when they want to learn about your app and download it.
 </p>
@@ -87,10 +85,10 @@
 
 <p>From the Developer Console you can quickly upload a release-ready APK and
 publish it when you're ready. The app is a <em>draft</em> until you publish it,
-at which time Google Play makes your product details page and app available to
+at which time Google Play makes your store listing page and app available to
 users. You can unpublish the app at any time.</p>
 
-<h3 id="controls">Distribution Controls</h3>
+<h3 id="controls">Distribution controls</h3>
 
 <p>In the Developer Console you can manage what countries and territories the
 app is distributed to and, for some countries, you can choose what carriers you
@@ -99,7 +97,7 @@
 <p>You can also see the list of devices that your app is currently available to,
 based on any distribution rules declared in its manifest file.</p>
 
-<h3 id="profile">Selling and pricing your Products</h3>
+<h3 id="selling">Selling and pricing your products</h3>
 
 <p>The Developer Console gives you tools to set prices for your apps and in-app
 products. Your app can either be free to download or priced (charged before
@@ -115,25 +113,25 @@
 <li>If you publish your app as free, <span style="font-weight:500;">it must
 remain free</span>. Free apps can be downloaded by any users in Google
 Play.</li>
-<li>If you publish it as priced, you can change it to free, Priced apps can be
-purchased and downloaded only by users who have registered a forms of payment
+<li>If you publish it as priced, you can later change it to free. Priced apps can be
+purchased and downloaded only by users who have registered a form of payment
 in Google Play.</li>
 </ul>
 
 <p>In addition, you can sell in-app products and subscriptions in your app,
-whether it is free or priced. You can set prices separately for priced apps,
+whether the app is free or priced. You can set prices separately for priced apps,
 in-app products, and subscriptions.</p>
 
 <p>If you are selling a priced app or in-app products or subscriptions, the
 Developer Console lets you set prices in a large number of different currencies.
-When users around the world visit your product details page, they see the price
+When users around the world visit your store listing, they see the price
 of your app in their own currency. For most countries, the price you set is the
 final price charged to users, inclusive of taxes. </p>
 
 <p>To help you manage your prices, the Developer Console provides an autofill
 capability that uses recent exchange rates to populate the prices in all
 supported currencies. You can change prices for apps and in-app products at any
-time, just by saving changes in the Develoer Console.</p>
+time, just by saving changes in the Developer Console.</p>
 
 <h3>In-app Billing</h3>
 
@@ -144,29 +142,30 @@
 <a href="{@docRoot}google/play/billing/index.html">In-app Billing</span></a>
 developer documentation.</p></div></div>
 
-<p><a href="{@docRoot}google/play/billing/index.html">In-app Billing</a> is a Google Play service that lets you monetize your apps in more ways by selling in-app products and subscriptions. In-app products are one-time purchases, while  subscriptions are recurring charges on an monthly or annual basis.</p>
+<p><a href="{@docRoot}google/play/billing/index.html">In-app Billing</a> is
+a Google Play service that lets you monetize your apps in more ways by selling
+in-app products and subscriptions. In-app products are one-time purchases, while
+subscriptions are recurring charges on an monthly or annual basis.</p>
 
 <p>From the Developer Console you can create product lists for in-app
 products and subscriptions, set prices, and publish.</p>
 
-<div style="width:410px;float:right;margin:1.5em;">
-<div style="width:410px;border:1px solid #DDD;">
-<img src="{@docRoot}images/gp-dc-reviews.png" style="width:400px;padding:4px;margin-bottom:0em;">
-</div>
-<p class="image-caption" style="padding:.5em"><span style="font-weight:500;">User
-reviews page</span>: Gives you access to user reviews for a specific app.
+<div class="figure-right" style="width:410px;">
+<img src="{@docRoot}images/gp-dc-reviews.png" class="frame">
+<p class="img-caption"><strong>User
+reviews page</strong>: Gives you access to user reviews for a specific app.
 You can filter  reviews in a number of ways to locate issues more easily
 and support your customers more effectively.</p>
 </div>
 
-<h3>User reviews and error reports</h3>
+<h3>User reviews and crash reports</h3>
 
 <p>Google Play makes it easy for users to submit reviews of your app for the
 benefit of other users. The reviews are also extremely important to you, since
 they give you usability feedback, support requests, and important functionality
 issues direct from your customers. </p>
 
-<p>The Developer console also lets you see error reports, with stack trace and
+<p>The Developer Console also lets you see crash reports, with stack trace and
 other data, submitted automatically from Android devices, for debugging and
 improving your app.</p>
 
@@ -177,7 +176,8 @@
 
 <p>You can view installations of your app measured by unique users, as well as
 by unique devices. For user installations, you can view active installs, total
-installs, and daily installs and uninstalls. For devices, you can see active
+installs, daily installs and uninstalls, and metrics about user ratings.
+For devices, you can see active
 installs as well as daily installs, uninstalls, and upgrades.</p>
 
 <p>You can zoom into the installation numbers along several dimensions,
@@ -191,12 +191,8 @@
 specific points (such as individual platform versions or languages) to the
 timeline.</p>
 
-<div style="width:610px;margin:1.5em;margin-left:0">
-<div style="width:610px;border:1px solid #DDD;">
-<img src="{@docRoot}images/gp-dc-stats.png" 
-style="width:600px;padding:4px;margin-bottom:0em;">
-</div>
-<p class="image-caption" style="padding:.5em"><span style="font-weight:500;">App
-installation statistics page</span>: Shows you a variety of statistics about a
-specific app's installation performance over time.</p>
+<div style="width:530px;">
+<img src="{@docRoot}images/gp-dc-stats.png" class="frame">
+<p class="img-caption"><strong>App statistics page</strong>: Shows you a variety
+of statistics about a specific app's installation performance over time.</p>
 </div>
diff --git a/docs/html/distribute/googleplay/publish/preparing.jd b/docs/html/distribute/googleplay/publish/preparing.jd
index e93b211..0925f3c 100644
--- a/docs/html/distribute/googleplay/publish/preparing.jd
+++ b/docs/html/distribute/googleplay/publish/preparing.jd
@@ -2,7 +2,7 @@
 @jd:body
 
 <div id="qv-wrapper"><div id="qv">
-<h2>Checklist:</h2>
+<h2>Checklist</h2>
 <ol>
 <li><a href="#process">1. Understand the publishing process</a></li>
 <li><a href="#policies">2. Understand Google Play policies</a></li>
@@ -399,14 +399,6 @@
 them for you. Screen shots and videos are also very important, because they show
 what your app looks like, how it's used or played, and what makes it different.</p>
 
-<div class="sidebox-wrapper">
-<div class="sidebox">
-<h3>Localize your promotional graphics and videos<span class="new"> new!</span></h3>
-<p>Google Play now lets you provide different promotional graphics for each
-language you support. Localizing your graphics helps you reach your global
-user base more effectively and is highly recommended.</p>
-</div>
-</div>
 <p>All of your graphic assets should be designed so that they are easy to see
 and highlight your app or brand in a colorful, interesting way. The assets
 should reference the same logo and icon as users will actually find in the All
@@ -417,8 +409,8 @@
 <p>To help you market your app more effectively to a global audience, Google
 Play lets you create localized versions of your promotional graphics,
 screenshots, and videos and upload them to the Developer Console. When a user
-visits your app's store listing, Google Play displays the promotional graphic
-and video that you've provided for the user's language.</p>
+visits your app's store listing, Google Play displays the promotional graphic,
+screenshots and video that you've provided for the user's language.</p>
 
 <p>To localize your promotional graphics, you can translate any embedded text, use
 different imagery or presentation, or change your marketing approach to best address the needs
@@ -427,11 +419,11 @@
 and add it to a localized version of the promotional graphic.</p>
 
 <p>Because your localized graphic assets and videos are so important, you should get
-started on creating them and localizing them as needed, well in advance of your target
+started on creating them and localizing them well in advance of your target
 publishing date. </p>
 
-<p class="note"><strong>Note:</strong> Localized promotional graphics and videos are supported
-in the Developer Console Preview only.</p>
+<p class="note"><strong>Note:</strong> Localized promotional graphics and videos
+are supported only in the new Developer Console design.</p>
 
 <table>
 <tr>
@@ -555,7 +547,7 @@
 <h2 id="final-checks">16. Final checks and publishing</h2> 
 
 <p>When you think you are ready to publish, sign in to the Developer Console and take a few moments for a few
-final checks:</p>
+final checks.</p>
 
 <p>Make sure that: </p>
 
diff --git a/docs/html/distribute/googleplay/publish/register.jd b/docs/html/distribute/googleplay/publish/register.jd
index 335d335..dd73898 100644
--- a/docs/html/distribute/googleplay/publish/register.jd
+++ b/docs/html/distribute/googleplay/publish/register.jd
@@ -46,10 +46,15 @@
 
 
 <ol>
-<li>Visit the Google Play Developer Console at <a href="https://play.google.com/apps/publish/">https://play.google.com/apps/publish/</a>.
-<li>Enter basic information about your <strong>developer identity</strong> &mdash; developer name, email address, and so on. You can modify this information later.</li>
-<li>Read and accept the <strong>Developer Distribution Agreement</strong> that applies to your country or region. Note that apps and store listings that you publish on Google Play must comply with the Developer Program Policies and US export law,</li>
-<li>Pay a <strong>$25 USD registration fee</strong> using Google Checkout. If you don't have a Google Checkout account, you can quickly set one up during the process.</li>
+<li>Visit the Google Play Developer Console at <a
+href="https://play.google.com/apps/publish/">https://play.google.com/apps/publish/</a>.
+<li>Enter basic information about your <strong>developer identity</strong> &mdash; developer
+name, email address, and so on. You can modify this information later.</li>
+<li>Read and accept the <strong>Developer Distribution Agreement</strong> that applies to your
+country or region. Note that apps and store listings that you publish on Google Play must comply
+with the Developer Program Policies and US export law,</li>
+<li>Pay a <strong>$25 USD registration fee</strong> using Google Checkout. If you don't have
+a Google Checkout account, you can quickly set one up during the process.</li>
 </ol>
 
 <p>When your registration is verified, you’ll be notified at the email address you specified during registration. </p>
@@ -61,12 +66,15 @@
 <p>To set up a Merchant account from the Developer Console:</p>
 
 <ol>
-<li><strong>Sign in</strong> to your Google Play Developer Console at <a href="https://play.google.com/apps/publish/">https://play.google.com/apps/publish/</a>
-<li>Click on the "<strong>Edit profile</strong>" link.
-<li>Select "<strong>Setup a Merchant Account at Google Checkout</strong>".</li>
+<li><strong>Sign in</strong> to your Google Play Developer Console at
+<a href="https://play.google.com/apps/publish/">https://play.google.com/apps/publish/</a>
+<li>Open <strong>Financial reports</strong> <img src="{@docRoot}images/distribute/console-reports.png"
+  style="vertical-align:baseline;margin:0"> on the side navigation.
+<li>Click <strong>Setup a Merchant Account now</strong>.</li>
 </ol>
 
-<p>This will take you to the Google Checkout site to sign up as a Merchant; you'll need to have information about your business handy to complete this step.</p>
+<p>This takes you to the Google Wallet site to sign up as a Merchant;
+you'll need information about your business available to complete this step.</p>
 
 <h3>Explore the Developer Console</h3>
 <p>When your registration is verified, you can sign in to your Developer Console, which will be the home for your app publishing operations and tools on Google Play. </p>
diff --git a/docs/html/google/play/billing/billing_admin.jd b/docs/html/google/play/billing/billing_admin.jd
index cfa7a30..91883da1 100644
--- a/docs/html/google/play/billing/billing_admin.jd
+++ b/docs/html/google/play/billing/billing_admin.jd
@@ -492,7 +492,7 @@
 <div style="margin:1em;">
 <img style="border:1px solid #ddd;padding-bottom:.5em" src="{@docRoot}images/in-app-billing/billing_app_key.png" xheight="510" id="figure4" />
 <p class="img-caption" style="padding-left:.5em;">
-  <strong>Figure 4.</strong> You can find the license key for each app in the <strong>Services & APIs</strong> panel.
+  <strong>Figure 4.</strong> You can find the license key for each app in the <strong>Services &amp; APIs</strong> panel.
 </p>
 </div>
 
diff --git a/docs/html/google/play/billing/v2/billing_integrate.jd b/docs/html/google/play/billing/v2/billing_integrate.jd
index 7b748a3..f2846f0 100755
--- a/docs/html/google/play/billing/v2/billing_integrate.jd
+++ b/docs/html/google/play/billing/v2/billing_integrate.jd
@@ -170,15 +170,14 @@
 following:</p>
 
 <ol>
-  <li><strong>Add your Google Play public key to the sample application code.</strong>
+  <li><strong>Add your app's public key to the sample application code.</strong>
     <p>This enables the application to verify the signature of the transaction information that is
     returned from Google Play. To add your public key to the sample application code, do the
     following:</p>
     <ol>
-      <li>Log in to your Google Play <a href="http://play.google.com/apps/publish">publisher
-      account</a>.</li>
-      <li>On the upper left part of the page, under your name, click <strong>Edit
-      Profile</strong>.</li>
+      <li>Log in to your Google Play <a href="http://play.google.com/apps/publish">Developer
+      console</a>.</li>
+      <li>On the upper left part of the page, All Applications, click the application name.</strong>.</li>
       <li>On the Edit Profile page, scroll down to the <strong>Licensing &amp; In-app
       Billing</strong> panel.</li>
       <li>Copy your public key.</li>
@@ -1044,14 +1043,14 @@
 
 <p>You will need to use your Google Play public key to perform the signature verification. The
 following procedure shows you how to retrieve Base64-encoded public key from the Google Play
-publisher site.</p>
+Developer Console.</p>
 
 <ol>
   <li>Log in to your <a href="http://play.google.com/apps/publish">publisher account</a>.</li>
-  <li>On the upper left part of the page, under your name, click <strong>Edit profile</strong>.</li>
-  <li>On the Edit Profile page, scroll down to the Licensing &amp; In-app Billing panel (see figure
-  2).</li>
-  <li>Copy your public key.</li>
+  <li>On the upper left part of the page, click <strong>All applications</strong> and then click
+  the app name in the listing.</li>
+  <li>Click <em>Services &amp; APIs</em> and find "Your License Key for this Application" on the page. </li>
+  <li>Copy the app's public key.</li>
 </ol>
 
 <p class="caution"><strong>Important</strong>: To keep your public key safe from malicious users and
@@ -1060,11 +1059,13 @@
 actual key. The key itself is not secret information, but you do not want to make it easy for a
 hacker or malicious user to replace the public key with another key.</p>
 
-<img src="{@docRoot}images/billing_public_key.png" height="510" id="figure2" />
-<p class="img-caption">
-  <strong>Figure 2.</strong> The Licensing and In-app Billing panel of your account's Edit Profile
-  page lets you see your public key.
-</p>
+<div style="width:640px;">
+<img src="{@docRoot}images/licensing_public_key.png" class="frame">
+<p class="img-caption"><strong>Figure
+2.</strong> An app's license key is available from the Services &amp; APIs page in
+the Developer Console.</p>
+</div>
+
 
 <h2 id="billing-implement">Modifying Your Application Code</h2>
 
diff --git a/docs/html/google/play/licensing/setting-up.jd b/docs/html/google/play/licensing/setting-up.jd
index 1d4e775..d83f91b 100644
--- a/docs/html/google/play/licensing/setting-up.jd
+++ b/docs/html/google/play/licensing/setting-up.jd
@@ -32,29 +32,25 @@
 </div>
 
 <p>Before you start adding license verification to your application, you need to set up your Google
-Play publishing account, your development environment, and test accounts required to verify
+Play publishing account, your development environment, and any test accounts required to verify
 your implementation.</p>
 
 
 <h2 id="account">Setting Up a Publisher Account</h2>
 
 <p>If you don't already have a publisher account for Google Play, you need to register for one
-using your Google account and agree to the terms of service on the Google Play publisher site:</p>
-
-<p style="margin-left:2em;"><a
-href="http://play.google.com/apps/publish">http://play.google.com/apps/publish</a>
-</p>
+using your Google account and agree to the Google Play terms of service.</p>
 
 <p>For more information, see <a
 href="{@docRoot}distribute/googleplay/publish/register.html">Get Started with Publishing</a>.</p>
 
-<p>If you already have a publisher account on Google Play, use your existing
-account to set up licensing.</p>
+<p>If you already have a publisher account on Google Play, use your
+Developer Console to set up licensing.</p>
 
-<p>Using your publisher account on Google Play, you can:</p>
+<p>Using the Google Play Developer Console, you can:</p>
 
 <ul>
-<li>Obtain a public key for licensing</li>
+<li>Obtain an app-specific public key for licensing</li>
 <li>Debug and test an application's licensing implementation, prior to
 publishing the application</li>
 <li>Publish the applications to which you have added licensing support</li>
@@ -63,33 +59,35 @@
 <h4>Administrative settings for licensing</h4>
 
 <p>You can manage several
-administrative controls for Google Play licensing on the publisher site. The controls are available
-in the Edit Profile page, in the "Licensing" panel, shown in figure 1. The controls
+administrative controls for Google Play licensing in the Developer Console. The controls
 let you: </p>
 
 <ul>
 <li>Set up multiple "test accounts," identified by email address. The licensing
 server allows users signed in to test accounts on a device or emulator to send
-license checks and receive static test responses.</li>
-<li>Obtain the account's public key for licensing. When you are implementing
-licensing in an application, you must copy the public key string into the
-application.</li>
+license checks and receive static test responses. You can set up accounts in the
+Account Details page of the Developer Console.</li>
 <li>Configure static test responses that the server sends, when it receives a
 license check for an application uploaded to the publisher account, from a user
-signed in to the publisher account or a test account.</li>
+signed in to the publisher account or a test account. You can set test responses
+in the Account Details page of the Developer Console.</li>
+<li>Obtain the app's public key for licensing. When you are implementing
+licensing in an application, you must copy the public key string into the
+application. You can obtain the app's public key for licensing in the Services
+& APIs page (under All Applications).</li>
 </ul>
 
-
-<img src="{@docRoot}images/licensing_public_key.png" alt=""/>
-<p class="img-caption"><strong>Figure 1.</strong> The Licensing
-panel of your account's Edit Profile page lets you manage administrative
-settings for licensing.</p>
+<div style="width:640px;">
+<img src="{@docRoot}images/licensing_public_key.png" class="frame">
+<p class="img-caption"><strong>Figure
+2.</strong> An app's license key is available from the Services &amp; APIs page in
+the Developer Console.</p>
+</div>
 
 <p>For more information about how to work with test accounts and static test
 responses, see <a href="#test-env">Setting Up a Testing Environment</a>, below.
 
 
-
 <h2 id="dev-setup">Setting Up the Development Environment</h2>
 
 <p>Setting up your environment for licensing involves these tasks:</p>
@@ -432,9 +430,9 @@
 
 <h2 id="test-env">Setting Up the Testing Environment</h2>
 
-<p>The Google Play publisher site provides configuration tools that let you
+<p>The Google Play Developer Console provides configuration tools that let you
 and others test licensing on your application before it is published. As you are
-implementing licensing, you can make use of the publisher site tools to test
+implementing licensing, you can make use of the Developer Console tools to test
 your application's Policy and handling of different licensing responses and
 error conditions.</p>
 
@@ -487,10 +485,12 @@
 Response Codes</a> in the <a
 href="{@docRoot}google/play/licensing/licensing-reference.html">Licensing Reference</a>.</p>
 
-<img src="{@docRoot}images/licensing_test_response.png" alt=""/>
-<p class="img-caption"><strong>Figure 4.</strong> The Licensing
-panel of your account's Edit Profile page, showing the Test Accounts field and the
-Test Response menu.</p>
+<div style="width:640px;">
+<img src="{@docRoot}images/licensing_test_response.png" class="frame">
+<p class="img-caption"><strong>Figure 4.</strong> The License Testing
+panel of your Account details page lets you set up test accounts and
+manage test responses.</p>
+</div>
 
 <p>Note that the test response that you configure applies account-wide &mdash;
 that is, it applies not to a single application, but to <em>all</em>
@@ -516,7 +516,7 @@
 <p>In some cases, you might want to let multiple teams of developers test
 licensing on applications that will ultimately be published through your
 publisher account, but without giving them access to your publisher account's
-sign-in credentials. To meet that need, the Google Play publisher site lets
+sign-in credentials. To meet that need, the Google Play Developer Console lets
 you set up one or more optional <em>test accounts</em> &mdash; accounts that are
 authorized to query the licensing server and receive static test responses from
 your publisher account.</p>
@@ -609,13 +609,13 @@
 
 <p>The licensing server handles static test responses in the normal way,
 including signing the license response data, adding extras parameters, and so
-on. To support developers who are implementing licensing using test accounts,
+on. To support developers who are implementing licensing using test accounts
 rather than the publisher account, you will need to distribute
-your public key to them. Developers without access to the publisher site do not
-have access to your public key, and without the key they won't be able to
-verify license responses. </p>
+the app's public key for licensing to them. Developers without access to the
+Developer Console do not have access to the app's public key, and without
+the key they won't be able to verify license responses. </p>
 
-<p>Note that if you decide to generate a new licensing key pair for your account
+<p>Note that if you decide to generate a new licensing key pair for the app
 for some reason, you need to notify all users of test accounts. For
 testers, you can embed the new key in the application package and distribute it
 to users. For developers, you will need to distribute the new key to them
@@ -661,7 +661,7 @@
 
 <p>Signing in using a publisher account offers the advantage of letting your
 applications receive static test responses even before the applications are
-uploaded to the publisher site.</p>
+uploaded to the Developer Console.</p>
 
 <p>If you are part of a larger organization or are working with external groups
 on applications that will be published through your site, you will more likely
diff --git a/docs/html/guide/webapps/webview.jd b/docs/html/guide/webapps/webview.jd
index f8b2a1d..d2b2532 100644
--- a/docs/html/guide/webapps/webview.jd
+++ b/docs/html/guide/webapps/webview.jd
@@ -178,8 +178,8 @@
 href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#target">{@code targetSdkVersion}</a>
 to 17 or higher, <strong>you
 must add the {@code &#64;JavascriptInterface} annotation</strong> to any method that you want
-available your web page code (the method must also be public). If you do not provide the
-annotation, then the method will not accessible by your web page when running on Android 4.2 or
+available to your JavaScript (the method must also be public). If you do not provide the
+annotation, the method is not accessible by your web page when running on Android 4.2 or
 higher.</p>
 
 <p>In this example, the {@code WebAppInterface} class allows the web page to create a {@link
diff --git a/docs/html/images/billing_public_key.png b/docs/html/images/billing_public_key.png
deleted file mode 100644
index a0620f8..0000000
--- a/docs/html/images/billing_public_key.png
+++ /dev/null
Binary files differ
diff --git a/docs/html/images/distribute/console-apps.png b/docs/html/images/distribute/console-apps.png
new file mode 100644
index 0000000..129de67
--- /dev/null
+++ b/docs/html/images/distribute/console-apps.png
Binary files differ
diff --git a/docs/html/images/distribute/console-reports.png b/docs/html/images/distribute/console-reports.png
new file mode 100644
index 0000000..d7fcb99
--- /dev/null
+++ b/docs/html/images/distribute/console-reports.png
Binary files differ
diff --git a/docs/html/images/distribute/console-settings.png b/docs/html/images/distribute/console-settings.png
new file mode 100644
index 0000000..4b5d4a6
--- /dev/null
+++ b/docs/html/images/distribute/console-settings.png
Binary files differ
diff --git a/docs/html/images/gp-buyer-currency.png b/docs/html/images/gp-buyer-currency.png
index 51b8108..96d7e65 100644
--- a/docs/html/images/gp-buyer-currency.png
+++ b/docs/html/images/gp-buyer-currency.png
Binary files differ
diff --git a/docs/html/images/gp-dc-countries.png b/docs/html/images/gp-dc-countries.png
index 00d0d5e..72ce796 100644
--- a/docs/html/images/gp-dc-countries.png
+++ b/docs/html/images/gp-dc-countries.png
Binary files differ
diff --git a/docs/html/images/gp-dc-details.png b/docs/html/images/gp-dc-details.png
index 567567e..5b7eba4 100644
--- a/docs/html/images/gp-dc-details.png
+++ b/docs/html/images/gp-dc-details.png
Binary files differ
diff --git a/docs/html/images/gp-dc-home.png b/docs/html/images/gp-dc-home.png
index 381d0db..5ed46c9 100644
--- a/docs/html/images/gp-dc-home.png
+++ b/docs/html/images/gp-dc-home.png
Binary files differ
diff --git a/docs/html/images/gp-dc-profile.png b/docs/html/images/gp-dc-profile.png
index e526369..e254e5d 100644
--- a/docs/html/images/gp-dc-profile.png
+++ b/docs/html/images/gp-dc-profile.png
Binary files differ
diff --git a/docs/html/images/gp-dc-reviews.png b/docs/html/images/gp-dc-reviews.png
index cab175a..4290136 100644
--- a/docs/html/images/gp-dc-reviews.png
+++ b/docs/html/images/gp-dc-reviews.png
Binary files differ
diff --git a/docs/html/images/gp-dc-stats-mini.png b/docs/html/images/gp-dc-stats-mini.png
index d29a270..211b5ea 100644
--- a/docs/html/images/gp-dc-stats-mini.png
+++ b/docs/html/images/gp-dc-stats-mini.png
Binary files differ
diff --git a/docs/html/images/gp-dc-stats.png b/docs/html/images/gp-dc-stats.png
index 06f88e5..7df6266 100644
--- a/docs/html/images/gp-dc-stats.png
+++ b/docs/html/images/gp-dc-stats.png
Binary files differ
diff --git a/docs/html/images/gp-devconsole-home.png b/docs/html/images/gp-devconsole-home.png
index 1d758fd..a86f591 100644
--- a/docs/html/images/gp-devconsole-home.png
+++ b/docs/html/images/gp-devconsole-home.png
Binary files differ
diff --git a/docs/html/images/gp-supported-dev-requirements.png b/docs/html/images/gp-supported-dev-requirements.png
index d84f34e..c38b8aa 100644
--- a/docs/html/images/gp-supported-dev-requirements.png
+++ b/docs/html/images/gp-supported-dev-requirements.png
Binary files differ
diff --git a/docs/html/images/licensing_public_key.png b/docs/html/images/licensing_public_key.png
index 1630209..a3cd785 100644
--- a/docs/html/images/licensing_public_key.png
+++ b/docs/html/images/licensing_public_key.png
Binary files differ
diff --git a/docs/html/images/licensing_test_response.png b/docs/html/images/licensing_test_response.png
index ead2152..219ae24 100644
--- a/docs/html/images/licensing_test_response.png
+++ b/docs/html/images/licensing_test_response.png
Binary files differ
diff --git a/libs/hwui/FontRenderer.cpp b/libs/hwui/FontRenderer.cpp
index 5d5e6a5..1d9eb0e 100644
--- a/libs/hwui/FontRenderer.cpp
+++ b/libs/hwui/FontRenderer.cpp
@@ -797,7 +797,7 @@
     mRsScript->blur(ain, aout);
 
     // replace the original image's pointer, avoiding a copy back to the original buffer
-    delete *image;
+    free(*image);
     *image = outImage;
 }
 
diff --git a/libs/hwui/TextDropShadowCache.cpp b/libs/hwui/TextDropShadowCache.cpp
index db7bd48..f1f35bd 100644
--- a/libs/hwui/TextDropShadowCache.cpp
+++ b/libs/hwui/TextDropShadowCache.cpp
@@ -222,7 +222,7 @@
         }
 
         // Cleanup shadow
-        delete shadow.image;
+        free(shadow.image);
     }
 
     return texture;
diff --git a/packages/SystemUI/res/layout/quick_settings_brightness_dialog.xml b/packages/SystemUI/res/layout/quick_settings_brightness_dialog.xml
index f7b1d78..b547d99 100644
--- a/packages/SystemUI/res/layout/quick_settings_brightness_dialog.xml
+++ b/packages/SystemUI/res/layout/quick_settings_brightness_dialog.xml
@@ -27,7 +27,7 @@
         android:paddingRight="10dp"
         android:src="@drawable/ic_qs_brightness_auto_off"
         />
-    <com.android.systemui.statusbar.policy.ToggleSlider
+    <com.android.systemui.settings.ToggleSlider
         android:id="@+id/brightness_slider"
         android:layout_width="0dp"
         android:layout_height="40dp"
diff --git a/packages/SystemUI/res/layout/system_bar_settings_view.xml b/packages/SystemUI/res/layout/system_bar_settings_view.xml
index 3e959d5..4987dd9 100644
--- a/packages/SystemUI/res/layout/system_bar_settings_view.xml
+++ b/packages/SystemUI/res/layout/system_bar_settings_view.xml
@@ -100,7 +100,7 @@
                 style="@style/SystemBarPanelSettingsIcon"
                 android:src="@drawable/ic_sysbar_brightness"
                 />
-        <com.android.systemui.statusbar.policy.ToggleSlider
+        <com.android.systemui.settings.ToggleSlider
                 android:id="@+id/brightness"
                 android:layout_width="0dp"
                 android:layout_height="fill_parent"
diff --git a/packages/SystemUI/src/com/android/systemui/SystemUIService.java b/packages/SystemUI/src/com/android/systemui/SystemUIService.java
index 427fe91..1f3e942 100644
--- a/packages/SystemUI/src/com/android/systemui/SystemUIService.java
+++ b/packages/SystemUI/src/com/android/systemui/SystemUIService.java
@@ -44,6 +44,7 @@
             0, // system bar or status bar, filled in below.
             com.android.systemui.power.PowerUI.class,
             com.android.systemui.media.RingtonePlayer.class,
+            com.android.systemui.settings.SettingsUI.class,
         };
 
     /**
diff --git a/packages/SystemUI/src/com/android/systemui/settings/BrightnessController.java b/packages/SystemUI/src/com/android/systemui/settings/BrightnessController.java
new file mode 100644
index 0000000..fdeead1
--- /dev/null
+++ b/packages/SystemUI/src/com/android/systemui/settings/BrightnessController.java
@@ -0,0 +1,241 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.systemui.settings;
+
+import android.content.ContentResolver;
+import android.content.Context;
+import android.content.Intent;
+import android.database.ContentObserver;
+import android.net.Uri;
+import android.os.AsyncTask;
+import android.os.Handler;
+import android.os.IPowerManager;
+import android.os.PowerManager;
+import android.os.RemoteException;
+import android.os.ServiceManager;
+import android.os.UserHandle;
+import android.provider.Settings;
+import android.provider.Settings.SettingNotFoundException;
+import android.util.Slog;
+import android.view.IWindowManager;
+import android.widget.CompoundButton;
+import android.widget.ImageView;
+
+import java.util.ArrayList;
+
+public class BrightnessController implements ToggleSlider.Listener {
+    private static final String TAG = "StatusBar.BrightnessController";
+
+    private final int mMinimumBacklight;
+    private final int mMaximumBacklight;
+
+    private final Context mContext;
+    private final ImageView mIcon;
+    private final ToggleSlider mControl;
+    private final boolean mAutomaticAvailable;
+    private final IPowerManager mPower;
+    private final CurrentUserTracker mUserTracker;
+    private final Handler mHandler;
+    private final BrightnessObserver mBrightnessObserver;
+
+    private ArrayList<BrightnessStateChangeCallback> mChangeCallbacks =
+            new ArrayList<BrightnessStateChangeCallback>();
+
+    public interface BrightnessStateChangeCallback {
+        public void onBrightnessLevelChanged();
+    }
+
+    /** ContentObserver to watch brightness **/
+    private class BrightnessObserver extends ContentObserver {
+
+        private final Uri BRIGHTNESS_MODE_URI =
+                Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS_MODE);
+        private final Uri BRIGHTNESS_URI =
+                Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS);
+
+        public BrightnessObserver(Handler handler) {
+            super(handler);
+        }
+
+        @Override
+        public void onChange(boolean selfChange) {
+            onChange(selfChange, null);
+        }
+
+        @Override
+        public void onChange(boolean selfChange, Uri uri) {
+            if (selfChange) return;
+            if (BRIGHTNESS_MODE_URI.equals(uri)) {
+                updateMode();
+            } else if (BRIGHTNESS_URI.equals(uri)) {
+                updateSlider();
+            } else {
+                updateMode();
+                updateSlider();
+            }
+            for (BrightnessStateChangeCallback cb : mChangeCallbacks) {
+                cb.onBrightnessLevelChanged();
+            }
+        }
+
+        public void startObserving() {
+            final ContentResolver cr = mContext.getContentResolver();
+            cr.unregisterContentObserver(this);
+            cr.registerContentObserver(
+                    BRIGHTNESS_MODE_URI,
+                    false, this, UserHandle.USER_ALL);
+            cr.registerContentObserver(
+                    BRIGHTNESS_URI,
+                    false, this, UserHandle.USER_ALL);
+        }
+
+        public void stopObserving() {
+            final ContentResolver cr = mContext.getContentResolver();
+            cr.unregisterContentObserver(this);
+        }
+
+    }
+
+    public BrightnessController(Context context, ImageView icon, ToggleSlider control) {
+        mContext = context;
+        mIcon = icon;
+        mControl = control;
+        mHandler = new Handler();
+        mUserTracker = new CurrentUserTracker(mContext) {
+            @Override
+            public void onUserSwitched(int newUserId) {
+                updateMode();
+                updateSlider();
+            }
+        };
+        mBrightnessObserver = new BrightnessObserver(mHandler);
+        mBrightnessObserver.startObserving();
+
+        PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
+        mMinimumBacklight = pm.getMinimumScreenBrightnessSetting();
+        mMaximumBacklight = pm.getMaximumScreenBrightnessSetting();
+
+        mAutomaticAvailable = context.getResources().getBoolean(
+                com.android.internal.R.bool.config_automatic_brightness_available);
+        mPower = IPowerManager.Stub.asInterface(ServiceManager.getService("power"));
+
+        // Update the slider and mode before attaching the listener so we don't receive the
+        // onChanged notifications for the initial values.
+        updateMode();
+        updateSlider();
+
+        control.setOnChangedListener(this);
+    }
+
+    public void addStateChangedCallback(BrightnessStateChangeCallback cb) {
+        mChangeCallbacks.add(cb);
+    }
+
+    public boolean removeStateChangedCallback(BrightnessStateChangeCallback cb) {
+        return mChangeCallbacks.remove(cb);
+    }
+
+    @Override
+    public void onInit(ToggleSlider control) {
+        // Do nothing
+    }
+
+    /** Unregister all call backs, both to and from the controller */
+    public void unregisterCallbacks() {
+        mBrightnessObserver.stopObserving();
+        mChangeCallbacks.clear();
+        mUserTracker.stopTracking();
+    }
+
+    public void onChanged(ToggleSlider view, boolean tracking, boolean automatic, int value) {
+        setMode(automatic ? Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC
+                : Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
+        updateIcon(automatic);
+        if (!automatic) {
+            final int val = value + mMinimumBacklight;
+            setBrightness(val);
+            if (!tracking) {
+                AsyncTask.execute(new Runnable() {
+                        public void run() {
+                            Settings.System.putIntForUser(mContext.getContentResolver(),
+                                    Settings.System.SCREEN_BRIGHTNESS, val,
+                                    UserHandle.USER_CURRENT);
+                        }
+                    });
+            }
+        }
+
+        for (BrightnessStateChangeCallback cb : mChangeCallbacks) {
+            cb.onBrightnessLevelChanged();
+        }
+    }
+
+    private void setMode(int mode) {
+        Settings.System.putIntForUser(mContext.getContentResolver(),
+                Settings.System.SCREEN_BRIGHTNESS_MODE, mode,
+                mUserTracker.getCurrentUserId());
+    }
+
+    private void setBrightness(int brightness) {
+        try {
+            mPower.setTemporaryScreenBrightnessSettingOverride(brightness);
+        } catch (RemoteException ex) {
+        }
+    }
+
+    private void updateIcon(boolean automatic) {
+        if (mIcon != null) {
+            mIcon.setImageResource(automatic ?
+                    com.android.systemui.R.drawable.ic_qs_brightness_auto_on :
+                    com.android.systemui.R.drawable.ic_qs_brightness_auto_off);
+        }
+    }
+
+    /** Fetch the brightness mode from the system settings and update the icon */
+    private void updateMode() {
+        if (mAutomaticAvailable) {
+            int automatic;
+            try {
+                automatic = Settings.System.getIntForUser(mContext.getContentResolver(),
+                        Settings.System.SCREEN_BRIGHTNESS_MODE,
+                        UserHandle.USER_CURRENT);
+            } catch (SettingNotFoundException snfe) {
+                automatic = 0;
+            }
+            mControl.setChecked(automatic != 0);
+            updateIcon(automatic != 0);
+        } else {
+            mControl.setChecked(false);
+            updateIcon(false /*automatic*/);
+        }
+    }
+
+    /** Fetch the brightness from the system settings and update the slider */
+    private void updateSlider() {
+        int value;
+        try {
+            value = Settings.System.getIntForUser(mContext.getContentResolver(),
+                    Settings.System.SCREEN_BRIGHTNESS,
+                    UserHandle.USER_CURRENT);
+        } catch (SettingNotFoundException ex) {
+            value = mMaximumBacklight;
+        }
+        mControl.setMax(mMaximumBacklight - mMinimumBacklight);
+        mControl.setValue(value - mMinimumBacklight);
+    }
+
+}
diff --git a/packages/SystemUI/src/com/android/systemui/settings/BrightnessDialog.java b/packages/SystemUI/src/com/android/systemui/settings/BrightnessDialog.java
new file mode 100644
index 0000000..1b05084
--- /dev/null
+++ b/packages/SystemUI/src/com/android/systemui/settings/BrightnessDialog.java
@@ -0,0 +1,114 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.systemui.settings;
+
+import com.android.systemui.R;
+
+import android.app.Dialog;
+import android.content.Context;
+import android.content.res.Resources;
+import android.os.Bundle;
+import android.os.Handler;
+import android.util.Log;
+import android.view.Window;
+import android.view.WindowManager;
+import android.widget.ImageView;
+
+import java.lang.Runnable;
+
+/** A dialog that provides controls for adjusting the screen brightness. */
+public class BrightnessDialog extends Dialog implements
+        BrightnessController.BrightnessStateChangeCallback {
+
+    private static final String TAG = "BrightnessDialog";
+    private static final boolean DEBUG = false;
+
+    protected Handler mHandler = new Handler();
+
+    private BrightnessController mBrightnessController;
+    private final int mBrightnessDialogLongTimeout;
+    private final int mBrightnessDialogShortTimeout;
+
+    private final Runnable mDismissDialogRunnable = new Runnable() {
+        public void run() {
+            if (BrightnessDialog.this.isShowing()) {
+                BrightnessDialog.this.dismiss();
+            }
+        };
+    };
+
+
+    public BrightnessDialog(Context ctx) {
+        super(ctx);
+        Resources r = ctx.getResources();
+        mBrightnessDialogLongTimeout =
+                r.getInteger(R.integer.quick_settings_brightness_dialog_long_timeout);
+        mBrightnessDialogShortTimeout =
+                r.getInteger(R.integer.quick_settings_brightness_dialog_short_timeout);
+    }
+
+
+    /**
+     * Create the brightness dialog and any resources that are used for the
+     * entire lifetime of the dialog.
+     */
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        Window window = getWindow();
+        window.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY);
+        window.getAttributes().privateFlags |=
+                WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
+        window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
+        window.requestFeature(Window.FEATURE_NO_TITLE);
+
+        setContentView(R.layout.quick_settings_brightness_dialog);
+        setCanceledOnTouchOutside(true);
+    }
+
+
+    @Override
+    protected void onStart() {
+        super.onStart();
+        mBrightnessController = new BrightnessController(getContext(),
+                (ImageView) findViewById(R.id.brightness_icon),
+                (ToggleSlider) findViewById(R.id.brightness_slider));
+        dismissBrightnessDialog(mBrightnessDialogLongTimeout);
+        mBrightnessController.addStateChangedCallback(this);
+    }
+
+    @Override
+    protected void onStop() {
+        super.onStop();
+        mBrightnessController.unregisterCallbacks();
+        removeAllBrightnessDialogCallbacks();
+    }
+
+    public void onBrightnessLevelChanged() {
+        dismissBrightnessDialog(mBrightnessDialogShortTimeout);
+    }
+
+    private void dismissBrightnessDialog(int timeout) {
+        removeAllBrightnessDialogCallbacks();
+        mHandler.postDelayed(mDismissDialogRunnable, timeout);
+    }
+
+    private void removeAllBrightnessDialogCallbacks() {
+        mHandler.removeCallbacks(mDismissDialogRunnable);
+    }
+
+}
diff --git a/packages/SystemUI/src/com/android/systemui/settings/CurrentUserTracker.java b/packages/SystemUI/src/com/android/systemui/settings/CurrentUserTracker.java
new file mode 100644
index 0000000..122f81e
--- /dev/null
+++ b/packages/SystemUI/src/com/android/systemui/settings/CurrentUserTracker.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.systemui.settings;
+
+import android.app.ActivityManager;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+
+public abstract class CurrentUserTracker extends BroadcastReceiver {
+
+    private Context mContext;
+    private int mCurrentUserId;
+
+    public CurrentUserTracker(Context context) {
+        IntentFilter filter = new IntentFilter(Intent.ACTION_USER_SWITCHED);
+        context.registerReceiver(this, filter);
+        mCurrentUserId = ActivityManager.getCurrentUser();
+        mContext = context;
+    }
+
+    public int getCurrentUserId() {
+        return mCurrentUserId;
+    }
+
+    @Override
+    public void onReceive(Context context, Intent intent) {
+        if (Intent.ACTION_USER_SWITCHED.equals(intent.getAction())) {
+            int oldUserId = mCurrentUserId;
+            mCurrentUserId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0);
+            if (oldUserId != mCurrentUserId) {
+                onUserSwitched(mCurrentUserId);
+            }
+        }
+    }
+
+    public void stopTracking() {
+        mContext.unregisterReceiver(this);
+    }
+
+    public abstract void onUserSwitched(int newUserId);
+}
diff --git a/packages/SystemUI/src/com/android/systemui/settings/SettingsUI.java b/packages/SystemUI/src/com/android/systemui/settings/SettingsUI.java
new file mode 100644
index 0000000..f65123a
--- /dev/null
+++ b/packages/SystemUI/src/com/android/systemui/settings/SettingsUI.java
@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.systemui.settings;
+
+import java.io.FileDescriptor;
+import java.io.PrintWriter;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.DialogInterface;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.os.Handler;
+import android.util.Slog;
+
+import com.android.systemui.SystemUI;
+
+public class SettingsUI extends SystemUI {
+    private static final String TAG = "SettingsUI";
+    private static final boolean DEBUG = false;
+
+    private final Handler mHandler = new Handler();
+    private BrightnessDialog mBrightnessDialog;
+
+    private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            String action = intent.getAction();
+            if (action.equals(Intent.ACTION_SHOW_BRIGHTNESS_DIALOG)) {
+                if (DEBUG) Slog.d(TAG, "showing brightness dialog");
+
+                if (mBrightnessDialog == null) {
+                    mBrightnessDialog = new BrightnessDialog(mContext);
+                    mBrightnessDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
+                        @Override
+                        public void onDismiss(DialogInterface dialog) {
+                            mBrightnessDialog = null;
+                        }
+                    });
+                }
+
+                if (!mBrightnessDialog.isShowing()) {
+                    mBrightnessDialog.show();
+                }
+
+            } else {
+                Slog.w(TAG, "unknown intent: " + intent);
+            }
+        }
+    };
+
+    public void start() {
+        IntentFilter filter = new IntentFilter();
+        filter.addAction(Intent.ACTION_SHOW_BRIGHTNESS_DIALOG);
+        mContext.registerReceiver(mIntentReceiver, filter, null, mHandler);
+    }
+
+    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
+        pw.print("mBrightnessDialog=");
+        pw.println(mBrightnessDialog == null ? "null" : mBrightnessDialog.toString());
+    }
+}
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/ToggleSlider.java b/packages/SystemUI/src/com/android/systemui/settings/ToggleSlider.java
similarity index 96%
rename from packages/SystemUI/src/com/android/systemui/statusbar/policy/ToggleSlider.java
rename to packages/SystemUI/src/com/android/systemui/settings/ToggleSlider.java
index 39f8fcc..c7c361c 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/ToggleSlider.java
+++ b/packages/SystemUI/src/com/android/systemui/settings/ToggleSlider.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010 The Android Open Source Project
+ * Copyright (C) 2013 The Android Open Source Project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-package com.android.systemui.statusbar.policy;
+package com.android.systemui.settings;
 
 import android.content.Context;
 import android.content.res.Resources;
@@ -31,7 +31,7 @@
 
 import com.android.systemui.R;
 
-public class ToggleSlider extends RelativeLayout 
+public class ToggleSlider extends RelativeLayout
         implements CompoundButton.OnCheckedChangeListener, SeekBar.OnSeekBarChangeListener {
     private static final String TAG = "StatusBar.ToggleSlider";
 
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/BaseStatusBar.java b/packages/SystemUI/src/com/android/systemui/statusbar/BaseStatusBar.java
index f941f89..4599dd4 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/BaseStatusBar.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/BaseStatusBar.java
@@ -462,7 +462,6 @@
         mSearchPanelView.setOnTouchListener(
                  new TouchOutsideListener(MSG_CLOSE_SEARCH_PANEL, mSearchPanelView));
         mSearchPanelView.setVisibility(View.GONE);
-        mSearchPanelView.setLayoutDirection(mLayoutDirection);
 
         WindowManager.LayoutParams lp = getSearchLayoutParams(mSearchPanelView.getLayoutParams());
 
@@ -739,7 +738,6 @@
         LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(
                 Context.LAYOUT_INFLATER_SERVICE);
         View row = inflater.inflate(R.layout.status_bar_notification_row, parent, false);
-        row.setLayoutDirection(mLayoutDirection);
 
         // for blaming (see SwipeHelper.setLongPressListener)
         row.setTag(sbn.pkg);
@@ -787,7 +785,6 @@
             params.minHeight = minHeight;
             params.maxHeight = minHeight;
             adaptive.addView(expandedOneU, params);
-            expandedOneU.setLayoutDirection(mLayoutDirection);
         }
         if (expandedLarge != null) {
             SizeAdaptiveLayout.LayoutParams params =
@@ -795,7 +792,6 @@
             params.minHeight = minHeight+1;
             params.maxHeight = maxHeight;
             adaptive.addView(expandedLarge, params);
-            expandedLarge.setLayoutDirection(mLayoutDirection);
         }
         row.setDrawingCacheEnabled(true);
 
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
index 9b1c1db..024faf7 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
@@ -348,7 +348,6 @@
                 }
                 return mStatusBarWindow.onTouchEvent(event);
             }});
-        mStatusBarWindow.setLayoutDirection(mLayoutDirection);
 
         mStatusBarView = (PhoneStatusBarView) mStatusBarWindow.findViewById(R.id.status_bar);
         mStatusBarView.setBar(this);
@@ -380,7 +379,6 @@
             mIntruderAlertView = (IntruderAlertView) View.inflate(context, R.layout.intruder_alert, null);
             mIntruderAlertView.setVisibility(View.GONE);
             mIntruderAlertView.setBar(this);
-            mIntruderAlertView.setLayoutDirection(mLayoutDirection);
         }
         if (MULTIUSER_DEBUG) {
             mNotificationPanelDebugText = (TextView) mNotificationPanel.findViewById(R.id.header_debug_info);
@@ -398,7 +396,6 @@
 
                 mNavigationBarView.setDisabledFlags(mDisabled);
                 mNavigationBarView.setBar(this);
-                mNavigationBarView.setLayoutDirection(mLayoutDirection);
             }
         } catch (RemoteException ex) {
             // no window manager? good luck with that
@@ -963,11 +960,6 @@
 
     @Override
     protected void refreshLayout(int layoutDirection) {
-        mStatusBarWindow.setLayoutDirection(layoutDirection);
-        if (ENABLE_INTRUDERS) {
-            mIntruderAlertView.setLayoutDirection(layoutDirection);
-        }
-
         if (mNavigationBarView != null) {
             mNavigationBarView.setLayoutDirection(layoutDirection);
         }
@@ -1030,7 +1022,6 @@
             View v = toShow.get(i);
             if (v.getParent() == null) {
                 mPile.addView(v, i);
-                v.setLayoutDirection(mLayoutDirection);
             }
         }
 
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/QuickSettings.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/QuickSettings.java
index 4ff3862..60e22c5 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/QuickSettings.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/QuickSettings.java
@@ -27,10 +27,8 @@
 import com.android.systemui.statusbar.phone.QuickSettingsModel.WifiState;
 import com.android.systemui.statusbar.policy.BatteryController;
 import com.android.systemui.statusbar.policy.BluetoothController;
-import com.android.systemui.statusbar.policy.BrightnessController;
 import com.android.systemui.statusbar.policy.LocationController;
 import com.android.systemui.statusbar.policy.NetworkController;
-import com.android.systemui.statusbar.policy.ToggleSlider;
 
 import android.app.ActivityManagerNative;
 import android.app.AlertDialog;
@@ -70,7 +68,6 @@
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
-import android.view.Window;
 import android.view.WindowManager;
 import android.view.WindowManagerGlobal;
 import android.widget.ImageView;
@@ -100,13 +97,8 @@
     private BluetoothAdapter mBluetoothAdapter;
     private WifiManager mWifiManager;
 
-    private BrightnessController mBrightnessController;
     private BluetoothController mBluetoothController;
 
-    private Dialog mBrightnessDialog;
-    private int mBrightnessDialogShortTimeout;
-    private int mBrightnessDialogLongTimeout;
-
     private AsyncTask<Void, Void, Pair<String, Drawable>> mUserInfoTask;
 
     private LevelListDrawable mBatteryLevels;
@@ -146,10 +138,6 @@
         mBatteryLevels = (LevelListDrawable) r.getDrawable(R.drawable.qs_sys_battery);
         mChargingBatteryLevels =
                 (LevelListDrawable) r.getDrawable(R.drawable.qs_sys_battery_charging);
-        mBrightnessDialogLongTimeout =
-                r.getInteger(R.integer.quick_settings_brightness_dialog_long_timeout);
-        mBrightnessDialogShortTimeout =
-                r.getInteger(R.integer.quick_settings_brightness_dialog_short_timeout);
 
         IntentFilter filter = new IntentFilter();
         filter.addAction(DisplayManager.ACTION_WIFI_DISPLAY_STATUS_CHANGED);
@@ -352,7 +340,6 @@
                 TextView tv = (TextView) view.findViewById(R.id.brightness_textview);
                 tv.setCompoundDrawablesWithIntrinsicBounds(0, state.iconId, 0, 0);
                 tv.setText(state.label);
-                dismissBrightnessDialog(mBrightnessDialogShortTimeout);
             }
         });
         parent.addView(brightnessTile);
@@ -776,72 +763,12 @@
         }
         ((QuickSettingsContainerView)mContainerView).updateResources();
         mContainerView.requestLayout();
-
-        // Reset the dialog
-        boolean isBrightnessDialogVisible = false;
-        if (mBrightnessDialog != null) {
-            removeAllBrightnessDialogCallbacks();
-
-            isBrightnessDialogVisible = mBrightnessDialog.isShowing();
-            mBrightnessDialog.dismiss();
-        }
-        mBrightnessDialog = null;
-        if (isBrightnessDialogVisible) {
-            showBrightnessDialog();
-        }
     }
 
-    private void removeAllBrightnessDialogCallbacks() {
-        mHandler.removeCallbacks(mDismissBrightnessDialogRunnable);
-    }
-
-    private Runnable mDismissBrightnessDialogRunnable = new Runnable() {
-        public void run() {
-            if (mBrightnessDialog != null && mBrightnessDialog.isShowing()) {
-                mBrightnessDialog.dismiss();
-            }
-            removeAllBrightnessDialogCallbacks();
-        };
-    };
 
     private void showBrightnessDialog() {
-        if (mBrightnessDialog == null) {
-            mBrightnessDialog = new Dialog(mContext);
-            mBrightnessDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
-            mBrightnessDialog.setContentView(R.layout.quick_settings_brightness_dialog);
-            mBrightnessDialog.setCanceledOnTouchOutside(true);
-
-            mBrightnessController = new BrightnessController(mContext,
-                    (ImageView) mBrightnessDialog.findViewById(R.id.brightness_icon),
-                    (ToggleSlider) mBrightnessDialog.findViewById(R.id.brightness_slider));
-            mBrightnessController.addStateChangedCallback(mModel);
-            mBrightnessDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
-                @Override
-                public void onDismiss(DialogInterface dialog) {
-                    mBrightnessController = null;
-                }
-            });
-
-            mBrightnessDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
-            mBrightnessDialog.getWindow().getAttributes().privateFlags |=
-                    WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
-            mBrightnessDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
-        }
-        if (!mBrightnessDialog.isShowing()) {
-            try {
-                WindowManagerGlobal.getWindowManagerService().dismissKeyguard();
-            } catch (RemoteException e) {
-            }
-            mBrightnessDialog.show();
-            dismissBrightnessDialog(mBrightnessDialogLongTimeout);
-        }
-    }
-
-    private void dismissBrightnessDialog(int timeout) {
-        removeAllBrightnessDialogCallbacks();
-        if (mBrightnessDialog != null) {
-            mHandler.postDelayed(mDismissBrightnessDialogRunnable, timeout);
-        }
+        Intent intent = new Intent(Intent.ACTION_SHOW_BRIGHTNESS_DIALOG);
+        mContext.sendBroadcast(intent);
     }
 
     private void showBugreportDialog() {
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/QuickSettingsModel.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/QuickSettingsModel.java
index 1037137..435ea4c 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/QuickSettingsModel.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/QuickSettingsModel.java
@@ -42,9 +42,9 @@
 
 import com.android.internal.view.RotationPolicy;
 import com.android.systemui.R;
+import com.android.systemui.settings.CurrentUserTracker;
+import com.android.systemui.settings.BrightnessController.BrightnessStateChangeCallback;
 import com.android.systemui.statusbar.policy.BatteryController.BatteryStateChangeCallback;
-import com.android.systemui.statusbar.policy.BrightnessController.BrightnessStateChangeCallback;
-import com.android.systemui.statusbar.policy.CurrentUserTracker;
 import com.android.systemui.statusbar.policy.LocationController.LocationGpsStateChangeCallback;
 import com.android.systemui.statusbar.policy.NetworkController.NetworkSignalChangedCallback;
 
@@ -239,10 +239,12 @@
         mContext = context;
         mHandler = new Handler();
         mUserTracker = new CurrentUserTracker(mContext) {
-            @Override
-            public void onReceive(Context context, Intent intent) {
-                super.onReceive(context, intent);
-                onUserSwitched();
+            public void onUserSwitched(int newUserId) {
+                mBrightnessObserver.startObserving();
+                onRotationLockChanged();
+                onBrightnessLevelChanged();
+                onNextAlarmChanged();
+                onBugreportChanged();
             }
         };
 
@@ -705,13 +707,4 @@
     void refreshBrightnessTile() {
         onBrightnessLevelChanged();
     }
-
-    // User switch: need to update visuals of all tiles known to have per-user state
-    void onUserSwitched() {
-        mBrightnessObserver.startObserving();
-        onRotationLockChanged();
-        onBrightnessLevelChanged();
-        onNextAlarmChanged();
-        onBugreportChanged();
-    }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/BrightnessController.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/BrightnessController.java
deleted file mode 100644
index e18b28a..0000000
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/BrightnessController.java
+++ /dev/null
@@ -1,153 +0,0 @@
-/*
- * Copyright (C) 2010 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.systemui.statusbar.policy;
-
-import android.content.ContentResolver;
-import android.content.Context;
-import android.os.AsyncTask;
-import android.os.IPowerManager;
-import android.os.PowerManager;
-import android.os.RemoteException;
-import android.os.ServiceManager;
-import android.provider.Settings;
-import android.provider.Settings.SettingNotFoundException;
-import android.util.Slog;
-import android.view.IWindowManager;
-import android.widget.CompoundButton;
-import android.widget.ImageView;
-
-import com.android.systemui.statusbar.policy.BatteryController.BatteryStateChangeCallback;
-
-import java.util.ArrayList;
-
-public class BrightnessController implements ToggleSlider.Listener {
-    private static final String TAG = "StatusBar.BrightnessController";
-
-    private final int mMinimumBacklight;
-    private final int mMaximumBacklight;
-
-    private final Context mContext;
-    private final ImageView mIcon;
-    private final ToggleSlider mControl;
-    private final boolean mAutomaticAvailable;
-    private final IPowerManager mPower;
-    private final CurrentUserTracker mUserTracker;
-
-    private ArrayList<BrightnessStateChangeCallback> mChangeCallbacks =
-            new ArrayList<BrightnessStateChangeCallback>();
-
-    public interface BrightnessStateChangeCallback {
-        public void onBrightnessLevelChanged();
-    }
-
-    public BrightnessController(Context context, ImageView icon, ToggleSlider control) {
-        mContext = context;
-        mIcon = icon;
-        mControl = control;
-        mUserTracker = new CurrentUserTracker(mContext);
-
-        PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
-        mMinimumBacklight = pm.getMinimumScreenBrightnessSetting();
-        mMaximumBacklight = pm.getMaximumScreenBrightnessSetting();
-
-        mAutomaticAvailable = context.getResources().getBoolean(
-                com.android.internal.R.bool.config_automatic_brightness_available);
-        mPower = IPowerManager.Stub.asInterface(ServiceManager.getService("power"));
-
-        control.setOnChangedListener(this);
-    }
-
-    public void addStateChangedCallback(BrightnessStateChangeCallback cb) {
-        mChangeCallbacks.add(cb);
-    }
-
-    @Override
-    public void onInit(ToggleSlider control) {
-        if (mAutomaticAvailable) {
-            int automatic;
-            try {
-                automatic = Settings.System.getIntForUser(mContext.getContentResolver(),
-                        Settings.System.SCREEN_BRIGHTNESS_MODE,
-                        mUserTracker.getCurrentUserId());
-            } catch (SettingNotFoundException snfe) {
-                automatic = 0;
-            }
-            control.setChecked(automatic != 0);
-            updateIcon(automatic != 0);
-        } else {
-            control.setChecked(false);
-            updateIcon(false /*automatic*/);
-            //control.hideToggle();
-        }
-        
-        int value;
-        try {
-            value = Settings.System.getIntForUser(mContext.getContentResolver(),
-                    Settings.System.SCREEN_BRIGHTNESS,
-                    mUserTracker.getCurrentUserId());
-        } catch (SettingNotFoundException ex) {
-            value = mMaximumBacklight;
-        }
-
-        control.setMax(mMaximumBacklight - mMinimumBacklight);
-        control.setValue(value - mMinimumBacklight);
-    }
-
-    public void onChanged(ToggleSlider view, boolean tracking, boolean automatic, int value) {
-        setMode(automatic ? Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC
-                : Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
-        updateIcon(automatic);
-        if (!automatic) {
-            final int val = value + mMinimumBacklight;
-            setBrightness(val);
-            if (!tracking) {
-                AsyncTask.execute(new Runnable() {
-                        public void run() {
-                            Settings.System.putIntForUser(mContext.getContentResolver(),
-                                    Settings.System.SCREEN_BRIGHTNESS, val,
-                                    mUserTracker.getCurrentUserId());
-                        }
-                    });
-            }
-        }
-
-        for (BrightnessStateChangeCallback cb : mChangeCallbacks) {
-            cb.onBrightnessLevelChanged();
-        }
-    }
-
-    private void setMode(int mode) {
-        Settings.System.putIntForUser(mContext.getContentResolver(),
-                Settings.System.SCREEN_BRIGHTNESS_MODE, mode,
-                mUserTracker.getCurrentUserId());
-    }
-    
-    private void setBrightness(int brightness) {
-        try {
-            mPower.setTemporaryScreenBrightnessSettingOverride(brightness);
-        } catch (RemoteException ex) {
-        }        
-    }
-
-    private void updateIcon(boolean automatic) {
-        if (mIcon != null) {
-            mIcon.setImageResource(automatic ?
-                    com.android.systemui.R.drawable.ic_qs_brightness_auto_on :
-                    com.android.systemui.R.drawable.ic_qs_brightness_auto_off);
-        }
-    }
-}
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/VolumeController.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/VolumeController.java
index 6fee432..70f9ac8 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/VolumeController.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/VolumeController.java
@@ -27,6 +27,8 @@
 import android.view.IWindowManager;
 import android.widget.CompoundButton;
 
+import com.android.systemui.settings.ToggleSlider;
+
 public class VolumeController implements ToggleSlider.Listener {
     private static final String TAG = "StatusBar.VolumeController";
     private static final int STREAM = AudioManager.STREAM_NOTIFICATION;
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/tablet/SettingsView.java b/packages/SystemUI/src/com/android/systemui/statusbar/tablet/SettingsView.java
index f71842e..e0dcbcd 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/tablet/SettingsView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/tablet/SettingsView.java
@@ -30,11 +30,11 @@
 import android.widget.TextView;
 
 import com.android.systemui.R;
+import com.android.systemui.settings.BrightnessController;
+import com.android.systemui.settings.ToggleSlider;
 import com.android.systemui.statusbar.policy.AirplaneModeController;
 import com.android.systemui.statusbar.policy.AutoRotateController;
-import com.android.systemui.statusbar.policy.BrightnessController;
 import com.android.systemui.statusbar.policy.DoNotDisturbController;
-import com.android.systemui.statusbar.policy.ToggleSlider;
 import com.android.systemui.statusbar.policy.VolumeController;
 
 public class SettingsView extends LinearLayout implements View.OnClickListener {
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/tablet/TabletStatusBar.java b/packages/SystemUI/src/com/android/systemui/statusbar/tablet/TabletStatusBar.java
index ccd87f7..3d6bfe7 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/tablet/TabletStatusBar.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/tablet/TabletStatusBar.java
@@ -247,7 +247,6 @@
         mNotificationPanel.show(false, false);
         mNotificationPanel.setOnTouchListener(
                 new TouchOutsideListener(MSG_CLOSE_NOTIFICATION_PANEL, mNotificationPanel));
-        mNotificationPanel.setLayoutDirection(mLayoutDirection);
 
         // the battery icon
         mBatteryController.addIconView((ImageView)mNotificationPanel.findViewById(R.id.battery));
@@ -313,7 +312,6 @@
         mInputMethodsPanel.setOnTouchListener(new TouchOutsideListener(
                 MSG_CLOSE_INPUT_METHODS_PANEL, mInputMethodsPanel));
         mInputMethodsPanel.setImeSwitchButton(mInputMethodSwitchButton);
-        mInputMethodsPanel.setLayoutDirection(mLayoutDirection);
         mStatusBarView.setIgnoreChildren(2, mInputMethodSwitchButton, mInputMethodsPanel);
         lp = new WindowManager.LayoutParams(
                 ViewGroup.LayoutParams.WRAP_CONTENT,
@@ -337,7 +335,6 @@
                 MSG_CLOSE_COMPAT_MODE_PANEL, mCompatModePanel));
         mCompatModePanel.setTrigger(mCompatModeButton);
         mCompatModePanel.setVisibility(View.GONE);
-        mCompatModePanel.setLayoutDirection(mLayoutDirection);
         mStatusBarView.setIgnoreChildren(3, mCompatModeButton, mCompatModePanel);
         lp = new WindowManager.LayoutParams(
                 250,
@@ -395,13 +392,7 @@
 
     @Override
     protected void refreshLayout(int layoutDirection) {
-        mStatusBarView.setLayoutDirection(layoutDirection);
-        if (mCompatibilityHelpDialog != null) {
-            mCompatibilityHelpDialog.setLayoutDirection(layoutDirection);
-        }
         mNotificationPanel.refreshLayout(layoutDirection);
-        mInputMethodsPanel.setLayoutDirection(layoutDirection);
-        mCompatModePanel.setLayoutDirection(layoutDirection);
     }
 
     protected void loadDimens() {
@@ -463,7 +454,6 @@
         final TabletStatusBarView sb = (TabletStatusBarView)View.inflate(
                 context, R.layout.system_bar, null);
         mStatusBarView = sb;
-        mStatusBarView.setLayoutDirection(mLayoutDirection);
 
         sb.setHandler(mHandler);
 
@@ -1134,7 +1124,6 @@
         }
 
         mCompatibilityHelpDialog = View.inflate(mContext, R.layout.compat_mode_help, null);
-        mCompatibilityHelpDialog.setLayoutDirection(mLayoutDirection);
         View button = mCompatibilityHelpDialog.findViewById(R.id.button);
 
         button.setOnClickListener(new View.OnClickListener() {
diff --git a/policy/src/com/android/internal/policy/impl/keyguard/KeyguardHostView.java b/policy/src/com/android/internal/policy/impl/keyguard/KeyguardHostView.java
index a0a5f5a..06f06b5 100644
--- a/policy/src/com/android/internal/policy/impl/keyguard/KeyguardHostView.java
+++ b/policy/src/com/android/internal/policy/impl/keyguard/KeyguardHostView.java
@@ -30,6 +30,7 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentSender;
+import android.content.pm.PackageManager.NameNotFoundException;
 import android.content.pm.UserInfo;
 import android.content.res.Resources;
 import android.graphics.Canvas;
@@ -102,8 +103,9 @@
 
     private boolean mUserSetupCompleted;
 
-    // User for whom this host view was created
-    private int mUserId;
+    // User for whom this host view was created.  Final because we should never change the
+    // id without reconstructing an instance of KeyguardHostView. See note below...
+    private final int mUserId;
 
     private KeyguardMultiUserSelectorView mKeyguardMultiUserSelectorView;
 
@@ -132,10 +134,35 @@
     public KeyguardHostView(Context context, AttributeSet attrs) {
         super(context, attrs);
         mLockPatternUtils = new LockPatternUtils(context);
+
+        // Note: This depends on KeyguardHostView getting reconstructed every time the
+        // user switches, since mUserId will be used for the entire session.
+        // Once created, keyguard should *never* re-use this instance with another user.
+        // In other words, mUserId should never change - hence it's marked final.
         mUserId = mLockPatternUtils.getCurrentUser();
-        mAppWidgetHost = new AppWidgetHost(
-                context, APPWIDGET_HOST_ID, mOnClickHandler, Looper.myLooper());
-        mAppWidgetHost.setUserId(mUserId);
+
+        Context userContext = null;
+        try {
+            final String packageName = "system";
+            userContext = mContext.createPackageContextAsUser(packageName, 0,
+                    new UserHandle(mUserId));
+
+        } catch (NameNotFoundException e) {
+            e.printStackTrace();
+            // This should never happen, but it's better to have no widgets than to crash.
+            userContext = context;
+        }
+
+        // These need to be created with the user context...
+        mAppWidgetHost = new AppWidgetHost(userContext, APPWIDGET_HOST_ID, mOnClickHandler,
+                Looper.myLooper());
+        mAppWidgetManager = AppWidgetManager.getInstance(userContext);
+
+        cleanupAppWidgetIds();
+
+        mSecurityModel = new KeyguardSecurityModel(context);
+
+        mViewStateManager = new KeyguardViewStateManager(this);
 
         DevicePolicyManager dpm =
                 (DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
@@ -355,21 +382,17 @@
     @Override
     protected void onAttachedToWindow() {
         super.onAttachedToWindow();
-        mAppWidgetHost.startListeningAsUser(mUserId);
+        mAppWidgetHost.startListening();
         KeyguardUpdateMonitor.getInstance(mContext).registerCallback(mUpdateMonitorCallbacks);
     }
 
     @Override
     protected void onDetachedFromWindow() {
         super.onDetachedFromWindow();
-        mAppWidgetHost.stopListeningAsUser(mUserId);
+        mAppWidgetHost.stopListening();
         KeyguardUpdateMonitor.getInstance(mContext).removeCallback(mUpdateMonitorCallbacks);
     }
 
-    private AppWidgetHost getAppWidgetHost() {
-        return mAppWidgetHost;
-    }
-
     void addWidget(AppWidgetHostView view, int pageIndex) {
         mAppWidgetContainer.addWidget(view, pageIndex);
     }
@@ -1020,12 +1043,13 @@
     private boolean addWidget(int appId, int pageIndex, boolean updateDbIfFailed) {
         AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appId);
         if (appWidgetInfo != null) {
-            AppWidgetHostView view = getAppWidgetHost().createView(mContext, appId, appWidgetInfo);
+            AppWidgetHostView view = mAppWidgetHost.createView(mContext, appId, appWidgetInfo);
             addWidget(view, pageIndex);
             return true;
         } else {
             if (updateDbIfFailed) {
-                Log.w(TAG, "AppWidgetInfo for app widget id " + appId + " was null, deleting");
+                Log.w(TAG, "*** AppWidgetInfo for app widget id " + appId + "  was null for user"
+                        + mUserId + ", deleting");
                 mAppWidgetHost.deleteAppWidgetId(appId);
                 mLockPatternUtils.removeAppWidget(appId);
             }
diff --git a/services/java/com/android/server/AppWidgetService.java b/services/java/com/android/server/AppWidgetService.java
index 06aeb29..d5715a5 100644
--- a/services/java/com/android/server/AppWidgetService.java
+++ b/services/java/com/android/server/AppWidgetService.java
@@ -16,7 +16,7 @@
 
 package com.android.server;
 
-import android.app.ActivityManagerNative;
+import android.app.ActivityManager;
 import android.appwidget.AppWidgetProviderInfo;
 import android.content.BroadcastReceiver;
 import android.content.ComponentName;
@@ -121,107 +121,67 @@
         }, userFilter);
     }
 
-    /**
-     * This returns the user id of the caller, if the caller is not the system process,
-     * otherwise it assumes that the calls are from the lockscreen and hence are meant for the
-     * current user. TODO: Instead, have lockscreen make explicit calls with userId
-     */
-    private int getCallingOrCurrentUserId() {
-        int callingUid = Binder.getCallingUid();
-        // Also check the PID because Settings (power control widget) also runs as System UID
-        if (callingUid == android.os.Process.myUid()
-                && Binder.getCallingPid() == android.os.Process.myPid()) {
-            try {
-                return ActivityManagerNative.getDefault().getCurrentUser().id;
-            } catch (RemoteException re) {
-                return UserHandle.getUserId(callingUid);
-            }
-        } else {
-            return UserHandle.getUserId(callingUid);
-        }
-    }
-
     @Override
-    public int allocateAppWidgetId(String packageName, int hostId) throws RemoteException {
-        return getImplForUser(getCallingOrCurrentUserId()).allocateAppWidgetId(
-                packageName, hostId);
-    }
-
-    @Override
-    public int[] getAppWidgetIdsForHost(int hostId) throws RemoteException {
-        return getImplForUser(getCallingOrCurrentUserId()).getAppWidgetIdsForHost(hostId);
-    }
-    
-    @Override
-    public void deleteAppWidgetId(int appWidgetId) throws RemoteException {
-        getImplForUser(getCallingOrCurrentUserId()).deleteAppWidgetId(appWidgetId);
-    }
-
-    @Override
-    public void deleteHost(int hostId) throws RemoteException {
-        getImplForUser(getCallingOrCurrentUserId()).deleteHost(hostId);
-    }
-
-    @Override
-    public void deleteAllHosts() throws RemoteException {
-        getImplForUser(getCallingOrCurrentUserId()).deleteAllHosts();
-    }
-
-    @Override
-    public void bindAppWidgetId(int appWidgetId, ComponentName provider, Bundle options)
+    public int allocateAppWidgetId(String packageName, int hostId, int userId)
             throws RemoteException {
-        getImplForUser(getCallingOrCurrentUserId()).bindAppWidgetId(appWidgetId, provider,
-                options);
+        return getImplForUser(userId).allocateAppWidgetId(packageName, hostId);
+    }
+
+    @Override
+    public int[] getAppWidgetIdsForHost(int hostId, int userId) throws RemoteException {
+        return getImplForUser(userId).getAppWidgetIdsForHost(hostId);
+    }
+
+    @Override
+    public void deleteAppWidgetId(int appWidgetId, int userId) throws RemoteException {
+        getImplForUser(userId).deleteAppWidgetId(appWidgetId);
+    }
+
+    @Override
+    public void deleteHost(int hostId, int userId) throws RemoteException {
+        getImplForUser(userId).deleteHost(hostId);
+    }
+
+    @Override
+    public void deleteAllHosts(int userId) throws RemoteException {
+        getImplForUser(userId).deleteAllHosts();
+    }
+
+    @Override
+    public void bindAppWidgetId(int appWidgetId, ComponentName provider, Bundle options, int userId)
+            throws RemoteException {
+        getImplForUser(userId).bindAppWidgetId(appWidgetId, provider, options);
     }
 
     @Override
     public boolean bindAppWidgetIdIfAllowed(
-            String packageName, int appWidgetId, ComponentName provider, Bundle options)
+            String packageName, int appWidgetId, ComponentName provider, Bundle options, int userId)
                     throws RemoteException {
-        return getImplForUser(getCallingOrCurrentUserId()).bindAppWidgetIdIfAllowed(
+        return getImplForUser(userId).bindAppWidgetIdIfAllowed(
                 packageName, appWidgetId, provider, options);
     }
 
     @Override
-    public boolean hasBindAppWidgetPermission(String packageName) throws RemoteException {
-        return getImplForUser(getCallingOrCurrentUserId()).hasBindAppWidgetPermission(
-                packageName);
+    public boolean hasBindAppWidgetPermission(String packageName, int userId)
+            throws RemoteException {
+        return getImplForUser(userId).hasBindAppWidgetPermission(packageName);
     }
 
     @Override
-    public void setBindAppWidgetPermission(String packageName, boolean permission)
+    public void setBindAppWidgetPermission(String packageName, boolean permission, int userId)
             throws RemoteException {
-        getImplForUser(getCallingOrCurrentUserId()).setBindAppWidgetPermission(
-                packageName, permission);
+        getImplForUser(userId).setBindAppWidgetPermission(packageName, permission);
     }
 
     @Override
     public void bindRemoteViewsService(int appWidgetId, Intent intent, IBinder connection,
             int userId) throws RemoteException {
-        if (Binder.getCallingPid() != android.os.Process.myPid()
-                && userId != UserHandle.getCallingUserId()) {
-            throw new SecurityException("Call from non-system process. Calling uid = "
-                    + Binder.getCallingUid());
-        }
-        getImplForUser(userId).bindRemoteViewsService(
-                appWidgetId, intent, connection);
+        getImplForUser(userId).bindRemoteViewsService(appWidgetId, intent, connection);
     }
 
     @Override
     public int[] startListening(IAppWidgetHost host, String packageName, int hostId,
-            List<RemoteViews> updatedViews) throws RemoteException {
-        return getImplForUser(getCallingOrCurrentUserId()).startListening(host,
-                packageName, hostId, updatedViews);
-    }
-
-    @Override
-    public int[] startListeningAsUser(IAppWidgetHost host, String packageName, int hostId,
             List<RemoteViews> updatedViews, int userId) throws RemoteException {
-        if (Binder.getCallingPid() != android.os.Process.myPid()
-                && userId != UserHandle.getCallingUserId()) {
-            throw new SecurityException("Call from non-system process. Calling uid = "
-                    + Binder.getCallingUid());
-        }
         return getImplForUser(userId).startListening(host, packageName, hostId, updatedViews);
     }
 
@@ -250,7 +210,19 @@
         }
     }
 
+    private void checkPermission(int userId) {
+        int realUserId = ActivityManager.handleIncomingUser(
+                Binder.getCallingPid(),
+                Binder.getCallingUid(),
+                userId,
+                false, /* allowAll */
+                true, /* requireFull */
+                this.getClass().getSimpleName(),
+                this.getClass().getPackage().getName());
+    }
+
     private AppWidgetServiceImpl getImplForUser(int userId) {
+        checkPermission(userId);
         boolean sendInitial = false;
         AppWidgetServiceImpl service;
         synchronized (mAppWidgetServices) {
@@ -272,86 +244,73 @@
     }
 
     @Override
-    public int[] getAppWidgetIds(ComponentName provider) throws RemoteException {
-        return getImplForUser(getCallingOrCurrentUserId()).getAppWidgetIds(provider);
+    public int[] getAppWidgetIds(ComponentName provider, int userId) throws RemoteException {
+        return getImplForUser(userId).getAppWidgetIds(provider);
     }
 
     @Override
-    public AppWidgetProviderInfo getAppWidgetInfo(int appWidgetId) throws RemoteException {
-        return getImplForUser(getCallingOrCurrentUserId()).getAppWidgetInfo(appWidgetId);
-    }
-
-    @Override
-    public RemoteViews getAppWidgetViews(int appWidgetId) throws RemoteException {
-        return getImplForUser(getCallingOrCurrentUserId()).getAppWidgetViews(appWidgetId);
-    }
-
-    @Override
-    public void updateAppWidgetOptions(int appWidgetId, Bundle options) {
-        getImplForUser(getCallingOrCurrentUserId()).updateAppWidgetOptions(appWidgetId, options);
-    }
-
-    @Override
-    public Bundle getAppWidgetOptions(int appWidgetId) {
-        return getImplForUser(getCallingOrCurrentUserId()).getAppWidgetOptions(appWidgetId);
-    }
-
-    @Override
-    public List<AppWidgetProviderInfo> getInstalledProviders(int categoryFilter)
+    public AppWidgetProviderInfo getAppWidgetInfo(int appWidgetId, int userId)
             throws RemoteException {
-        return getImplForUser(getCallingOrCurrentUserId()).getInstalledProviders(categoryFilter);
+        return getImplForUser(userId).getAppWidgetInfo(appWidgetId);
     }
 
     @Override
-    public void notifyAppWidgetViewDataChanged(int[] appWidgetIds, int viewId)
+    public RemoteViews getAppWidgetViews(int appWidgetId, int userId) throws RemoteException {
+        return getImplForUser(userId).getAppWidgetViews(appWidgetId);
+    }
+
+    @Override
+    public void updateAppWidgetOptions(int appWidgetId, Bundle options, int userId) {
+        getImplForUser(userId).updateAppWidgetOptions(appWidgetId, options);
+    }
+
+    @Override
+    public Bundle getAppWidgetOptions(int appWidgetId, int userId) {
+        return getImplForUser(userId).getAppWidgetOptions(appWidgetId);
+    }
+
+    @Override
+    public List<AppWidgetProviderInfo> getInstalledProviders(int categoryFilter, int userId)
             throws RemoteException {
-        getImplForUser(getCallingOrCurrentUserId()).notifyAppWidgetViewDataChanged(
+        return getImplForUser(userId).getInstalledProviders(categoryFilter);
+    }
+
+    @Override
+    public void notifyAppWidgetViewDataChanged(int[] appWidgetIds, int viewId, int userId)
+            throws RemoteException {
+        getImplForUser(userId).notifyAppWidgetViewDataChanged(
                 appWidgetIds, viewId);
     }
 
     @Override
-    public void partiallyUpdateAppWidgetIds(int[] appWidgetIds, RemoteViews views)
+    public void partiallyUpdateAppWidgetIds(int[] appWidgetIds, RemoteViews views, int userId)
             throws RemoteException {
-        getImplForUser(getCallingOrCurrentUserId()).partiallyUpdateAppWidgetIds(
+        getImplForUser(userId).partiallyUpdateAppWidgetIds(
                 appWidgetIds, views);
     }
 
     @Override
-    public void stopListening(int hostId) throws RemoteException {
-        getImplForUser(getCallingOrCurrentUserId()).stopListening(hostId);
-    }
-
-    @Override
-    public void stopListeningAsUser(int hostId, int userId) throws RemoteException {
-        if (Binder.getCallingPid() != android.os.Process.myPid()
-                && userId != UserHandle.getCallingUserId()) {
-            throw new SecurityException("Call from non-system process. Calling uid = "
-                    + Binder.getCallingUid());
-        }
+    public void stopListening(int hostId, int userId) throws RemoteException {
         getImplForUser(userId).stopListening(hostId);
     }
 
     @Override
     public void unbindRemoteViewsService(int appWidgetId, Intent intent, int userId)
             throws RemoteException {
-        if (Binder.getCallingPid() != android.os.Process.myPid()
-                && userId != UserHandle.getCallingUserId()) {
-            throw new SecurityException("Call from non-system process. Calling uid = "
-                    + Binder.getCallingUid());
-        }
         getImplForUser(userId).unbindRemoteViewsService(
                 appWidgetId, intent);
     }
 
     @Override
-    public void updateAppWidgetIds(int[] appWidgetIds, RemoteViews views) throws RemoteException {
-        getImplForUser(getCallingOrCurrentUserId()).updateAppWidgetIds(appWidgetIds, views);
+    public void updateAppWidgetIds(int[] appWidgetIds, RemoteViews views, int userId)
+            throws RemoteException {
+        getImplForUser(userId).updateAppWidgetIds(appWidgetIds, views);
     }
 
     @Override
-    public void updateAppWidgetProvider(ComponentName provider, RemoteViews views)
+    public void updateAppWidgetProvider(ComponentName provider, RemoteViews views, int userId)
             throws RemoteException {
-        getImplForUser(getCallingOrCurrentUserId()).updateAppWidgetProvider(provider, views);
+        getImplForUser(userId).updateAppWidgetProvider(provider, views);
     }
 
     @Override
diff --git a/services/java/com/android/server/AppWidgetServiceImpl.java b/services/java/com/android/server/AppWidgetServiceImpl.java
index d1829ab..6eea928 100644
--- a/services/java/com/android/server/AppWidgetServiceImpl.java
+++ b/services/java/com/android/server/AppWidgetServiceImpl.java
@@ -1046,7 +1046,7 @@
             if (id.host.callbacks != null) {
                 try {
                     // the lock is held, but this is a oneway call
-                    id.host.callbacks.updateAppWidget(id.appWidgetId, views);
+                    id.host.callbacks.updateAppWidget(id.appWidgetId, views, mUserId);
                 } catch (RemoteException e) {
                     // It failed; remove the callback. No need to prune because
                     // we know that this host is still referenced by this instance.
@@ -1065,7 +1065,7 @@
             if (id.host.callbacks != null) {
                 try {
                     // the lock is held, but this is a oneway call
-                    id.host.callbacks.viewDataChanged(id.appWidgetId, viewId);
+                    id.host.callbacks.viewDataChanged(id.appWidgetId, viewId, mUserId);
                 } catch (RemoteException e) {
                     // It failed; remove the callback. No need to prune because
                     // we know that this host is still referenced by this instance.
@@ -1934,7 +1934,8 @@
                                 id.views = null;
                                 if (id.host != null && id.host.callbacks != null) {
                                     try {
-                                        id.host.callbacks.providerChanged(id.appWidgetId, p.info);
+                                        id.host.callbacks.providerChanged(id.appWidgetId, p.info,
+                                                mUserId);
                                     } catch (RemoteException ex) {
                                         // It failed; remove the callback. No need to prune because
                                         // we know that this host is still referenced by this
@@ -2001,7 +2002,7 @@
             Host host = mHosts.get(i);
             try {
                 if (host.callbacks != null) {
-                    host.callbacks.providersChanged();
+                    host.callbacks.providersChanged(mUserId);
                 }
             } catch (RemoteException ex) {
                 // It failed; remove the callback. No need to prune because
diff --git a/services/java/com/android/server/SystemServer.java b/services/java/com/android/server/SystemServer.java
index 949c2ed..8ef247e 100644
--- a/services/java/com/android/server/SystemServer.java
+++ b/services/java/com/android/server/SystemServer.java
@@ -62,6 +62,7 @@
 import com.android.server.power.ShutdownThread;
 import com.android.server.search.SearchManagerService;
 import com.android.server.usb.UsbService;
+import com.android.server.wifi.WifiService;
 import com.android.server.wm.WindowManagerService;
 
 import dalvik.system.VMRuntime;
diff --git a/services/java/com/android/server/accessibility/AccessibilityManagerService.java b/services/java/com/android/server/accessibility/AccessibilityManagerService.java
index 9c518a1..fd5e79a 100644
--- a/services/java/com/android/server/accessibility/AccessibilityManagerService.java
+++ b/services/java/com/android/server/accessibility/AccessibilityManagerService.java
@@ -934,12 +934,10 @@
      * @param service The service.
      * @return True if the service was removed, false otherwise.
      */
-    private void removeServiceLocked(Service service) {
-        UserState userState = getUserStateLocked(service.mUserId);
+    private void removeServiceLocked(Service service, UserState userState) {
         userState.mBoundServices.remove(service);
         userState.mComponentNameToServiceMap.remove(service.mComponentName);
         service.unlinkToOwnDeath();
-        service.dispose();
     }
 
     /**
@@ -1672,11 +1670,9 @@
         public boolean bindLocked() {
             UserState userState = getUserStateLocked(mUserId);
             if (!mIsAutomation) {
-                if (mService == null) {
-                    if (mContext.bindServiceAsUser(mIntent, this, Context.BIND_AUTO_CREATE,
-                            new UserHandle(mUserId))) {
-                        userState.mBindingServices.add(mComponentName);
-                    }
+                if (mService == null && mContext.bindServiceAsUser(
+                        mIntent, this, Context.BIND_AUTO_CREATE, new UserHandle(mUserId))) {
+                    userState.mBindingServices.add(mComponentName);
                 }
             } else {
                 userState.mBindingServices.add(mComponentName);
@@ -1697,14 +1693,15 @@
             if (mService == null) {
                 return false;
             }
+            UserState userState = getUserStateLocked(mUserId);
             if (!mIsAutomation) {
                 mContext.unbindService(this);
             } else {
-                UserState userState = getUserStateLocked(mUserId);
                 userState.mUiAutomationService = null;
                 userState.mUiAutomationServiceClient = null;
             }
-            removeServiceLocked(this);
+            removeServiceLocked(this, userState);
+            dispose();
             return true;
         }
 
@@ -1750,11 +1747,11 @@
                 mService = service;
                 mServiceInterface = IAccessibilityServiceClient.Stub.asInterface(service);
                 UserState userState = getUserStateLocked(mUserId);
+                addServiceLocked(this, userState);
                 if (!userState.mBindingServices.contains(mComponentName)) {
                     binderDied();
                 } else {
                     userState.mBindingServices.remove(mComponentName);
-                    addServiceLocked(this, userState);
                     onUserStateChangedLocked(userState);
                 }
             }
@@ -2106,9 +2103,10 @@
 
         public void binderDied() {
             synchronized (mLock) {
-                // The death recipient is unregistered in tryRemoveServiceLocked
-                removeServiceLocked(this);
                 UserState userState = getUserStateLocked(mUserId);
+                // The death recipient is unregistered in removeServiceLocked
+                removeServiceLocked(this, userState);
+                dispose();
                 if (mIsAutomation) {
                     // We no longer have an automation service, so restore
                     // the state based on values in the settings database.
diff --git a/services/java/com/android/server/am/ActivityRecord.java b/services/java/com/android/server/am/ActivityRecord.java
index f2d401f..a2f3372 100644
--- a/services/java/com/android/server/am/ActivityRecord.java
+++ b/services/java/com/android/server/am/ActivityRecord.java
@@ -471,6 +471,7 @@
     void setTask(TaskRecord newTask, ThumbnailHolder newThumbHolder, boolean isRoot) {
         if (inHistory && !finishing) {
             if (task != null) {
+                // TODO: If this is the last ActivityRecord in task, remove from ActivityStack.
                 task.removeActivity(this);
                 task.numActivities--;
             }
diff --git a/services/java/com/android/server/am/ActivityStack.java b/services/java/com/android/server/am/ActivityStack.java
index a18a0d1..30a7e23 100644
--- a/services/java/com/android/server/am/ActivityStack.java
+++ b/services/java/com/android/server/am/ActivityStack.java
@@ -77,7 +77,6 @@
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
-import java.util.NoSuchElementException;
 
 /**
  * State and management of a single stack of activities.
@@ -142,9 +141,6 @@
     // is being started.
     static final boolean SHOW_APP_STARTING_PREVIEW = true;
 
-    static final boolean FORWARD_ITERATOR = false;
-    static final boolean REVERSE_ITERATOR = true;
-
     enum ActivityState {
         INITIALIZING,
         RESUMED,
@@ -307,12 +303,6 @@
      */
     boolean mDismissKeyguardOnNextActivity = false;
 
-    /** So we don't have to keep constructing a new object for utility non-nested use. */
-    final ActivityIterator mTmpActivityIterator = new ActivityIterator(FORWARD_ITERATOR, true);
-
-    /** So we don't have to keep constructing a new object for utility non-nested use. */
-    final TaskIterator mTmpTaskIterator = new TaskIterator();
-
     /**
      * Save the most recent screenshot for reuse. This keeps Recents from taking two identical
      * screenshots, one for the Recents thumbnail and one for the pauseActivity thumbnail.
@@ -355,6 +345,8 @@
 
     final Handler mHandler;
 
+    String mLastHistoryModifier;
+
     final class ActivityStackHandler extends Handler {
         //public Handler() {
         //    if (localLOGV) Slog.v(TAG, "Handler started!");
@@ -481,26 +473,66 @@
     }
 
     final ActivityRecord topRunningActivityLocked(ActivityRecord notTop) {
+        ActivityRecord newAr = newTopRunningActivityLocked(notTop);
+
         int i = mHistory.size()-1;
         while (i >= 0) {
             ActivityRecord r = mHistory.get(i);
             if (!r.finishing && r != notTop && okToShow(r)) {
+                if (VALIDATE_TASK_REPLACE && newAr != r) logHistories(
+                        "topRunningActivityLocked", true);
                 return r;
             }
             i--;
         }
+        if (VALIDATE_TASK_REPLACE && newAr != null) Slog.w(TAG,
+                "topRunningActivityLocked: mismatch: newAr!=null");
+        return null;
+    }
+
+    final ActivityRecord newTopRunningActivityLocked(ActivityRecord notTop) {
+        for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
+            final TaskRecord task = mTaskHistory.get(i);
+            final ArrayList<ActivityRecord> activities = task.mActivities;
+            for (int j = activities.size() - 1; j >= 0; --j) {
+                ActivityRecord r = activities.get(j);
+                if (!r.finishing && r != notTop && okToShow(r)) {
+                    return r;
+                }
+            }
+        }
         return null;
     }
 
     final ActivityRecord topRunningNonDelayedActivityLocked(ActivityRecord notTop) {
+        ActivityRecord newAr = newTopRunningNonDelayedActivityLocked(notTop);
+
         int i = mHistory.size()-1;
         while (i >= 0) {
             ActivityRecord r = mHistory.get(i);
             if (!r.finishing && !r.delayedResume && r != notTop && okToShow(r)) {
+                if (VALIDATE_TASK_REPLACE && newAr != r) Slog.w(TAG,
+                    "topRunningNonDelayedActivityLocked: mismatch: newAr=" + newAr + " r=" + r);
                 return r;
             }
             i--;
         }
+        if (VALIDATE_TASK_REPLACE && newAr != null) Slog.w(TAG,
+                "topRunningNonDelayedActivityLocked: mismatch: newAr!=null");
+        return null;
+    }
+
+    final ActivityRecord newTopRunningNonDelayedActivityLocked(ActivityRecord notTop) {
+        for (int i = mTaskHistory.size() - 1; i >= 0; --i) {
+            final TaskRecord task = mTaskHistory.get(i);
+            final ArrayList<ActivityRecord> activities = task.mActivities;
+            for (int j = activities.size() - 1; j >= 0; --j) {
+                ActivityRecord r = activities.get(j);
+                if (!r.finishing && !r.delayedResume && r != notTop && okToShow(r)) {
+                    return r;
+                }
+            }
+        }
         return null;
     }
 
@@ -514,16 +546,40 @@
      * @return Returns the HistoryRecord of the next activity on the stack.
      */
     final ActivityRecord topRunningActivityLocked(IBinder token, int taskId) {
+        ActivityRecord newAr = newTopRunningActivityLocked(token, taskId);
+
         int i = mHistory.size()-1;
         while (i >= 0) {
             ActivityRecord r = mHistory.get(i);
             // Note: the taskId check depends on real taskId fields being non-zero
             if (!r.finishing && (token != r.appToken) && (taskId != r.task.taskId)
                     && okToShow(r)) {
+                if (VALIDATE_TASK_REPLACE && newAr != r) Slog.w(TAG,
+                        "topRunningActivityLocked(token): mismatch: newAr=" + newAr + " r=" + r);
                 return r;
             }
             i--;
         }
+        if (VALIDATE_TASK_REPLACE && newAr != null) Slog.w(TAG,
+                "topRunningActivityLocked(token): mismatch: newAr!=null");
+        return null;
+    }
+
+    final ActivityRecord newTopRunningActivityLocked(IBinder token, int taskId) {
+        for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
+            TaskRecord task = mTaskHistory.get(taskNdx);
+            if (task.taskId == taskId) {
+                continue;
+            }
+            ArrayList<ActivityRecord> activities = task.mActivities;
+            for (int i = activities.size() - 1; i >= 0; --i) {
+                final ActivityRecord r = activities.get(i);
+                // Note: the taskId check depends on real taskId fields being non-zero
+                if (!r.finishing && (token != r.appToken) && okToShow(r)) {
+                    return r;
+                }
+            }
+        }
         return null;
     }
 
@@ -1002,7 +1058,7 @@
         }
         // Ensure activities are no longer sleeping.
         if (VALIDATE_TASK_REPLACE) {
-            verifyActivityRecords();
+            verifyActivityRecords(true);
         }
         for (int i=mHistory.size()-1; i>=0; i--) {
             ActivityRecord r = mHistory.get(i);
@@ -1049,7 +1105,7 @@
             // Make sure any stopped but visible activities are now sleeping.
             // This ensures that the activity's onStop() is called.
             if (VALIDATE_TASK_REPLACE) {
-                verifyActivityRecords();
+                verifyActivityRecords(true);
             }
             for (int i=mHistory.size()-1; i>=0; i--) {
                 ActivityRecord r = mHistory.get(i);
@@ -1196,7 +1252,7 @@
 
         synchronized (mService) {
             if (VALIDATE_TASK_REPLACE) {
-                verifyActivityRecords();
+                verifyActivityRecords(true);
             }
             int index = indexOfTokenLocked(token);
             if (index >= 0) {
@@ -1216,7 +1272,7 @@
 
         synchronized (mService) {
             if (VALIDATE_TASK_REPLACE) {
-                verifyActivityRecords();
+                verifyActivityRecords(true);
             }
             int index = indexOfTokenLocked(token);
             if (index >= 0) {
@@ -1431,7 +1487,7 @@
         // If the top activity is not fullscreen, then we need to
         // make sure any activities under it are now visible.
         if (VALIDATE_TASK_REPLACE) {
-            verifyActivityRecords();
+            verifyActivityRecords(true);
         }
         final int count = mHistory.size();
         int i = count-1;
@@ -1969,8 +2025,21 @@
         return true;
     }
 
+    /** Temporary until startActivityLocked is rewritten for tasks. */
+    private int convertAddPos(int addPos) {
+        final int taskId = mHistory.get(addPos).task.taskId;
+        addPos--;
+        int taskOffset = 0;
+        while (addPos >= 0 && taskId == mHistory.get(addPos).task.taskId) {
+            ++taskOffset;
+            --addPos;
+        }
+        return taskOffset;
+    }
+
     private final void startActivityLocked(ActivityRecord r, boolean newTask,
             boolean doResume, boolean keepCurTransition, Bundle options) {
+        mLastHistoryModifier = "startActivityLocked";
         final int NH = mHistory.size();
 
         int addPos = -1;
@@ -1998,11 +2067,12 @@
                         r.task.addActivityToTop(r);
                         mHistory.add(addPos, r);
                         r.putInHistory();
-                        mService.mWindowManager.addAppToken(addPos, r.appToken, r.task.taskId,
-                                r.info.screenOrientation, r.fullscreen,
+                        mService.mWindowManager.addAppToken(convertAddPos(addPos), r.appToken,
+                                r.task.taskId, r.info.screenOrientation, r.fullscreen,
                                 (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
                         if (VALIDATE_TOKENS) {
                             validateAppTokensLocked();
+                            verifyActivityRecords(true);
                         }
                         ActivityOptions.abort(options);
                         return;
@@ -2039,6 +2109,11 @@
         mHistory.add(addPos, r);
         r.putInHistory();
         r.frontOfTask = newTask;
+        if (VALIDATE_TASK_REPLACE) {
+            if (verifyActivityRecords(false)) {
+                Slog.w(TAG, "startActivityLocked: addPos=" + addPos);
+            }
+        }
         if (NH > 0) {
             // We want to show the starting preview window if we are
             // switching to a new task, or the next activity's process is
@@ -2064,8 +2139,8 @@
                 mNoAnimActivities.remove(r);
             }
             r.updateOptionsLocked(options);
-            mService.mWindowManager.addAppToken(
-                    addPos, r.appToken, r.task.taskId, r.info.screenOrientation, r.fullscreen,
+            mService.mWindowManager.addAppToken(convertAddPos(addPos),
+                    r.appToken, r.task.taskId, r.info.screenOrientation, r.fullscreen,
                     (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
             boolean doShow = true;
             if (newTask) {
@@ -2103,7 +2178,7 @@
         } else {
             // If this is the first activity, don't do any fancy animations,
             // because there is nothing for it to animate on top of.
-            mService.mWindowManager.addAppToken(addPos, r.appToken, r.task.taskId,
+            mService.mWindowManager.addAppToken(convertAddPos(addPos), r.appToken, r.task.taskId,
                     r.info.screenOrientation, r.fullscreen,
                     (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
             ActivityOptions.abort(options);
@@ -2116,7 +2191,9 @@
             resumeTopActivityLocked(null);
         }
         if (VALIDATE_TASK_REPLACE) {
-            verifyActivityRecords();
+            if (verifyActivityRecords(true)) {
+                Slog.w(TAG, "startActivityLocked: addPos=" + addPos);
+            }
         }
     }
 
@@ -2144,6 +2221,8 @@
      */
     private final ActivityRecord resetTaskIfNeededLocked(ActivityRecord taskTop,
             ActivityRecord newActivity) {
+        mLastHistoryModifier = "resetTaskIfNeededLocked";
+
         boolean forceReset = (newActivity.info.flags
                 &ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
         if (ACTIVITY_INACTIVE_RESET_TIME > 0
@@ -2281,6 +2360,7 @@
                             dstPos++;
                             i++;
                         }
+                        rebuildTaskHistory();
                         mService.mWindowManager.moveTaskToBottom(taskId);
                         if (VALIDATE_TOKENS) {
                             validateAppTokensLocked();
@@ -2429,6 +2509,7 @@
                                 + " in to resetting task " + task);
                         mService.mWindowManager.setAppGroupId(p.appToken, taskId);
                     }
+                    rebuildTaskHistory();
                     // TODO: This is wrong because it doesn't take lastReparentPos into account.
                     mService.mWindowManager.moveTaskToTop(taskId);
                     if (VALIDATE_TOKENS) {
@@ -2478,7 +2559,7 @@
         }
 
         if (VALIDATE_TASK_REPLACE) {
-            verifyActivityRecords();
+            verifyActivityRecords(true);
         }
         return taskTop;
     }
@@ -2644,6 +2725,7 @@
      */
     private final ActivityRecord moveActivityToFrontLocked(int where) {
         ActivityRecord newTop = mHistory.remove(where);
+        newMoveActivityToFrontLocked(newTop);
         int top = mHistory.size();
         ActivityRecord oldTop = mHistory.get(top-1);
         if (DEBUG_ADD_REMOVE) {
@@ -2653,6 +2735,17 @@
                     + top, here);
         }
         mHistory.add(top, newTop);
+        if (VALIDATE_TASK_REPLACE) {
+            verifyActivityRecords(true);
+        }
+        return newTop;
+    }
+
+    private final ActivityRecord newMoveActivityToFrontLocked(ActivityRecord newTop) {
+        final TaskRecord task = newTop.task;
+        ActivityRecord oldTop = task.getTopActivity();
+        task.mActivities.remove(newTop);
+        task.mActivities.add(newTop);
         oldTop.frontOfTask = false;
         newTop.frontOfTask = true;
         return newTop;
@@ -2663,6 +2756,7 @@
             String resultWho, int requestCode,
             int callingPid, int callingUid, String callingPackage, int startFlags, Bundle options,
             boolean componentSpecified, ActivityRecord[] outActivity) {
+        mLastHistoryModifier = "startActivityLocked(IApplicationThread)";
 
         int err = ActivityManager.START_SUCCESS;
 
@@ -4294,8 +4388,9 @@
             here.fillInStackTrace();
             Slog.i(TAG, "Removing activity " + r + " from stack");
         }
-        if (r.task != null) {
-            r.task.removeActivity(r);
+        final TaskRecord task = r.task;
+        if (task != null) {
+            task.removeActivity(r);
         }
         mHistory.remove(r);
         r.takeFromHistory();
@@ -4597,6 +4692,7 @@
      * of the stack.
      */
     final void moveHomeToFrontLocked() {
+        newMoveHomeToFrontLocked();
         TaskRecord homeTask = null;
         for (int i=mHistory.size()-1; i>=0; i--) {
             ActivityRecord hr = mHistory.get(i);
@@ -4606,6 +4702,23 @@
             }
         }
         if (homeTask != null) {
+//            moveTaskToFrontLocked(homeTask, null, null);
+        }
+    }
+
+    final void newMoveHomeToFrontLocked() {
+        TaskRecord homeTask = null;
+        for (int taskNdx = mTaskHistory.size() - 1; homeTask == null && taskNdx >= 0; --taskNdx) {
+            final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
+            for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
+                final ActivityRecord r = activities.get(activityNdx);
+                if (r.isHomeActivity) {
+                    homeTask = r.task;
+                    break;
+                }
+            }
+        }
+        if (homeTask != null) {
             moveTaskToFrontLocked(homeTask, null, null);
         }
     }
@@ -4642,31 +4755,19 @@
     }
 
     final void moveTaskToFrontLocked(TaskRecord tr, ActivityRecord reason, Bundle options) {
-        if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
 
         final int task = tr.taskId;
         int top = mHistory.size()-1;
 
         if (top < 0 || (mHistory.get(top)).task.taskId == task) {
             // nothing to do!
-            if (reason != null &&
-                    (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
-                ActivityOptions.abort(options);
-            } else {
-                updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
-            }
             return;
         }
 
-        ArrayList<IBinder> moved = new ArrayList<IBinder>();
-
-        // Applying the affinities may have removed entries from the history,
-        // so get the size again.
-        top = mHistory.size()-1;
-        int pos = top;
 
         // Shift all activities with this task up to the top
         // of the stack, keeping them in the same internal order.
+        int pos = top;
         while (pos >= 0) {
             ActivityRecord r = mHistory.get(pos);
             if (localLOGV) Slog.v(
@@ -4680,18 +4781,37 @@
                 }
                 mHistory.remove(pos);
                 mHistory.add(top, r);
-                moved.add(0, r.appToken);
                 top--;
             }
             pos--;
         }
+        //
+        // Start new code here! Delete everything above.
+        //
+        if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
 
-        if (DEBUG_TRANSITION) Slog.v(TAG,
-                "Prepare to front transition: task=" + tr);
+        final int numTasks = mTaskHistory.size();
+        final int index = mTaskHistory.indexOf(tr);
+        if (numTasks == 0 || index < 0 || index == numTasks - 1)  {
+            // nothing to do!
+            if (reason != null &&
+                    (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
+                ActivityOptions.abort(options);
+            } else {
+                updateTransitLocked(AppTransition.TRANSIT_TASK_TO_FRONT, options);
+            }
+            return;
+        }
+
+        // Shift all activities with this task up to the top
+        // of the stack, keeping them in the same internal order.
+        mTaskHistory.remove(tr);
+        mTaskHistory.add(tr);
+
+        if (DEBUG_TRANSITION) Slog.v(TAG, "Prepare to front transition: task=" + tr);
         if (reason != null &&
                 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
-            mService.mWindowManager.prepareAppTransition(
-                    AppTransition.TRANSIT_NONE, false);
+            mService.mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
             ActivityRecord r = topRunningActivityLocked(null);
             if (r != null) {
                 mNoAnimActivities.add(r);
@@ -4702,12 +4822,16 @@
         }
 
         mService.mWindowManager.moveTaskToTop(task);
-        if (VALIDATE_TOKENS) {
-            validateAppTokensLocked();
-        }
 
         finishTaskMoveLocked(task);
         EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, tr.userId, task);
+
+        if (VALIDATE_TOKENS) {
+            validateAppTokensLocked();
+        }
+        if (VALIDATE_TASK_REPLACE) {
+            verifyActivityRecords(true);
+        }
     }
 
     private final void finishTaskMoveLocked(int task) {
@@ -4726,6 +4850,42 @@
      * @return Returns true if the move completed, false if not.
      */
     final boolean moveTaskToBackLocked(int task, ActivityRecord reason) {
+        if (!newMoveTaskToBackLocked(task, reason)) {
+            return false;
+        }
+
+        final int N = mHistory.size();
+        int bottom = 0;
+        int pos = 0;
+
+        // Shift all activities with this task down to the bottom
+        // of the stack, keeping them in the same internal order.
+        while (pos < N) {
+            ActivityRecord r = mHistory.get(pos);
+            if (localLOGV) Slog.v(
+                TAG, "At " + pos + " ckp " + r.task + ": " + r);
+            if (r.task.taskId == task) {
+                if (localLOGV) Slog.v(TAG, "Removing and adding at " + (N-1));
+                if (DEBUG_ADD_REMOVE) {
+                    RuntimeException here = new RuntimeException("here");
+                    here.fillInStackTrace();
+                    Slog.i(TAG, "Removing and adding activity " + r + " to stack at "
+                            + bottom, here);
+                }
+                mHistory.remove(pos);
+                mHistory.add(bottom, r);
+                bottom++;
+            }
+            pos++;
+        }
+        if (VALIDATE_TASK_REPLACE) {
+            verifyActivityRecords(true);
+        }
+
+        return true;
+    }
+
+    final boolean newMoveTaskToBackLocked(int task, ActivityRecord reason) {
         Slog.i(TAG, "moveTaskToBack: " + task);
         
         // If we have a watcher, preflight the move before committing to it.  First check
@@ -4750,41 +4910,16 @@
             }
         }
 
-        ArrayList<IBinder> moved = new ArrayList<IBinder>();
-
         if (DEBUG_TRANSITION) Slog.v(TAG,
                 "Prepare to back transition: task=" + task);
-        
-        final int N = mHistory.size();
-        int bottom = 0;
-        int pos = 0;
 
-        // Shift all activities with this task down to the bottom
-        // of the stack, keeping them in the same internal order.
-        while (pos < N) {
-            ActivityRecord r = mHistory.get(pos);
-            if (localLOGV) Slog.v(
-                TAG, "At " + pos + " ckp " + r.task + ": " + r);
-            if (r.task.taskId == task) {
-                if (localLOGV) Slog.v(TAG, "Removing and adding at " + (N-1));
-                if (DEBUG_ADD_REMOVE) {
-                    RuntimeException here = new RuntimeException("here");
-                    here.fillInStackTrace();
-                    Slog.i(TAG, "Removing and adding activity " + r + " to stack at "
-                            + bottom, here);
-                }
-                mHistory.remove(pos);
-                mHistory.add(bottom, r);
-                moved.add(r.appToken);
-                bottom++;
-            }
-            pos++;
-        }
+        final TaskRecord tr = mTaskIdToTaskRecord.get(task);
+        mTaskHistory.remove(tr);
+        mTaskHistory.add(0, tr);
 
         if (reason != null &&
                 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
-            mService.mWindowManager.prepareAppTransition(
-                    AppTransition.TRANSIT_NONE, false);
+            mService.mWindowManager.prepareAppTransition(AppTransition.TRANSIT_NONE, false);
             ActivityRecord r = topRunningActivityLocked(null);
             if (r != null) {
                 mNoAnimActivities.add(r);
@@ -5307,33 +5442,65 @@
         return starting;
     }
 
-    void verifyActivityRecords() {
-        /* Until we have activity movement implemented for tasks just do the simple test
-        ActivityIterator iterator = new ActivityIterator();
-        int i;
-        int N = mHistory.size();
-        for (i = 0; i < N && iterator.hasNext(); ++i) {
-            ActivityRecord r1 = mHistory.get(i);
-            ActivityRecord r2 = iterator.next();
-            if (r1 != r2) {
+    void rebuildTaskHistory() {
+        mTaskHistory.clear();
+        final int numActivities = mHistory.size();
+        TaskRecord task = null;
+        for (int i = 0; i < numActivities; ++i) {
+            final ActivityRecord r = mHistory.get(i);
+            if (r.task != task) {
+                task = r.task;
+                task.mActivities.clear();
+                mTaskHistory.add(task);
+            }
+            task.mActivities.add(r);
+        }
+    }
+
+    boolean verifyActivityRecords(boolean rebuild) {
+        final int numHistory = mHistory.size();
+        int historyNdx = 0;
+
+        final int numTasks = mTaskHistory.size();
+        int taskNdx;
+        for (taskNdx = historyNdx = 0; taskNdx < numTasks; ++taskNdx) {
+            final TaskRecord task = mTaskHistory.get(taskNdx);
+            final ArrayList<ActivityRecord> activities = task.mActivities;
+            final int numActivities = activities.size();
+            int activityNdx;
+            for (activityNdx = 0;
+                    activityNdx < numActivities && historyNdx < numHistory;
+                    ++activityNdx, ++historyNdx) {
+                ActivityRecord r1 = mHistory.get(historyNdx);
+                ActivityRecord r2 = activities.get(activityNdx);
+                if (r1 != r2) {
+                    break;
+                }
+            }
+            if (activityNdx != numActivities) {
+                // either a mismatch or mHistory ran out before mTaskHistory.
                 break;
             }
         }
-        if (i != N || iterator.hasNext()) {
-            Slog.w(TAG, "verifyActivityRecords mHistory=" + mHistory
-                + " mTaskHistory=" + iterator + " Callers=" + Debug.getCallers(2));
-        } */
-        // Simple test
-        ActivityIterator iterator = new ActivityIterator();
-        while (iterator.hasNext()) {
-            ActivityRecord r = iterator.next();
-            if (!mHistory.contains(r)) {
-                break;
-            }
+        if (taskNdx != numTasks || historyNdx != numHistory) {
+            logHistories("verifyActivityRecords", rebuild);
+            return true;
         }
-        if (iterator.size() != mHistory.size() || iterator.hasNext()) {
-            Slog.w(TAG, "verifyActivityRecords mHistory=" + mHistory
-                + " mTaskHistory=" + iterator + " Callers=" + Debug.getCallers(2));
+        return false;
+    }
+
+    private void logHistories(String caller, boolean rebuild) {
+        Slog.w(TAG, "Mismatch! " + caller + "  mHistory=" + mHistory);
+        ArrayList<ArrayList<ActivityRecord>> nestedRecords =
+                new ArrayList<ArrayList<ActivityRecord>>();
+        for (TaskRecord task : mTaskHistory) {
+            nestedRecords.add(task.mActivities);
+        }
+        Slog.w(TAG, "Mismatch! " + caller + " mTaskHistory" + nestedRecords);
+        Slog.w(TAG, "Mismatch! " + caller + " lastHistoryModifier=" + mLastHistoryModifier
+                + " Caller=" + Debug.getCallers(4));
+        if (rebuild) {
+            rebuildTaskHistory();
         }
     }
 
@@ -5353,138 +5520,4 @@
         }
         return task;
     }
-
-    class TaskIterator implements Iterator<TaskRecord> {
-        private int mCur;
-        private boolean mReverse;
-
-        TaskIterator() {
-            this(FORWARD_ITERATOR);
-        }
-
-        TaskIterator(boolean reverse) {
-            reset(reverse);
-        }
-
-        public void reset(boolean reverse) {
-            mReverse = reverse;
-            mCur = reverse ? mTaskHistory.size() - 1 : 0;
-        }
-
-        @Override
-        public boolean hasNext() {
-            if (mReverse) {
-                return mCur >= 0;
-            }
-            return mCur < mTaskHistory.size();
-        }
-
-        @Override
-        public TaskRecord next() {
-            if (hasNext()) {
-                TaskRecord task = mTaskHistory.get(mCur);
-                mCur += (mReverse ? -1 : 1);
-                return task;
-            }
-            throw new NoSuchElementException();
-        }
-
-        @Override
-        public void remove() {
-            throw new IllegalArgumentException();
-        }
-    }
-
-    class ActivityIterator implements Iterator<ActivityRecord> {
-        final TaskIterator mIterator;
-        boolean mReverse;
-        int mCur;
-        TaskRecord mTaskRecord;
-        final boolean mSkipFinishing;
-
-        public ActivityIterator() {
-            this(FORWARD_ITERATOR);
-        }
-
-        public ActivityIterator(boolean reverse) {
-            this(reverse, false);
-        }
-
-        public ActivityIterator(boolean reverse, boolean skipFinishing) {
-            mSkipFinishing = skipFinishing;
-            mIterator = new TaskIterator();
-            reset(reverse);
-        }
-
-        public void reset(boolean reverse) {
-            mReverse = reverse;
-            mIterator.reset(reverse);
-            getNextTaskRecord();
-        }
-
-        private void getNextTaskRecord() {
-            if (mIterator.hasNext()) {
-                mTaskRecord = mIterator.next();
-                mCur = mReverse ? mTaskRecord.mActivities.size() - 1 : 0;
-            }
-        }
-
-        @Override
-        public boolean hasNext() {
-            if (mTaskRecord == null) {
-                return false;
-            }
-            if (mReverse) {
-                return mCur >= 0;
-            }
-            return mCur < mTaskRecord.mActivities.size();
-        }
-
-        @Override
-        public ActivityRecord next() {
-            while (hasNext()) {
-                ActivityRecord r = mTaskRecord.mActivities.get(mCur);
-                mCur += mReverse ? -1 : 1;
-                if (!hasNext()) {
-                    getNextTaskRecord();
-                }
-                if (mSkipFinishing && r.finishing) {
-                    continue;
-                }
-                return r;
-            }
-            throw new NoSuchElementException();
-        }
-
-        @Override
-        public void remove() {
-            throw new UnsupportedOperationException();
-        }
-
-        int size() {
-            int size = 0;
-            final TaskIterator iterator = new TaskIterator();
-            while (iterator.hasNext()) {
-                size += iterator.next().mActivities.size();
-            }
-            return size;
-        }
-
-        ActivityRecord peek() {
-            if (mTaskRecord != null && mCur >= 0 && mCur < mTaskRecord.mActivities.size()) {
-                return mTaskRecord.mActivities.get(mCur);
-            }
-            return null;
-        }
-
-        @Override
-        public String toString() {
-            StringBuffer sb = new StringBuffer();
-            for (int i = 0; i < mTaskHistory.size(); ++i) {
-                final TaskRecord task = mTaskHistory.get(i);
-                sb.append("task_").append(i).append("-").append(task.mActivities).append(" ");
-            }
-            return sb.toString();
-        }
-    }
 }
diff --git a/services/java/com/android/server/am/TaskRecord.java b/services/java/com/android/server/am/TaskRecord.java
index 347aa7d..f9b0d4c 100644
--- a/services/java/com/android/server/am/TaskRecord.java
+++ b/services/java/com/android/server/am/TaskRecord.java
@@ -134,7 +134,21 @@
             // Was not previously in list.
             numFullscreen++;
         }
-        mActivities.add(r);
+        // TODO: This only matters to achieve identical results as mHistory. Later we won't need
+        // to skip over finishing activities.
+        int i;
+        for (i = mActivities.size() - 1; i >= 0; --i) {
+            if (!mActivities.get(i).finishing) {
+                break;
+            }
+        }
+        if (i >= 0) {
+            // Add below finishing activities.
+            mActivities.add(i + 1, r);
+        } else {
+            // All activities are finishing, add to top.
+            mActivities.add(r);
+        }
     }
 
     /** @return true if this was the last activity in the task */
diff --git a/services/java/com/android/server/net/LockdownVpnTracker.java b/services/java/com/android/server/net/LockdownVpnTracker.java
index f32dd09..5b6e485 100644
--- a/services/java/com/android/server/net/LockdownVpnTracker.java
+++ b/services/java/com/android/server/net/LockdownVpnTracker.java
@@ -56,7 +56,9 @@
     private static final int MAX_ERROR_COUNT = 4;
 
     private static final String ACTION_LOCKDOWN_RESET = "com.android.server.action.LOCKDOWN_RESET";
+
     private static final String ACTION_VPN_SETTINGS = "android.net.vpn.SETTINGS";
+    private static final String EXTRA_PICK_LOCKDOWN = "android.net.vpn.PICK_LOCKDOWN";
 
     private final Context mContext;
     private final INetworkManagementService mNetService;
@@ -66,7 +68,8 @@
 
     private final Object mStateLock = new Object();
 
-    private PendingIntent mResetIntent;
+    private final PendingIntent mConfigIntent;
+    private final PendingIntent mResetIntent;
 
     private String mAcceptedEgressIface;
     private String mAcceptedIface;
@@ -86,6 +89,10 @@
         mVpn = Preconditions.checkNotNull(vpn);
         mProfile = Preconditions.checkNotNull(profile);
 
+        final Intent configIntent = new Intent(ACTION_VPN_SETTINGS);
+        configIntent.putExtra(EXTRA_PICK_LOCKDOWN, true);
+        mConfigIntent = PendingIntent.getActivity(mContext, 0, configIntent, 0);
+
         final Intent resetIntent = new Intent(ACTION_LOCKDOWN_RESET);
         resetIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
         mResetIntent = PendingIntent.getBroadcast(mContext, 0, resetIntent, 0);
@@ -193,6 +200,7 @@
             // TODO: support non-standard port numbers
             mNetService.setFirewallEgressDestRule(mProfile.server, 500, true);
             mNetService.setFirewallEgressDestRule(mProfile.server, 4500, true);
+            mNetService.setFirewallEgressDestRule(mProfile.server, 1701, true);
         } catch (RemoteException e) {
             throw new RuntimeException("Problem setting firewall rules", e);
         }
@@ -218,6 +226,7 @@
         try {
             mNetService.setFirewallEgressDestRule(mProfile.server, 500, false);
             mNetService.setFirewallEgressDestRule(mProfile.server, 4500, false);
+            mNetService.setFirewallEgressDestRule(mProfile.server, 1701, false);
         } catch (RemoteException e) {
             throw new RuntimeException("Problem setting firewall rules", e);
         }
@@ -281,10 +290,13 @@
         builder.setWhen(0);
         builder.setSmallIcon(iconRes);
         builder.setContentTitle(mContext.getString(titleRes));
-        builder.setContentText(mContext.getString(R.string.vpn_lockdown_reset));
-        builder.setContentIntent(mResetIntent);
+        builder.setContentText(mContext.getString(R.string.vpn_lockdown_config));
+        builder.setContentIntent(mConfigIntent);
         builder.setPriority(Notification.PRIORITY_LOW);
         builder.setOngoing(true);
+        builder.addAction(
+                R.drawable.ic_menu_refresh, mContext.getString(R.string.reset), mResetIntent);
+
         NotificationManager.from(mContext).notify(TAG, 0, builder.build());
     }
 
diff --git a/services/java/com/android/server/wifi/WifiNotificationController.java b/services/java/com/android/server/wifi/WifiNotificationController.java
new file mode 100644
index 0000000..17ef7c8
--- /dev/null
+++ b/services/java/com/android/server/wifi/WifiNotificationController.java
@@ -0,0 +1,292 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.wifi;
+
+import android.app.Notification;
+import android.app.NotificationManager;
+import android.app.TaskStackBuilder;
+import android.content.BroadcastReceiver;
+import android.content.ContentResolver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.database.ContentObserver;
+import android.net.NetworkInfo;
+import android.net.wifi.ScanResult;
+import android.net.wifi.WifiManager;
+import android.net.wifi.WifiStateMachine;
+import android.os.Handler;
+import android.os.Message;
+import android.os.UserHandle;
+import android.provider.Settings;
+
+import java.io.FileDescriptor;
+import java.io.PrintWriter;
+import java.util.List;
+
+/* Takes care of handling the "open wi-fi network available" notification @hide */
+final class WifiNotificationController {
+    /**
+     * The icon to show in the 'available networks' notification. This will also
+     * be the ID of the Notification given to the NotificationManager.
+     */
+    private static final int ICON_NETWORKS_AVAILABLE =
+            com.android.internal.R.drawable.stat_notify_wifi_in_range;
+    /**
+     * When a notification is shown, we wait this amount before possibly showing it again.
+     */
+    private final long NOTIFICATION_REPEAT_DELAY_MS;
+    /**
+     * Whether the user has set the setting to show the 'available networks' notification.
+     */
+    private boolean mNotificationEnabled;
+    /**
+     * Observes the user setting to keep {@link #mNotificationEnabled} in sync.
+     */
+    private NotificationEnabledSettingObserver mNotificationEnabledSettingObserver;
+    /**
+     * The {@link System#currentTimeMillis()} must be at least this value for us
+     * to show the notification again.
+     */
+    private long mNotificationRepeatTime;
+    /**
+     * The Notification object given to the NotificationManager.
+     */
+    private Notification mNotification;
+    /**
+     * Whether the notification is being shown, as set by us. That is, if the
+     * user cancels the notification, we will not receive the callback so this
+     * will still be true. We only guarantee if this is false, then the
+     * notification is not showing.
+     */
+    private boolean mNotificationShown;
+    /**
+     * The number of continuous scans that must occur before consider the
+     * supplicant in a scanning state. This allows supplicant to associate with
+     * remembered networks that are in the scan results.
+     */
+    private static final int NUM_SCANS_BEFORE_ACTUALLY_SCANNING = 3;
+    /**
+     * The number of scans since the last network state change. When this
+     * exceeds {@link #NUM_SCANS_BEFORE_ACTUALLY_SCANNING}, we consider the
+     * supplicant to actually be scanning. When the network state changes to
+     * something other than scanning, we reset this to 0.
+     */
+    private int mNumScansSinceNetworkStateChange;
+
+    private final Context mContext;
+    private final WifiStateMachine mWifiStateMachine;
+    private NetworkInfo mNetworkInfo;
+
+    WifiNotificationController(Context context, WifiStateMachine wsm) {
+        mContext = context;
+        mWifiStateMachine = wsm;
+
+        IntentFilter filter = new IntentFilter();
+        filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
+        filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
+        filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
+
+        mContext.registerReceiver(
+                new BroadcastReceiver() {
+                    @Override
+                    public void onReceive(Context context, Intent intent) {
+                        if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
+                            resetNotification();
+                        } else if (intent.getAction().equals(
+                                WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
+                            mNetworkInfo = (NetworkInfo) intent.getParcelableExtra(
+                                    WifiManager.EXTRA_NETWORK_INFO);
+                            // reset & clear notification on a network connect & disconnect
+                            switch(mNetworkInfo.getDetailedState()) {
+                                case CONNECTED:
+                                case DISCONNECTED:
+                                case CAPTIVE_PORTAL_CHECK:
+                                    resetNotification();
+                                    break;
+                            }
+                        } else if (intent.getAction().equals(
+                                WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
+                            checkAndSetNotification(mNetworkInfo,
+                                    mWifiStateMachine.syncGetScanResultsList());
+                        }
+                    }
+                }, filter);
+
+        // Setting is in seconds
+        NOTIFICATION_REPEAT_DELAY_MS = Settings.Global.getInt(context.getContentResolver(),
+                Settings.Global.WIFI_NETWORKS_AVAILABLE_REPEAT_DELAY, 900) * 1000l;
+        mNotificationEnabledSettingObserver = new NotificationEnabledSettingObserver(new Handler());
+        mNotificationEnabledSettingObserver.register();
+    }
+
+    private synchronized void checkAndSetNotification(NetworkInfo networkInfo,
+            List<ScanResult> scanResults) {
+        // TODO: unregister broadcast so we do not have to check here
+        // If we shouldn't place a notification on available networks, then
+        // don't bother doing any of the following
+        if (!mNotificationEnabled) return;
+        if (networkInfo == null) return;
+
+        NetworkInfo.State state = networkInfo.getState();
+        if ((state == NetworkInfo.State.DISCONNECTED)
+                || (state == NetworkInfo.State.UNKNOWN)) {
+            if (scanResults != null) {
+                int numOpenNetworks = 0;
+                for (int i = scanResults.size() - 1; i >= 0; i--) {
+                    ScanResult scanResult = scanResults.get(i);
+
+                    //A capability of [ESS] represents an open access point
+                    //that is available for an STA to connect
+                    if (scanResult.capabilities != null &&
+                            scanResult.capabilities.equals("[ESS]")) {
+                        numOpenNetworks++;
+                    }
+                }
+
+                if (numOpenNetworks > 0) {
+                    if (++mNumScansSinceNetworkStateChange >= NUM_SCANS_BEFORE_ACTUALLY_SCANNING) {
+                        /*
+                         * We've scanned continuously at least
+                         * NUM_SCANS_BEFORE_NOTIFICATION times. The user
+                         * probably does not have a remembered network in range,
+                         * since otherwise supplicant would have tried to
+                         * associate and thus resetting this counter.
+                         */
+                        setNotificationVisible(true, numOpenNetworks, false, 0);
+                    }
+                    return;
+                }
+            }
+        }
+
+        // No open networks in range, remove the notification
+        setNotificationVisible(false, 0, false, 0);
+    }
+
+    /**
+     * Clears variables related to tracking whether a notification has been
+     * shown recently and clears the current notification.
+     */
+    private synchronized void resetNotification() {
+        mNotificationRepeatTime = 0;
+        mNumScansSinceNetworkStateChange = 0;
+        setNotificationVisible(false, 0, false, 0);
+    }
+
+    /**
+     * Display or don't display a notification that there are open Wi-Fi networks.
+     * @param visible {@code true} if notification should be visible, {@code false} otherwise
+     * @param numNetworks the number networks seen
+     * @param force {@code true} to force notification to be shown/not-shown,
+     * even if it is already shown/not-shown.
+     * @param delay time in milliseconds after which the notification should be made
+     * visible or invisible.
+     */
+    private void setNotificationVisible(boolean visible, int numNetworks, boolean force,
+            int delay) {
+
+        // Since we use auto cancel on the notification, when the
+        // mNetworksAvailableNotificationShown is true, the notification may
+        // have actually been canceled.  However, when it is false we know
+        // for sure that it is not being shown (it will not be shown any other
+        // place than here)
+
+        // If it should be hidden and it is already hidden, then noop
+        if (!visible && !mNotificationShown && !force) {
+            return;
+        }
+
+        NotificationManager notificationManager = (NotificationManager) mContext
+                .getSystemService(Context.NOTIFICATION_SERVICE);
+
+        Message message;
+        if (visible) {
+
+            // Not enough time has passed to show the notification again
+            if (System.currentTimeMillis() < mNotificationRepeatTime) {
+                return;
+            }
+
+            if (mNotification == null) {
+                // Cache the Notification object.
+                mNotification = new Notification();
+                mNotification.when = 0;
+                mNotification.icon = ICON_NETWORKS_AVAILABLE;
+                mNotification.flags = Notification.FLAG_AUTO_CANCEL;
+                mNotification.contentIntent = TaskStackBuilder.create(mContext)
+                        .addNextIntentWithParentStack(
+                                new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK))
+                        .getPendingIntent(0, 0, null, UserHandle.CURRENT);
+            }
+
+            CharSequence title = mContext.getResources().getQuantityText(
+                    com.android.internal.R.plurals.wifi_available, numNetworks);
+            CharSequence details = mContext.getResources().getQuantityText(
+                    com.android.internal.R.plurals.wifi_available_detailed, numNetworks);
+            mNotification.tickerText = title;
+            mNotification.setLatestEventInfo(mContext, title, details, mNotification.contentIntent);
+
+            mNotificationRepeatTime = System.currentTimeMillis() + NOTIFICATION_REPEAT_DELAY_MS;
+
+            notificationManager.notifyAsUser(null, ICON_NETWORKS_AVAILABLE, mNotification,
+                    UserHandle.ALL);
+        } else {
+            notificationManager.cancelAsUser(null, ICON_NETWORKS_AVAILABLE, UserHandle.ALL);
+        }
+
+        mNotificationShown = visible;
+    }
+
+    void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
+        pw.println("mNotificationEnabled " + mNotificationEnabled);
+        pw.println("mNotificationRepeatTime " + mNotificationRepeatTime);
+        pw.println("mNotificationShown " + mNotificationShown);
+        pw.println("mNumScansSinceNetworkStateChange " + mNumScansSinceNetworkStateChange);
+    }
+
+    private class NotificationEnabledSettingObserver extends ContentObserver {
+        public NotificationEnabledSettingObserver(Handler handler) {
+            super(handler);
+        }
+
+        public void register() {
+            ContentResolver cr = mContext.getContentResolver();
+            cr.registerContentObserver(Settings.Global.getUriFor(
+                    Settings.Global.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON), true, this);
+            synchronized (WifiNotificationController.this) {
+                mNotificationEnabled = getValue();
+            }
+        }
+
+        @Override
+        public void onChange(boolean selfChange) {
+            super.onChange(selfChange);
+
+            synchronized (WifiNotificationController.this) {
+                mNotificationEnabled = getValue();
+                resetNotification();
+            }
+        }
+
+        private boolean getValue() {
+            return Settings.Global.getInt(mContext.getContentResolver(),
+                    Settings.Global.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 1) == 1;
+        }
+    }
+
+}
diff --git a/services/java/com/android/server/WifiService.java b/services/java/com/android/server/wifi/WifiService.java
similarity index 70%
rename from services/java/com/android/server/WifiService.java
rename to services/java/com/android/server/wifi/WifiService.java
index ad6eb4d..3c14e3d 100644
--- a/services/java/com/android/server/WifiService.java
+++ b/services/java/com/android/server/wifi/WifiService.java
@@ -14,45 +14,33 @@
  * limitations under the License.
  */
 
-package com.android.server;
+package com.android.server.wifi;
 
 import android.app.ActivityManager;
 import android.app.AlarmManager;
 import android.app.AppOpsManager;
-import android.app.Notification;
-import android.app.NotificationManager;
 import android.app.PendingIntent;
-import android.app.TaskStackBuilder;
 import android.bluetooth.BluetoothAdapter;
 import android.content.BroadcastReceiver;
-import android.content.ContentResolver;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.content.pm.PackageManager;
-import android.database.ContentObserver;
 import android.net.wifi.IWifiManager;
 import android.net.wifi.ScanResult;
-import android.net.wifi.SupplicantState;
 import android.net.wifi.WifiInfo;
 import android.net.wifi.WifiManager;
 import android.net.wifi.WifiStateMachine;
 import android.net.wifi.WifiConfiguration;
 import android.net.wifi.WifiWatchdogStateMachine;
-import android.net.wifi.WifiConfiguration.KeyMgmt;
-import android.net.wifi.WpsInfo;
-import android.net.wifi.WpsResult;
 import android.net.ConnectivityManager;
 import android.net.DhcpInfo;
 import android.net.DhcpResults;
 import android.net.LinkAddress;
-import android.net.LinkProperties;
 import android.net.NetworkInfo;
-import android.net.NetworkInfo.State;
 import android.net.NetworkInfo.DetailedState;
 import android.net.NetworkUtils;
 import android.net.RouteInfo;
-import android.net.TrafficStats;
 import android.os.Binder;
 import android.os.Handler;
 import android.os.Messenger;
@@ -61,12 +49,10 @@
 import android.os.INetworkManagementService;
 import android.os.Message;
 import android.os.RemoteException;
-import android.os.ServiceManager;
 import android.os.SystemProperties;
 import android.os.UserHandle;
 import android.os.WorkSource;
 import android.provider.Settings;
-import android.text.TextUtils;
 import android.util.Log;
 import android.util.Slog;
 
@@ -75,11 +61,7 @@
 import java.net.InetAddress;
 import java.net.Inet4Address;
 import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Set;
-import java.util.concurrent.atomic.AtomicInteger;
-import java.util.concurrent.atomic.AtomicBoolean;
 
 import com.android.internal.app.IBatteryStats;
 import com.android.internal.telephony.TelephonyIntents;
@@ -97,13 +79,13 @@
 // as a SM to track soft AP/client/adhoc bring up based
 // on device idle state, airplane mode and boot.
 
-public class WifiService extends IWifiManager.Stub {
+public final class WifiService extends IWifiManager.Stub {
     private static final String TAG = "WifiService";
     private static final boolean DBG = false;
 
     private final WifiStateMachine mWifiStateMachine;
 
-    private Context mContext;
+    private final Context mContext;
 
     private AlarmManager mAlarmManager;
     private PendingIntent mIdleIntent;
@@ -130,19 +112,14 @@
     private final IBatteryStats mBatteryStats;
     private final AppOpsManager mAppOps;
 
-    private boolean mEnableTrafficStatsPoll = false;
-    private int mTrafficStatsPollToken = 0;
-    private long mTxPkts;
-    private long mRxPkts;
-    /* Tracks last reported data activity */
-    private int mDataActivity;
     private String mInterfaceName;
 
-    /**
-     * Interval in milliseconds between polling for traffic
-     * statistics
-     */
-    private static final int POLL_TRAFFIC_STATS_INTERVAL_MSECS = 1000;
+    /* Tracks the open wi-fi network notification */
+    private WifiNotificationController mNotificationController;
+    /* Polls traffic stats and notifies clients */
+    private WifiTrafficPoller mTrafficPoller;
+    /* Tracks the persisted states for wi-fi & airplane mode */
+    private WifiSettingsStore mSettingsStore;
 
     /**
      * See {@link Settings.Global#WIFI_IDLE_MS}. This is the default value if a
@@ -156,78 +133,14 @@
     private static final String ACTION_DEVICE_IDLE =
             "com.android.server.WifiManager.action.DEVICE_IDLE";
 
-    private static final int WIFI_DISABLED                  = 0;
-    private static final int WIFI_ENABLED                   = 1;
-    /* Wifi enabled while in airplane mode */
-    private static final int WIFI_ENABLED_AIRPLANE_OVERRIDE = 2;
-    /* Wifi disabled due to airplane mode on */
-    private static final int WIFI_DISABLED_AIRPLANE_ON      = 3;
-
-    /* Persisted state that tracks the wifi & airplane interaction from settings */
-    private AtomicInteger mPersistWifiState = new AtomicInteger(WIFI_DISABLED);
-    /* Tracks current airplane mode state */
-    private AtomicBoolean mAirplaneModeOn = new AtomicBoolean(false);
-    /* Tracks whether wifi is enabled from WifiStateMachine's perspective */
-    private boolean mWifiEnabled;
-
     /* The work source (UID) that triggered the current WIFI scan, synchronized
      * on this */
     private WorkSource mScanWorkSource;
 
     private boolean mIsReceiverRegistered = false;
 
-
     NetworkInfo mNetworkInfo = new NetworkInfo(ConnectivityManager.TYPE_WIFI, 0, "WIFI", "");
 
-    // Variables relating to the 'available networks' notification
-    /**
-     * The icon to show in the 'available networks' notification. This will also
-     * be the ID of the Notification given to the NotificationManager.
-     */
-    private static final int ICON_NETWORKS_AVAILABLE =
-            com.android.internal.R.drawable.stat_notify_wifi_in_range;
-    /**
-     * When a notification is shown, we wait this amount before possibly showing it again.
-     */
-    private final long NOTIFICATION_REPEAT_DELAY_MS;
-    /**
-     * Whether the user has set the setting to show the 'available networks' notification.
-     */
-    private boolean mNotificationEnabled;
-    /**
-     * Observes the user setting to keep {@link #mNotificationEnabled} in sync.
-     */
-    private NotificationEnabledSettingObserver mNotificationEnabledSettingObserver;
-    /**
-     * The {@link System#currentTimeMillis()} must be at least this value for us
-     * to show the notification again.
-     */
-    private long mNotificationRepeatTime;
-    /**
-     * The Notification object given to the NotificationManager.
-     */
-    private Notification mNotification;
-    /**
-     * Whether the notification is being shown, as set by us. That is, if the
-     * user cancels the notification, we will not receive the callback so this
-     * will still be true. We only guarantee if this is false, then the
-     * notification is not showing.
-     */
-    private boolean mNotificationShown;
-    /**
-     * The number of continuous scans that must occur before consider the
-     * supplicant in a scanning state. This allows supplicant to associate with
-     * remembered networks that are in the scan results.
-     */
-    private static final int NUM_SCANS_BEFORE_ACTUALLY_SCANNING = 3;
-    /**
-     * The number of scans since the last network state change. When this
-     * exceeds {@link #NUM_SCANS_BEFORE_ACTUALLY_SCANNING}, we consider the
-     * supplicant to actually be scanning. When the network state changes to
-     * something other than scanning, we reset this to 0.
-     */
-    private int mNumScansSinceNetworkStateChange;
-
     /**
      * Asynchronous channel to WifiStateMachine
      */
@@ -241,9 +154,9 @@
     /**
      * Handles client connections
      */
-    private class AsyncServiceHandler extends Handler {
+    private class ClientHandler extends Handler {
 
-        AsyncServiceHandler(android.os.Looper looper) {
+        ClientHandler(android.os.Looper looper) {
             super(looper);
         }
 
@@ -273,48 +186,13 @@
                     ac.connect(mContext, this, msg.replyTo);
                     break;
                 }
-                case WifiManager.ENABLE_TRAFFIC_STATS_POLL: {
-                    mEnableTrafficStatsPoll = (msg.arg1 == 1);
-                    mTrafficStatsPollToken++;
-                    if (mEnableTrafficStatsPoll) {
-                        notifyOnDataActivity();
-                        sendMessageDelayed(Message.obtain(this, WifiManager.TRAFFIC_STATS_POLL,
-                                mTrafficStatsPollToken, 0), POLL_TRAFFIC_STATS_INTERVAL_MSECS);
-                    }
-                    break;
-                }
-                case WifiManager.TRAFFIC_STATS_POLL: {
-                    if (msg.arg1 == mTrafficStatsPollToken) {
-                        notifyOnDataActivity();
-                        sendMessageDelayed(Message.obtain(this, WifiManager.TRAFFIC_STATS_POLL,
-                                mTrafficStatsPollToken, 0), POLL_TRAFFIC_STATS_INTERVAL_MSECS);
-                    }
-                    break;
-                }
-                case WifiManager.CONNECT_NETWORK: {
-                    mWifiStateMachine.sendMessage(Message.obtain(msg));
-                    break;
-                }
-                case WifiManager.SAVE_NETWORK: {
-                    mWifiStateMachine.sendMessage(Message.obtain(msg));
-                    break;
-                }
-                case WifiManager.FORGET_NETWORK: {
-                    mWifiStateMachine.sendMessage(Message.obtain(msg));
-                    break;
-                }
-                case WifiManager.START_WPS: {
-                    mWifiStateMachine.sendMessage(Message.obtain(msg));
-                    break;
-                }
-                case WifiManager.CANCEL_WPS: {
-                    mWifiStateMachine.sendMessage(Message.obtain(msg));
-                    break;
-                }
-                case WifiManager.DISABLE_NETWORK: {
-                    mWifiStateMachine.sendMessage(Message.obtain(msg));
-                    break;
-                }
+                /* Client commands are forwarded to state machine */
+                case WifiManager.CONNECT_NETWORK:
+                case WifiManager.SAVE_NETWORK:
+                case WifiManager.FORGET_NETWORK:
+                case WifiManager.START_WPS:
+                case WifiManager.CANCEL_WPS:
+                case WifiManager.DISABLE_NETWORK:
                 case WifiManager.RSSI_PKTCNT_FETCH: {
                     mWifiStateMachine.sendMessage(Message.obtain(msg));
                     break;
@@ -326,7 +204,7 @@
             }
         }
     }
-    private AsyncServiceHandler mAsyncServiceHandler;
+    private ClientHandler mClientHandler;
 
     /**
      * Handles interaction with WifiStateMachine
@@ -375,7 +253,7 @@
     private final WorkSource mTmpWorkSource = new WorkSource();
     private WifiWatchdogStateMachine mWifiWatchdogStateMachine;
 
-    WifiService(Context context) {
+    public WifiService(Context context) {
         mContext = context;
 
         mInterfaceName =  SystemProperties.get("wifi.interface", "wlan0");
@@ -389,65 +267,36 @@
         Intent idleIntent = new Intent(ACTION_DEVICE_IDLE, null);
         mIdleIntent = PendingIntent.getBroadcast(mContext, IDLE_REQUEST, idleIntent, 0);
 
+        mNotificationController = new WifiNotificationController(mContext, mWifiStateMachine);
+        mTrafficPoller = new WifiTrafficPoller(mContext, mClients, mInterfaceName);
+        mSettingsStore = new WifiSettingsStore(mContext);
+
         mContext.registerReceiver(
                 new BroadcastReceiver() {
                     @Override
                     public void onReceive(Context context, Intent intent) {
-                        mAirplaneModeOn.set(isAirplaneModeOn());
-                        handleAirplaneModeToggled(mAirplaneModeOn.get());
-                        updateWifiState();
+                        if (mSettingsStore.handleAirplaneModeToggled()) {
+                            updateWifiState();
+                        }
                     }
                 },
                 new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED));
 
-        IntentFilter filter = new IntentFilter();
-        filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
-        filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
-        filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
-
         mContext.registerReceiver(
                 new BroadcastReceiver() {
                     @Override
                     public void onReceive(Context context, Intent intent) {
-                        if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
-                            int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
-                                    WifiManager.WIFI_STATE_DISABLED);
-
-                            mWifiEnabled = (wifiState == WifiManager.WIFI_STATE_ENABLED);
-
-                           // reset & clear notification on any wifi state change
-                            resetNotification();
-                        } else if (intent.getAction().equals(
-                                WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
-                            mNetworkInfo = (NetworkInfo) intent.getParcelableExtra(
-                                    WifiManager.EXTRA_NETWORK_INFO);
-                            // reset & clear notification on a network connect & disconnect
-                            switch(mNetworkInfo.getDetailedState()) {
-                                case CONNECTED:
-                                case DISCONNECTED:
-                                case CAPTIVE_PORTAL_CHECK:
-                                    evaluateTrafficStatsPolling();
-                                    resetNotification();
-                                    break;
-                            }
-                        } else if (intent.getAction().equals(
+                        if (intent.getAction().equals(
                                 WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
                             noteScanEnd();
-                            checkAndSetNotification();
                         }
                     }
-                }, filter);
+                }, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
 
         HandlerThread wifiThread = new HandlerThread("WifiService");
         wifiThread.start();
-        mAsyncServiceHandler = new AsyncServiceHandler(wifiThread.getLooper());
+        mClientHandler = new ClientHandler(wifiThread.getLooper());
         mWifiStateMachineHandler = new WifiStateMachineHandler(wifiThread.getLooper());
-
-        // Setting is in seconds
-        NOTIFICATION_REPEAT_DELAY_MS = Settings.Global.getInt(context.getContentResolver(),
-                Settings.Global.WIFI_NETWORKS_AVAILABLE_REPEAT_DELAY, 900) * 1000l;
-        mNotificationEnabledSettingObserver = new NotificationEnabledSettingObserver(new Handler());
-        mNotificationEnabledSettingObserver.register();
     }
 
     /** Tell battery stats about a new WIFI scan */
@@ -495,10 +344,8 @@
      * This function is used only at boot time
      */
     public void checkAndStartWifi() {
-        mAirplaneModeOn.set(isAirplaneModeOn());
-        mPersistWifiState.set(getPersistedWifiState());
-        /* Start if Wi-Fi should be enabled or the saved state indicates Wi-Fi was on */
-        boolean wifiEnabled = shouldWifiBeEnabled() || testAndClearWifiSavedState();
+        /* Check if wi-fi needs to be enabled */
+        boolean wifiEnabled = mSettingsStore.shouldWifiBeEnabled();
         Slog.i(TAG, "WifiService starting up with Wi-Fi " +
                 (wifiEnabled ? "enabled" : "disabled"));
 
@@ -511,75 +358,6 @@
 
     }
 
-    private boolean testAndClearWifiSavedState() {
-        final ContentResolver cr = mContext.getContentResolver();
-        int wifiSavedState = 0;
-        try {
-            wifiSavedState = Settings.Global.getInt(cr, Settings.Global.WIFI_SAVED_STATE);
-            if(wifiSavedState == 1)
-                Settings.Global.putInt(cr, Settings.Global.WIFI_SAVED_STATE, 0);
-        } catch (Settings.SettingNotFoundException e) {
-            ;
-        }
-        return (wifiSavedState == 1);
-    }
-
-    private int getPersistedWifiState() {
-        final ContentResolver cr = mContext.getContentResolver();
-        try {
-            return Settings.Global.getInt(cr, Settings.Global.WIFI_ON);
-        } catch (Settings.SettingNotFoundException e) {
-            Settings.Global.putInt(cr, Settings.Global.WIFI_ON, WIFI_DISABLED);
-            return WIFI_DISABLED;
-        }
-    }
-
-    private boolean shouldWifiBeEnabled() {
-        if (mAirplaneModeOn.get()) {
-            return mPersistWifiState.get() == WIFI_ENABLED_AIRPLANE_OVERRIDE;
-        } else {
-            return mPersistWifiState.get() != WIFI_DISABLED;
-        }
-    }
-
-    private void handleWifiToggled(boolean wifiEnabled) {
-        boolean airplaneEnabled = mAirplaneModeOn.get() && isAirplaneToggleable();
-        if (wifiEnabled) {
-            if (airplaneEnabled) {
-                persistWifiState(WIFI_ENABLED_AIRPLANE_OVERRIDE);
-            } else {
-                persistWifiState(WIFI_ENABLED);
-            }
-        } else {
-            // When wifi state is disabled, we do not care
-            // if airplane mode is on or not. The scenario of
-            // wifi being disabled due to airplane mode being turned on
-            // is handled handleAirplaneModeToggled()
-            persistWifiState(WIFI_DISABLED);
-        }
-    }
-
-    private void handleAirplaneModeToggled(boolean airplaneEnabled) {
-        if (airplaneEnabled) {
-            // Wifi disabled due to airplane on
-            if (mWifiEnabled) {
-                persistWifiState(WIFI_DISABLED_AIRPLANE_ON);
-            }
-        } else {
-            /* On airplane mode disable, restore wifi state if necessary */
-            if (testAndClearWifiSavedState() ||
-                    mPersistWifiState.get() == WIFI_ENABLED_AIRPLANE_OVERRIDE) {
-                persistWifiState(WIFI_ENABLED);
-            }
-        }
-    }
-
-    private void persistWifiState(int state) {
-        final ContentResolver cr = mContext.getContentResolver();
-        mPersistWifiState.set(state);
-        Settings.Global.putInt(cr, Settings.Global.WIFI_ON, state);
-    }
-
     /**
      * see {@link android.net.wifi.WifiManager#pingSupplicant()}
      * @return {@code true} if the operation succeeds, {@code false} otherwise
@@ -640,24 +418,28 @@
             Slog.e(TAG, "Invoking mWifiStateMachine.setWifiEnabled\n");
         }
 
-        if (enable) {
-            reportStartWorkSource();
-        }
-        mWifiStateMachine.setWifiEnabled(enable);
-
         /*
-         * Caller might not have WRITE_SECURE_SETTINGS,
-         * only CHANGE_WIFI_STATE is enforced
-         */
+        * Caller might not have WRITE_SECURE_SETTINGS,
+        * only CHANGE_WIFI_STATE is enforced
+        */
 
         long ident = Binder.clearCallingIdentity();
         try {
-            handleWifiToggled(enable);
+            if (! mSettingsStore.handleWifiToggled(enable)) {
+                // Nothing to do if wifi cannot be toggled
+                return true;
+            }
         } finally {
             Binder.restoreCallingIdentity(ident);
         }
 
         if (enable) {
+            reportStartWorkSource();
+        }
+
+        mWifiStateMachine.setWifiEnabled(enable);
+
+        if (enable) {
             if (!mIsReceiverRegistered) {
                 registerForBroadcasts();
                 mIsReceiverRegistered = true;
@@ -1047,7 +829,7 @@
     public Messenger getWifiServiceMessenger() {
         enforceAccessPermission();
         enforceChangePermission();
-        return new Messenger(mAsyncServiceHandler);
+        return new Messenger(mClientHandler);
     }
 
     /** Get a reference to WifiStateMachine handler for AsyncChannel communication */
@@ -1082,14 +864,12 @@
                 }
                 mAlarmManager.cancel(mIdleIntent);
                 mScreenOff = false;
-                evaluateTrafficStatsPolling();
                 setDeviceIdleAndUpdateWifi(false);
             } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
                 if (DBG) {
                     Slog.d(TAG, "ACTION_SCREEN_OFF");
                 }
                 mScreenOff = true;
-                evaluateTrafficStatsPolling();
                 /*
                  * Set a timer to put Wi-Fi to sleep, but only if the screen is off
                  * AND the "stay on while plugged in" setting doesn't match the
@@ -1221,11 +1001,11 @@
         }
 
         /* Disable tethering when airplane mode is enabled */
-        if (mAirplaneModeOn.get()) {
+        if (mSettingsStore.isAirplaneModeOn()) {
             mWifiStateMachine.setWifiApEnabled(null, false);
         }
 
-        if (shouldWifiBeEnabled()) {
+        if (mSettingsStore.shouldWifiBeEnabled()) {
             if (wifiShouldBeStarted) {
                 reportStartWorkSource();
                 mWifiStateMachine.setWifiEnabled(true);
@@ -1253,30 +1033,6 @@
         mContext.registerReceiver(mReceiver, intentFilter);
     }
 
-    private boolean isAirplaneSensitive() {
-        String airplaneModeRadios = Settings.Global.getString(mContext.getContentResolver(),
-                Settings.Global.AIRPLANE_MODE_RADIOS);
-        return airplaneModeRadios == null
-            || airplaneModeRadios.contains(Settings.Global.RADIO_WIFI);
-    }
-
-    private boolean isAirplaneToggleable() {
-        String toggleableRadios = Settings.Global.getString(mContext.getContentResolver(),
-                Settings.Global.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
-        return toggleableRadios != null
-            && toggleableRadios.contains(Settings.Global.RADIO_WIFI);
-    }
-
-    /**
-     * Returns true if Wi-Fi is sensitive to airplane mode, and airplane mode is
-     * currently on.
-     * @return {@code true} if airplane mode is on.
-     */
-    private boolean isAirplaneModeOn() {
-        return isAirplaneSensitive() && Settings.Global.getInt(mContext.getContentResolver(),
-                Settings.Global.AIRPLANE_MODE_ON, 0) == 1;
-    }
-
     @Override
     protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
         if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
@@ -1296,18 +1052,9 @@
         pw.println("mEmergencyCallbackMode " + mEmergencyCallbackMode);
         pw.println("mMulticastEnabled " + mMulticastEnabled);
         pw.println("mMulticastDisabled " + mMulticastDisabled);
-        pw.println("mEnableTrafficStatsPoll " + mEnableTrafficStatsPoll);
-        pw.println("mTrafficStatsPollToken " + mTrafficStatsPollToken);
-        pw.println("mTxPkts " + mTxPkts);
-        pw.println("mRxPkts " + mRxPkts);
-        pw.println("mDataActivity " + mDataActivity);
-        pw.println("mPersistWifiState " + mPersistWifiState.get());
-        pw.println("mAirplaneModeOn " + mAirplaneModeOn.get());
-        pw.println("mWifiEnabled " + mWifiEnabled);
-        pw.println("mNotificationEnabled " + mNotificationEnabled);
-        pw.println("mNotificationRepeatTime " + mNotificationRepeatTime);
-        pw.println("mNotificationShown " + mNotificationShown);
-        pw.println("mNumScansSinceNetworkStateChange " + mNumScansSinceNetworkStateChange);
+        mSettingsStore.dump(fd, pw, args);
+        mNotificationController.dump(fd, pw, args);
+        mTrafficPoller.dump(fd, pw, args);
 
         pw.println("Latest scan results:");
         List<ScanResult> scanResults = mWifiStateMachine.syncGetScanResultsList();
@@ -1700,194 +1447,4 @@
             return (mMulticasters.size() > 0);
         }
     }
-
-    /**
-     * Evaluate if traffic stats polling is needed based on
-     * connection and screen on status
-     */
-    private void evaluateTrafficStatsPolling() {
-        Message msg;
-        if (mNetworkInfo.getDetailedState() == DetailedState.CONNECTED && !mScreenOff) {
-            msg = Message.obtain(mAsyncServiceHandler,
-                    WifiManager.ENABLE_TRAFFIC_STATS_POLL, 1, 0);
-        } else {
-            msg = Message.obtain(mAsyncServiceHandler,
-                    WifiManager.ENABLE_TRAFFIC_STATS_POLL, 0, 0);
-        }
-        msg.sendToTarget();
-    }
-
-    private void notifyOnDataActivity() {
-        long sent, received;
-        long preTxPkts = mTxPkts, preRxPkts = mRxPkts;
-        int dataActivity = WifiManager.DATA_ACTIVITY_NONE;
-
-        mTxPkts = TrafficStats.getTxPackets(mInterfaceName);
-        mRxPkts = TrafficStats.getRxPackets(mInterfaceName);
-
-        if (preTxPkts > 0 || preRxPkts > 0) {
-            sent = mTxPkts - preTxPkts;
-            received = mRxPkts - preRxPkts;
-            if (sent > 0) {
-                dataActivity |= WifiManager.DATA_ACTIVITY_OUT;
-            }
-            if (received > 0) {
-                dataActivity |= WifiManager.DATA_ACTIVITY_IN;
-            }
-
-            if (dataActivity != mDataActivity && !mScreenOff) {
-                mDataActivity = dataActivity;
-                for (AsyncChannel client : mClients) {
-                    client.sendMessage(WifiManager.DATA_ACTIVITY_NOTIFICATION, mDataActivity);
-                }
-            }
-        }
-    }
-
-
-    private void checkAndSetNotification() {
-        // If we shouldn't place a notification on available networks, then
-        // don't bother doing any of the following
-        if (!mNotificationEnabled) return;
-
-        State state = mNetworkInfo.getState();
-        if ((state == NetworkInfo.State.DISCONNECTED)
-                || (state == NetworkInfo.State.UNKNOWN)) {
-            // Look for an open network
-            List<ScanResult> scanResults = mWifiStateMachine.syncGetScanResultsList();
-            if (scanResults != null) {
-                int numOpenNetworks = 0;
-                for (int i = scanResults.size() - 1; i >= 0; i--) {
-                    ScanResult scanResult = scanResults.get(i);
-
-                    //A capability of [ESS] represents an open access point
-                    //that is available for an STA to connect
-                    if (scanResult.capabilities != null &&
-                            scanResult.capabilities.equals("[ESS]")) {
-                        numOpenNetworks++;
-                    }
-                }
-
-                if (numOpenNetworks > 0) {
-                    if (++mNumScansSinceNetworkStateChange >= NUM_SCANS_BEFORE_ACTUALLY_SCANNING) {
-                        /*
-                         * We've scanned continuously at least
-                         * NUM_SCANS_BEFORE_NOTIFICATION times. The user
-                         * probably does not have a remembered network in range,
-                         * since otherwise supplicant would have tried to
-                         * associate and thus resetting this counter.
-                         */
-                        setNotificationVisible(true, numOpenNetworks, false, 0);
-                    }
-                    return;
-                }
-            }
-        }
-
-        // No open networks in range, remove the notification
-        setNotificationVisible(false, 0, false, 0);
-    }
-
-    /**
-     * Clears variables related to tracking whether a notification has been
-     * shown recently and clears the current notification.
-     */
-    private void resetNotification() {
-        mNotificationRepeatTime = 0;
-        mNumScansSinceNetworkStateChange = 0;
-        setNotificationVisible(false, 0, false, 0);
-    }
-
-    /**
-     * Display or don't display a notification that there are open Wi-Fi networks.
-     * @param visible {@code true} if notification should be visible, {@code false} otherwise
-     * @param numNetworks the number networks seen
-     * @param force {@code true} to force notification to be shown/not-shown,
-     * even if it is already shown/not-shown.
-     * @param delay time in milliseconds after which the notification should be made
-     * visible or invisible.
-     */
-    private void setNotificationVisible(boolean visible, int numNetworks, boolean force,
-            int delay) {
-
-        // Since we use auto cancel on the notification, when the
-        // mNetworksAvailableNotificationShown is true, the notification may
-        // have actually been canceled.  However, when it is false we know
-        // for sure that it is not being shown (it will not be shown any other
-        // place than here)
-
-        // If it should be hidden and it is already hidden, then noop
-        if (!visible && !mNotificationShown && !force) {
-            return;
-        }
-
-        NotificationManager notificationManager = (NotificationManager) mContext
-                .getSystemService(Context.NOTIFICATION_SERVICE);
-
-        Message message;
-        if (visible) {
-
-            // Not enough time has passed to show the notification again
-            if (System.currentTimeMillis() < mNotificationRepeatTime) {
-                return;
-            }
-
-            if (mNotification == null) {
-                // Cache the Notification object.
-                mNotification = new Notification();
-                mNotification.when = 0;
-                mNotification.icon = ICON_NETWORKS_AVAILABLE;
-                mNotification.flags = Notification.FLAG_AUTO_CANCEL;
-                mNotification.contentIntent = TaskStackBuilder.create(mContext)
-                        .addNextIntentWithParentStack(
-                                new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK))
-                        .getPendingIntent(0, 0, null, UserHandle.CURRENT);
-            }
-
-            CharSequence title = mContext.getResources().getQuantityText(
-                    com.android.internal.R.plurals.wifi_available, numNetworks);
-            CharSequence details = mContext.getResources().getQuantityText(
-                    com.android.internal.R.plurals.wifi_available_detailed, numNetworks);
-            mNotification.tickerText = title;
-            mNotification.setLatestEventInfo(mContext, title, details, mNotification.contentIntent);
-
-            mNotificationRepeatTime = System.currentTimeMillis() + NOTIFICATION_REPEAT_DELAY_MS;
-
-            notificationManager.notifyAsUser(null, ICON_NETWORKS_AVAILABLE, mNotification,
-                    UserHandle.ALL);
-        } else {
-            notificationManager.cancelAsUser(null, ICON_NETWORKS_AVAILABLE, UserHandle.ALL);
-        }
-
-        mNotificationShown = visible;
-    }
-
-    private class NotificationEnabledSettingObserver extends ContentObserver {
-
-        public NotificationEnabledSettingObserver(Handler handler) {
-            super(handler);
-        }
-
-        public void register() {
-            ContentResolver cr = mContext.getContentResolver();
-            cr.registerContentObserver(Settings.Global.getUriFor(
-                Settings.Global.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON), true, this);
-            mNotificationEnabled = getValue();
-        }
-
-        @Override
-        public void onChange(boolean selfChange) {
-            super.onChange(selfChange);
-
-            mNotificationEnabled = getValue();
-            resetNotification();
-        }
-
-        private boolean getValue() {
-            return Settings.Global.getInt(mContext.getContentResolver(),
-                    Settings.Global.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 1) == 1;
-        }
-    }
-
-
 }
diff --git a/services/java/com/android/server/wifi/WifiSettingsStore.java b/services/java/com/android/server/wifi/WifiSettingsStore.java
new file mode 100644
index 0000000..d7c8752
--- /dev/null
+++ b/services/java/com/android/server/wifi/WifiSettingsStore.java
@@ -0,0 +1,178 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.wifi;
+
+import android.content.ContentResolver;
+import android.content.Context;
+import android.provider.Settings;
+
+import java.io.FileDescriptor;
+import java.io.PrintWriter;
+
+/* Tracks persisted settings for Wi-Fi and airplane mode interaction */
+final class WifiSettingsStore {
+    /* Values tracked in Settings.Global.WIFI_ON */
+    private static final int WIFI_DISABLED                      = 0;
+    private static final int WIFI_ENABLED                       = 1;
+    /* Wifi enabled while in airplane mode */
+    private static final int WIFI_ENABLED_AIRPLANE_OVERRIDE     = 2;
+    /* Wifi disabled due to airplane mode on */
+    private static final int WIFI_DISABLED_AIRPLANE_ON          = 3;
+
+    /* Persisted state that tracks the wifi & airplane interaction from settings */
+    private int mPersistWifiState = WIFI_DISABLED;
+    /* Tracks current airplane mode state */
+    private boolean mAirplaneModeOn = false;
+    /* Tracks whether wifi is enabled from WifiStateMachine's perspective */
+    private final Context mContext;
+
+    /* Tracks if we have checked the saved wi-fi state after boot */
+    private boolean mCheckSavedStateAtBoot = false;
+
+    WifiSettingsStore(Context context) {
+        mContext = context;
+        mAirplaneModeOn = getPersistedAirplaneModeOn();
+        mPersistWifiState = getPersistedWifiState();
+    }
+
+    synchronized boolean shouldWifiBeEnabled() {
+        if (!mCheckSavedStateAtBoot) {
+            mCheckSavedStateAtBoot = true;
+            if (testAndClearWifiSavedState()) return true;
+        }
+
+        if (mAirplaneModeOn) {
+            return mPersistWifiState == WIFI_ENABLED_AIRPLANE_OVERRIDE;
+        } else {
+            return mPersistWifiState != WIFI_DISABLED;
+        }
+    }
+
+    /**
+     * Returns true if airplane mode is currently on.
+     * @return {@code true} if airplane mode is on.
+     */
+    synchronized boolean isAirplaneModeOn() {
+       return mAirplaneModeOn;
+    }
+
+    synchronized boolean handleWifiToggled(boolean wifiEnabled) {
+        // Can Wi-Fi be toggled in airplane mode ?
+        if (mAirplaneModeOn && !isAirplaneToggleable()) {
+            return false;
+        }
+
+        if (wifiEnabled) {
+            if (mAirplaneModeOn) {
+                persistWifiState(WIFI_ENABLED_AIRPLANE_OVERRIDE);
+            } else {
+                persistWifiState(WIFI_ENABLED);
+            }
+        } else {
+            // When wifi state is disabled, we do not care
+            // if airplane mode is on or not. The scenario of
+            // wifi being disabled due to airplane mode being turned on
+            // is handled handleAirplaneModeToggled()
+            persistWifiState(WIFI_DISABLED);
+        }
+        return true;
+    }
+
+    synchronized boolean handleAirplaneModeToggled() {
+        // Is Wi-Fi sensitive to airplane mode changes ?
+        if (!isAirplaneSensitive()) {
+            return false;
+        }
+
+        mAirplaneModeOn = getPersistedAirplaneModeOn();
+        if (mAirplaneModeOn) {
+            // Wifi disabled due to airplane on
+            if (mPersistWifiState == WIFI_ENABLED) {
+                persistWifiState(WIFI_DISABLED_AIRPLANE_ON);
+            }
+        } else {
+            /* On airplane mode disable, restore wifi state if necessary */
+            if (testAndClearWifiSavedState() ||
+                    mPersistWifiState == WIFI_ENABLED_AIRPLANE_OVERRIDE) {
+                persistWifiState(WIFI_ENABLED);
+            }
+        }
+        return true;
+    }
+
+    void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
+        pw.println("mPersistWifiState " + mPersistWifiState);
+        pw.println("mAirplaneModeOn " + mAirplaneModeOn);
+    }
+
+    private void persistWifiState(int state) {
+        final ContentResolver cr = mContext.getContentResolver();
+        mPersistWifiState = state;
+        Settings.Global.putInt(cr, Settings.Global.WIFI_ON, state);
+    }
+
+    /* Does Wi-Fi need to be disabled when airplane mode is on ? */
+    private boolean isAirplaneSensitive() {
+        String airplaneModeRadios = Settings.Global.getString(mContext.getContentResolver(),
+                Settings.Global.AIRPLANE_MODE_RADIOS);
+        return airplaneModeRadios == null
+                || airplaneModeRadios.contains(Settings.Global.RADIO_WIFI);
+    }
+
+    /* Is Wi-Fi allowed to be re-enabled while airplane mode is on ? */
+    private boolean isAirplaneToggleable() {
+        String toggleableRadios = Settings.Global.getString(mContext.getContentResolver(),
+                Settings.Global.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
+        return toggleableRadios != null
+                && toggleableRadios.contains(Settings.Global.RADIO_WIFI);
+    }
+
+     /* After a reboot, we restore wi-fi to be on if it was turned off temporarily for tethering.
+      * The settings app tracks the saved state, but the framework has to check it at boot to
+      * make sure the wi-fi is turned on in case it was turned off for the purpose of tethering.
+      *
+      * Note that this is not part of the regular WIFI_ON setting because this only needs to
+      * be controlled through the settings app and not the Wi-Fi public API.
+      */
+    private boolean testAndClearWifiSavedState() {
+        final ContentResolver cr = mContext.getContentResolver();
+        int wifiSavedState = 0;
+        try {
+            wifiSavedState = Settings.Global.getInt(cr, Settings.Global.WIFI_SAVED_STATE);
+            if(wifiSavedState == 1)
+                Settings.Global.putInt(cr, Settings.Global.WIFI_SAVED_STATE, 0);
+        } catch (Settings.SettingNotFoundException e) {
+            ;
+        }
+        return (wifiSavedState == 1);
+    }
+
+    private int getPersistedWifiState() {
+        final ContentResolver cr = mContext.getContentResolver();
+        try {
+            return Settings.Global.getInt(cr, Settings.Global.WIFI_ON);
+        } catch (Settings.SettingNotFoundException e) {
+            Settings.Global.putInt(cr, Settings.Global.WIFI_ON, WIFI_DISABLED);
+            return WIFI_DISABLED;
+        }
+    }
+
+    private boolean getPersistedAirplaneModeOn() {
+        return Settings.Global.getInt(mContext.getContentResolver(),
+                Settings.Global.AIRPLANE_MODE_ON, 0) == 1;
+    }
+}
diff --git a/services/java/com/android/server/wifi/WifiTrafficPoller.java b/services/java/com/android/server/wifi/WifiTrafficPoller.java
new file mode 100644
index 0000000..3fcb6c1
--- /dev/null
+++ b/services/java/com/android/server/wifi/WifiTrafficPoller.java
@@ -0,0 +1,163 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.wifi;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.net.NetworkInfo;
+import static android.net.NetworkInfo.DetailedState.CONNECTED;
+import android.net.TrafficStats;
+import android.net.wifi.WifiManager;
+import android.os.Handler;
+import android.os.Message;
+
+import java.io.FileDescriptor;
+import java.io.PrintWriter;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import com.android.internal.util.AsyncChannel;
+
+/* Polls for traffic stats and notifies the clients */
+final class WifiTrafficPoller {
+    /**
+     * Interval in milliseconds between polling for traffic
+     * statistics
+     */
+    private static final int POLL_TRAFFIC_STATS_INTERVAL_MSECS = 1000;
+
+    private boolean mEnableTrafficStatsPoll = false;
+    private int mTrafficStatsPollToken = 0;
+    private long mTxPkts;
+    private long mRxPkts;
+    /* Tracks last reported data activity */
+    private int mDataActivity;
+
+    private final List<AsyncChannel> mClients;
+    // err on the side of updating at boot since screen on broadcast may be missed
+    // the first time
+    private AtomicBoolean mScreenOn = new AtomicBoolean(true);
+    private final TrafficHandler mTrafficHandler;
+    private NetworkInfo mNetworkInfo;
+    private final String mInterface;
+
+    WifiTrafficPoller(Context context, List<AsyncChannel> clients, String iface) {
+        mClients = clients;
+        mInterface = iface;
+        mTrafficHandler = new TrafficHandler();
+
+        IntentFilter filter = new IntentFilter();
+        filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
+        filter.addAction(Intent.ACTION_SCREEN_OFF);
+        filter.addAction(Intent.ACTION_SCREEN_ON);
+
+        context.registerReceiver(
+                new BroadcastReceiver() {
+                    @Override
+                    public void onReceive(Context context, Intent intent) {
+                        if (intent.getAction().equals(
+                                WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
+                            mNetworkInfo = (NetworkInfo) intent.getParcelableExtra(
+                                    WifiManager.EXTRA_NETWORK_INFO);
+                        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
+                            mScreenOn.set(false);
+                        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
+                            mScreenOn.set(true);
+                        }
+                        evaluateTrafficStatsPolling();
+                    }
+                }, filter);
+    }
+
+
+    private class TrafficHandler extends Handler {
+        public void handleMessage(Message msg) {
+            switch (msg.what) {
+                case WifiManager.ENABLE_TRAFFIC_STATS_POLL: {
+                    mEnableTrafficStatsPoll = (msg.arg1 == 1);
+                    mTrafficStatsPollToken++;
+                    if (mEnableTrafficStatsPoll) {
+                        notifyOnDataActivity();
+                        sendMessageDelayed(Message.obtain(this, WifiManager.TRAFFIC_STATS_POLL,
+                                mTrafficStatsPollToken, 0), POLL_TRAFFIC_STATS_INTERVAL_MSECS);
+                    }
+                    break;
+                }
+                case WifiManager.TRAFFIC_STATS_POLL: {
+                    if (msg.arg1 == mTrafficStatsPollToken) {
+                        notifyOnDataActivity();
+                        sendMessageDelayed(Message.obtain(this, WifiManager.TRAFFIC_STATS_POLL,
+                                mTrafficStatsPollToken, 0), POLL_TRAFFIC_STATS_INTERVAL_MSECS);
+                    }
+                    break;
+                }
+            }
+
+        }
+    }
+
+    private void evaluateTrafficStatsPolling() {
+        Message msg;
+        if (mNetworkInfo == null) return;
+        if (mNetworkInfo.getDetailedState() == CONNECTED && mScreenOn.get()) {
+            msg = Message.obtain(mTrafficHandler,
+                    WifiManager.ENABLE_TRAFFIC_STATS_POLL, 1, 0);
+        } else {
+            msg = Message.obtain(mTrafficHandler,
+                    WifiManager.ENABLE_TRAFFIC_STATS_POLL, 0, 0);
+        }
+        msg.sendToTarget();
+    }
+
+    private void notifyOnDataActivity() {
+        long sent, received;
+        long preTxPkts = mTxPkts, preRxPkts = mRxPkts;
+        int dataActivity = WifiManager.DATA_ACTIVITY_NONE;
+
+        mTxPkts = TrafficStats.getTxPackets(mInterface);
+        mRxPkts = TrafficStats.getRxPackets(mInterface);
+
+        if (preTxPkts > 0 || preRxPkts > 0) {
+            sent = mTxPkts - preTxPkts;
+            received = mRxPkts - preRxPkts;
+            if (sent > 0) {
+                dataActivity |= WifiManager.DATA_ACTIVITY_OUT;
+            }
+            if (received > 0) {
+                dataActivity |= WifiManager.DATA_ACTIVITY_IN;
+            }
+
+            if (dataActivity != mDataActivity && mScreenOn.get()) {
+                mDataActivity = dataActivity;
+                for (AsyncChannel client : mClients) {
+                    client.sendMessage(WifiManager.DATA_ACTIVITY_NOTIFICATION, mDataActivity);
+                }
+            }
+        }
+    }
+
+    void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
+        pw.println("mEnableTrafficStatsPoll " + mEnableTrafficStatsPoll);
+        pw.println("mTrafficStatsPollToken " + mTrafficStatsPollToken);
+        pw.println("mTxPkts " + mTxPkts);
+        pw.println("mRxPkts " + mRxPkts);
+        pw.println("mDataActivity " + mDataActivity);
+    }
+
+}
diff --git a/services/java/com/android/server/wm/DisplayContent.java b/services/java/com/android/server/wm/DisplayContent.java
index 938fa5c..6aae202 100644
--- a/services/java/com/android/server/wm/DisplayContent.java
+++ b/services/java/com/android/server/wm/DisplayContent.java
@@ -19,9 +19,6 @@
 import static com.android.server.wm.WindowManagerService.FORWARD_ITERATOR;
 import static com.android.server.wm.WindowManagerService.REVERSE_ITERATOR;
 
-import android.graphics.Rect;
-import android.os.Debug;
-import android.util.Slog;
 import android.util.SparseArray;
 import android.view.Display;
 import android.view.DisplayInfo;
@@ -138,7 +135,7 @@
             mTaskIdToTaskList.put(wtoken.groupId, task);
             mTaskLists.add(task);
         } else {
-            task.mAppTokens.add(wtoken);
+            task.mAppTokens.add(addPos, wtoken);
         }
     }
 
@@ -189,55 +186,10 @@
         return mTmpAppIterator;
     }
 
-    class TaskListsIterator implements Iterator<TaskList> {
-        private int mCur;
-        private boolean mReverse;
-
-        TaskListsIterator() {
-            this(false);
-        }
-
-        TaskListsIterator(boolean reverse) {
-            reset(reverse);
-        }
-
-        void reset(boolean reverse) {
-            mReverse = reverse;
-            mCur = reverse ? mTaskLists.size() - 1 : 0;
-        }
-
-        @Override
-        public boolean hasNext() {
-            if (mReverse) {
-                return mCur >= 0;
-            }
-            return mCur < mTaskLists.size();
-        }
-
-        @Override
-        public TaskList next() {
-            if (hasNext()) {
-                TaskList taskList = mTaskLists.get(mCur);
-                mCur += (mReverse ? -1 : 1);
-                return taskList;
-            }
-            throw new NoSuchElementException();
-        }
-
-        @Override
-        public void remove() {
-            throw new IllegalArgumentException();
-        }
-
-        @Override public String toString() {
-            return mTaskLists.toString();
-        }
-    }
-
     class AppTokenIterator implements Iterator<AppWindowToken> {
-        final TaskListsIterator mIterator = new TaskListsIterator();
         boolean mReverse;
-        int mCur;
+        int mTasksNdx;
+        int mActivityNdx;
         TaskList mTaskList;
 
         public AppTokenIterator() {
@@ -250,14 +202,23 @@
 
         void reset(boolean reverse) {
             mReverse = reverse;
-            mIterator.reset(reverse);
+            mTasksNdx = reverse ? mTaskLists.size() - 1 : 0;
             getNextTaskList();
         }
 
         private void getNextTaskList() {
-            if (mIterator.hasNext()) {
-                mTaskList = mIterator.next();
-                mCur = mReverse ? mTaskList.mAppTokens.size() - 1 : 0;
+            if (mReverse) {
+                if (mTasksNdx >= 0) {
+                    mTaskList = mTaskLists.get(mTasksNdx);
+                    --mTasksNdx;
+                    mActivityNdx = mTaskList.mAppTokens.size() - 1;
+                }
+            } else {
+                if (mTasksNdx < mTaskLists.size()) {
+                    mTaskList = mTaskLists.get(mTasksNdx);
+                    ++mTasksNdx;
+                    mActivityNdx = 0;
+                }
             }
         }
 
@@ -267,16 +228,16 @@
                 return false;
             }
             if (mReverse) {
-                return mCur >= 0;
+                return mActivityNdx >= 0;
             }
-            return mCur < mTaskList.mAppTokens.size();
+            return mActivityNdx < mTaskList.mAppTokens.size();
         }
 
         @Override
         public AppWindowToken next() {
             if (hasNext()) {
-                AppWindowToken wtoken = mTaskList.mAppTokens.get(mCur);
-                mCur += mReverse ? -1 : 1;
+                AppWindowToken wtoken = mTaskList.mAppTokens.get(mActivityNdx);
+                mActivityNdx += mReverse ? -1 : 1;
                 if (!hasNext()) {
                     getNextTaskList();
                 }
@@ -292,15 +253,14 @@
 
         int size() {
             int size = 0;
-            final TaskListsIterator iterator = new TaskListsIterator();
-            while (iterator.hasNext()) {
-                size += iterator.next().mAppTokens.size();
+            for (int i = mTaskLists.size() - 1; i >= 0; --i) {
+                size += mTaskLists.get(i).mAppTokens.size();
             }
             return size;
         }
 
         @Override public String toString() {
-            return mIterator.toString();
+            return mTaskLists.toString();
         }
     }
 
diff --git a/services/java/com/android/server/wm/WindowManagerService.java b/services/java/com/android/server/wm/WindowManagerService.java
index b193430..c2213b3 100644
--- a/services/java/com/android/server/wm/WindowManagerService.java
+++ b/services/java/com/android/server/wm/WindowManagerService.java
@@ -3139,6 +3139,10 @@
                 }
 
                 while (v >= 0) {
+                    if (!iterator.hasNext()) {
+                        mismatch = true;
+                        break;
+                    }
                     AppWindowToken atoken = iterator.next();
                     if (atoken.removed) {
                         continue;
@@ -3152,8 +3156,8 @@
             }
 
             if (mismatch || iterator.hasNext()) {
-                Slog.w(TAG, "validateAppTokens: Mismatch! ActivityManager=" + tasks
-                        + " WindowManager=" + iterator);
+                Slog.w(TAG, "validateAppTokens: Mismatch! ActivityManager=" + tasks);
+                Slog.w(TAG, "validateAppTokens: Mismatch! WindowManager=" + iterator);
             }
         }
     }
@@ -3332,12 +3336,17 @@
         }
 
         synchronized(mWindowMap) {
-            AppWindowToken atoken = findAppWindowToken(token);
+            final AppWindowToken atoken = findAppWindowToken(token);
             if (atoken == null) {
                 Slog.w(TAG, "Attempted to set group id of non-existing app token: " + token);
                 return;
             }
-            mTaskIdToDisplayContents.get(atoken.groupId).setAppTaskId(atoken, groupId);
+            DisplayContent displayContent = mTaskIdToDisplayContents.get(atoken.groupId);
+            if (displayContent == null) {
+                Slog.w(TAG, "setAppGroupId: No DisplayContent for taskId=" + atoken.groupId);
+                displayContent = getDefaultDisplayContentLocked();
+            }
+            displayContent.setAppTaskId(atoken, groupId);
         }
     }
 
@@ -7281,6 +7290,7 @@
         performLayoutAndPlaceSurfacesLocked();
     }
 
+    @Override
     public void setOverscan(int displayId, int left, int top, int right, int bottom) {
         if (mContext.checkCallingOrSelfPermission(
                 android.Manifest.permission.WRITE_SECURE_SETTINGS) !=
diff --git a/test-runner/src/android/test/mock/MockContext.java b/test-runner/src/android/test/mock/MockContext.java
index cfc6bd7..29d6e4d 100644
--- a/test-runner/src/android/test/mock/MockContext.java
+++ b/test-runner/src/android/test/mock/MockContext.java
@@ -551,6 +551,12 @@
         throw new UnsupportedOperationException();
     }
 
+    /** {@hide} */
+    @Override
+    public int getUserId() {
+        throw new UnsupportedOperationException();
+    }
+
     @Override
     public Context createConfigurationContext(Configuration overrideConfiguration) {
         throw new UnsupportedOperationException();
diff --git a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeContext.java b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeContext.java
index 8b643c0..21bef1c 100644
--- a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeContext.java
+++ b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeContext.java
@@ -1398,4 +1398,12 @@
         // pass
         return null;
     }
+
+    /**
+     * @hide
+     */
+    @Override
+    public int getUserId() {
+        return 0; // not used
+    }
 }