Merge "Allow empty strings in playFromSearch" into lmp-dev
diff --git a/api/current.txt b/api/current.txt
index e3c3ae2..d12898f 100644
--- a/api/current.txt
+++ b/api/current.txt
@@ -35857,20 +35857,16 @@
     method public int getComposingTextStart();
     method public float getInsertionMarkerBaseline();
     method public float getInsertionMarkerBottom();
+    method public int getInsertionMarkerFlags();
     method public float getInsertionMarkerHorizontal();
     method public float getInsertionMarkerTop();
     method public android.graphics.Matrix getMatrix();
     method public int getSelectionEnd();
     method public int getSelectionStart();
-    method public boolean isInsertionMarkerClipped();
     method public void writeToParcel(android.os.Parcel, int);
-    field public static final int CHARACTER_RECT_TYPE_FULLY_VISIBLE = 1; // 0x1
-    field public static final int CHARACTER_RECT_TYPE_INVISIBLE = 3; // 0x3
-    field public static final int CHARACTER_RECT_TYPE_MASK = 15; // 0xf
-    field public static final int CHARACTER_RECT_TYPE_NOT_FEASIBLE = 4; // 0x4
-    field public static final int CHARACTER_RECT_TYPE_PARTIALLY_VISIBLE = 2; // 0x2
-    field public static final int CHARACTER_RECT_TYPE_UNSPECIFIED = 0; // 0x0
     field public static final android.os.Parcelable.Creator CREATOR;
+    field public static final int FLAG_HAS_INVISIBLE_REGION = 2; // 0x2
+    field public static final int FLAG_HAS_VISIBLE_REGION = 1; // 0x1
   }
 
   public static final class CursorAnchorInfo.Builder {
@@ -35879,7 +35875,7 @@
     method public android.view.inputmethod.CursorAnchorInfo build();
     method public void reset();
     method public android.view.inputmethod.CursorAnchorInfo.Builder setComposingText(int, java.lang.CharSequence);
-    method public android.view.inputmethod.CursorAnchorInfo.Builder setInsertionMarkerLocation(float, float, float, float, boolean);
+    method public android.view.inputmethod.CursorAnchorInfo.Builder setInsertionMarkerLocation(float, float, float, float, int);
     method public android.view.inputmethod.CursorAnchorInfo.Builder setMatrix(android.graphics.Matrix);
     method public android.view.inputmethod.CursorAnchorInfo.Builder setSelectionRange(int, int);
   }
diff --git a/api/removed.txt b/api/removed.txt
index 8915fc3..36f8920 100644
--- a/api/removed.txt
+++ b/api/removed.txt
@@ -48,6 +48,24 @@
 
 }
 
+package android.view.inputmethod {
+
+  public final class CursorAnchorInfo implements android.os.Parcelable {
+    method public boolean isInsertionMarkerClipped();
+    field public static final int CHARACTER_RECT_TYPE_FULLY_VISIBLE = 1; // 0x1
+    field public static final int CHARACTER_RECT_TYPE_INVISIBLE = 3; // 0x3
+    field public static final int CHARACTER_RECT_TYPE_MASK = 15; // 0xf
+    field public static final int CHARACTER_RECT_TYPE_NOT_FEASIBLE = 4; // 0x4
+    field public static final int CHARACTER_RECT_TYPE_PARTIALLY_VISIBLE = 2; // 0x2
+    field public static final int CHARACTER_RECT_TYPE_UNSPECIFIED = 0; // 0x0
+  }
+
+  public static final class CursorAnchorInfo.Builder {
+    method public android.view.inputmethod.CursorAnchorInfo.Builder setInsertionMarkerLocation(float, float, float, float, boolean);
+  }
+
+}
+
 package com.android.internal {
 
   public static final class R.attr {
diff --git a/core/java/android/net/ConnectivityManager.java b/core/java/android/net/ConnectivityManager.java
index 4bfef41..3c219fd 100644
--- a/core/java/android/net/ConnectivityManager.java
+++ b/core/java/android/net/ConnectivityManager.java
@@ -685,6 +685,23 @@
     }
 
     /**
+     * Returns the {@link Network} object currently serving a given type, or
+     * null if the given type is not connected.
+     *
+     * <p>This method requires the caller to hold the permission
+     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
+     *
+     * @hide
+     */
+    public Network getNetworkForType(int networkType) {
+        try {
+            return mService.getNetworkForType(networkType);
+        } catch (RemoteException e) {
+            return null;
+        }
+    }
+
+    /**
      * Returns an array of all {@link Network} currently tracked by the
      * framework.
      *
diff --git a/core/java/android/net/IConnectivityManager.aidl b/core/java/android/net/IConnectivityManager.aidl
index b2fc3be..974c4cd 100644
--- a/core/java/android/net/IConnectivityManager.aidl
+++ b/core/java/android/net/IConnectivityManager.aidl
@@ -48,6 +48,7 @@
     NetworkInfo getNetworkInfo(int networkType);
     NetworkInfo getNetworkInfoForNetwork(in Network network);
     NetworkInfo[] getAllNetworkInfo();
+    Network getNetworkForType(int networkType);
     Network[] getAllNetworks();
 
     NetworkInfo getProvisioningOrActiveNetworkInfo();
diff --git a/core/java/android/net/Network.java b/core/java/android/net/Network.java
index d2a4728..e686be7 100644
--- a/core/java/android/net/Network.java
+++ b/core/java/android/net/Network.java
@@ -35,6 +35,7 @@
 import java.util.concurrent.atomic.AtomicReference;
 import javax.net.SocketFactory;
 
+import com.android.okhttp.ConnectionPool;
 import com.android.okhttp.HostResolver;
 import com.android.okhttp.OkHttpClient;
 
@@ -60,6 +61,17 @@
     private volatile OkHttpClient mOkHttpClient = null;
     private Object mLock = new Object();
 
+    // Default connection pool values. These are evaluated at startup, just
+    // like the OkHttp code. Also like the OkHttp code, we will throw parse
+    // exceptions at class loading time if the properties are set but are not
+    // valid integers.
+    private static final boolean httpKeepAlive =
+            Boolean.parseBoolean(System.getProperty("http.keepAlive", "true"));
+    private static final int httpMaxConnections =
+            httpKeepAlive ? Integer.parseInt(System.getProperty("http.maxConnections", "5")) : 0;
+    private static final long httpKeepAliveDurationMs =
+            Long.parseLong(System.getProperty("http.keepAliveDuration", "300000"));  // 5 minutes.
+
     /**
      * @hide
      */
@@ -183,6 +195,20 @@
         return mNetworkBoundSocketFactory;
     }
 
+    // TODO: This creates an OkHttpClient with its own connection pool for
+    // every Network object, instead of one for every NetId. This is
+    // suboptimal, because an app could potentially have more than one
+    // Network object for the same NetId, causing increased memory footprint
+    // and performance penalties due to lack of connection reuse (connection
+    // setup time, congestion window growth time, etc.).
+    //
+    // Instead, investigate only having one OkHttpClient for every NetId,
+    // perhaps by using a static HashMap of NetIds to OkHttpClient objects. The
+    // tricky part is deciding when to remove an OkHttpClient; a WeakHashMap
+    // shouldn't be used because whether a Network is referenced doesn't
+    // correlate with whether a new Network will be instantiated in the near
+    // future with the same NetID. A good solution would involve purging empty
+    // (or when all connections are timed out) ConnectionPools.
     private void maybeInitHttpClient() {
         if (mOkHttpClient == null) {
             synchronized (mLock) {
@@ -193,9 +219,12 @@
                             return Network.this.getAllByName(host);
                         }
                     };
+                    ConnectionPool pool = new ConnectionPool(httpMaxConnections,
+                                                             httpKeepAliveDurationMs);
                     mOkHttpClient = new OkHttpClient()
                             .setSocketFactory(getSocketFactory())
-                            .setHostResolver(hostResolver);
+                            .setHostResolver(hostResolver)
+                            .setConnectionPool(pool);
                 }
             }
         }
diff --git a/core/java/android/os/INetworkManagementService.aidl b/core/java/android/os/INetworkManagementService.aidl
index 6d4a302..b3e28ea 100644
--- a/core/java/android/os/INetworkManagementService.aidl
+++ b/core/java/android/os/INetworkManagementService.aidl
@@ -19,6 +19,7 @@
 
 import android.net.InterfaceConfiguration;
 import android.net.INetworkManagementEventObserver;
+import android.net.Network;
 import android.net.NetworkStats;
 import android.net.RouteInfo;
 import android.net.UidRange;
@@ -164,10 +165,10 @@
     /**
      * Sets the list of DNS forwarders (in order of priority)
      */
-    void setDnsForwarders(in String[] dns);
+    void setDnsForwarders(in Network network, in String[] dns);
 
     /**
-     * Returns the list of DNS fowarders (in order of priority)
+     * Returns the list of DNS forwarders (in order of priority)
      */
     String[] getDnsForwarders();
 
diff --git a/core/java/android/preference/CheckBoxPreference.java b/core/java/android/preference/CheckBoxPreference.java
index 1ce98b8..fee3f0f1 100644
--- a/core/java/android/preference/CheckBoxPreference.java
+++ b/core/java/android/preference/CheckBoxPreference.java
@@ -66,7 +66,6 @@
         View checkboxView = view.findViewById(com.android.internal.R.id.checkbox);
         if (checkboxView != null && checkboxView instanceof Checkable) {
             ((Checkable) checkboxView).setChecked(mChecked);
-            sendAccessibilityEvent(checkboxView);
         }
 
         syncSummaryView(view);
diff --git a/core/java/android/preference/SwitchPreference.java b/core/java/android/preference/SwitchPreference.java
index 46be928..53b5aad 100644
--- a/core/java/android/preference/SwitchPreference.java
+++ b/core/java/android/preference/SwitchPreference.java
@@ -130,8 +130,6 @@
 
             ((Checkable) checkableView).setChecked(mChecked);
 
-            sendAccessibilityEvent(checkableView);
-
             if (checkableView instanceof Switch) {
                 final Switch switchView = (Switch) checkableView;
                 switchView.setTextOn(mSwitchOn);
diff --git a/core/java/android/preference/TwoStatePreference.java b/core/java/android/preference/TwoStatePreference.java
index 6f8be1f..3823b27 100644
--- a/core/java/android/preference/TwoStatePreference.java
+++ b/core/java/android/preference/TwoStatePreference.java
@@ -24,8 +24,6 @@
 import android.text.TextUtils;
 import android.util.AttributeSet;
 import android.view.View;
-import android.view.accessibility.AccessibilityEvent;
-import android.view.accessibility.AccessibilityManager;
 import android.widget.TextView;
 
 /**
@@ -39,7 +37,6 @@
     private CharSequence mSummaryOff;
     boolean mChecked;
     private boolean mCheckedSet;
-    private boolean mSendClickAccessibilityEvent;
     private boolean mDisableDependentsState;
 
     public TwoStatePreference(
@@ -63,15 +60,10 @@
     protected void onClick() {
         super.onClick();
 
-        boolean newValue = !isChecked();
-
-        mSendClickAccessibilityEvent = true;
-
-        if (!callChangeListener(newValue)) {
-            return;
+        final boolean newValue = !isChecked();
+        if (callChangeListener(newValue)) {
+            setChecked(newValue);
         }
-
-        setChecked(newValue);
     }
 
     /**
@@ -196,21 +188,6 @@
                 : (Boolean) defaultValue);
     }
 
-    void sendAccessibilityEvent(View view) {
-        // Since the view is still not attached we create, populate,
-        // and send the event directly since we do not know when it
-        // will be attached and posting commands is not as clean.
-        AccessibilityManager accessibilityManager = AccessibilityManager.getInstance(getContext());
-        if (mSendClickAccessibilityEvent && accessibilityManager.isEnabled()) {
-            AccessibilityEvent event = AccessibilityEvent.obtain();
-            event.setEventType(AccessibilityEvent.TYPE_VIEW_CLICKED);
-            view.onInitializeAccessibilityEvent(event);
-            view.dispatchPopulateAccessibilityEvent(event);
-            accessibilityManager.sendAccessibilityEvent(event);
-        }
-        mSendClickAccessibilityEvent = false;
-    }
-
     /**
      * Sync a summary view contained within view's subhierarchy with the correct summary text.
      * @param view View where a summary should be located
diff --git a/core/java/android/view/inputmethod/CursorAnchorInfo.java b/core/java/android/view/inputmethod/CursorAnchorInfo.java
index 0492824..fe0f5b9 100644
--- a/core/java/android/view/inputmethod/CursorAnchorInfo.java
+++ b/core/java/android/view/inputmethod/CursorAnchorInfo.java
@@ -45,9 +45,9 @@
     private final CharSequence mComposingText;
 
     /**
-     * {@code True} if the insertion marker is partially or entirely clipped by other UI elements.
+     * Flags of the insertion marker. See {@link #FLAG_HAS_VISIBLE_REGION} for example.
      */
-    private final boolean mInsertionMarkerClipped;
+    private final int mInsertionMarkerFlags;
     /**
      * Horizontal position of the insertion marker, in the local coordinates that will be
      * transformed with the transformation matrix when rendered on the screen. This should be
@@ -90,27 +90,47 @@
      */
     private final Matrix mMatrix;
 
+    /**
+     * Flag for {@link #getInsertionMarkerFlags()} and {@link #getCharacterRectFlags(int)}: the
+     * insertion marker or character bounds have at least one visible region.
+     */
+    public static final int FLAG_HAS_VISIBLE_REGION = 0x01;
+
+    /**
+     * Flag for {@link #getInsertionMarkerFlags()} and {@link #getCharacterRectFlags(int)}: the
+     * insertion marker or character bounds have at least one invisible (clipped) region.
+     */
+    public static final int FLAG_HAS_INVISIBLE_REGION = 0x02;
+
+    /**
+     * @removed
+     */
     public static final int CHARACTER_RECT_TYPE_MASK = 0x0f;
     /**
      * Type for {@link #CHARACTER_RECT_TYPE_MASK}: the editor did not specify any type of this
      * character. Editor authors should not use this flag.
+     * @removed
      */
     public static final int CHARACTER_RECT_TYPE_UNSPECIFIED = 0;
     /**
      * Type for {@link #CHARACTER_RECT_TYPE_MASK}: the character is entirely visible.
+     * @removed
      */
     public static final int CHARACTER_RECT_TYPE_FULLY_VISIBLE = 1;
     /**
      * Type for {@link #CHARACTER_RECT_TYPE_MASK}: some area of the character is invisible.
+     * @removed
      */
     public static final int CHARACTER_RECT_TYPE_PARTIALLY_VISIBLE = 2;
     /**
      * Type for {@link #CHARACTER_RECT_TYPE_MASK}: the character is entirely invisible.
+     * @removed
      */
     public static final int CHARACTER_RECT_TYPE_INVISIBLE = 3;
     /**
      * Type for {@link #CHARACTER_RECT_TYPE_MASK}: the editor gave up to calculate the rectangle
      * for this character. Input method authors should ignore the returned rectangle.
+     * @removed
      */
     public static final int CHARACTER_RECT_TYPE_NOT_FEASIBLE = 4;
 
@@ -119,7 +139,7 @@
         mSelectionEnd = source.readInt();
         mComposingTextStart = source.readInt();
         mComposingText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
-        mInsertionMarkerClipped = (source.readInt() != 0);
+        mInsertionMarkerFlags = source.readInt();
         mInsertionMarkerHorizontal = source.readFloat();
         mInsertionMarkerTop = source.readFloat();
         mInsertionMarkerBaseline = source.readFloat();
@@ -141,7 +161,7 @@
         dest.writeInt(mSelectionEnd);
         dest.writeInt(mComposingTextStart);
         TextUtils.writeToParcel(mComposingText, dest, flags);
-        dest.writeInt(mInsertionMarkerClipped ? 1 : 0);
+        dest.writeInt(mInsertionMarkerFlags);
         dest.writeFloat(mInsertionMarkerHorizontal);
         dest.writeFloat(mInsertionMarkerTop);
         dest.writeFloat(mInsertionMarkerBaseline);
@@ -159,7 +179,7 @@
                 + mInsertionMarkerBaseline + mInsertionMarkerBottom;
         int hash = floatHash > 0 ? (int) floatHash : (int)(-floatHash);
         hash *= 31;
-        hash += (mInsertionMarkerClipped ? 2 : 1);
+        hash += mInsertionMarkerFlags;
         hash *= 31;
         hash += mSelectionStart + mSelectionEnd + mComposingTextStart;
         hash *= 31;
@@ -204,7 +224,7 @@
                 || !Objects.equals(mComposingText, that.mComposingText)) {
             return false;
         }
-        if (mInsertionMarkerClipped != that.mInsertionMarkerClipped
+        if (mInsertionMarkerFlags != that.mInsertionMarkerFlags
                 || !areSameFloatImpl(mInsertionMarkerHorizontal, that.mInsertionMarkerHorizontal)
                 || !areSameFloatImpl(mInsertionMarkerTop, that.mInsertionMarkerTop)
                 || !areSameFloatImpl(mInsertionMarkerBaseline, that.mInsertionMarkerBaseline)
@@ -225,7 +245,7 @@
         return "SelectionInfo{mSelection=" + mSelectionStart + "," + mSelectionEnd
                 + " mComposingTextStart=" + mComposingTextStart
                 + " mComposingText=" + Objects.toString(mComposingText)
-                + " mInsertionMarkerClipped=" + mInsertionMarkerClipped
+                + " mInsertionMarkerFlags=" + mInsertionMarkerFlags
                 + " mInsertionMarkerHorizontal=" + mInsertionMarkerHorizontal
                 + " mInsertionMarkerTop=" + mInsertionMarkerTop
                 + " mInsertionMarkerBaseline=" + mInsertionMarkerBaseline
@@ -272,6 +292,20 @@
         private CharSequence mComposingText = null;
 
         /**
+         * @removed
+         */
+        public Builder setInsertionMarkerLocation(final float horizontalPosition,
+                final float lineTop, final float lineBaseline, final float lineBottom,
+                final boolean clipped){
+            mInsertionMarkerHorizontal = horizontalPosition;
+            mInsertionMarkerTop = lineTop;
+            mInsertionMarkerBaseline = lineBaseline;
+            mInsertionMarkerBottom = lineBottom;
+            mInsertionMarkerFlags = clipped ? FLAG_HAS_INVISIBLE_REGION : 0;
+            return this;
+        }
+
+        /**
          * Sets the location of the text insertion point (zero width cursor) as a rectangle in
          * local coordinates. Calling this can be skipped when there is no text insertion point;
          * however if there is an insertion point, editors must call this method.
@@ -288,24 +322,24 @@
          * @param lineBottom vertical position of the insertion marker, in the local coordinates
          * that will be transformed with the transformation matrix when rendered on the screen. This
          * should be calculated or compatible with {@link Layout#getLineBottom(int)}.
-         * @param clipped {@code true} is the insertion marker is partially or entierly clipped by
-         * other UI elements.
+         * @param flags flags of the insertion marker. See {@link #FLAG_HAS_VISIBLE_REGION} for
+         * example.
          */
         public Builder setInsertionMarkerLocation(final float horizontalPosition,
                 final float lineTop, final float lineBaseline, final float lineBottom,
-                final boolean clipped){
+                final int flags){
             mInsertionMarkerHorizontal = horizontalPosition;
             mInsertionMarkerTop = lineTop;
             mInsertionMarkerBaseline = lineBaseline;
             mInsertionMarkerBottom = lineBottom;
-            mInsertionMarkerClipped = clipped;
+            mInsertionMarkerFlags = flags;
             return this;
         }
         private float mInsertionMarkerHorizontal = Float.NaN;
         private float mInsertionMarkerTop = Float.NaN;
         private float mInsertionMarkerBaseline = Float.NaN;
         private float mInsertionMarkerBottom = Float.NaN;
-        private boolean mInsertionMarkerClipped = false;
+        private int mInsertionMarkerFlags = 0;
 
         /**
          * Adds the bounding box of the character specified with the index.
@@ -320,8 +354,8 @@
          * coordinates, that is, right edge for LTR text and left edge for RTL text.
          * @param trailingEdgeY y coordinate of the trailing edge of the character in local
          * coordinates.
-         * @param flags type and flags for this character. See
-         * {@link #CHARACTER_RECT_TYPE_FULLY_VISIBLE} for example.
+         * @param flags flags for this character rect. See {@link #FLAG_HAS_VISIBLE_REGION} for
+         * example.
          * @throws IllegalArgumentException If the index is a negative value, or not greater than
          * all of the previously called indices.
          */
@@ -331,11 +365,6 @@
             if (index < 0) {
                 throw new IllegalArgumentException("index must not be a negative integer.");
             }
-            final int type = flags & CHARACTER_RECT_TYPE_MASK;
-            if (type == CHARACTER_RECT_TYPE_UNSPECIFIED) {
-                throw new IllegalArgumentException("Type except for "
-                        + "CHARACTER_RECT_TYPE_UNSPECIFIED must be specified.");
-            }
             if (mCharacterRectBuilder == null) {
                 mCharacterRectBuilder = new SparseRectFArrayBuilder();
             }
@@ -388,7 +417,7 @@
             mSelectionEnd = -1;
             mComposingTextStart = -1;
             mComposingText = null;
-            mInsertionMarkerClipped = false;
+            mInsertionMarkerFlags = 0;
             mInsertionMarkerHorizontal = Float.NaN;
             mInsertionMarkerTop = Float.NaN;
             mInsertionMarkerBaseline = Float.NaN;
@@ -406,7 +435,7 @@
         mSelectionEnd = builder.mSelectionEnd;
         mComposingTextStart = builder.mComposingTextStart;
         mComposingText = builder.mComposingText;
-        mInsertionMarkerClipped = builder.mInsertionMarkerClipped;
+        mInsertionMarkerFlags = builder.mInsertionMarkerFlags;
         mInsertionMarkerHorizontal = builder.mInsertionMarkerHorizontal;
         mInsertionMarkerTop = builder.mInsertionMarkerTop;
         mInsertionMarkerBaseline = builder.mInsertionMarkerBaseline;
@@ -449,11 +478,20 @@
     }
 
     /**
+     * Returns the flag of the insertion marker.
+     * @return the flag of the insertion marker. {@code 0} if no flag is specified.
+     */
+    public int getInsertionMarkerFlags() {
+        return mInsertionMarkerFlags;
+    }
+
+    /**
      * Returns the visibility of the insertion marker.
      * @return {@code true} if the insertion marker is partially or entirely clipped.
+     * @removed
      */
     public boolean isInsertionMarkerClipped() {
-        return mInsertionMarkerClipped;
+        return (mInsertionMarkerFlags & FLAG_HAS_VISIBLE_REGION) != 0;
     }
 
     /**
@@ -522,17 +560,17 @@
     }
 
     /**
-     * Returns the flags associated with the character specified with the index.
+     * Returns the flags associated with the character rect specified with the index.
      * @param index index of the character in a Java chars.
-     * @return {@link #CHARACTER_RECT_TYPE_UNSPECIFIED} if no flag is specified.
+     * @return {@code 0} if no flag is specified.
      */
     // TODO: Prepare a document about the expected behavior for surrogate pairs, combining
     // characters, and non-graphical chars.
     public int getCharacterRectFlags(final int index) {
         if (mCharacterRects == null) {
-            return CHARACTER_RECT_TYPE_UNSPECIFIED;
+            return 0;
         }
-        return mCharacterRects.getFlags(index, CHARACTER_RECT_TYPE_UNSPECIFIED);
+        return mCharacterRects.getFlags(index, 0);
     }
 
     /**
diff --git a/core/java/android/widget/AdapterView.java b/core/java/android/widget/AdapterView.java
index 1da22ca..b9f891c 100644
--- a/core/java/android/widget/AdapterView.java
+++ b/core/java/android/widget/AdapterView.java
@@ -297,10 +297,10 @@
     public boolean performItemClick(View view, int position, long id) {
         if (mOnItemClickListener != null) {
             playSoundEffect(SoundEffectConstants.CLICK);
+            mOnItemClickListener.onItemClick(this, view, position, id);
             if (view != null) {
                 view.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
             }
-            mOnItemClickListener.onItemClick(this, view, position, id);
             return true;
         }
 
diff --git a/core/java/android/widget/Editor.java b/core/java/android/widget/Editor.java
index 46b225d..22138d0 100644
--- a/core/java/android/widget/Editor.java
+++ b/core/java/android/widget/Editor.java
@@ -3089,13 +3089,12 @@
                     final boolean isLeadingEdgeTopVisible = isPositionVisible(leadingEdgeX, top);
                     final boolean isTrailingEdgeBottomVisible =
                             isPositionVisible(trailingEdgeX, bottom);
-                    final int characterRectFlags;
-                    if (isLeadingEdgeTopVisible && isTrailingEdgeBottomVisible) {
-                        characterRectFlags = CursorAnchorInfo.CHARACTER_RECT_TYPE_FULLY_VISIBLE;
-                    } else if (isLeadingEdgeTopVisible || isTrailingEdgeBottomVisible) {
-                        characterRectFlags = CursorAnchorInfo.CHARACTER_RECT_TYPE_PARTIALLY_VISIBLE;
-                    } else {
-                        characterRectFlags = CursorAnchorInfo.CHARACTER_RECT_TYPE_INVISIBLE;
+                    int characterRectFlags = 0;
+                    if (isLeadingEdgeTopVisible || isTrailingEdgeBottomVisible) {
+                        characterRectFlags |= CursorAnchorInfo.FLAG_HAS_VISIBLE_REGION;
+                    }
+                    if (!isLeadingEdgeTopVisible || !isTrailingEdgeBottomVisible) {
+                        characterRectFlags |= CursorAnchorInfo.FLAG_HAS_INVISIBLE_REGION;
                     }
                     // Here offset is the index in Java chars.
                     // TODO: We must have a well-defined specification. For example, how
@@ -3117,11 +3116,19 @@
                         + viewportToContentVerticalOffset;
                 final float insertionMarkerBottom = layout.getLineBottom(line)
                         + viewportToContentVerticalOffset;
-                // Take TextView's padding and scroll into account.
-                final boolean isClipped = !isPositionVisible(insertionMarkerX, insertionMarkerTop)
-                        || !isPositionVisible(insertionMarkerX, insertionMarkerBottom);
+                final boolean isTopVisible =
+                        isPositionVisible(insertionMarkerX, insertionMarkerTop);
+                final boolean isBottomVisible =
+                        isPositionVisible(insertionMarkerX, insertionMarkerBottom);
+                int insertionMarkerFlags = 0;
+                if (isTopVisible || isBottomVisible) {
+                    insertionMarkerFlags |= CursorAnchorInfo.FLAG_HAS_VISIBLE_REGION;
+                }
+                if (!isTopVisible || !isBottomVisible) {
+                    insertionMarkerFlags |= CursorAnchorInfo.FLAG_HAS_INVISIBLE_REGION;
+                }
                 builder.setInsertionMarkerLocation(insertionMarkerX, insertionMarkerTop,
-                        insertionMarkerBaseline, insertionMarkerBottom, isClipped);
+                        insertionMarkerBaseline, insertionMarkerBottom, insertionMarkerFlags);
             }
 
             imm.updateCursorAnchorInfo(mTextView, builder.build());
diff --git a/core/java/android/widget/Toolbar.java b/core/java/android/widget/Toolbar.java
index ece8aa4..3ba03b8 100644
--- a/core/java/android/widget/Toolbar.java
+++ b/core/java/android/widget/Toolbar.java
@@ -248,11 +248,12 @@
         final Drawable navIcon = a.getDrawable(R.styleable.Toolbar_navigationIcon);
         if (navIcon != null) {
             setNavigationIcon(navIcon);
-            final CharSequence navDesc = a.getText(
-                    R.styleable.Toolbar_navigationContentDescription);
-            if (!TextUtils.isEmpty(navDesc)) {
-                setNavigationContentDescription(navDesc);
-            }
+        }
+
+        final CharSequence navDesc = a.getText(
+                R.styleable.Toolbar_navigationContentDescription);
+        if (!TextUtils.isEmpty(navDesc)) {
+            setNavigationContentDescription(navDesc);
         }
         a.recycle();
     }
diff --git a/core/java/com/android/internal/app/ToolbarActionBar.java b/core/java/com/android/internal/app/ToolbarActionBar.java
index abe8a9f..6f1c7ec 100644
--- a/core/java/com/android/internal/app/ToolbarActionBar.java
+++ b/core/java/com/android/internal/app/ToolbarActionBar.java
@@ -22,6 +22,7 @@
 import android.content.Context;
 import android.content.res.Configuration;
 import android.graphics.drawable.Drawable;
+import android.text.TextUtils;
 import android.view.ActionMode;
 import android.view.KeyEvent;
 import android.view.LayoutInflater;
@@ -32,6 +33,7 @@
 import android.view.WindowCallbackWrapper;
 import android.widget.SpinnerAdapter;
 import android.widget.Toolbar;
+import com.android.internal.R;
 import com.android.internal.view.menu.MenuBuilder;
 import com.android.internal.widget.DecorToolbar;
 import com.android.internal.widget.ToolbarWidgetWrapper;
@@ -44,6 +46,8 @@
     private boolean mToolbarMenuPrepared;
     private Window.Callback mWindowCallback;
 
+    private CharSequence mHomeDescription;
+
     private boolean mLastMenuVisibility;
     private ArrayList<OnMenuVisibilityListener> mMenuVisibilityListeners =
             new ArrayList<OnMenuVisibilityListener>();
@@ -70,6 +74,8 @@
         mDecorToolbar.setWindowCallback(mWindowCallback);
         toolbar.setOnMenuItemClickListener(mMenuClicker);
         mDecorToolbar.setWindowTitle(title);
+        mHomeDescription = mToolbar.getNavigationContentDescription();
+        updateNavDescription();
     }
 
     public Window.Callback getWrappedWindowCallback() {
@@ -161,6 +167,7 @@
     @Override
     public void setHomeActionContentDescription(CharSequence description) {
         mToolbar.setNavigationContentDescription(description);
+        mHomeDescription = description;
     }
 
     @Override
@@ -171,6 +178,7 @@
     @Override
     public void setHomeActionContentDescription(int resId) {
         mToolbar.setNavigationContentDescription(resId);
+        mHomeDescription = mToolbar.getNavigationContentDescription();
     }
 
     @Override
@@ -247,8 +255,22 @@
 
     @Override
     public void setDisplayOptions(@DisplayOptions int options, @DisplayOptions int mask) {
-        mDecorToolbar.setDisplayOptions((options & mask) |
-                mDecorToolbar.getDisplayOptions() & ~mask);
+        final int currentOptions = mDecorToolbar.getDisplayOptions();
+        final int changed = (options ^ currentOptions) & mask;
+        mDecorToolbar.setDisplayOptions(options & mask | currentOptions & ~mask);
+        if ((changed & ActionBar.DISPLAY_HOME_AS_UP) != 0) {
+            updateNavDescription();
+        }
+    }
+
+    private void updateNavDescription() {
+        if ((mDecorToolbar.getDisplayOptions() & ActionBar.DISPLAY_HOME_AS_UP) != 0) {
+            if (TextUtils.isEmpty(mHomeDescription)) {
+                mToolbar.setNavigationContentDescription(R.string.action_bar_up_description);
+            } else {
+                mToolbar.setNavigationContentDescription(mHomeDescription);
+            }
+        }
     }
 
     @Override
diff --git a/core/res/res/layout/preference_material.xml b/core/res/res/layout/preference_material.xml
index 778e70a..a137149 100644
--- a/core/res/res/layout/preference_material.xml
+++ b/core/res/res/layout/preference_material.xml
@@ -24,7 +24,8 @@
     android:gravity="center_vertical"
     android:paddingStart="?attr/listPreferredItemPaddingStart"
     android:paddingEnd="?attr/listPreferredItemPaddingEnd"
-    android:background="?attr/activatedBackgroundIndicator">
+    android:background="?attr/activatedBackgroundIndicator"
+    android:clipToPadding="false">
 
     <LinearLayout
         android:id="@+id/icon_frame"
diff --git a/core/res/res/layout/screen_toolbar.xml b/core/res/res/layout/screen_toolbar.xml
index 039e89f..88c9cf6 100644
--- a/core/res/res/layout/screen_toolbar.xml
+++ b/core/res/res/layout/screen_toolbar.xml
@@ -41,6 +41,7 @@
             android:id="@+id/action_bar"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
+            android:navigationContentDescription="@string/action_bar_up_description"
             style="?attr/toolbarStyle" />
         <com.android.internal.widget.ActionBarContextView
             android:id="@+id/action_context_bar"
diff --git a/graphics/java/android/graphics/drawable/RippleBackground.java b/graphics/java/android/graphics/drawable/RippleBackground.java
index 40a5e18..ab43e01 100644
--- a/graphics/java/android/graphics/drawable/RippleBackground.java
+++ b/graphics/java/android/graphics/drawable/RippleBackground.java
@@ -107,9 +107,6 @@
     /** Whether we have an explicit maximum radius. */
     private boolean mHasMaxRadius;
 
-    /** Whether we were canceled externally and should avoid self-removal. */
-    private boolean mCanceled;
-
     /**
      * Creates a new ripple.
      */
@@ -406,6 +403,9 @@
 
         mHardwareAnimating = true;
 
+        // Set up the software values to match the hardware end values.
+        mOuterOpacity = 0;
+
         invalidateSelf();
     }
 
@@ -414,10 +414,8 @@
      * removing the ripple from the list of animating ripples.
      */
     public void jump() {
-        mCanceled = true;
         endSoftwareAnimations();
         endHardwareAnimations();
-        mCanceled = false;
     }
 
     private void endSoftwareAnimations() {
@@ -446,7 +444,6 @@
         // listener on a pending animation, we also need to remove ourselves.
         if (!mPendingAnimations.isEmpty()) {
             mPendingAnimations.clear();
-            removeSelf();
         }
 
         mHardwareAnimating = false;
@@ -526,10 +523,8 @@
      * the ripple from the list of animating ripples.
      */
     public void cancel() {
-        mCanceled = true;
         cancelSoftwareAnimations();
         cancelHardwareAnimations(true);
-        mCanceled = false;
     }
 
     private void cancelSoftwareAnimations() {
@@ -555,7 +550,6 @@
         for (int i = 0; i < N; i++) {
             runningAnimations.get(i).cancel();
         }
-
         runningAnimations.clear();
 
         if (cancelPending && !mPendingAnimations.isEmpty()) {
@@ -565,13 +559,6 @@
         mHardwareAnimating = false;
     }
 
-    private void removeSelf() {
-        // The owner will invalidate itself.
-        if (!mCanceled) {
-            mOwner.removeBackground(this);
-        }
-    }
-
     private void invalidateSelf() {
         mOwner.invalidateSelf();
     }
@@ -579,7 +566,7 @@
     private final AnimatorListenerAdapter mAnimationListener = new AnimatorListenerAdapter() {
         @Override
         public void onAnimationEnd(Animator animation) {
-            removeSelf();
+            mHardwareAnimating = false;
         }
     };
 
diff --git a/graphics/java/android/graphics/drawable/RippleDrawable.java b/graphics/java/android/graphics/drawable/RippleDrawable.java
index b90fd81..e87a114 100644
--- a/graphics/java/android/graphics/drawable/RippleDrawable.java
+++ b/graphics/java/android/graphics/drawable/RippleDrawable.java
@@ -628,11 +628,10 @@
 
     @Override
     public void draw(@NonNull Canvas canvas) {
-        final boolean isProjected = isProjected();
         final boolean hasMask = mMask != null;
         final boolean drawNonMaskContent = mLayerState.mNum > (hasMask ? 1 : 0);
         final boolean drawMask = hasMask && mMask.getOpacity() != PixelFormat.OPAQUE;
-        final Rect bounds = isProjected ? getDirtyBounds() : getBounds();
+        final Rect bounds = getDirtyBounds();
 
         // If we have content, draw it into a layer first.
         final int contentLayer = drawNonMaskContent ?
@@ -685,13 +684,6 @@
         }
     }
 
-    void removeBackground(RippleBackground background) {
-        if (mBackground == background) {
-            mBackground = null;
-            invalidateSelf();
-        }
-    }
-
     private int getRippleIndex(Ripple ripple) {
         final Ripple[] ripples = mExitingRipples;
         final int count = mExitingRipplesCount;
diff --git a/libs/hwui/renderthread/CanvasContext.cpp b/libs/hwui/renderthread/CanvasContext.cpp
index 491a295..0a17e132 100644
--- a/libs/hwui/renderthread/CanvasContext.cpp
+++ b/libs/hwui/renderthread/CanvasContext.cpp
@@ -110,8 +110,7 @@
 }
 
 void CanvasContext::pauseSurface(ANativeWindow* window) {
-    // TODO: For now we just need a fence, in the future suspend any animations
-    // and such to prevent from trying to render into this surface
+    stopDrawing();
 }
 
 void CanvasContext::setup(int width, int height, const Vector3& lightCenter, float lightRadius,
diff --git a/media/java/android/media/AudioService.java b/media/java/android/media/AudioService.java
index d15085b..dd14c11 100644
--- a/media/java/android/media/AudioService.java
+++ b/media/java/android/media/AudioService.java
@@ -544,6 +544,9 @@
         MAX_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL] = SystemProperties.getInt(
             "ro.config.vc_call_vol_steps",
            MAX_STREAM_VOLUME[AudioSystem.STREAM_VOICE_CALL]);
+        MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC] = SystemProperties.getInt(
+            "ro.config.music_vol_steps",
+           MAX_STREAM_VOLUME[AudioSystem.STREAM_MUSIC]);
 
         sSoundEffectVolumeDb = context.getResources().getInteger(
                 com.android.internal.R.integer.config_soundEffectVolumeDb);
diff --git a/services/core/java/com/android/server/ConnectivityService.java b/services/core/java/com/android/server/ConnectivityService.java
index 0919f77..ef0b155 100644
--- a/services/core/java/com/android/server/ConnectivityService.java
+++ b/services/core/java/com/android/server/ConnectivityService.java
@@ -967,6 +967,17 @@
     }
 
     @Override
+    public Network getNetworkForType(int networkType) {
+        enforceAccessPermission();
+        final int uid = Binder.getCallingUid();
+        if (isNetworkBlocked(networkType, uid)) {
+            return null;
+        }
+        NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType);
+        return (nai == null) ? null : nai.network;
+    }
+
+    @Override
     public Network[] getAllNetworks() {
         enforceAccessPermission();
         final ArrayList<Network> result = new ArrayList();
@@ -1724,7 +1735,7 @@
         pw.println();
 
         synchronized (this) {
-            pw.println("NetworkTranstionWakeLock is currently " +
+            pw.println("NetworkTransitionWakeLock is currently " +
                     (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held.");
             pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy);
         }
diff --git a/services/core/java/com/android/server/NetworkManagementService.java b/services/core/java/com/android/server/NetworkManagementService.java
index f9b65b8..1318f66 100644
--- a/services/core/java/com/android/server/NetworkManagementService.java
+++ b/services/core/java/com/android/server/NetworkManagementService.java
@@ -44,6 +44,7 @@
 import android.net.InterfaceConfiguration;
 import android.net.IpPrefix;
 import android.net.LinkAddress;
+import android.net.Network;
 import android.net.NetworkStats;
 import android.net.NetworkUtils;
 import android.net.RouteInfo;
@@ -1200,10 +1201,12 @@
     }
 
     @Override
-    public void setDnsForwarders(String[] dns) {
+    public void setDnsForwarders(Network network, String[] dns) {
         mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
 
-        final Command cmd = new Command("tether", "dns", "set");
+        int netId = (network != null) ? network.netId : ConnectivityManager.NETID_UNSET;
+        final Command cmd = new Command("tether", "dns", "set", netId);
+
         for (String s : dns) {
             cmd.appendArg(NetworkUtils.numericToInetAddress(s).getHostAddress());
         }
diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java
index 389369f..3770dc2 100755
--- a/services/core/java/com/android/server/am/ActivityManagerService.java
+++ b/services/core/java/com/android/server/am/ActivityManagerService.java
@@ -1107,12 +1107,7 @@
 
     final ActivityThread mSystemThread;
 
-    // Holds the current foreground user's id
     int mCurrentUserId = 0;
-    // Holds the target user's id during a user switch
-    int mTargetUserId = UserHandle.USER_NULL;
-    // If there are multiple profiles for the current user, their ids are here
-    // Currently only the primary user can have managed profiles
     int[] mCurrentProfileIds = new int[] {UserHandle.USER_OWNER}; // Accessed by ActivityStack
 
     /**
@@ -2903,9 +2898,11 @@
             boolean knownToBeDead, int intentFlags, String hostingType, ComponentName hostingName,
             boolean allowWhileBooting, boolean isolated, int isolatedUid, boolean keepIfLarge,
             String abiOverride, String entryPoint, String[] entryPointArgs, Runnable crashHandler) {
+        long startTime = SystemClock.elapsedRealtime();
         ProcessRecord app;
         if (!isolated) {
             app = getProcessRecordLocked(processName, info.uid, keepIfLarge);
+            checkTime(startTime, "startProcess: after getProcessRecord");
         } else {
             // If this is an isolated process, it can't re-use an existing process.
             app = null;
@@ -2927,14 +2924,17 @@
                 if (DEBUG_PROCESSES) Slog.v(TAG, "App already running: " + app);
                 // If this is a new package in the process, add the package to the list
                 app.addPackage(info.packageName, info.versionCode, mProcessStats);
+                checkTime(startTime, "startProcess: done, added package to proc");
                 return app;
             }
 
             // An application record is attached to a previous process,
             // clean it up now.
             if (DEBUG_PROCESSES || DEBUG_CLEANUP) Slog.v(TAG, "App died: " + app);
+            checkTime(startTime, "startProcess: bad proc running, killing");
             Process.killProcessGroup(app.info.uid, app.pid);
             handleAppDiedLocked(app, true, true);
+            checkTime(startTime, "startProcess: done killing old proc");
         }
 
         String hostingNameStr = hostingName != null
@@ -2970,6 +2970,7 @@
         }
 
         if (app == null) {
+            checkTime(startTime, "startProcess: creating new process record");
             app = newProcessRecordLocked(info, processName, isolated, isolatedUid);
             app.crashHandler = crashHandler;
             if (app == null) {
@@ -2981,9 +2982,11 @@
             if (isolated) {
                 mIsolatedProcesses.put(app.uid, app);
             }
+            checkTime(startTime, "startProcess: done creating new process record");
         } else {
             // If this is a new package in the process, add the package to the list
             app.addPackage(info.packageName, info.versionCode, mProcessStats);
+            checkTime(startTime, "startProcess: added package to existing proc");
         }
 
         // If the system is not ready yet, then hold off on starting this
@@ -3000,6 +3003,7 @@
 
         startProcessLocked(
                 app, hostingType, hostingNameStr, abiOverride, entryPoint, entryPointArgs);
+        checkTime(startTime, "startProcess: done starting proc!");
         return (app.pid != 0) ? app : null;
     }
 
@@ -3015,11 +3019,14 @@
 
     private final void startProcessLocked(ProcessRecord app, String hostingType,
             String hostingNameStr, String abiOverride, String entryPoint, String[] entryPointArgs) {
+        long startTime = SystemClock.elapsedRealtime();
         if (app.pid > 0 && app.pid != MY_PID) {
+            checkTime(startTime, "startProcess: removing from pids map");
             synchronized (mPidsSelfLocked) {
                 mPidsSelfLocked.remove(app.pid);
                 mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app);
             }
+            checkTime(startTime, "startProcess: done removing from pids map");
             app.setPid(0);
         }
 
@@ -3027,7 +3034,9 @@
                 "startProcessLocked removing on hold: " + app);
         mProcessesOnHold.remove(app);
 
+        checkTime(startTime, "startProcess: starting to update cpu stats");
         updateCpuStats();
+        checkTime(startTime, "startProcess: done updating cpu stats");
 
         try {
             int uid = app.uid;
@@ -3037,10 +3046,12 @@
             if (!app.isolated) {
                 int[] permGids = null;
                 try {
+                    checkTime(startTime, "startProcess: getting gids from package manager");
                     final PackageManager pm = mContext.getPackageManager();
                     permGids = pm.getPackageGids(app.info.packageName);
 
                     if (Environment.isExternalStorageEmulated()) {
+                        checkTime(startTime, "startProcess: checking external storage perm");
                         if (pm.checkPermission(
                                 android.Manifest.permission.ACCESS_ALL_EXTERNAL_STORAGE,
                                 app.info.packageName) == PERMISSION_GRANTED) {
@@ -3066,6 +3077,7 @@
                 gids[0] = UserHandle.getSharedAppGid(UserHandle.getAppId(uid));
                 gids[1] = UserHandle.getUserGid(UserHandle.getUserId(uid));
             }
+            checkTime(startTime, "startProcess: building args");
             if (mFactoryTest != FactoryTest.FACTORY_TEST_OFF) {
                 if (mFactoryTest == FactoryTest.FACTORY_TEST_LOW_LEVEL
                         && mTopComponent != null
@@ -3109,14 +3121,18 @@
             // the PID of the new process, or else throw a RuntimeException.
             boolean isActivityProcess = (entryPoint == null);
             if (entryPoint == null) entryPoint = "android.app.ActivityThread";
+            checkTime(startTime, "startProcess: asking zygote to start proc");
             Process.ProcessStartResult startResult = Process.start(entryPoint,
                     app.processName, uid, uid, gids, debugFlags, mountExternal,
                     app.info.targetSdkVersion, app.info.seinfo, requiredAbi, entryPointArgs);
+            checkTime(startTime, "startProcess: returned from zygote!");
 
+            checkTime(startTime, "startProcess: noting battery stats update");
             if (app.isolated) {
                 mBatteryStatsService.addIsolatedUid(app.uid, app.info.uid);
             }
             mBatteryStatsService.noteProcessStart(app.processName, app.info.uid);
+            checkTime(startTime, "startProcess: done updating battery stats");
 
             EventLog.writeEvent(EventLogTags.AM_PROC_START,
                     UserHandle.getUserId(uid), startResult.pid, uid,
@@ -3127,6 +3143,7 @@
                 Watchdog.getInstance().processStarted(app.processName, startResult.pid);
             }
 
+            checkTime(startTime, "startProcess: building log message");
             StringBuilder buf = mStringBuilder;
             buf.setLength(0);
             buf.append("Start proc ");
@@ -3164,6 +3181,7 @@
             app.usingWrapper = startResult.usingWrapper;
             app.removed = false;
             app.killedByAm = false;
+            checkTime(startTime, "startProcess: starting to update pids map");
             synchronized (mPidsSelfLocked) {
                 this.mPidsSelfLocked.put(startResult.pid, app);
                 if (isActivityProcess) {
@@ -3173,6 +3191,7 @@
                             ? PROC_START_TIMEOUT_WITH_WRAPPER : PROC_START_TIMEOUT);
                 }
             }
+            checkTime(startTime, "startProcess: done updating pids map");
         } catch (RuntimeException e) {
             // XXX do better error recovery.
             app.setPid(0);
@@ -10277,13 +10296,13 @@
                 if (r == null) {
                     return false;
                 }
-                final boolean translucentChanged = r.changeWindowTranslucency(true);
-                if (translucentChanged) {
+                if (r.changeWindowTranslucency(true)) {
+                    mWindowManager.setAppFullscreen(token, true);
                     r.task.stack.releaseBackgroundResources();
                     mStackSupervisor.ensureActivitiesVisibleLocked(null, 0);
+                    return true;
                 }
-                mWindowManager.setAppFullscreen(token, true);
-                return translucentChanged;
+                return false;
             }
         } finally {
             Binder.restoreCallingIdentity(origId);
@@ -10304,13 +10323,15 @@
                     ActivityRecord under = r.task.mActivities.get(index - 1);
                     under.returningOptions = options;
                 }
-                final boolean translucentChanged = r.changeWindowTranslucency(false);
-                if (translucentChanged) {
+                if (r.changeWindowTranslucency(false)) {
                     r.task.stack.convertToTranslucent(r);
+                    mWindowManager.setAppFullscreen(token, false);
                     mStackSupervisor.ensureActivitiesVisibleLocked(null, 0);
+                    return true;
+                } else {
+                    mStackSupervisor.ensureActivitiesVisibleLocked(null, 0);
+                    return false;
                 }
-                mWindowManager.setAppFullscreen(token, false);
-                return translucentChanged;
             }
         } finally {
             Binder.restoreCallingIdentity(origId);
@@ -17940,7 +17961,6 @@
                 return false;
             }
             userName = userInfo.name;
-            mTargetUserId = userId;
         }
         mHandler.removeMessages(START_USER_SWITCH_MSG);
         mHandler.sendMessage(mHandler.obtainMessage(START_USER_SWITCH_MSG, userId, 0, userName));
@@ -18008,7 +18028,6 @@
 
                 if (foreground) {
                     mCurrentUserId = userId;
-                    mTargetUserId = UserHandle.USER_NULL; // reset, mCurrentUserId has caught up
                     updateCurrentProfileIdsLocked();
                     mWindowManager.setCurrentUser(userId, mCurrentProfileIds);
                     // Once the internal notion of the active user has switched, we lock the device
@@ -18376,7 +18395,7 @@
 
     private int stopUserLocked(final int userId, final IStopUserCallback callback) {
         if (DEBUG_MU) Slog.i(TAG_MU, "stopUserLocked userId=" + userId);
-        if (mCurrentUserId == userId && mTargetUserId == UserHandle.USER_NULL) {
+        if (mCurrentUserId == userId) {
             return ActivityManager.USER_OP_IS_CURRENT;
         }
 
@@ -18516,13 +18535,12 @@
             throw new SecurityException(msg);
         }
         synchronized (this) {
-            int userId = mTargetUserId != UserHandle.USER_NULL ? mTargetUserId : mCurrentUserId;
-            return getUserManagerLocked().getUserInfo(userId);
+            return getUserManagerLocked().getUserInfo(mCurrentUserId);
         }
     }
 
     int getCurrentUserIdLocked() {
-        return mTargetUserId != UserHandle.USER_NULL ? mTargetUserId : mCurrentUserId;
+        return mCurrentUserId;
     }
 
     @Override
diff --git a/services/core/java/com/android/server/connectivity/Tethering.java b/services/core/java/com/android/server/connectivity/Tethering.java
index 1fd114c..7c303ff 100644
--- a/services/core/java/com/android/server/connectivity/Tethering.java
+++ b/services/core/java/com/android/server/connectivity/Tethering.java
@@ -31,6 +31,7 @@
 import android.net.InterfaceConfiguration;
 import android.net.LinkAddress;
 import android.net.LinkProperties;
+import android.net.Network;
 import android.net.NetworkInfo;
 import android.net.NetworkUtils;
 import android.net.RouteInfo;
@@ -56,6 +57,7 @@
 import java.net.InetAddress;
 import java.net.Inet4Address;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.Iterator;
@@ -742,7 +744,7 @@
         static final int CMD_IP_FORWARDING_ENABLE_ERROR  =  7;
         // notification from the master SM that it had trouble disabling IP Forwarding
         static final int CMD_IP_FORWARDING_DISABLE_ERROR =  8;
-        // notification from the master SM that it had trouble staring tethering
+        // notification from the master SM that it had trouble starting tethering
         static final int CMD_START_TETHERING_ERROR       =  9;
         // notification from the master SM that it had trouble stopping tethering
         static final int CMD_STOP_TETHERING_ERROR        = 10;
@@ -1235,12 +1237,6 @@
                         return false;
                     }
                 }
-                try {
-                    mNMService.setDnsForwarders(mDefaultDnsServers);
-                } catch (Exception e) {
-                    transitionTo(mSetDnsForwardersErrorState);
-                    return false;
-                }
                 return true;
             }
             protected boolean turnOffMasterTetherSettings() {
@@ -1348,8 +1344,17 @@
                             }
                         }
                         try {
-                            mNMService.setDnsForwarders(dnsServers);
+                            Network network = getConnectivityManager().getNetworkForType(upType);
+                            if (network == null) {
+                                Log.e(TAG, "No Network for upstream type " + upType + "!");
+                            }
+                            if (VDBG) {
+                                Log.d(TAG, "Setting DNS forwarders: Network=" + network +
+                                       ", dnsServers=" + Arrays.toString(dnsServers));
+                            }
+                            mNMService.setDnsForwarders(network, dnsServers);
                         } catch (Exception e) {
+                            Log.e(TAG, "Setting DNS forwarders failed!");
                             transitionTo(mSetDnsForwardersErrorState);
                         }
                     }