Merge "Add support to change IPv6 privacy extensions"
diff --git a/api/current.txt b/api/current.txt
index 3890aac..a8bdaea 100644
--- a/api/current.txt
+++ b/api/current.txt
@@ -10869,19 +10869,9 @@
     method public abstract void onScanCompleted(java.lang.String, android.net.Uri);
   }
 
-  public class Metadata {
-    method public boolean getBoolean(int);
-    method public boolean has(int);
-    method public java.util.Set<java.lang.Integer> keySet();
-    field public static final int PAUSE_AVAILABLE = 1; // 0x1
-    field public static final int SEEK_AVAILABLE = 4; // 0x4
-    field public static final int SEEK_BACKWARD_AVAILABLE = 2; // 0x2
-    field public static final int SEEK_FORWARD_AVAILABLE = 3; // 0x3
-  }
-
   public class RemoteControlClient {
-    ctor public RemoteControlClient(android.content.ComponentName);
-    ctor public RemoteControlClient(android.content.ComponentName, android.os.Looper);
+    ctor public RemoteControlClient(android.app.PendingIntent);
+    ctor public RemoteControlClient(android.app.PendingIntent, android.os.Looper);
     method public android.media.RemoteControlClient.MetadataEditor editMetadata(boolean);
     method public void setPlaybackState(int);
     method public void setTransportControlFlags(int);
@@ -11775,7 +11765,6 @@
     method public static void incrementOperationCount(int);
     method public static void incrementOperationCount(int, int);
     method public static void setThreadStatsTag(int);
-    method public static deprecated void setThreadStatsTag(java.lang.String);
     method public static void tagSocket(java.net.Socket) throws java.net.SocketException;
     method public static void untagSocket(java.net.Socket) throws java.net.SocketException;
     field public static final int UNSUPPORTED = -1; // 0xffffffff
diff --git a/core/java/android/accounts/AccountManager.java b/core/java/android/accounts/AccountManager.java
index be9070d..dbf4de8 100644
--- a/core/java/android/accounts/AccountManager.java
+++ b/core/java/android/accounts/AccountManager.java
@@ -815,7 +815,10 @@
             final Activity activity, AccountManagerCallback<Bundle> callback, Handler handler) {
         if (account == null) throw new IllegalArgumentException("account is null");
         if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
-        final Bundle optionsIn = options == null ? new Bundle() : options;
+        final Bundle optionsIn = new Bundle();
+        if (options != null) {
+            optionsIn.putAll(options);
+        }
         optionsIn.putString(KEY_ANDROID_PACKAGE_NAME, mContext.getPackageName());
         return new AmsTask(activity, handler, callback) {
             public void doWork() throws RemoteException {
@@ -982,7 +985,10 @@
 
         if (account == null) throw new IllegalArgumentException("account is null");
         if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
-        final Bundle optionsIn = options == null ? new Bundle() : options;
+        final Bundle optionsIn = new Bundle();
+        if (options != null) {
+            optionsIn.putAll(options);
+        }
         optionsIn.putString(KEY_ANDROID_PACKAGE_NAME, mContext.getPackageName());
         return new AmsTask(null, handler, callback) {
             public void doWork() throws RemoteException {
@@ -1045,14 +1051,16 @@
             final Bundle addAccountOptions,
             final Activity activity, AccountManagerCallback<Bundle> callback, Handler handler) {
         if (accountType == null) throw new IllegalArgumentException("accountType is null");
-        final Bundle options = (addAccountOptions == null) ? new Bundle() :
-            addAccountOptions;
-        options.putString(KEY_ANDROID_PACKAGE_NAME, mContext.getPackageName());
+        final Bundle optionsIn = new Bundle();
+        if (addAccountOptions != null) {
+            optionsIn.putAll(addAccountOptions);
+        }
+        optionsIn.putString(KEY_ANDROID_PACKAGE_NAME, mContext.getPackageName());
 
         return new AmsTask(activity, handler, callback) {
             public void doWork() throws RemoteException {
                 mService.addAcount(mResponse, accountType, authTokenType,
-                        requiredFeatures, activity != null, options);
+                        requiredFeatures, activity != null, optionsIn);
             }
         }.start();
     }
diff --git a/core/java/android/app/ApplicationPackageManager.java b/core/java/android/app/ApplicationPackageManager.java
index 4b2a8d2..bd42e34 100644
--- a/core/java/android/app/ApplicationPackageManager.java
+++ b/core/java/android/app/ApplicationPackageManager.java
@@ -42,6 +42,7 @@
 import android.content.pm.ServiceInfo;
 import android.content.pm.UserInfo;
 import android.content.pm.ManifestDigest;
+import android.content.pm.VerifierDeviceIdentity;
 import android.content.res.Resources;
 import android.content.res.XmlResourceParser;
 import android.graphics.drawable.Drawable;
@@ -1208,6 +1209,19 @@
         // TODO:
     }
 
+    /**
+     * @hide
+     */
+    @Override
+    public VerifierDeviceIdentity getVerifierDeviceIdentity() {
+        try {
+            return mPM.getVerifierDeviceIdentity();
+        } catch (RemoteException e) {
+            // Should never happen!
+        }
+        return null;
+    }
+
     private final ContextImpl mContext;
     private final IPackageManager mPM;
 
diff --git a/core/java/android/content/pm/ActivityInfo.aidl b/core/java/android/content/pm/ActivityInfo.aidl
old mode 100755
new mode 100644
diff --git a/core/java/android/content/pm/ApplicationInfo.aidl b/core/java/android/content/pm/ApplicationInfo.aidl
old mode 100755
new mode 100644
diff --git a/core/java/android/content/pm/ConfigurationInfo.java b/core/java/android/content/pm/ConfigurationInfo.java
old mode 100755
new mode 100644
diff --git a/core/java/android/content/pm/FeatureInfo.aidl b/core/java/android/content/pm/FeatureInfo.aidl
old mode 100755
new mode 100644
diff --git a/core/java/android/content/pm/IPackageDataObserver.aidl b/core/java/android/content/pm/IPackageDataObserver.aidl
old mode 100755
new mode 100644
diff --git a/core/java/android/content/pm/IPackageManager.aidl b/core/java/android/content/pm/IPackageManager.aidl
index 08aef16..5e6e768 100644
--- a/core/java/android/content/pm/IPackageManager.aidl
+++ b/core/java/android/content/pm/IPackageManager.aidl
@@ -38,6 +38,7 @@
 import android.content.pm.ResolveInfo;
 import android.content.pm.ServiceInfo;
 import android.content.pm.UserInfo;
+import android.content.pm.VerifierDeviceIdentity;
 import android.net.Uri;
 import android.content.IntentSender;
 
@@ -359,4 +360,6 @@
             in ManifestDigest manifestDigest);
 
     void verifyPendingInstall(int id, boolean verified, in String message);
+
+    VerifierDeviceIdentity getVerifierDeviceIdentity();
 }
diff --git a/core/java/android/content/pm/IPackageStatsObserver.aidl b/core/java/android/content/pm/IPackageStatsObserver.aidl
old mode 100755
new mode 100644
diff --git a/core/java/android/content/pm/InstrumentationInfo.aidl b/core/java/android/content/pm/InstrumentationInfo.aidl
old mode 100755
new mode 100644
diff --git a/core/java/android/content/pm/ManifestDigest.aidl b/core/java/android/content/pm/ManifestDigest.aidl
old mode 100755
new mode 100644
diff --git a/core/java/android/content/pm/PackageInfo.aidl b/core/java/android/content/pm/PackageInfo.aidl
old mode 100755
new mode 100644
diff --git a/core/java/android/content/pm/PackageInfoLite.aidl b/core/java/android/content/pm/PackageInfoLite.aidl
old mode 100755
new mode 100644
diff --git a/core/java/android/content/pm/PackageManager.java b/core/java/android/content/pm/PackageManager.java
index b4e3988..dcb6776 100644
--- a/core/java/android/content/pm/PackageManager.java
+++ b/core/java/android/content/pm/PackageManager.java
@@ -2614,4 +2614,14 @@
     public static int getAppId(int uid) {
         return uid % PER_USER_RANGE;
     }
+
+    /**
+     * Returns the device identity that verifiers can use to associate their
+     * scheme to a particular device. This should not be used by anything other
+     * than a package verifier.
+     *
+     * @return identity that uniquely identifies current device
+     * @hide
+     */
+    public abstract VerifierDeviceIdentity getVerifierDeviceIdentity();
 }
diff --git a/core/java/android/content/pm/PackageStats.aidl b/core/java/android/content/pm/PackageStats.aidl
old mode 100755
new mode 100644
diff --git a/core/java/android/content/pm/PackageStats.java b/core/java/android/content/pm/PackageStats.java
old mode 100755
new mode 100644
diff --git a/core/java/android/content/pm/ParceledListSlice.aidl b/core/java/android/content/pm/ParceledListSlice.aidl
old mode 100755
new mode 100644
diff --git a/core/java/android/content/pm/PermissionGroupInfo.aidl b/core/java/android/content/pm/PermissionGroupInfo.aidl
old mode 100755
new mode 100644
diff --git a/core/java/android/content/pm/PermissionInfo.aidl b/core/java/android/content/pm/PermissionInfo.aidl
old mode 100755
new mode 100644
diff --git a/core/java/android/content/pm/ProviderInfo.aidl b/core/java/android/content/pm/ProviderInfo.aidl
old mode 100755
new mode 100644
diff --git a/core/java/android/content/pm/ResolveInfo.aidl b/core/java/android/content/pm/ResolveInfo.aidl
old mode 100755
new mode 100644
diff --git a/core/java/android/content/pm/ServiceInfo.aidl b/core/java/android/content/pm/ServiceInfo.aidl
old mode 100755
new mode 100644
diff --git a/core/java/android/content/pm/Signature.aidl b/core/java/android/content/pm/Signature.aidl
old mode 100755
new mode 100644
diff --git a/core/java/android/content/pm/VerifierDeviceIdentity.aidl b/core/java/android/content/pm/VerifierDeviceIdentity.aidl
new file mode 100644
index 0000000..eb076ae
--- /dev/null
+++ b/core/java/android/content/pm/VerifierDeviceIdentity.aidl
@@ -0,0 +1,19 @@
+/*
+ * Copyright 2011, 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 android.content.pm;
+
+parcelable VerifierDeviceIdentity;
diff --git a/core/java/android/content/pm/VerifierDeviceIdentity.java b/core/java/android/content/pm/VerifierDeviceIdentity.java
new file mode 100644
index 0000000..bfebe0f
--- /dev/null
+++ b/core/java/android/content/pm/VerifierDeviceIdentity.java
@@ -0,0 +1,231 @@
+/*
+ * Copyright (C) 2011 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 android.content.pm;
+
+import android.os.Parcel;
+import android.os.Parcelable;
+
+import java.io.UnsupportedEncodingException;
+import java.security.SecureRandom;
+import java.util.Random;
+
+/**
+ * An identity that uniquely identifies a particular device. In this
+ * implementation, the identity is represented as a 64-bit integer encoded to a
+ * 13-character string using RFC 4648's Base32 encoding without the trailing
+ * padding. This makes it easy for users to read and write the code without
+ * confusing 'I' (letter) with '1' (one) or 'O' (letter) with '0' (zero).
+ *
+ * @hide
+ */
+public class VerifierDeviceIdentity implements Parcelable {
+    /**
+     * Encoded size of a long (64-bit) into Base32. This format will end up
+     * looking like XXXX-XXXX-XXXX-X (length ignores hyphens) when applied with
+     * the GROUP_SIZE below.
+     */
+    private static final int LONG_SIZE = 13;
+
+    /**
+     * Size of groupings when outputting as strings. This helps people read it
+     * out and keep track of where they are.
+     */
+    private static final int GROUP_SIZE = 4;
+
+    private final long mIdentity;
+
+    private final String mIdentityString;
+
+    /**
+     * Create a verifier device identity from a long.
+     *
+     * @param identity device identity in a 64-bit integer.
+     * @throws
+     */
+    public VerifierDeviceIdentity(long identity) {
+        mIdentity = identity;
+        mIdentityString = encodeBase32(identity);
+    }
+
+    private VerifierDeviceIdentity(Parcel source) {
+        final long identity = source.readLong();
+
+        mIdentity = identity;
+        mIdentityString = encodeBase32(identity);
+    }
+
+    /**
+     * Generate a new device identity.
+     *
+     * @return random uniformly-distributed device identity
+     */
+    public static VerifierDeviceIdentity generate() {
+        final SecureRandom sr = new SecureRandom();
+        return generate(sr);
+    }
+
+    /**
+     * Generate a new device identity using a provided random number generator
+     * class. This is used for testing.
+     *
+     * @param rng random number generator to retrieve the next long from
+     * @return verifier device identity based on the input from the provided
+     *         random number generator
+     */
+    static VerifierDeviceIdentity generate(Random rng) {
+        long identity = rng.nextLong();
+        return new VerifierDeviceIdentity(identity);
+    }
+
+    private static final char ENCODE[] = {
+        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
+        'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
+        'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
+        'Y', 'Z', '2', '3', '4', '5', '6', '7',
+    };
+
+    private static final char SEPARATOR = '-';
+
+    private static final String encodeBase32(long input) {
+        final char[] alphabet = ENCODE;
+
+        /*
+         * Make a character array with room for the separators between each
+         * group.
+         */
+        final char encoded[] = new char[LONG_SIZE + (LONG_SIZE / GROUP_SIZE)];
+
+        int index = encoded.length;
+        for (int i = 0; i < LONG_SIZE; i++) {
+            /*
+             * Make sure we don't put a separator at the beginning. Since we're
+             * building from the rear of the array, we use (LONG_SIZE %
+             * GROUP_SIZE) to make the odd-size group appear at the end instead
+             * of the beginning.
+             */
+            if (i > 0 && (i % GROUP_SIZE) == (LONG_SIZE % GROUP_SIZE)) {
+                encoded[--index] = SEPARATOR;
+            }
+
+            /*
+             * Extract 5 bits of data, then shift it out.
+             */
+            final int group = (int) (input & 0x1F);
+            input >>>= 5;
+
+            encoded[--index] = alphabet[group];
+        }
+
+        return String.valueOf(encoded);
+    }
+
+    // TODO move this out to its own class (android.util.Base32)
+    private static final long decodeBase32(byte[] input) throws IllegalArgumentException {
+        long output = 0L;
+        int numParsed = 0;
+
+        final int N = input.length;
+        for (int i = 0; i < N; i++) {
+            final int group = input[i];
+
+            /*
+             * This essentially does the reverse of the ENCODED alphabet above
+             * without a table. A..Z are 0..25 and 2..7 are 26..31.
+             */
+            final int value;
+            if ('A' <= group && group <= 'Z') {
+                value = group - 'A';
+            } else if ('2' <= group && group <= '7') {
+                value = group - ('2' - 26);
+            } else if (group == SEPARATOR) {
+                continue;
+            } else {
+                throw new IllegalArgumentException("base base-32 character: " + group);
+            }
+
+            output = (output << 5) | value;
+            numParsed++;
+
+            if (numParsed == 1) {
+                if ((value & 0xF) != value) {
+                    throw new IllegalArgumentException("illegal start character; will overflow");
+                }
+            } else if (numParsed > 13) {
+                throw new IllegalArgumentException("too long; should have 13 characters");
+            }
+        }
+
+        if (numParsed != 13) {
+            throw new IllegalArgumentException("too short; should have 13 characters");
+        }
+
+        return output;
+    }
+
+    @Override
+    public int hashCode() {
+        return (int) mIdentity;
+    }
+
+    @Override
+    public boolean equals(Object other) {
+        if (!(other instanceof VerifierDeviceIdentity)) {
+            return false;
+        }
+
+        final VerifierDeviceIdentity o = (VerifierDeviceIdentity) other;
+        return mIdentity == o.mIdentity;
+    }
+
+    @Override
+    public String toString() {
+        return mIdentityString;
+    }
+
+    public static VerifierDeviceIdentity parse(String deviceIdentity)
+            throws IllegalArgumentException {
+        final byte[] input;
+        try {
+            input = deviceIdentity.getBytes("US-ASCII");
+        } catch (UnsupportedEncodingException e) {
+            throw new IllegalArgumentException("bad base-32 characters in input");
+        }
+
+        return new VerifierDeviceIdentity(decodeBase32(input));
+    }
+
+    @Override
+    public int describeContents() {
+        return 0;
+    }
+
+    @Override
+    public void writeToParcel(Parcel dest, int flags) {
+        dest.writeLong(mIdentity);
+    }
+
+    public static final Parcelable.Creator<VerifierDeviceIdentity> CREATOR
+            = new Parcelable.Creator<VerifierDeviceIdentity>() {
+        public VerifierDeviceIdentity createFromParcel(Parcel source) {
+            return new VerifierDeviceIdentity(source);
+        }
+
+        public VerifierDeviceIdentity[] newArray(int size) {
+            return new VerifierDeviceIdentity[size];
+        }
+    };
+}
diff --git a/core/java/android/inputmethodservice/InputMethodService.java b/core/java/android/inputmethodservice/InputMethodService.java
index 440d649..d2effff 100644
--- a/core/java/android/inputmethodservice/InputMethodService.java
+++ b/core/java/android/inputmethodservice/InputMethodService.java
@@ -688,6 +688,10 @@
         mRootView.getViewTreeObserver().removeOnComputeInternalInsetsListener(
                 mInsetsComputer);
         if (mWindowAdded) {
+            // Disable exit animation for the current IME window
+            // to avoid the race condition between the exit and enter animations
+            // when the current IME is being switched to another one.
+            mWindow.getWindow().setWindowAnimations(0);
             mWindow.dismiss();
         }
     }
diff --git a/core/java/android/net/TrafficStats.java b/core/java/android/net/TrafficStats.java
index ec3b1e1..47cfa73 100644
--- a/core/java/android/net/TrafficStats.java
+++ b/core/java/android/net/TrafficStats.java
@@ -104,14 +104,6 @@
         return NetworkManagementSocketTagger.getThreadSocketStatsTag();
     }
 
-    /**
-     * @deprecated unsupported, will eventually be removed
-     */
-    @Deprecated
-    public static void setThreadStatsTag(String tag) {
-        setThreadStatsTag(tag.hashCode());
-    }
-
     public static void clearThreadStatsTag() {
         NetworkManagementSocketTagger.setThreadSocketStatsTag(-1);
     }
diff --git a/core/java/android/provider/Telephony.java b/core/java/android/provider/Telephony.java
old mode 100644
new mode 100755
index 6585e82..e849b71
--- a/core/java/android/provider/Telephony.java
+++ b/core/java/android/provider/Telephony.java
@@ -1798,6 +1798,20 @@
         public static final String ROAMING_PROTOCOL = "roaming_protocol";
 
         public static final String CURRENT = "current";
+
+        /**
+          * Current status of APN
+          * true : enabled APN, false : disabled APN.
+          */
+        public static final String CARRIER_ENABLED = "carrier_enabled";
+
+        /**
+          * Radio Access Technology info
+          * To check what values can hold, refer to ServiceState.java.
+          * This should be spread to other technologies,
+          * but currently only used for LTE(14) and EHRPD(13).
+          */
+        public static final String BEARER = "bearer";
     }
 
     public static final class Intents {
diff --git a/core/java/android/server/BluetoothEventLoop.java b/core/java/android/server/BluetoothEventLoop.java
index 56da69d..1b473ec 100644
--- a/core/java/android/server/BluetoothEventLoop.java
+++ b/core/java/android/server/BluetoothEventLoop.java
@@ -401,6 +401,7 @@
             Intent intent = new Intent(BluetoothDevice.ACTION_NAME_CHANGED);
             intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
             intent.putExtra(BluetoothDevice.EXTRA_NAME, propValues[1]);
+            intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
             mContext.sendBroadcast(intent, BLUETOOTH_PERM);
         } else if (name.equals("Alias")) {
             mBluetoothService.setRemoteDeviceProperty(address, name, propValues[1]);
@@ -410,6 +411,7 @@
             intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
             intent.putExtra(BluetoothDevice.EXTRA_CLASS,
                     new BluetoothClass(Integer.valueOf(propValues[1])));
+            intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
             mContext.sendBroadcast(intent, BLUETOOTH_PERM);
         } else if (name.equals("Connected")) {
             mBluetoothService.setRemoteDeviceProperty(address, name, propValues[1]);
@@ -425,6 +427,7 @@
                 intent = new Intent(BluetoothDevice.ACTION_ACL_DISCONNECTED);
             }
             intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
+            intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
             mContext.sendBroadcast(intent, BLUETOOTH_PERM);
         } else if (name.equals("UUIDs")) {
             String uuid = null;
diff --git a/core/java/android/server/BluetoothHealthProfileHandler.java b/core/java/android/server/BluetoothHealthProfileHandler.java
index 1d8afe3..2961fd2 100644
--- a/core/java/android/server/BluetoothHealthProfileHandler.java
+++ b/core/java/android/server/BluetoothHealthProfileHandler.java
@@ -29,12 +29,12 @@
 import android.os.RemoteException;
 import android.util.Log;
 
-import java.io.FileDescriptor;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map.Entry;
+import java.util.concurrent.atomic.AtomicInteger;
 
 /**
  * This handles all the operations on the Bluetooth Health profile.
@@ -58,6 +58,7 @@
     private static final int MESSAGE_REGISTER_APPLICATION = 0;
     private static final int MESSAGE_UNREGISTER_APPLICATION = 1;
     private static final int MESSAGE_CONNECT_CHANNEL = 2;
+    private static final AtomicInteger sChannelId = new AtomicInteger();
 
     class HealthChannel {
         private ParcelFileDescriptor mChannelFd;
@@ -67,6 +68,7 @@
         private BluetoothHealthAppConfiguration mConfig;
         private int mState;
         private int mChannelType;
+        private int mId;
 
         HealthChannel(BluetoothDevice device, BluetoothHealthAppConfiguration config,
                 ParcelFileDescriptor fd, boolean mainChannel, String channelPath) {
@@ -76,6 +78,7 @@
              mDevice = device;
              mConfig = config;
              mState = BluetoothHealth.STATE_CHANNEL_DISCONNECTED;
+             mId = getChannelId();
         }
     }
 
@@ -117,7 +120,7 @@
                 for (HealthChannel chan : mHealthChannels) {
                     if (chan.mConfig.equals(unregisterApp) &&
                             chan.mState != BluetoothHealth.STATE_CHANNEL_DISCONNECTED) {
-                        disconnectChannel(chan.mDevice, unregisterApp, chan.hashCode());
+                        disconnectChannel(chan.mDevice, unregisterApp, chan.mId);
                     }
                 }
 
@@ -141,11 +144,11 @@
                 String channelType = getStringChannelType(chan.mChannelType);
 
                 if (!mBluetoothService.createChannelNative(deviceObjectPath, configPath,
-                          channelType, chan.hashCode())) {
+                          channelType, chan.mId)) {
                     int prevState = chan.mState;
                     int state = BluetoothHealth.STATE_CHANNEL_DISCONNECTED;
                     callHealthChannelCallback(chan.mConfig, chan.mDevice, prevState, state, null,
-                            chan.hashCode());
+                            chan.mId);
                     mHealthChannels.remove(chan);
                 }
             }
@@ -216,7 +219,7 @@
 
         int prevState = BluetoothHealth.STATE_CHANNEL_DISCONNECTED;
         int state = BluetoothHealth.STATE_CHANNEL_CONNECTING;
-        callHealthChannelCallback(config, device, prevState, state, null, chan.hashCode());
+        callHealthChannelCallback(config, device, prevState, state, null, chan.mId);
 
         Message msg = mHandler.obtainMessage(MESSAGE_CONNECT_CHANNEL);
         msg.obj = chan;
@@ -245,6 +248,23 @@
         }
     }
 
+    private int getChannelId() {
+        // The function doesn't need to be synchronized, as the health profile handler
+        // will only allow one health channel object creation at a time.
+        // In the worst case the while loop will have to break out at some point of
+        // time, because only a limited number of L2CAP channels are possible.
+        int id;
+        boolean found;
+        do {
+            id = sChannelId.incrementAndGet();
+            found = false;
+            for (HealthChannel chan: mHealthChannels) {
+                if (chan.mId == id) found = true;
+            }
+        } while (found);
+        return id;
+    }
+
     boolean disconnectChannel(BluetoothDevice device,
             BluetoothHealthAppConfiguration config, int id) {
         HealthChannel chan = findChannelById(id);
@@ -260,14 +280,14 @@
         int prevState = chan.mState;
         chan.mState = BluetoothHealth.STATE_CHANNEL_DISCONNECTING;
         callHealthChannelCallback(config, device, prevState, chan.mState,
-                null, chan.hashCode());
+                null, chan.mId);
 
         if (!mBluetoothService.destroyChannelNative(deviceObjectPath, chan.mChannelPath,
-                                                    chan.hashCode())) {
+                                                    chan.mId)) {
             prevState = chan.mState;
             chan.mState = BluetoothHealth.STATE_CHANNEL_CONNECTED;
             callHealthChannelCallback(config, device, prevState, chan.mState,
-                    chan.mChannelFd, chan.hashCode());
+                    chan.mChannelFd, chan.mId);
             return false;
         } else {
             return true;
@@ -276,7 +296,7 @@
 
     private HealthChannel findChannelById(int id) {
         for (HealthChannel chan : mHealthChannels) {
-            if (chan.hashCode() == id) return chan;
+            if (chan.mId == id) return chan;
         }
         return null;
     }
@@ -434,7 +454,7 @@
             fd = mBluetoothService.getChannelFdNative(channelPath);
             if (fd == null) {
                 errorLog("Error obtaining fd for channel:" + channelPath);
-                disconnectChannel(device, config, channel.hashCode());
+                disconnectChannel(device, config, channel.mId);
                 return;
             }
             boolean mainChannel =
@@ -467,7 +487,7 @@
         }
         channel.mState = state;
         callHealthChannelCallback(config, device, prevState, state, channel.mChannelFd,
-                channel.hashCode());
+                channel.mId);
     }
 
     private void callHealthChannelCallback(BluetoothHealthAppConfiguration config,
diff --git a/core/java/android/server/BluetoothService.java b/core/java/android/server/BluetoothService.java
index 00d3331..f0fb4e0 100755
--- a/core/java/android/server/BluetoothService.java
+++ b/core/java/android/server/BluetoothService.java
@@ -145,7 +145,7 @@
     private final ArrayList<String> mUuidIntentTracker;
     private final HashMap<RemoteService, IBluetoothCallback> mUuidCallbackTracker;
 
-    private final HashMap<Integer, Integer> mServiceRecordToPid;
+    private final HashMap<Integer, Pair<Integer, IBinder>> mServiceRecordToPid;
 
     private final HashMap<String, BluetoothDeviceProfileState> mDeviceProfileState;
     private final BluetoothProfileState mA2dpProfileState;
@@ -221,7 +221,7 @@
         mDeviceOobData = new HashMap<String, Pair<byte[], byte[]>>();
         mUuidIntentTracker = new ArrayList<String>();
         mUuidCallbackTracker = new HashMap<RemoteService, IBluetoothCallback>();
-        mServiceRecordToPid = new HashMap<Integer, Integer>();
+        mServiceRecordToPid = new HashMap<Integer, Pair<Integer, IBinder>>();
         mDeviceProfileState = new HashMap<String, BluetoothDeviceProfileState>();
         mA2dpProfileState = new BluetoothProfileState(mContext, BluetoothProfileState.A2DP);
         mHfpProfileState = new BluetoothProfileState(mContext, BluetoothProfileState.HFP);
@@ -1516,10 +1516,10 @@
         }
 
         int pid = Binder.getCallingPid();
-        mServiceRecordToPid.put(new Integer(handle), new Integer(pid));
+        mServiceRecordToPid.put(new Integer(handle), new Pair<Integer, IBinder>(pid, b));
         try {
             b.linkToDeath(new Reaper(handle, pid, RFCOMM_RECORD_REAPER), 0);
-        } catch (RemoteException e) {}
+        } catch (RemoteException e) {Log.e(TAG, "", e);}
         return handle;
     }
 
@@ -1532,12 +1532,12 @@
     }
 
     private synchronized void checkAndRemoveRecord(int handle, int pid) {
-        Integer handleInt = new Integer(handle);
-        Integer owner = mServiceRecordToPid.get(handleInt);
+        Pair<Integer, IBinder> pidPair = mServiceRecordToPid.get(handle);
+        Integer owner = pidPair.first;
         if (owner != null && pid == owner.intValue()) {
             if (DBG) Log.d(TAG, "Removing service record " +
                 Integer.toHexString(handle) + " for pid " + pid);
-            mServiceRecordToPid.remove(handleInt);
+            mServiceRecordToPid.remove(handle);
             removeServiceRecordNative(handle);
         }
     }
@@ -1593,6 +1593,7 @@
             try {
                 binder.linkToDeath(new Reaper(pid, STATE_CHANGE_REAPER), 0);
             } catch (RemoteException e) {
+                Log.e(TAG, "", e);
                 return false;
             }
         }
@@ -1867,7 +1868,7 @@
     private void dumpApplicationServiceRecords(PrintWriter pw) {
         pw.println("\n--Application Service Records--");
         for (Integer handle : mServiceRecordToPid.keySet()) {
-            Integer pid = mServiceRecordToPid.get(handle);
+            Integer pid = mServiceRecordToPid.get(handle).first;
             pw.println("\tpid " + pid + " handle " + Integer.toHexString(handle));
         }
         mAdapter.closeProfileProxy(BluetoothProfile.PAN, mBluetoothHeadset);
diff --git a/core/java/android/speech/tts/AudioPlaybackHandler.java b/core/java/android/speech/tts/AudioPlaybackHandler.java
index 75dcd6e..d970ae6 100644
--- a/core/java/android/speech/tts/AudioPlaybackHandler.java
+++ b/core/java/android/speech/tts/AudioPlaybackHandler.java
@@ -450,6 +450,19 @@
      */
     private static final long MIN_SLEEP_TIME_MS = 20;
 
+    /**
+     * The maximum increment of time to sleep while waiting for an audiotrack
+     * to finish playing.
+     */
+    private static final long MAX_SLEEP_TIME_MS = 2500;
+
+    /**
+     * The maximum amount of time to wait for an audio track to make progress while
+     * it remains in PLAYSTATE_PLAYING. This should never happen in normal usage, but
+     * could happen in exceptional circumstances like a media_server crash.
+     */
+    private static final long MAX_PROGRESS_WAIT_MS = MAX_SLEEP_TIME_MS;
+
     private static void blockUntilDone(SynthesisMessageParams params) {
         if (params.mAudioTrack == null || params.mBytesWritten <= 0) {
             return;
@@ -490,16 +503,34 @@
         final AudioTrack audioTrack = params.mAudioTrack;
         final int lengthInFrames = params.mBytesWritten / params.mBytesPerFrame;
 
+        int previousPosition = -1;
         int currentPosition = 0;
-        while ((currentPosition = audioTrack.getPlaybackHeadPosition()) < lengthInFrames) {
-            if (audioTrack.getPlayState() != AudioTrack.PLAYSTATE_PLAYING) {
-                break;
-            }
+        long blockedTimeMs = 0;
+
+        while ((currentPosition = audioTrack.getPlaybackHeadPosition()) < lengthInFrames &&
+                audioTrack.getPlayState() == AudioTrack.PLAYSTATE_PLAYING) {
 
             final long estimatedTimeMs = ((lengthInFrames - currentPosition) * 1000) /
                     audioTrack.getSampleRate();
+            final long sleepTimeMs = clip(estimatedTimeMs, MIN_SLEEP_TIME_MS, MAX_SLEEP_TIME_MS);
 
-            final long sleepTimeMs = Math.max(estimatedTimeMs, MIN_SLEEP_TIME_MS);
+            // Check if the audio track has made progress since the last loop
+            // iteration. We should then add in the amount of time that was
+            // spent sleeping in the last iteration.
+            if (currentPosition == previousPosition) {
+                // This works only because the sleep time that would have been calculated
+                // would be the same in the previous iteration too.
+                blockedTimeMs += sleepTimeMs;
+                // If we've taken too long to make progress, bail.
+                if (blockedTimeMs > MAX_PROGRESS_WAIT_MS) {
+                    Log.w(TAG, "Waited unsuccessfully for " + MAX_PROGRESS_WAIT_MS + "ms " +
+                            "for AudioTrack to make progress, Aborting");
+                    break;
+                }
+            } else {
+                blockedTimeMs = 0;
+            }
+            previousPosition = currentPosition;
 
             if (DBG) Log.d(TAG, "About to sleep for : " + sleepTimeMs + " ms," +
                     " Playback position : " + currentPosition + ", Length in frames : "
@@ -512,6 +543,18 @@
         }
     }
 
+    private static final long clip(long value, long min, long max) {
+        if (value < min) {
+            return min;
+        }
+
+        if (value > max) {
+            return max;
+        }
+
+        return value;
+    }
+
     private static AudioTrack createStreamingAudioTrack(SynthesisMessageParams params) {
         final int channelConfig = getChannelConfig(params.mChannelCount);
         final int sampleRateInHz = params.mSampleRateInHz;
diff --git a/core/java/android/webkit/WebView.java b/core/java/android/webkit/WebView.java
index 065beb1..2f73474 100644
--- a/core/java/android/webkit/WebView.java
+++ b/core/java/android/webkit/WebView.java
@@ -4021,7 +4021,14 @@
     @Override
     protected void finalize() throws Throwable {
         try {
-            destroyImpl();
+            if (mNativeClass != 0) {
+                post(new Runnable() {
+                    @Override
+                    public void run() {
+                        destroy();
+                    }
+                });
+            }
         } finally {
             super.finalize();
         }
@@ -5575,7 +5582,6 @@
             JWebCoreJavaBridge.setActiveWebView(this);
             if (mPictureUpdatePausedForFocusChange) {
                 WebViewCore.resumeUpdatePicture(mWebViewCore);
-                nativeSetIsScrolling(false);
                 mPictureUpdatePausedForFocusChange = false;
             }
         } else {
@@ -5584,7 +5590,6 @@
             if (settings != null && settings.enableSmoothTransition() &&
                     mWebViewCore != null && !WebViewCore.isUpdatePicturePaused(mWebViewCore)) {
                 WebViewCore.pauseUpdatePicture(mWebViewCore);
-                nativeSetIsScrolling(true);
                 mPictureUpdatePausedForFocusChange = true;
             }
         }
diff --git a/core/java/android/webkit/WebViewCore.java b/core/java/android/webkit/WebViewCore.java
index 843a624..48359d44 100644
--- a/core/java/android/webkit/WebViewCore.java
+++ b/core/java/android/webkit/WebViewCore.java
@@ -2085,6 +2085,10 @@
             if (!core.getSettings().enableSmoothTransition()) return;
 
             synchronized (core) {
+                if (core.mNativeClass == 0) {
+                    Log.w(LOGTAG, "Cannot pauseUpdatePicture, core destroyed or not initialized!");
+                    return;
+                }
                 core.nativeSetIsPaused(true);
                 core.mDrawIsPaused = true;
             }
@@ -2099,6 +2103,10 @@
                 return;
 
             synchronized (core) {
+                if (core.mNativeClass == 0) {
+                    Log.w(LOGTAG, "Cannot resumeUpdatePicture, core destroyed!");
+                    return;
+                }
                 core.nativeSetIsPaused(false);
                 core.mDrawIsPaused = false;
                 // always redraw on resume to reenable gif animations
diff --git a/core/java/android/widget/AbsListView.java b/core/java/android/widget/AbsListView.java
index 353d83c..cfe4cb7 100644
--- a/core/java/android/widget/AbsListView.java
+++ b/core/java/android/widget/AbsListView.java
@@ -635,7 +635,7 @@
     /**
      * Track if we are currently attached to a window.
      */
-    private boolean mIsAttached;
+    boolean mIsAttached;
 
     /**
      * Track the item count from the last time we handled a data change.
@@ -2576,13 +2576,11 @@
             final int longPressPosition, final long longPressId) {
         // CHOICE_MODE_MULTIPLE_MODAL takes over long press.
         if (mChoiceMode == CHOICE_MODE_MULTIPLE_MODAL) {
-            if (mChoiceActionMode == null) {
-                mChoiceActionMode = startActionMode(mMultiChoiceModeCallback);
+            if (mChoiceActionMode == null &&
+                    (mChoiceActionMode = startActionMode(mMultiChoiceModeCallback)) != null) {
                 setItemChecked(longPressPosition, true);
+                performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
             }
-            // TODO Should we select the long pressed item if we were already in
-            // selection mode? (i.e. treat it like an item click?)
-            performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
             return true;
         }
 
diff --git a/core/java/android/widget/ListPopupWindow.java b/core/java/android/widget/ListPopupWindow.java
index a2910af..5c97593 100644
--- a/core/java/android/widget/ListPopupWindow.java
+++ b/core/java/android/widget/ListPopupWindow.java
@@ -988,7 +988,6 @@
                 mDropDownList.setSelector(mDropDownListHighlight);
             }
             mDropDownList.setAdapter(mAdapter);
-            mDropDownList.setVerticalFadingEdgeEnabled(true);
             mDropDownList.setOnItemClickListener(mItemClickListener);
             mDropDownList.setFocusable(true);
             mDropDownList.setFocusableInTouchMode(true);
diff --git a/core/java/android/widget/ListView.java b/core/java/android/widget/ListView.java
index e37ccf8..42e27b1 100644
--- a/core/java/android/widget/ListView.java
+++ b/core/java/android/widget/ListView.java
@@ -2074,7 +2074,7 @@
     }
 
     private boolean commonKey(int keyCode, int count, KeyEvent event) {
-        if (mAdapter == null) {
+        if (mAdapter == null || !mIsAttached) {
             return false;
         }
 
diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java
index ecd99b2..edb1bfc 100644
--- a/core/java/android/widget/TextView.java
+++ b/core/java/android/widget/TextView.java
@@ -9617,10 +9617,9 @@
         }
 
         private class SuggestionInfo {
-            int suggestionStart, suggestionEnd; // range of suggestion item with replacement text
-            int spanStart, spanEnd; // range in TextView where text should be inserted
+            int suggestionStart, suggestionEnd; // range of actual suggestion within text
             SuggestionSpan suggestionSpan; // the SuggestionSpan that this TextView represents
-            int suggestionIndex; // the index of the suggestion inside suggestionSpan
+            int suggestionIndex; // the index of this suggestion inside suggestionSpan
             SpannableStringBuilder text = new SpannableStringBuilder();
             TextAppearanceSpan highlightSpan = new TextAppearanceSpan(mContext,
                     android.R.style.TextAppearance_SuggestionHighlight);
@@ -9804,8 +9803,6 @@
                 int nbSuggestions = suggestions.length;
                 for (int suggestionIndex = 0; suggestionIndex < nbSuggestions; suggestionIndex++) {
                     SuggestionInfo suggestionInfo = mSuggestionInfos[mNumberOfSuggestions];
-                    suggestionInfo.spanStart = spanStart;
-                    suggestionInfo.spanEnd = spanEnd;
                     suggestionInfo.suggestionSpan = suggestionSpan;
                     suggestionInfo.suggestionIndex = suggestionIndex;
                     suggestionInfo.text.replace(0, suggestionInfo.text.length(),
@@ -9829,8 +9826,6 @@
                 final int misspelledEnd = spannable.getSpanEnd(misspelledSpan);
                 if (misspelledStart >= 0 && misspelledEnd > misspelledStart) {
                     SuggestionInfo suggestionInfo = mSuggestionInfos[mNumberOfSuggestions];
-                    suggestionInfo.spanStart = misspelledStart;
-                    suggestionInfo.spanEnd = misspelledEnd;
                     suggestionInfo.suggestionSpan = misspelledSpan;
                     suggestionInfo.suggestionIndex = -1;
                     suggestionInfo.text.replace(0, suggestionInfo.text.length(),
@@ -9862,8 +9857,9 @@
 
         private void highlightTextDifferences(SuggestionInfo suggestionInfo, int unionStart,
                 int unionEnd) {
-            final int spanStart = suggestionInfo.spanStart;
-            final int spanEnd = suggestionInfo.spanEnd;
+            final Spannable text = (Spannable) mText;
+            final int spanStart = text.getSpanStart(suggestionInfo.suggestionSpan);
+            final int spanEnd = text.getSpanEnd(suggestionInfo.suggestionSpan);
 
             // Adjust the start/end of the suggestion span
             suggestionInfo.suggestionStart = spanStart - unionStart;
@@ -9883,10 +9879,11 @@
         public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
             if (view instanceof TextView) {
                 TextView textView = (TextView) view;
+                Editable editable = (Editable) mText;
 
                 SuggestionInfo suggestionInfo = mSuggestionInfos[position];
-                final int spanStart = suggestionInfo.spanStart;
-                final int spanEnd = suggestionInfo.spanEnd;
+                final int spanStart = editable.getSpanStart(suggestionInfo.suggestionSpan);
+                final int spanEnd = editable.getSpanEnd(suggestionInfo.suggestionSpan);
                 final String originalText = mText.subSequence(spanStart, spanEnd).toString();
 
                 if (suggestionInfo.suggestionIndex < 0) {
@@ -9897,7 +9894,6 @@
                     suggestionInfo.removeMisspelledFlag();
                 } else {
                     // SuggestionSpans are removed by replace: save them before
-                    Editable editable = (Editable) mText;
                     SuggestionSpan[] suggestionSpans = editable.getSpans(spanStart, spanEnd,
                             SuggestionSpan.class);
                     final int length = suggestionSpans.length;
diff --git a/core/jni/android/graphics/Paint.cpp b/core/jni/android/graphics/Paint.cpp
index 423eff4..7d222f6 100644
--- a/core/jni/android/graphics/Paint.cpp
+++ b/core/jni/android/graphics/Paint.cpp
@@ -366,26 +366,22 @@
         NPE_CHECK_RETURN_ZERO(env, jpaint);
         NPE_CHECK_RETURN_ZERO(env, text);
 
+        size_t textLength = env->GetStringLength(text);
         int count = end - start;
-        if ((start | count) < 0) {
+        if ((start | count) < 0 || (size_t)end > textLength) {
             doThrowAIOOBE(env);
             return 0;
         }
         if (count == 0) {
             return 0;
         }
-        size_t textLength = env->GetStringLength(text);
-        if ((size_t)count > textLength) {
-            doThrowAIOOBE(env);
-            return 0;
-        }
 
         const jchar* textArray = env->GetStringChars(text, NULL);
         SkPaint* paint = GraphicsJNI::getNativePaint(env, jpaint);
         jfloat width = 0;
 
 #if RTL_USE_HARFBUZZ
-        TextLayout::getTextRunAdvances(paint, textArray, start, count, end,
+        TextLayout::getTextRunAdvances(paint, textArray, start, count, textLength,
                 paint->getFlags(), NULL /* dont need all advances */, width);
 #else
 
diff --git a/core/jni/android/graphics/TextLayoutCache.cpp b/core/jni/android/graphics/TextLayoutCache.cpp
index 7f79277..d04e059 100644
--- a/core/jni/android/graphics/TextLayoutCache.cpp
+++ b/core/jni/android/graphics/TextLayoutCache.cpp
@@ -163,20 +163,20 @@
                 // Update timing information for statistics
                 value->setElapsedTime(endTime - startTime);
 
-                LOGD("CACHE MISS: Added entry for text='%s' with start=%d, count=%d, "
+                LOGD("CACHE MISS: Added entry with start=%d, count=%d, "
                         "contextCount=%d, entry size %d bytes, remaining space %d bytes"
-                        " - Compute time in nanos: %d",
-                        String8(text, contextCount).string(), start, count, contextCount,
-                        size, mMaxSize - mSize, value->getElapsedTime());
+                        " - Compute time in nanos: %d - Text='%s' ",
+                        start, count, contextCount, size, mMaxSize - mSize, value->getElapsedTime(),
+                        String8(text, contextCount).string());
             }
         } else {
             if (mDebugEnabled) {
                 LOGD("CACHE MISS: Calculated but not storing entry because it is too big "
-                        "for text='%s' with start=%d, count=%d, contextCount=%d, "
+                        "with start=%d, count=%d, contextCount=%d, "
                         "entry size %d bytes, remaining space %d bytes"
-                        " - Compute time in nanos: %lld",
-                        String8(text, contextCount).string(), start, count, contextCount,
-                        size, mMaxSize - mSize, endTime);
+                        " - Compute time in nanos: %lld - Text='%s'",
+                        start, count, contextCount, size, mMaxSize - mSize, endTime,
+                        String8(text, contextCount).string());
             }
             value.clear();
         }
@@ -190,12 +190,12 @@
             if (value->getElapsedTime() > 0) {
                 float deltaPercent = 100 * ((value->getElapsedTime() - elapsedTimeThruCacheGet)
                         / ((float)value->getElapsedTime()));
-                LOGD("CACHE HIT #%d for text='%s' with start=%d, count=%d, contextCount=%d "
+                LOGD("CACHE HIT #%d with start=%d, count=%d, contextCount=%d "
                         "- Compute time in nanos: %d - "
-                        "Cache get time in nanos: %lld - Gain in percent: %2.2f",
-                        mCacheHitCount, String8(text, contextCount).string(), start, count,
-                        contextCount,
-                        value->getElapsedTime(), elapsedTimeThruCacheGet, deltaPercent);
+                        "Cache get time in nanos: %lld - Gain in percent: %2.2f - Text='%s' ",
+                        mCacheHitCount, start, count, contextCount,
+                        value->getElapsedTime(), elapsedTimeThruCacheGet, deltaPercent,
+                        String8(text, contextCount).string());
             }
             if (mCacheHitCount % DEFAULT_DUMP_STATS_CACHE_HIT_INTERVAL == 0) {
                 dumpCacheStats();
diff --git a/core/res/res/drawable-mdpi/unlock_default.png b/core/res/res/drawable-mdpi/unlock_default.png
deleted file mode 100644
index 0a441c0..0000000
--- a/core/res/res/drawable-mdpi/unlock_default.png
+++ /dev/null
Binary files differ
diff --git a/core/res/res/drawable-mdpi/unlock_halo.png b/core/res/res/drawable-mdpi/unlock_halo.png
deleted file mode 100644
index 09b0526..0000000
--- a/core/res/res/drawable-mdpi/unlock_halo.png
+++ /dev/null
Binary files differ
diff --git a/core/res/res/drawable-mdpi/unlock_ring.png b/core/res/res/drawable-mdpi/unlock_ring.png
deleted file mode 100644
index 0363a8b..0000000
--- a/core/res/res/drawable-mdpi/unlock_ring.png
+++ /dev/null
Binary files differ
diff --git a/core/res/res/drawable-mdpi/unlock_wave.png b/core/res/res/drawable-mdpi/unlock_wave.png
deleted file mode 100644
index 21bfa24..0000000
--- a/core/res/res/drawable-mdpi/unlock_wave.png
+++ /dev/null
Binary files differ
diff --git a/core/res/res/drawable-sw600dp-hdpi/unlock_default.png b/core/res/res/drawable-sw600dp-hdpi/unlock_default.png
new file mode 100644
index 0000000..95b006d
--- /dev/null
+++ b/core/res/res/drawable-sw600dp-hdpi/unlock_default.png
Binary files differ
diff --git a/core/res/res/drawable-sw600dp-hdpi/unlock_halo.png b/core/res/res/drawable-sw600dp-hdpi/unlock_halo.png
new file mode 100644
index 0000000..acccb28
--- /dev/null
+++ b/core/res/res/drawable-sw600dp-hdpi/unlock_halo.png
Binary files differ
diff --git a/core/res/res/drawable-sw600dp-hdpi/unlock_ring.png b/core/res/res/drawable-sw600dp-hdpi/unlock_ring.png
new file mode 100644
index 0000000..27260dd
--- /dev/null
+++ b/core/res/res/drawable-sw600dp-hdpi/unlock_ring.png
Binary files differ
diff --git a/core/res/res/drawable-sw600dp-hdpi/unlock_wave.png b/core/res/res/drawable-sw600dp-hdpi/unlock_wave.png
new file mode 100644
index 0000000..e6b17db
--- /dev/null
+++ b/core/res/res/drawable-sw600dp-hdpi/unlock_wave.png
Binary files differ
diff --git a/core/res/res/drawable-sw600dp-mdpi/unlock_default.png b/core/res/res/drawable-sw600dp-mdpi/unlock_default.png
new file mode 100644
index 0000000..dd6f3c1
--- /dev/null
+++ b/core/res/res/drawable-sw600dp-mdpi/unlock_default.png
Binary files differ
diff --git a/core/res/res/drawable-sw600dp-mdpi/unlock_halo.png b/core/res/res/drawable-sw600dp-mdpi/unlock_halo.png
new file mode 100644
index 0000000..c9ed4c7
--- /dev/null
+++ b/core/res/res/drawable-sw600dp-mdpi/unlock_halo.png
Binary files differ
diff --git a/core/res/res/drawable-sw600dp-mdpi/unlock_ring.png b/core/res/res/drawable-sw600dp-mdpi/unlock_ring.png
new file mode 100644
index 0000000..d50de84
--- /dev/null
+++ b/core/res/res/drawable-sw600dp-mdpi/unlock_ring.png
Binary files differ
diff --git a/core/res/res/drawable-sw600dp-mdpi/unlock_wave.png b/core/res/res/drawable-sw600dp-mdpi/unlock_wave.png
new file mode 100644
index 0000000..9e38499
--- /dev/null
+++ b/core/res/res/drawable-sw600dp-mdpi/unlock_wave.png
Binary files differ
diff --git a/core/res/res/drawable-sw600dp-xhdpi/unlock_default.png b/core/res/res/drawable-sw600dp-xhdpi/unlock_default.png
new file mode 100644
index 0000000..8eea0f0
--- /dev/null
+++ b/core/res/res/drawable-sw600dp-xhdpi/unlock_default.png
Binary files differ
diff --git a/core/res/res/drawable-sw600dp-xhdpi/unlock_halo.png b/core/res/res/drawable-sw600dp-xhdpi/unlock_halo.png
new file mode 100644
index 0000000..5c504e8
--- /dev/null
+++ b/core/res/res/drawable-sw600dp-xhdpi/unlock_halo.png
Binary files differ
diff --git a/core/res/res/drawable-sw600dp-xhdpi/unlock_ring.png b/core/res/res/drawable-sw600dp-xhdpi/unlock_ring.png
new file mode 100644
index 0000000..7f698fd
--- /dev/null
+++ b/core/res/res/drawable-sw600dp-xhdpi/unlock_ring.png
Binary files differ
diff --git a/core/res/res/drawable-sw600dp-xhdpi/unlock_wave.png b/core/res/res/drawable-sw600dp-xhdpi/unlock_wave.png
new file mode 100644
index 0000000..a11c956
--- /dev/null
+++ b/core/res/res/drawable-sw600dp-xhdpi/unlock_wave.png
Binary files differ
diff --git a/core/res/res/values/config.xml b/core/res/res/values/config.xml
index 8fd2cd1..1e5358e 100755
--- a/core/res/res/values/config.xml
+++ b/core/res/res/values/config.xml
@@ -426,6 +426,10 @@
     <integer-array name="config_autoBrightnessLevels">
     </integer-array>
 
+
+    <!-- Minimum screen brightness allowed by the power manager. -->
+    <integer name="config_screenBrightnessDim">20</integer>
+
     <!-- Array of output values for LCD backlight corresponding to the LUX values
          in the config_autoBrightnessLevels array.  This array should have size one greater
          than the size of the config_autoBrightnessLevels array.
diff --git a/core/res/res/xml/apns.xml b/core/res/res/xml/apns.xml
index 2c69b40..8c7245c 100644
--- a/core/res/res/xml/apns.xml
+++ b/core/res/res/xml/apns.xml
@@ -17,10 +17,9 @@
 */
 -->
 
-<!-- use empty string to specify no proxy or port -->
 
-<!-- If you edit this version, also edit the version in the partner-supplied 
+<!-- If you edit this version, also edit the version in the partner-supplied
     apns-conf.xml configuration file -->
-<apns version="6">
+<apns version="7">
 
 </apns>
diff --git a/core/tests/coretests/AndroidManifest.xml b/core/tests/coretests/AndroidManifest.xml
index 146466f..cadc895 100644
--- a/core/tests/coretests/AndroidManifest.xml
+++ b/core/tests/coretests/AndroidManifest.xml
@@ -88,6 +88,7 @@
     <uses-permission android:name="android.permission.INSTALL_PACKAGES" />
     <uses-permission android:name="android.permission.DELETE_PACKAGES" />
     <uses-permission android:name="android.permission.MOVE_PACKAGE" />
+    <uses-permission android:name="android.permission.PACKAGE_VERIFICATION_AGENT" />
 
     <!--os storage test permissions -->
     <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
diff --git a/core/tests/coretests/src/android/content/pm/ManifestDigestTest.java b/core/tests/coretests/src/android/content/pm/ManifestDigestTest.java
index 8922f27..cc8c4a6 100644
--- a/core/tests/coretests/src/android/content/pm/ManifestDigestTest.java
+++ b/core/tests/coretests/src/android/content/pm/ManifestDigestTest.java
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2011 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 android.content.pm;
 
 import android.os.Parcel;
diff --git a/core/tests/coretests/src/android/content/pm/PackageManagerTests.java b/core/tests/coretests/src/android/content/pm/PackageManagerTests.java
index 68ddcc4..9575ced 100755
--- a/core/tests/coretests/src/android/content/pm/PackageManagerTests.java
+++ b/core/tests/coretests/src/android/content/pm/PackageManagerTests.java
@@ -42,6 +42,7 @@
 import android.provider.Settings.SettingNotFoundException;
 import android.test.AndroidTestCase;
 import android.test.suitebuilder.annotation.LargeTest;
+import android.test.suitebuilder.annotation.SmallTest;
 import android.util.DisplayMetrics;
 import android.util.Log;
 
@@ -3121,6 +3122,14 @@
         invokeInstallPackageFail(Uri.fromFile(invalidFile), 0, retCode);
     }
 
+    @SmallTest
+    public void testGetVerifierDeviceIdentity() {
+        PackageManager pm = getPm();
+        VerifierDeviceIdentity id = pm.getVerifierDeviceIdentity();
+
+        assertNotNull("Verifier device identity should not be null", id);
+    }
+
     /*---------- Recommended install location tests ----*/
     /*
      * TODO's
diff --git a/core/tests/coretests/src/android/content/pm/VerifierDeviceIdentityTest.java b/core/tests/coretests/src/android/content/pm/VerifierDeviceIdentityTest.java
new file mode 100644
index 0000000..e6a6a26
--- /dev/null
+++ b/core/tests/coretests/src/android/content/pm/VerifierDeviceIdentityTest.java
@@ -0,0 +1,217 @@
+/*
+ * Copyright (C) 2011 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 android.content.pm;
+
+import android.os.Parcel;
+
+import java.util.Random;
+
+public class VerifierDeviceIdentityTest extends android.test.AndroidTestCase {
+    private static final long TEST_1 = 0x7A5F00FF5A55AAA5L;
+
+    private static final String TEST_1_ENCODED = "HUXY-A75N-FLKV-F";
+
+    private static final long TEST_2 = 0x5A05FF5A05F0A555L;
+
+    private static final long TEST_MAXVALUE = Long.MAX_VALUE;
+
+    private static final String TEST_MAXVALUE_ENCODED = "H777-7777-7777-7";
+
+    private static final long TEST_MINVALUE = Long.MIN_VALUE;
+
+    private static final String TEST_MINVALUE_ENCODED = "IAAA-AAAA-AAAA-A";
+
+    private static final long TEST_ZERO = 0L;
+
+    private static final String TEST_ZERO_ENCODED = "AAAA-AAAA-AAAA-A";
+
+    private static final long TEST_NEGONE = -1L;
+
+    private static final String TEST_NEGONE_ENCODED = "P777-7777-7777-7";
+
+    private static final String TEST_OVERFLOW_ENCODED = "QAAA-AAAA-AAAA-A";
+
+    public void testVerifierDeviceIdentity_Equals_Success() {
+        VerifierDeviceIdentity id1 = new VerifierDeviceIdentity(TEST_1);
+        VerifierDeviceIdentity id2 = new VerifierDeviceIdentity(TEST_1);
+
+        assertTrue("The two VerifierDeviceIdentity instances should be equal", id1.equals(id2));
+    }
+
+    public void testVerifierDeviceIdentity_Equals_Failure() {
+        VerifierDeviceIdentity id1 = new VerifierDeviceIdentity(TEST_1);
+        VerifierDeviceIdentity id2 = new VerifierDeviceIdentity(TEST_2);
+
+        assertFalse("The two VerifierDeviceIdentity instances should be unique", id1.equals(id2));
+    }
+
+    public void testVerifierDeviceIdentity_HashCode() {
+        VerifierDeviceIdentity id1 = new VerifierDeviceIdentity(TEST_1);
+
+        assertEquals("The VerifierDeviceIdentity should have the same hashcode as its identity",
+                (int) TEST_1, id1.hashCode());
+    }
+
+    public void testVerifierDeviceIdentity_ToString_Success() {
+        VerifierDeviceIdentity id1 = new VerifierDeviceIdentity(TEST_1);
+
+        assertEquals("The identity should encode correctly to the expected Base 32 string",
+                TEST_1_ENCODED, id1.toString());
+    }
+
+    public void testVerifierDeviceIdentity_ToString_Largest() {
+        VerifierDeviceIdentity id1 = new VerifierDeviceIdentity(TEST_MAXVALUE);
+
+        assertEquals("The identity should encode correctly to the expected Base 32 string",
+                TEST_MAXVALUE_ENCODED, id1.toString());
+    }
+
+    public void testVerifierDeviceIdentity_ToString_Zero() {
+        VerifierDeviceIdentity id1 = new VerifierDeviceIdentity(TEST_ZERO);
+
+        assertEquals("The identity should encode correctly to the expected Base 32 string",
+                TEST_ZERO_ENCODED, id1.toString());
+    }
+
+    public void testVerifierDeviceIdentity_ToString_NegOne() {
+        VerifierDeviceIdentity id1 = new VerifierDeviceIdentity(TEST_NEGONE);
+
+        assertEquals("The identity should encode correctly to the expected Base 32 string",
+                TEST_NEGONE_ENCODED, id1.toString());
+    }
+
+    public void testVerifierDeviceIdentity_ToString_MinValue() {
+        VerifierDeviceIdentity id1 = new VerifierDeviceIdentity(TEST_MINVALUE);
+
+        assertEquals("The identity should encode correctly to the expected Base 32 string",
+                TEST_MINVALUE_ENCODED, id1.toString());
+    }
+
+    public void testVerifierDeviceIdentity_Parcel_ReadNegative() {
+        VerifierDeviceIdentity id1 = new VerifierDeviceIdentity(TEST_MINVALUE);
+
+        Parcel parcel = Parcel.obtain();
+        parcel.writeLong(TEST_MINVALUE);
+        parcel.setDataPosition(0);
+
+        VerifierDeviceIdentity id2 = VerifierDeviceIdentity.CREATOR.createFromParcel(parcel);
+
+        assertEquals("Parcel created should match expected value", id1, id2);
+    }
+
+    public void testVerifierDeviceIdentity_Parcel_Read_Pass() {
+        VerifierDeviceIdentity id1 = new VerifierDeviceIdentity(TEST_1);
+
+        Parcel parcel = Parcel.obtain();
+        id1.writeToParcel(parcel, 0);
+        parcel.setDataPosition(0);
+
+        VerifierDeviceIdentity id2 = VerifierDeviceIdentity.CREATOR.createFromParcel(parcel);
+
+        assertEquals("Original identity and parceled identity should be the same", id1, id2);
+    }
+
+    private static class MockRandom extends Random {
+        private long mNextLong;
+
+        public MockRandom() {
+        }
+
+        public void setNextLong(long nextLong) {
+            mNextLong = nextLong;
+        }
+
+        @Override
+        public long nextLong() {
+            return mNextLong;
+        }
+    }
+
+    public void testVerifierDeviceIdentity_Generate_MinValue() {
+        VerifierDeviceIdentity id1 = new VerifierDeviceIdentity(TEST_MINVALUE);
+
+        MockRandom random = new MockRandom();
+        random.setNextLong(Long.MIN_VALUE);
+        VerifierDeviceIdentity id2 = VerifierDeviceIdentity.generate(random);
+
+        assertEquals("Identity created from Long.MIN_VALUE and one created from return from RNG"
+                + " should be the same", id1, id2);
+    }
+
+    public void testVerifierDeviceIdentity_Generate_Random() {
+        VerifierDeviceIdentity id1 = new VerifierDeviceIdentity(TEST_1);
+
+        MockRandom random = new MockRandom();
+        random.setNextLong(TEST_1);
+        VerifierDeviceIdentity id2 = VerifierDeviceIdentity.generate(random);
+
+        assertEquals("Identity should end up being same when coming from RNG", id1, id2);
+    }
+
+    public void testVerifierDeviceIdentity_Parse_Normal() {
+        VerifierDeviceIdentity id1 = new VerifierDeviceIdentity(TEST_1);
+
+        VerifierDeviceIdentity id2 = VerifierDeviceIdentity.parse(TEST_1_ENCODED);
+
+        assertEquals("Parsed device identity should have the same value as original identity",
+                id1, id2);
+    }
+
+    public void testVerifierDeviceIdentity_Parse_MaxValue() {
+        VerifierDeviceIdentity id1 = new VerifierDeviceIdentity(TEST_MAXVALUE);
+
+        VerifierDeviceIdentity id2 = VerifierDeviceIdentity.parse(TEST_MAXVALUE_ENCODED);
+
+        assertEquals("Original max value and parsed max value should be equal", id1, id2);
+    }
+
+    public void testVerifierDeviceIdentity_Parse_TooShort() {
+        try {
+            VerifierDeviceIdentity id = VerifierDeviceIdentity.parse("AAAA-AAAA-AAAA-");
+            fail("Parsing should fail when device identifier is too short");
+        } catch (IllegalArgumentException e) {
+            // success
+        }
+    }
+
+    public void testVerifierDeviceIdentity_Parse_WayTooShort() {
+        try {
+            VerifierDeviceIdentity id = VerifierDeviceIdentity.parse("----------------");
+            fail("Parsing should fail when device identifier is too short");
+        } catch (IllegalArgumentException e) {
+            // success
+        }
+    }
+
+    public void testVerifierDeviceIdentity_Parse_TooLong() {
+        try {
+            VerifierDeviceIdentity id = VerifierDeviceIdentity.parse("AAAA-AAAA-AAAA-AA");
+            fail("Parsing should fail when device identifier is too long");
+        } catch (IllegalArgumentException e) {
+            // success
+        }
+    }
+
+    public void testVerifierDeviceIdentity_Parse_Overflow() {
+        try {
+            VerifierDeviceIdentity id = VerifierDeviceIdentity.parse(TEST_OVERFLOW_ENCODED);
+            fail("Parsing should fail when the value will overflow");
+        } catch (IllegalArgumentException e) {
+            // success
+        }
+    }
+}
diff --git a/docs/html/guide/developing/debugging/ddms.jd b/docs/html/guide/developing/debugging/ddms.jd
index 0d47ae5..4398ec9 100644
--- a/docs/html/guide/developing/debugging/ddms.jd
+++ b/docs/html/guide/developing/debugging/ddms.jd
@@ -60,9 +60,9 @@
 
   <p>The following screenshot shows a typical DDMS screen in Eclipse. If you are starting DDMS from
   the command line, the screen is slightly different, but much of the functionality is identical.
-  Notice that the highlighted process, <code>com.example.android.notepad</code>, that is running in the emulator
-  has the debugging port 8700 assigned to it as well as 8609. This signifies that DDMS is currently
-  forwarding port 8609 to the static debugging port of 8700.</p>
+  Notice that the highlighted process, <code>com.android.email</code>, that is running in the emulator
+  has the debugging port 8700 assigned to it as well as 8606. This signifies that DDMS is currently
+  forwarding port 8606 to the static debugging port of 8700.</p>
 
   <img src="{@docRoot}images/debug-ddms.png"
        width="1024" />
@@ -76,7 +76,7 @@
 
   <p class="note"><strong>Tip:</strong> You can set a number of DDMS preferences in
   <strong>File</strong> &gt; <strong>Preferences</strong>. Preferences are saved to
-  <code>$HOME/.ddmsrc</code>.</p>
+  <code>$HOME/.android/ddms.cfg</code>.</p>
 
   <p class="warning"><strong>Known debugging issues with Dalvik</strong><br />
   Debugging an application in the Dalvik VM should work the same as it does in other VMs. However,
diff --git a/docs/html/sdk/win-usb.jd b/docs/html/sdk/win-usb.jd
index ffaec4c..3dfe55d 100644
--- a/docs/html/sdk/win-usb.jd
+++ b/docs/html/sdk/win-usb.jd
@@ -181,7 +181,7 @@
   <li>Expand the <em>Third party Add-ons</em> and <em>Google Inc. add-ons</em>.</li>
   <li>Check <strong>Google Usb Driver package</strong> and click <strong>Install selected</strong>.</li>
   <li>Proceed to install the package. When done, the driver files are
-downloaded into the <code>&lt;sdk&gt;\google-usb_driver\</code> directory.</li>
+downloaded into the <code>&lt;sdk&gt;\extras\google\usb_driver\</code> directory.</li>
 </ol>
 
 
@@ -229,7 +229,7 @@
   <li>Select <strong>Browse my computer for driver software</strong> and click
     <strong>Next</strong>.</li>
   <li>Click <strong>Browse</strong> and locate the USB driver folder. (The Google USB
-Driver is located in {@code &lt;sdk&gt;\google-usb_driver\}.)</li>
+Driver is located in {@code &lt;sdk&gt;\extras\google\usb_driver\}.)</li>
   <li>Click <strong>Next</strong> to install the driver.</li>
 </ol>
 
@@ -252,7 +252,7 @@
 <strong>Search removable media</strong>; and check <strong>Include this location in the
 search</strong>.</li>
   <li>Click <strong>Browse</strong> and locate the USB driver folder. (The Google USB
-Driver is located in {@code &lt;sdk&gt;\google-usb_driver\}.)</li>
+Driver is located in {@code &lt;sdk&gt;\extras\google\usb_driver\}.)</li>
   <li>Click <strong>Next</strong> to upgrade the driver.</li>
 </ol>
 
@@ -274,7 +274,7 @@
     removable media</strong>; and check <strong>Include
 this location in the search</strong>.</li>
   <li>Click <strong>Browse</strong> and locate the USB driver folder. (The Google USB
-Driver is located in {@code &lt;sdk&gt;\google-usb_driver\}.)</li>
+Driver is located in {@code &lt;sdk&gt;\extras\google\usb_driver\}.)</li>
   <li>Click <strong>Next</strong> to install the driver.</li>
 </ol>
 
@@ -296,7 +296,7 @@
     removable media</strong>; and check <strong>Include
 this location in the search</strong>.</li>
   <li>Click <strong>Browse</strong> and locate the USB driver folder. (The Google USB
-Driver is located in {@code &lt;sdk&gt;\google-usb_driver\}.)</li>
+Driver is located in {@code &lt;sdk&gt;\extras\google\usb_driver\}.)</li>
   <li>Click <strong>Next</strong> to upgrade the driver.</li>
 </ol>
 
@@ -314,8 +314,8 @@
   <li>Select <strong>I don't have the disk. Show me other options</strong>.</li>
   <li>Select <strong>Browse my computer for driver software</strong>.</li>
   <li>Click <strong>Browse</strong> and locate the USB driver folder. (The Google USB
-Driver is located in {@code &lt;sdk&gt;\google-usb_driver\}.) As long as you specified the exact
-location of the 
+Driver is located in {@code &lt;sdk&gt;\extras\google\usb_driver\}.) As long as you specified the
+exact location of the 
     installation package, you may leave <strong>Include subfolders</strong> checked or
   unchecked&mdash;it doesn't matter.</li>
   <li>Click <strong>Next</strong>. Vista may prompt you to confirm the privilege elevation
@@ -339,7 +339,7 @@
   search for the driver
     software. Select <strong>Browse my computer for driver software</strong>.</li>
   <li>Click <strong>Browse</strong> and locate the USB driver folder. (The Google USB
-Driver is located in {@code &lt;sdk&gt;\google-usb_driver\}.) As long as you specified the
+Driver is located in {@code &lt;sdk&gt;\extras\google\usb_driver\}.) As long as you specified the
 exact location of the 
     installation package, you may leave <strong>Include subfolders</strong> checked or
     unchecked&mdash;it doesn't matter.</li>
diff --git a/include/media/AudioTrack.h b/include/media/AudioTrack.h
index 923518d..d1a8105 100644
--- a/include/media/AudioTrack.h
+++ b/include/media/AudioTrack.h
@@ -486,6 +486,7 @@
     int                     mSessionId;
     int                     mAuxEffectId;
     Mutex                   mLock;
+    status_t                mRestoreStatus;
 };
 
 
diff --git a/libs/rs/driver/rsdAllocation.cpp b/libs/rs/driver/rsdAllocation.cpp
index 17dd931..2ebfe0a 100644
--- a/libs/rs/driver/rsdAllocation.cpp
+++ b/libs/rs/driver/rsdAllocation.cpp
@@ -73,27 +73,27 @@
 }
 
 
-static void Update2DTexture(const Allocation *alloc, const void *ptr, uint32_t xoff, uint32_t yoff,
-                     uint32_t lod, RsAllocationCubemapFace face,
-                     uint32_t w, uint32_t h) {
+static void Update2DTexture(const Context *rsc, const Allocation *alloc, const void *ptr,
+                            uint32_t xoff, uint32_t yoff, uint32_t lod,
+                            RsAllocationCubemapFace face, uint32_t w, uint32_t h) {
     DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
 
     rsAssert(drv->textureID);
-    glBindTexture(drv->glTarget, drv->textureID);
-    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
+    RSD_CALL_GL(glBindTexture, drv->glTarget, drv->textureID);
+    RSD_CALL_GL(glPixelStorei, GL_UNPACK_ALIGNMENT, 1);
     GLenum t = GL_TEXTURE_2D;
     if (alloc->mHal.state.hasFaces) {
         t = gFaceOrder[face];
     }
-    glTexSubImage2D(t, lod, xoff, yoff, w, h, drv->glFormat, drv->glType, ptr);
+    RSD_CALL_GL(glTexSubImage2D, t, lod, xoff, yoff, w, h, drv->glFormat, drv->glType, ptr);
 }
 
 
 static void Upload2DTexture(const Context *rsc, const Allocation *alloc, bool isFirstUpload) {
     DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
 
-    glBindTexture(drv->glTarget, drv->textureID);
-    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
+    RSD_CALL_GL(glBindTexture, drv->glTarget, drv->textureID);
+    RSD_CALL_GL(glPixelStorei, GL_UNPACK_ALIGNMENT, 1);
 
     uint32_t faceCount = 1;
     if (alloc->mHal.state.hasFaces) {
@@ -112,12 +112,12 @@
             }
 
             if (isFirstUpload) {
-                glTexImage2D(t, lod, drv->glFormat,
+                RSD_CALL_GL(glTexImage2D, t, lod, drv->glFormat,
                              alloc->mHal.state.type->getLODDimX(lod),
                              alloc->mHal.state.type->getLODDimY(lod),
                              0, drv->glFormat, drv->glType, p);
             } else {
-                glTexSubImage2D(t, lod, 0, 0,
+                RSD_CALL_GL(glTexSubImage2D, t, lod, 0, 0,
                                 alloc->mHal.state.type->getLODDimX(lod),
                                 alloc->mHal.state.type->getLODDimY(lod),
                                 drv->glFormat, drv->glType, p);
@@ -126,7 +126,7 @@
     }
 
     if (alloc->mHal.state.mipmapControl == RS_ALLOCATION_MIPMAP_ON_SYNC_TO_TEXTURE) {
-        glGenerateMipmap(drv->glTarget);
+        RSD_CALL_GL(glGenerateMipmap, drv->glTarget);
     }
     rsdGLCheckError(rsc, "Upload2DTexture");
 }
@@ -145,7 +145,7 @@
     bool isFirstUpload = false;
 
     if (!drv->textureID) {
-        glGenTextures(1, &drv->textureID);
+        RSD_CALL_GL(glGenTextures, 1, &drv->textureID);
         isFirstUpload = true;
     }
 
@@ -168,7 +168,7 @@
     }
 
     if (!drv->renderTargetID) {
-        glGenRenderbuffers(1, &drv->renderTargetID);
+        RSD_CALL_GL(glGenRenderbuffers, 1, &drv->renderTargetID);
 
         if (!drv->renderTargetID) {
             // This should generally not happen
@@ -176,8 +176,8 @@
             rsc->dumpDebug();
             return;
         }
-        glBindRenderbuffer(GL_RENDERBUFFER, drv->renderTargetID);
-        glRenderbufferStorage(GL_RENDERBUFFER, drv->glFormat,
+        RSD_CALL_GL(glBindRenderbuffer, GL_RENDERBUFFER, drv->renderTargetID);
+        RSD_CALL_GL(glRenderbufferStorage, GL_RENDERBUFFER, drv->glFormat,
                               alloc->mHal.state.dimensionX, alloc->mHal.state.dimensionY);
     }
     rsdGLCheckError(rsc, "AllocateRenderTarget");
@@ -192,17 +192,17 @@
     //alloc->mHal.state.usageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_VERTEX;
 
     if (!drv->bufferID) {
-        glGenBuffers(1, &drv->bufferID);
+        RSD_CALL_GL(glGenBuffers, 1, &drv->bufferID);
     }
     if (!drv->bufferID) {
         LOGE("Upload to buffer object failed");
         drv->uploadDeferred = true;
         return;
     }
-    glBindBuffer(drv->glTarget, drv->bufferID);
-    glBufferData(drv->glTarget, alloc->mHal.state.type->getSizeBytes(),
+    RSD_CALL_GL(glBindBuffer, drv->glTarget, drv->bufferID);
+    RSD_CALL_GL(glBufferData, drv->glTarget, alloc->mHal.state.type->getSizeBytes(),
                  drv->mallocPtr, GL_DYNAMIC_DRAW);
-    glBindBuffer(drv->glTarget, 0);
+    RSD_CALL_GL(glBindBuffer, drv->glTarget, 0);
     rsdGLCheckError(rsc, "UploadToBufferObject");
 }
 
@@ -261,11 +261,11 @@
         //mBufferID = 0;
     }
     if (drv->textureID) {
-        glDeleteTextures(1, &drv->textureID);
+        RSD_CALL_GL(glDeleteTextures, 1, &drv->textureID);
         drv->textureID = 0;
     }
     if (drv->renderTargetID) {
-        glDeleteRenderbuffers(1, &drv->renderTargetID);
+        RSD_CALL_GL(glDeleteRenderbuffers, 1, &drv->renderTargetID);
         drv->renderTargetID = 0;
     }
 
@@ -323,7 +323,7 @@
     drv->readBackFBO->setActive(rsc);
 
     // Do the readback
-    glReadPixels(0, 0, alloc->getType()->getDimX(), alloc->getType()->getDimY(),
+    RSD_CALL_GL(glReadPixels, 0, 0, alloc->getType()->getDimX(), alloc->getType()->getDimY(),
                  drv->glFormat, drv->glType, alloc->getPtr());
 
     // Revert framebuffer to its original
@@ -414,7 +414,7 @@
         }
         drv->uploadDeferred = true;
     } else {
-        Update2DTexture(alloc, data, xoff, yoff, lod, face, w, h);
+        Update2DTexture(rsc, alloc, data, xoff, yoff, lod, face, w, h);
     }
 }
 
diff --git a/libs/rs/driver/rsdFrameBufferObj.cpp b/libs/rs/driver/rsdFrameBufferObj.cpp
index 145bf34..91452b0 100644
--- a/libs/rs/driver/rsdFrameBufferObj.cpp
+++ b/libs/rs/driver/rsdFrameBufferObj.cpp
@@ -17,6 +17,7 @@
 
 #include "rsdFrameBufferObj.h"
 #include "rsdAllocation.h"
+#include "rsdGL.h"
 
 #include <GLES2/gl2.h>
 #include <GLES2/gl2ext.h>
@@ -124,9 +125,9 @@
     bool framebuffer = renderToFramebuffer();
     if (!framebuffer) {
         if(mFBOId == 0) {
-            glGenFramebuffers(1, &mFBOId);
+            RSD_CALL_GL(glGenFramebuffers, 1, &mFBOId);
         }
-        glBindFramebuffer(GL_FRAMEBUFFER, mFBOId);
+        RSD_CALL_GL(glBindFramebuffer, GL_FRAMEBUFFER, mFBOId);
 
         if (mDirty) {
             setDepthAttachment();
@@ -134,10 +135,10 @@
             mDirty = false;
         }
 
-        glViewport(0, 0, mWidth, mHeight);
+        RSD_CALL_GL(glViewport, 0, 0, mWidth, mHeight);
         checkError(rsc);
     } else {
-        glBindFramebuffer(GL_FRAMEBUFFER, 0);
-        glViewport(0, 0, rsc->getWidth(), rsc->getHeight());
+        RSD_CALL_GL(glBindFramebuffer, GL_FRAMEBUFFER, 0);
+        RSD_CALL_GL(glViewport, 0, 0, rsc->getWidth(), rsc->getHeight());
     }
 }
diff --git a/libs/rs/driver/rsdGL.cpp b/libs/rs/driver/rsdGL.cpp
index 04446ad..c5b81db 100644
--- a/libs/rs/driver/rsdGL.cpp
+++ b/libs/rs/driver/rsdGL.cpp
@@ -135,18 +135,19 @@
     LOGV("%p, deinitEGL", rsc);
 
     if (dc->gl.egl.context != EGL_NO_CONTEXT) {
-        eglMakeCurrent(dc->gl.egl.display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
-        eglDestroySurface(dc->gl.egl.display, dc->gl.egl.surfaceDefault);
+        RSD_CALL_GL(eglMakeCurrent, dc->gl.egl.display,
+                    EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
+        RSD_CALL_GL(eglDestroySurface, dc->gl.egl.display, dc->gl.egl.surfaceDefault);
         if (dc->gl.egl.surface != EGL_NO_SURFACE) {
-            eglDestroySurface(dc->gl.egl.display, dc->gl.egl.surface);
+            RSD_CALL_GL(eglDestroySurface, dc->gl.egl.display, dc->gl.egl.surface);
         }
-        eglDestroyContext(dc->gl.egl.display, dc->gl.egl.context);
+        RSD_CALL_GL(eglDestroyContext, dc->gl.egl.display, dc->gl.egl.context);
         checkEglError("eglDestroyContext");
     }
 
     gGLContextCount--;
     if (!gGLContextCount) {
-        eglTerminate(dc->gl.egl.display);
+        RSD_CALL_GL(eglTerminate, dc->gl.egl.display);
     }
 }
 
@@ -202,21 +203,25 @@
     rsAssert(configAttribsPtr < (configAttribs + (sizeof(configAttribs) / sizeof(EGLint))));
 
     LOGV("%p initEGL start", rsc);
+    rsc->setWatchdogGL("eglGetDisplay", __LINE__, __FILE__);
     dc->gl.egl.display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
     checkEglError("eglGetDisplay");
 
-    eglInitialize(dc->gl.egl.display, &dc->gl.egl.majorVersion, &dc->gl.egl.minorVersion);
+    RSD_CALL_GL(eglInitialize, dc->gl.egl.display,
+                &dc->gl.egl.majorVersion, &dc->gl.egl.minorVersion);
     checkEglError("eglInitialize");
 
     EGLBoolean ret;
 
     EGLint numConfigs = -1, n = 0;
+    rsc->setWatchdogGL("eglChooseConfig", __LINE__, __FILE__);
     ret = eglChooseConfig(dc->gl.egl.display, configAttribs, 0, 0, &numConfigs);
     checkEglError("eglGetConfigs", ret);
 
     if (numConfigs) {
         EGLConfig* const configs = new EGLConfig[numConfigs];
 
+        rsc->setWatchdogGL("eglChooseConfig", __LINE__, __FILE__);
         ret = eglChooseConfig(dc->gl.egl.display,
                 configAttribs, configs, numConfigs, &n);
         if (!ret || !n) {
@@ -261,32 +266,38 @@
     }
     //}
 
+    rsc->setWatchdogGL("eglCreateContext", __LINE__, __FILE__);
     dc->gl.egl.context = eglCreateContext(dc->gl.egl.display, dc->gl.egl.config,
                                           EGL_NO_CONTEXT, context_attribs2);
     checkEglError("eglCreateContext");
     if (dc->gl.egl.context == EGL_NO_CONTEXT) {
         LOGE("%p, eglCreateContext returned EGL_NO_CONTEXT", rsc);
+        rsc->setWatchdogGL(NULL, 0, NULL);
         return false;
     }
     gGLContextCount++;
 
 
     EGLint pbuffer_attribs[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE };
+    rsc->setWatchdogGL("eglCreatePbufferSurface", __LINE__, __FILE__);
     dc->gl.egl.surfaceDefault = eglCreatePbufferSurface(dc->gl.egl.display, dc->gl.egl.config,
                                                         pbuffer_attribs);
     checkEglError("eglCreatePbufferSurface");
     if (dc->gl.egl.surfaceDefault == EGL_NO_SURFACE) {
         LOGE("eglCreatePbufferSurface returned EGL_NO_SURFACE");
         rsdGLShutdown(rsc);
+        rsc->setWatchdogGL(NULL, 0, NULL);
         return false;
     }
 
+    rsc->setWatchdogGL("eglMakeCurrent", __LINE__, __FILE__);
     ret = eglMakeCurrent(dc->gl.egl.display, dc->gl.egl.surfaceDefault,
                          dc->gl.egl.surfaceDefault, dc->gl.egl.context);
     if (ret == EGL_FALSE) {
         LOGE("eglMakeCurrent returned EGL_FALSE");
         checkEglError("eglMakeCurrent", ret);
         rsdGLShutdown(rsc);
+        rsc->setWatchdogGL(NULL, 0, NULL);
         return false;
     }
 
@@ -314,6 +325,7 @@
     if (!verptr) {
         LOGE("Error, OpenGL ES Lite not supported");
         rsdGLShutdown(rsc);
+        rsc->setWatchdogGL(NULL, 0, NULL);
         return false;
     } else {
         sscanf(verptr, " %i.%i", &dc->gl.gl.majorVersion, &dc->gl.gl.minorVersion);
@@ -352,6 +364,7 @@
     dc->gl.currentFrameBuffer = NULL;
 
     LOGV("initGLThread end %p", rsc);
+    rsc->setWatchdogGL(NULL, 0, NULL);
     return true;
 }
 
@@ -363,10 +376,12 @@
     // WAR: Some drivers fail to handle 0 size surfaces correcntly.
     // Use the pbuffer to avoid this pitfall.
     if ((dc->gl.egl.surface != NULL) || (w == 0) || (h == 0)) {
+        rsc->setWatchdogGL("eglMakeCurrent", __LINE__, __FILE__);
         ret = eglMakeCurrent(dc->gl.egl.display, dc->gl.egl.surfaceDefault,
                              dc->gl.egl.surfaceDefault, dc->gl.egl.context);
         checkEglError("eglMakeCurrent", ret);
 
+        rsc->setWatchdogGL("eglDestroySurface", __LINE__, __FILE__);
         ret = eglDestroySurface(dc->gl.egl.display, dc->gl.egl.surface);
         checkEglError("eglDestroySurface", ret);
 
@@ -385,6 +400,7 @@
         dc->gl.width = w;
         dc->gl.height = h;
 
+        rsc->setWatchdogGL("eglCreateWindowSurface", __LINE__, __FILE__);
         dc->gl.egl.surface = eglCreateWindowSurface(dc->gl.egl.display, dc->gl.egl.config,
                                                     dc->gl.wndSurface, NULL);
         checkEglError("eglCreateWindowSurface");
@@ -392,16 +408,18 @@
             LOGE("eglCreateWindowSurface returned EGL_NO_SURFACE");
         }
 
+        rsc->setWatchdogGL("eglMakeCurrent", __LINE__, __FILE__);
         ret = eglMakeCurrent(dc->gl.egl.display, dc->gl.egl.surface,
                              dc->gl.egl.surface, dc->gl.egl.context);
         checkEglError("eglMakeCurrent", ret);
     }
+    rsc->setWatchdogGL(NULL, 0, NULL);
     return true;
 }
 
 void rsdGLSwap(const android::renderscript::Context *rsc) {
     RsdHal *dc = (RsdHal *)rsc->mHal.drv;
-    eglSwapBuffers(dc->gl.egl.display, dc->gl.egl.surface);
+    RSD_CALL_GL(eglSwapBuffers, dc->gl.egl.display, dc->gl.egl.surface);
 }
 
 void rsdGLCheckError(const android::renderscript::Context *rsc,
diff --git a/libs/rs/driver/rsdGL.h b/libs/rs/driver/rsdGL.h
index 0d5b7e7..fc33885 100644
--- a/libs/rs/driver/rsdGL.h
+++ b/libs/rs/driver/rsdGL.h
@@ -20,6 +20,8 @@
 #include <rs_hal.h>
 #include <EGL/egl.h>
 
+#define RSD_CALL_GL(x, ...) rsc->setWatchdogGL(#x, __LINE__, __FILE__); x(__VA_ARGS__); rsc->setWatchdogGL(NULL, 0, NULL)
+
 class RsdShaderCache;
 class RsdVertexArrayState;
 class RsdFrameBufferObj;
@@ -73,7 +75,6 @@
 } RsdGL;
 
 
-
 bool rsdGLInit(const android::renderscript::Context *rsc);
 void rsdGLShutdown(const android::renderscript::Context *rsc);
 bool rsdGLSetSurface(const android::renderscript::Context *rsc,
diff --git a/libs/rs/driver/rsdMeshObj.cpp b/libs/rs/driver/rsdMeshObj.cpp
index 4315c0d..019167b 100644
--- a/libs/rs/driver/rsdMeshObj.cpp
+++ b/libs/rs/driver/rsdMeshObj.cpp
@@ -130,7 +130,8 @@
     return true;
 }
 
-void RsdMeshObj::renderPrimitiveRange(const Context *rsc, uint32_t primIndex, uint32_t start, uint32_t len) const {
+void RsdMeshObj::renderPrimitiveRange(const Context *rsc, uint32_t primIndex,
+                                      uint32_t start, uint32_t len) const {
     if (len < 1 || primIndex >= mRSMesh->mHal.state.primitivesCount || mAttribCount == 0) {
         LOGE("Invalid mesh or parameters");
         return;
@@ -171,14 +172,16 @@
         }
 
         if (drvAlloc->bufferID) {
-            glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, drvAlloc->bufferID);
-            glDrawElements(mGLPrimitives[primIndex], len, GL_UNSIGNED_SHORT, (uint16_t *)(start * 2));
+            RSD_CALL_GL(glBindBuffer, GL_ELEMENT_ARRAY_BUFFER, drvAlloc->bufferID);
+            RSD_CALL_GL(glDrawElements, mGLPrimitives[primIndex], len, GL_UNSIGNED_SHORT,
+                        (uint16_t *)(start * 2));
         } else {
-            glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
-            glDrawElements(mGLPrimitives[primIndex], len, GL_UNSIGNED_SHORT, drvAlloc->mallocPtr);
+            RSD_CALL_GL(glBindBuffer, GL_ELEMENT_ARRAY_BUFFER, 0);
+            RSD_CALL_GL(glDrawElements, mGLPrimitives[primIndex], len, GL_UNSIGNED_SHORT,
+                        drvAlloc->mallocPtr);
         }
     } else {
-        glDrawArrays(mGLPrimitives[primIndex], start, len);
+        RSD_CALL_GL(glDrawArrays, mGLPrimitives[primIndex], start, len);
     }
 
     rsdGLCheckError(rsc, "Mesh::renderPrimitiveRange");
diff --git a/libs/rs/driver/rsdProgramRaster.cpp b/libs/rs/driver/rsdProgramRaster.cpp
index 65995be..b493759 100644
--- a/libs/rs/driver/rsdProgramRaster.cpp
+++ b/libs/rs/driver/rsdProgramRaster.cpp
@@ -32,18 +32,18 @@
     return true;
 }
 
-void rsdProgramRasterSetActive(const Context *, const ProgramRaster *pr) {
+void rsdProgramRasterSetActive(const Context *rsc, const ProgramRaster *pr) {
     switch (pr->mHal.state.cull) {
         case RS_CULL_BACK:
-            glEnable(GL_CULL_FACE);
-            glCullFace(GL_BACK);
+            RSD_CALL_GL(glEnable, GL_CULL_FACE);
+            RSD_CALL_GL(glCullFace, GL_BACK);
             break;
         case RS_CULL_FRONT:
-            glEnable(GL_CULL_FACE);
-            glCullFace(GL_FRONT);
+            RSD_CALL_GL(glEnable, GL_CULL_FACE);
+            RSD_CALL_GL(glCullFace, GL_FRONT);
             break;
         case RS_CULL_NONE:
-            glDisable(GL_CULL_FACE);
+            RSD_CALL_GL(glDisable, GL_CULL_FACE);
             break;
     }
 
diff --git a/libs/rs/driver/rsdProgramStore.cpp b/libs/rs/driver/rsdProgramStore.cpp
index e591453..af44b02 100644
--- a/libs/rs/driver/rsdProgramStore.cpp
+++ b/libs/rs/driver/rsdProgramStore.cpp
@@ -156,29 +156,29 @@
 void rsdProgramStoreSetActive(const Context *rsc, const ProgramStore *ps) {
     DrvProgramStore *drv = (DrvProgramStore *)ps->mHal.drv;
 
-    glColorMask(ps->mHal.state.colorRWriteEnable,
+    RSD_CALL_GL(glColorMask, ps->mHal.state.colorRWriteEnable,
                 ps->mHal.state.colorGWriteEnable,
                 ps->mHal.state.colorBWriteEnable,
                 ps->mHal.state.colorAWriteEnable);
 
     if (drv->blendEnable) {
-        glEnable(GL_BLEND);
-        glBlendFunc(drv->blendSrc, drv->blendDst);
+        RSD_CALL_GL(glEnable, GL_BLEND);
+        RSD_CALL_GL(glBlendFunc, drv->blendSrc, drv->blendDst);
     } else {
-        glDisable(GL_BLEND);
+        RSD_CALL_GL(glDisable, GL_BLEND);
     }
 
     if (rsc->mUserSurfaceConfig.depthMin > 0) {
-        glDepthMask(ps->mHal.state.depthWriteEnable);
+        RSD_CALL_GL(glDepthMask, ps->mHal.state.depthWriteEnable);
         if (drv->depthTestEnable || ps->mHal.state.depthWriteEnable) {
-            glEnable(GL_DEPTH_TEST);
-            glDepthFunc(drv->depthFunc);
+            RSD_CALL_GL(glEnable, GL_DEPTH_TEST);
+            RSD_CALL_GL(glDepthFunc, drv->depthFunc);
         } else {
-            glDisable(GL_DEPTH_TEST);
+            RSD_CALL_GL(glDisable, GL_DEPTH_TEST);
         }
     } else {
-        glDepthMask(false);
-        glDisable(GL_DEPTH_TEST);
+        RSD_CALL_GL(glDepthMask, false);
+        RSD_CALL_GL(glDisable, GL_DEPTH_TEST);
     }
 
     /*
@@ -190,9 +190,9 @@
     */
 
     if (ps->mHal.state.ditherEnable) {
-        glEnable(GL_DITHER);
+        RSD_CALL_GL(glEnable, GL_DITHER);
     } else {
-        glDisable(GL_DITHER);
+        RSD_CALL_GL(glDisable, GL_DITHER);
     }
 }
 
diff --git a/libs/rs/driver/rsdShader.cpp b/libs/rs/driver/rsdShader.cpp
index 90fe4b2..16ff063 100644
--- a/libs/rs/driver/rsdShader.cpp
+++ b/libs/rs/driver/rsdShader.cpp
@@ -178,22 +178,22 @@
 
     if (mShaderID) {
         const char * ss = mShader.string();
-        glShaderSource(mShaderID, 1, &ss, NULL);
-        glCompileShader(mShaderID);
+        RSD_CALL_GL(glShaderSource, mShaderID, 1, &ss, NULL);
+        RSD_CALL_GL(glCompileShader, mShaderID);
 
         GLint compiled = 0;
-        glGetShaderiv(mShaderID, GL_COMPILE_STATUS, &compiled);
+        RSD_CALL_GL(glGetShaderiv, mShaderID, GL_COMPILE_STATUS, &compiled);
         if (!compiled) {
             GLint infoLen = 0;
-            glGetShaderiv(mShaderID, GL_INFO_LOG_LENGTH, &infoLen);
+            RSD_CALL_GL(glGetShaderiv, mShaderID, GL_INFO_LOG_LENGTH, &infoLen);
             if (infoLen) {
                 char* buf = (char*) malloc(infoLen);
                 if (buf) {
-                    glGetShaderInfoLog(mShaderID, infoLen, NULL, buf);
+                    RSD_CALL_GL(glGetShaderInfoLog, mShaderID, infoLen, NULL, buf);
                     LOGE("Could not compile shader \n%s\n", buf);
                     free(buf);
                 }
-                glDeleteShader(mShaderID);
+                RSD_CALL_GL(glDeleteShader, mShaderID);
                 mShaderID = 0;
                 rsc->setError(RS_ERROR_BAD_SHADER, "Error returned from GL driver loading shader text,");
                 return false;
@@ -297,24 +297,24 @@
                          int32_t slot, uint32_t arraySize ) {
     RsDataType dataType = field->getType();
     if (dataType == RS_TYPE_MATRIX_4X4) {
-        glUniformMatrix4fv(slot, arraySize, GL_FALSE, fd);
+        RSD_CALL_GL(glUniformMatrix4fv, slot, arraySize, GL_FALSE, fd);
     } else if (dataType == RS_TYPE_MATRIX_3X3) {
-        glUniformMatrix3fv(slot, arraySize, GL_FALSE, fd);
+        RSD_CALL_GL(glUniformMatrix3fv, slot, arraySize, GL_FALSE, fd);
     } else if (dataType == RS_TYPE_MATRIX_2X2) {
-        glUniformMatrix2fv(slot, arraySize, GL_FALSE, fd);
+        RSD_CALL_GL(glUniformMatrix2fv, slot, arraySize, GL_FALSE, fd);
     } else {
         switch (field->getComponent().getVectorSize()) {
         case 1:
-            glUniform1fv(slot, arraySize, fd);
+            RSD_CALL_GL(glUniform1fv, slot, arraySize, fd);
             break;
         case 2:
-            glUniform2fv(slot, arraySize, fd);
+            RSD_CALL_GL(glUniform2fv, slot, arraySize, fd);
             break;
         case 3:
-            glUniform3fv(slot, arraySize, fd);
+            RSD_CALL_GL(glUniform3fv, slot, arraySize, fd);
             break;
         case 4:
-            glUniform4fv(slot, arraySize, fd);
+            RSD_CALL_GL(glUniform4fv, slot, arraySize, fd);
             break;
         default:
             rsAssert(0);
@@ -351,37 +351,44 @@
         if (tex->getHasGraphicsMipmaps() &&
             (dc->gl.gl.GL_NV_texture_npot_2D_mipmap || dc->gl.gl.GL_IMG_texture_npot)) {
             if (dc->gl.gl.GL_NV_texture_npot_2D_mipmap) {
-                glTexParameteri(target, GL_TEXTURE_MIN_FILTER, trans[s->mHal.state.minFilter]);
+                RSD_CALL_GL(glTexParameteri, target, GL_TEXTURE_MIN_FILTER,
+                            trans[s->mHal.state.minFilter]);
             } else {
                 switch (trans[s->mHal.state.minFilter]) {
                 case GL_LINEAR_MIPMAP_LINEAR:
-                    glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
+                    RSD_CALL_GL(glTexParameteri, target, GL_TEXTURE_MIN_FILTER,
+                                GL_LINEAR_MIPMAP_NEAREST);
                     break;
                 default:
-                    glTexParameteri(target, GL_TEXTURE_MIN_FILTER, trans[s->mHal.state.minFilter]);
+                    RSD_CALL_GL(glTexParameteri, target, GL_TEXTURE_MIN_FILTER,
+                                trans[s->mHal.state.minFilter]);
                     break;
                 }
             }
         } else {
-            glTexParameteri(target, GL_TEXTURE_MIN_FILTER, transNP[s->mHal.state.minFilter]);
+            RSD_CALL_GL(glTexParameteri, target, GL_TEXTURE_MIN_FILTER,
+                        transNP[s->mHal.state.minFilter]);
         }
-        glTexParameteri(target, GL_TEXTURE_MAG_FILTER, transNP[s->mHal.state.magFilter]);
-        glTexParameteri(target, GL_TEXTURE_WRAP_S, transNP[s->mHal.state.wrapS]);
-        glTexParameteri(target, GL_TEXTURE_WRAP_T, transNP[s->mHal.state.wrapT]);
+        RSD_CALL_GL(glTexParameteri, target, GL_TEXTURE_MAG_FILTER,
+                    transNP[s->mHal.state.magFilter]);
+        RSD_CALL_GL(glTexParameteri, target, GL_TEXTURE_WRAP_S, transNP[s->mHal.state.wrapS]);
+        RSD_CALL_GL(glTexParameteri, target, GL_TEXTURE_WRAP_T, transNP[s->mHal.state.wrapT]);
     } else {
         if (tex->getHasGraphicsMipmaps()) {
-            glTexParameteri(target, GL_TEXTURE_MIN_FILTER, trans[s->mHal.state.minFilter]);
+            RSD_CALL_GL(glTexParameteri, target, GL_TEXTURE_MIN_FILTER,
+                        trans[s->mHal.state.minFilter]);
         } else {
-            glTexParameteri(target, GL_TEXTURE_MIN_FILTER, transNP[s->mHal.state.minFilter]);
+            RSD_CALL_GL(glTexParameteri, target, GL_TEXTURE_MIN_FILTER,
+                        transNP[s->mHal.state.minFilter]);
         }
-        glTexParameteri(target, GL_TEXTURE_MAG_FILTER, trans[s->mHal.state.magFilter]);
-        glTexParameteri(target, GL_TEXTURE_WRAP_S, trans[s->mHal.state.wrapS]);
-        glTexParameteri(target, GL_TEXTURE_WRAP_T, trans[s->mHal.state.wrapT]);
+        RSD_CALL_GL(glTexParameteri, target, GL_TEXTURE_MAG_FILTER, trans[s->mHal.state.magFilter]);
+        RSD_CALL_GL(glTexParameteri, target, GL_TEXTURE_WRAP_S, trans[s->mHal.state.wrapS]);
+        RSD_CALL_GL(glTexParameteri, target, GL_TEXTURE_WRAP_T, trans[s->mHal.state.wrapT]);
     }
 
     float anisoValue = rsMin(dc->gl.gl.EXT_texture_max_aniso, s->mHal.state.aniso);
     if (dc->gl.gl.EXT_texture_max_aniso > 1.0f) {
-        glTexParameterf(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, anisoValue);
+        RSD_CALL_GL(glTexParameterf, target, GL_TEXTURE_MAX_ANISOTROPY_EXT, anisoValue);
     }
 
     rsdGLCheckError(rsc, "Sampler::setup tex env");
@@ -404,12 +411,12 @@
     }
 
     for (uint32_t ct=0; ct < numTexturesToBind; ct++) {
-        glActiveTexture(GL_TEXTURE0 + ct);
-        glUniform1i(sc->fragUniformSlot(mTextureUniformIndexStart + ct), ct);
+        RSD_CALL_GL(glActiveTexture, GL_TEXTURE0 + ct);
+        RSD_CALL_GL(glUniform1i, sc->fragUniformSlot(mTextureUniformIndexStart + ct), ct);
 
         if (!mRSProgram->mHal.state.textures[ct].get()) {
             // if nothing is bound, reset to default GL texture
-            glBindTexture(mTextureTargets[ct], 0);
+            RSD_CALL_GL(glBindTexture, mTextureTargets[ct], 0);
             continue;
         }
 
@@ -418,21 +425,22 @@
             LOGE("Attempting to bind unknown texture to shader id %u, texture unit %u", (uint)this, ct);
             rsc->setError(RS_ERROR_BAD_SHADER, "Non-texture allocation bound to a shader");
         }
-        glBindTexture(drvTex->glTarget, drvTex->textureID);
+        RSD_CALL_GL(glBindTexture, drvTex->glTarget, drvTex->textureID);
         rsdGLCheckError(rsc, "ProgramFragment::setup tex bind");
         if (mRSProgram->mHal.state.samplers[ct].get()) {
-            setupSampler(rsc, mRSProgram->mHal.state.samplers[ct].get(), mRSProgram->mHal.state.textures[ct].get());
+            setupSampler(rsc, mRSProgram->mHal.state.samplers[ct].get(),
+                         mRSProgram->mHal.state.textures[ct].get());
         } else {
-            glTexParameteri(drvTex->glTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
-            glTexParameteri(drvTex->glTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
-            glTexParameteri(drvTex->glTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
-            glTexParameteri(drvTex->glTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+            RSD_CALL_GL(glTexParameteri, drvTex->glTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+            RSD_CALL_GL(glTexParameteri, drvTex->glTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+            RSD_CALL_GL(glTexParameteri, drvTex->glTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+            RSD_CALL_GL(glTexParameteri, drvTex->glTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
             rsdGLCheckError(rsc, "ProgramFragment::setup tex env");
         }
         rsdGLCheckError(rsc, "ProgramFragment::setup uniforms");
     }
 
-    glActiveTexture(GL_TEXTURE0);
+    RSD_CALL_GL(glActiveTexture, GL_TEXTURE0);
     mDirty = false;
     rsdGLCheckError(rsc, "ProgramFragment::setup");
 }
@@ -442,7 +450,8 @@
     for (uint32_t ct=0; ct < mRSProgram->mHal.state.constantsCount; ct++) {
         Allocation *alloc = mRSProgram->mHal.state.constants[ct].get();
         if (!alloc) {
-            LOGE("Attempting to set constants on shader id %u, but alloc at slot %u is not set", (uint32_t)this, ct);
+            LOGE("Attempting to set constants on shader id %u, but alloc at slot %u is not set",
+                 (uint32_t)this, ct);
             rsc->setError(RS_ERROR_BAD_SHADER, "No constant allocation bound");
             continue;
         }
@@ -470,7 +479,8 @@
                 arraySize = sc->fragUniformSize(uidx);
             }
             if (rsc->props.mLogShadersUniforms) {
-                LOGV("Uniform  slot=%i, offset=%i, constant=%i, field=%i, uidx=%i, name=%s", slot, offset, ct, field, uidx, fieldName);
+                LOGV("Uniform  slot=%i, offset=%i, constant=%i, field=%i, uidx=%i, name=%s",
+                     slot, offset, ct, field, uidx, fieldName);
             }
             uidx ++;
             if (slot < 0) {
@@ -528,7 +538,8 @@
     }
 }
 
-void RsdShader::initAddUserElement(const Element *e, String8 *names, uint32_t *arrayLengths, uint32_t *count, const char *prefix) {
+void RsdShader::initAddUserElement(const Element *e, String8 *names, uint32_t *arrayLengths,
+                                   uint32_t *count, const char *prefix) {
     rsAssert(e->getFieldCount());
     for (uint32_t ct=0; ct < e->getFieldCount(); ct++) {
         const Element *ce = e->getField(ct);
diff --git a/libs/rs/rsContext.cpp b/libs/rs/rsContext.cpp
index e3a9cf8..948d51c 100644
--- a/libs/rs/rsContext.cpp
+++ b/libs/rs/rsContext.cpp
@@ -86,7 +86,9 @@
 uint32_t Context::runRootScript() {
     timerSet(RS_TIMER_SCRIPT);
     mStateFragmentStore.mLast.clear();
+    watchdog.inRoot = true;
     uint32_t ret = runScript(mRootScript.get());
+    watchdog.inRoot = false;
 
     return ret;
 }
@@ -317,6 +319,13 @@
     mExit = true;
 }
 
+void Context::printWatchdogInfo(void *ctx) {
+    Context *rsc = (Context *)ctx;
+    LOGE("RS watchdog timeout: %i  %s  line %i %s", rsc->watchdog.inRoot,
+         rsc->watchdog.command, rsc->watchdog.line, rsc->watchdog.file);
+}
+
+
 void Context::setPriority(int32_t p) {
     // Note: If we put this in the proper "background" policy
     // the wallpapers can become completly unresponsive at times.
@@ -368,6 +377,7 @@
     pthread_mutex_lock(&gInitMutex);
 
     mIO.init();
+    mIO.setTimoutCallback(printWatchdogInfo, this, 2e9);
 
     dev->addContext(this);
     mDev = dev;
diff --git a/libs/rs/rsContext.h b/libs/rs/rsContext.h
index 3c7a3d2..199cc5a 100644
--- a/libs/rs/rsContext.h
+++ b/libs/rs/rsContext.h
@@ -191,6 +191,19 @@
         bool mLogVisual;
     } props;
 
+    mutable struct {
+        bool inRoot;
+        const char *command;
+        const char *file;
+        uint32_t line;
+    } watchdog;
+    static void printWatchdogInfo(void *ctx);
+    void setWatchdogGL(const char *cmd, uint32_t line, const char *file) const {
+        watchdog.command = cmd;
+        watchdog.file = file;
+        watchdog.line = line;
+    }
+
     void dumpDebug() const;
     void setError(RsError e, const char *msg = NULL) const;
 
diff --git a/libs/rs/rsLocklessFifo.cpp b/libs/rs/rsLocklessFifo.cpp
index 4d02269..8879805 100644
--- a/libs/rs/rsLocklessFifo.cpp
+++ b/libs/rs/rsLocklessFifo.cpp
@@ -22,6 +22,9 @@
 using namespace android::renderscript;
 
 LocklessCommandFifo::LocklessCommandFifo() : mBuffer(0), mInitialized(false) {
+    mTimeoutCallback = NULL;
+    mTimeoutCallbackData = NULL;
+    mTimeoutWait = 0;
 }
 
 LocklessCommandFifo::~LocklessCommandFifo() {
@@ -125,11 +128,21 @@
 void LocklessCommandFifo::flush() {
     //dumpState("flush 1");
     while (mPut != mGet) {
-        mSignalToControl.wait();
+        while (!mSignalToControl.wait(mTimeoutWait)) {
+            if (mTimeoutCallback) {
+                mTimeoutCallback(mTimeoutCallbackData);
+            }
+        }
     }
     //dumpState("flush 2");
 }
 
+void LocklessCommandFifo::setTimoutCallback(void (*cbk)(void *), void *data, uint64_t timeout) {
+    mTimeoutCallback = cbk;
+    mTimeoutCallbackData = data;
+    mTimeoutWait = timeout;
+}
+
 bool LocklessCommandFifo::wait(uint64_t timeout) {
     while (isEmpty() && !mInShutdown) {
         mSignalToControl.set();
diff --git a/libs/rs/rsLocklessFifo.h b/libs/rs/rsLocklessFifo.h
index fa53d40..dafc512 100644
--- a/libs/rs/rsLocklessFifo.h
+++ b/libs/rs/rsLocklessFifo.h
@@ -34,6 +34,7 @@
 public:
     bool init(uint32_t size);
     void shutdown();
+    void setTimoutCallback(void (*)(void *), void *, uint64_t timeout);
 
     void printDebugData() const;
 
@@ -71,6 +72,10 @@
 
 private:
     void dumpState(const char *) const;
+
+    void (*mTimeoutCallback)(void *);
+    void * mTimeoutCallbackData;
+    uint64_t mTimeoutWait;
 };
 
 
diff --git a/libs/rs/rsScriptC_LibGL.cpp b/libs/rs/rsScriptC_LibGL.cpp
index 3259cb4..7862f3c 100644
--- a/libs/rs/rsScriptC_LibGL.cpp
+++ b/libs/rs/rsScriptC_LibGL.cpp
@@ -158,7 +158,7 @@
     RsdVertexArray va(attribs, 2);
     va.setup(rsc);
 
-    glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
+    RSD_CALL_GL(glDrawArrays, GL_TRIANGLE_FAN, 0, 4);
 }
 
 void rsrDrawQuad(Context *rsc, Script *sc,
@@ -245,7 +245,7 @@
 }
 
 void rsrFinish(Context *rsc, Script *sc) {
-    glFinish();
+    RSD_CALL_GL(glFinish);
 }
 
 
@@ -253,16 +253,16 @@
     rsc->mFBOCache.setup(rsc);
     rsc->setupProgramStore();
 
-    glClearColor(r, g, b, a);
-    glClear(GL_COLOR_BUFFER_BIT);
+    RSD_CALL_GL(glClearColor, r, g, b, a);
+    RSD_CALL_GL(glClear, GL_COLOR_BUFFER_BIT);
 }
 
 void rsrClearDepth(Context *rsc, Script *sc, float v) {
     rsc->mFBOCache.setup(rsc);
     rsc->setupProgramStore();
 
-    glClearDepthf(v);
-    glClear(GL_DEPTH_BUFFER_BIT);
+    RSD_CALL_GL(glClearDepthf, v);
+    RSD_CALL_GL(glClear, GL_DEPTH_BUFFER_BIT);
 }
 
 uint32_t rsrGetWidth(Context *rsc, Script *sc) {
diff --git a/libs/rs/rsThreadIO.cpp b/libs/rs/rsThreadIO.cpp
index fe2c52e..b1a579a 100644
--- a/libs/rs/rsThreadIO.cpp
+++ b/libs/rs/rsThreadIO.cpp
@@ -112,6 +112,10 @@
     memcpy(data, &mToCoreRet, dataLen);
 }
 
+void ThreadIO::setTimoutCallback(void (*cb)(void *), void *dat, uint64_t timeout) {
+    mToCore.setTimoutCallback(cb, dat, timeout);
+}
+
 
 bool ThreadIO::playCoreCommands(Context *con, bool waitForCommand, uint64_t timeToWait) {
     bool ret = false;
diff --git a/libs/rs/rsThreadIO.h b/libs/rs/rsThreadIO.h
index 9036118..ebce0ab 100644
--- a/libs/rs/rsThreadIO.h
+++ b/libs/rs/rsThreadIO.h
@@ -39,6 +39,7 @@
     // Returns true if any commands were processed.
     bool playCoreCommands(Context *con, bool waitForCommand, uint64_t timeToWait);
 
+    void setTimoutCallback(void (*)(void *), void *, uint64_t timeout);
     //LocklessCommandFifo mToCore;
 
 
diff --git a/media/java/android/media/AudioService.java b/media/java/android/media/AudioService.java
index f5e1416..db27cfd 100644
--- a/media/java/android/media/AudioService.java
+++ b/media/java/android/media/AudioService.java
@@ -773,20 +773,20 @@
         }
 
         public void binderDied() {
-            IBinder newModeOwner = null;
+            int newModeOwnerPid = 0;
             synchronized(mSetModeDeathHandlers) {
                 Log.w(TAG, "setMode() client died");
                 int index = mSetModeDeathHandlers.indexOf(this);
                 if (index < 0) {
                     Log.w(TAG, "unregistered setMode() client died");
                 } else {
-                    newModeOwner = setModeInt(AudioSystem.MODE_NORMAL, mCb, mPid);
+                    newModeOwnerPid = setModeInt(AudioSystem.MODE_NORMAL, mCb, mPid);
                 }
             }
             // when entering RINGTONE, IN_CALL or IN_COMMUNICATION mode, clear all
             // SCO connections not started by the application changing the mode
-            if (newModeOwner != null) {
-                 disconnectBluetoothSco(newModeOwner);
+            if (newModeOwnerPid != 0) {
+                 disconnectBluetoothSco(newModeOwnerPid);
             }
         }
 
@@ -817,28 +817,28 @@
             return;
         }
 
-        IBinder newModeOwner = null;
+        int newModeOwnerPid = 0;
         synchronized(mSetModeDeathHandlers) {
             if (mode == AudioSystem.MODE_CURRENT) {
                 mode = mMode;
             }
-            newModeOwner = setModeInt(mode, cb, Binder.getCallingPid());
+            newModeOwnerPid = setModeInt(mode, cb, Binder.getCallingPid());
         }
         // when entering RINGTONE, IN_CALL or IN_COMMUNICATION mode, clear all
         // SCO connections not started by the application changing the mode
-        if (newModeOwner != null) {
-             disconnectBluetoothSco(newModeOwner);
+        if (newModeOwnerPid != 0) {
+             disconnectBluetoothSco(newModeOwnerPid);
         }
     }
 
     // must be called synchronized on mSetModeDeathHandlers
-    // setModeInt() returns a non null IBInder if the audio mode was successfully set to
+    // setModeInt() returns a valid PID if the audio mode was successfully set to
     // any mode other than NORMAL.
-    IBinder setModeInt(int mode, IBinder cb, int pid) {
-        IBinder newModeOwner = null;
+    int setModeInt(int mode, IBinder cb, int pid) {
+        int newModeOwnerPid = 0;
         if (cb == null) {
             Log.e(TAG, "setModeInt() called with null binder");
-            return newModeOwner;
+            return newModeOwnerPid;
         }
 
         SetModeDeathHandler hdlr = null;
@@ -901,13 +901,17 @@
 
         if (status == AudioSystem.AUDIO_STATUS_OK) {
             if (mode != AudioSystem.MODE_NORMAL) {
-                newModeOwner = cb;
+                if (mSetModeDeathHandlers.isEmpty()) {
+                    Log.e(TAG, "setMode() different from MODE_NORMAL with empty mode client stack");
+                } else {
+                    newModeOwnerPid = mSetModeDeathHandlers.get(0).getPid();
+                }
             }
             int streamType = getActiveStreamType(AudioManager.USE_DEFAULT_STREAM_TYPE);
             int index = mStreamStates[STREAM_VOLUME_ALIAS[streamType]].mIndex;
             setStreamVolumeInt(STREAM_VOLUME_ALIAS[streamType], index, true, false);
         }
-        return newModeOwner;
+        return newModeOwnerPid;
     }
 
     /** pre-condition: oldMode != newMode */
@@ -1351,6 +1355,10 @@
             return mCb;
         }
 
+        public int getPid() {
+            return mCreatorPid;
+        }
+
         public int totalCount() {
             synchronized(mScoClients) {
                 int count = 0;
@@ -1445,13 +1453,13 @@
         }
     }
 
-    public void clearAllScoClients(IBinder exceptBinder, boolean stopSco) {
+    public void clearAllScoClients(int exceptPid, boolean stopSco) {
         synchronized(mScoClients) {
             ScoClient savedClient = null;
             int size = mScoClients.size();
             for (int i = 0; i < size; i++) {
                 ScoClient cl = mScoClients.get(i);
-                if (cl.getBinder() != exceptBinder) {
+                if (cl.getPid() != exceptPid) {
                     cl.clearCount(stopSco);
                 } else {
                     savedClient = cl;
@@ -1480,7 +1488,7 @@
         return result;
     }
 
-    private void disconnectBluetoothSco(IBinder exceptBinder) {
+    private void disconnectBluetoothSco(int exceptPid) {
         synchronized(mScoClients) {
             checkScoAudioState();
             if (mScoAudioState == SCO_STATE_ACTIVE_EXTERNAL ||
@@ -1498,14 +1506,14 @@
                     }
                 }
             } else {
-                clearAllScoClients(exceptBinder, true);
+                clearAllScoClients(exceptPid, true);
             }
         }
     }
 
     private void resetBluetoothSco() {
         synchronized(mScoClients) {
-            clearAllScoClients(null, false);
+            clearAllScoClients(0, false);
             mScoAudioState = SCO_STATE_INACTIVE;
             broadcastScoConnectionState(AudioManager.SCO_AUDIO_STATE_DISCONNECTED);
         }
@@ -2509,7 +2517,7 @@
                     case BluetoothHeadset.STATE_AUDIO_DISCONNECTED:
                         state = AudioManager.SCO_AUDIO_STATE_DISCONNECTED;
                         mScoAudioState = SCO_STATE_INACTIVE;
-                        clearAllScoClients(null, false);
+                        clearAllScoClients(0, false);
                         break;
                     case BluetoothHeadset.STATE_AUDIO_CONNECTING:
                         if (mScoAudioState != SCO_STATE_ACTIVE_INTERNAL &&
diff --git a/media/java/android/media/Metadata.java b/media/java/android/media/Metadata.java
index 591a8b9..b566653 100644
--- a/media/java/android/media/Metadata.java
+++ b/media/java/android/media/Metadata.java
@@ -39,6 +39,8 @@
 
    The caller is expected to know the type of the metadata and call
    the right get* method to fetch its value.
+   
+   @hide
  */
 public class Metadata
 {
diff --git a/media/java/android/media/RemoteControlClient.java b/media/java/android/media/RemoteControlClient.java
index daa25e0..f66f1b0 100644
--- a/media/java/android/media/RemoteControlClient.java
+++ b/media/java/android/media/RemoteControlClient.java
@@ -16,7 +16,9 @@
 
 package android.media;
 
+import android.app.PendingIntent;
 import android.content.ComponentName;
+import android.content.Intent;
 import android.graphics.Bitmap;
 import android.graphics.Canvas;
 import android.graphics.Paint;
@@ -203,6 +205,8 @@
     public final static int FLAG_INFORMATION_REQUEST_ALBUM_ART = 1 << 3;
 
     /**
+     * @hide
+     * TODO remove after modifying known (internal) media apps using this API
      * Class constructor.
      * @param mediaButtonEventReceiver The receiver for the media button events. It needs to have
      *     been registered with {@link AudioManager#registerMediaButtonEventReceiver(ComponentName)}
@@ -226,6 +230,8 @@
     }
 
     /**
+     * @hide
+     * TODO remove after modifying known (internal) media apps using this API
      * Class constructor for a remote control client whose internal event handling
      * happens on a user-provided Looper.
      * @param mediaButtonEventReceiver The receiver for the media button events. It needs to have
@@ -242,6 +248,56 @@
         mEventHandler = new EventHandler(this, looper);
     }
 
+    /**
+     * Class constructor.
+     * @param mediaButtonIntent The intent that will be sent for the media button events sent
+     *     by remote controls.
+     *     This intent needs to have been constructed with the {@link Intent#ACTION_MEDIA_BUTTON}
+     *     action, and have a component that will handle the intent (set with
+     *     {@link Intent#setComponent(ComponentName)}) registered with
+     *     {@link AudioManager#registerMediaButtonEventReceiver(ComponentName)}
+     *     before this new RemoteControlClient can itself be registered with
+     *     {@link AudioManager#registerRemoteControlClient(RemoteControlClient)}.
+     * @see AudioManager#registerMediaButtonEventReceiver(ComponentName)
+     * @see AudioManager#registerRemoteControlClient(RemoteControlClient)
+     */
+    public RemoteControlClient(PendingIntent mediaButtonIntent) {
+        // TODO implement using PendingIntent instead of ComponentName
+        mRcEventReceiver = null;
+
+        Looper looper;
+        if ((looper = Looper.myLooper()) != null) {
+            mEventHandler = new EventHandler(this, looper);
+        } else if ((looper = Looper.getMainLooper()) != null) {
+            mEventHandler = new EventHandler(this, looper);
+        } else {
+            mEventHandler = null;
+            Log.e(TAG, "RemoteControlClient() couldn't find main application thread");
+        }
+    }
+
+    /**
+     * Class constructor for a remote control client whose internal event handling
+     * happens on a user-provided Looper.
+     * @param mediaButtonIntent The intent that will be sent for the media button events sent
+     *     by remote controls.
+     *     This intent needs to have been constructed with the {@link Intent#ACTION_MEDIA_BUTTON}
+     *     action, and have a component that will handle the intent (set with
+     *     {@link Intent#setComponent(ComponentName)}) registered with
+     *     {@link AudioManager#registerMediaButtonEventReceiver(ComponentName)}
+     *     before this new RemoteControlClient can itself be registered with
+     *     {@link AudioManager#registerRemoteControlClient(RemoteControlClient)}.
+     * @param looper The Looper running the event loop.
+     * @see AudioManager#registerMediaButtonEventReceiver(ComponentName)
+     * @see AudioManager#registerRemoteControlClient(RemoteControlClient)
+     */
+    public RemoteControlClient(PendingIntent mediaButtonIntent, Looper looper) {
+        // TODO implement using PendingIntent instead of ComponentName
+        mRcEventReceiver = null;
+
+        mEventHandler = new EventHandler(this, looper);
+    }
+
     private static final int[] METADATA_KEYS_TYPE_STRING = {
         MediaMetadataRetriever.METADATA_KEY_ALBUM,
         MediaMetadataRetriever.METADATA_KEY_ALBUMARTIST,
diff --git a/media/libmedia/AudioTrack.cpp b/media/libmedia/AudioTrack.cpp
index c2c6715..8ebb652 100644
--- a/media/libmedia/AudioTrack.cpp
+++ b/media/libmedia/AudioTrack.cpp
@@ -262,7 +262,7 @@
     mFlushed = false;
     mFlags = flags;
     AudioSystem::acquireAudioSessionId(mSessionId);
-
+    mRestoreStatus = NO_ERROR;
     return NO_ERROR;
 }
 
@@ -1161,8 +1161,8 @@
     status_t result;
 
     if (!(android_atomic_or(CBLK_RESTORING_ON, &cblk->flags) & CBLK_RESTORING_MSK)) {
-        LOGW("dead IAudioTrack, creating a new one from %s",
-             fromStart ? "start()" : "obtainBuffer()");
+        LOGW("dead IAudioTrack, creating a new one from %s TID %d",
+             fromStart ? "start()" : "obtainBuffer()", gettid());
 
         // signal old cblk condition so that other threads waiting for available buffers stop
         // waiting now
@@ -1217,33 +1217,35 @@
             }
             if (mActive) {
                 result = mAudioTrack->start();
+                LOGW_IF(result != NO_ERROR, "restoreTrack_l() start() failed status %d", result);
             }
             if (fromStart && result == NO_ERROR) {
                 mNewPosition = mCblk->server + mUpdatePeriod;
             }
         }
         if (result != NO_ERROR) {
-            mActive = false;
+            android_atomic_and(~CBLK_RESTORING_ON, &cblk->flags);
+            LOGW_IF(result != NO_ERROR, "restoreTrack_l() failed status %d", result);
         }
-
+        mRestoreStatus = result;
         // signal old cblk condition for other threads waiting for restore completion
         android_atomic_or(CBLK_RESTORED_ON, &cblk->flags);
         cblk->cv.broadcast();
     } else {
         if (!(cblk->flags & CBLK_RESTORED_MSK)) {
-            LOGW("dead IAudioTrack, waiting for a new one");
+            LOGW("dead IAudioTrack, waiting for a new one TID %d", gettid());
             mLock.unlock();
             result = cblk->cv.waitRelative(cblk->lock, milliseconds(RESTORE_TIMEOUT_MS));
+            if (result == NO_ERROR) {
+                result = mRestoreStatus;
+            }
             cblk->lock.unlock();
             mLock.lock();
         } else {
-            LOGW("dead IAudioTrack, already restored");
-            result = NO_ERROR;
+            LOGW("dead IAudioTrack, already restored TID %d", gettid());
+            result = mRestoreStatus;
             cblk->lock.unlock();
         }
-        if (result != NO_ERROR || mActive == 0) {
-            result = status_t(STOPPED);
-        }
     }
     LOGV("restoreTrack_l() status %d mActive %d cblk %p, old cblk %p flags %08x old flags %08x",
          result, mActive, mCblk, cblk, mCblk->flags, cblk->flags);
@@ -1254,7 +1256,7 @@
     }
     cblk->lock.lock();
 
-    LOGW_IF(result != NO_ERROR, "restoreTrack_l() error %d", result);
+    LOGW_IF(result != NO_ERROR, "restoreTrack_l() error %d TID %d", result, gettid());
 
     return result;
 }
diff --git a/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/functional/VideoEditorAPITest.java b/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/functional/VideoEditorAPITest.java
index afe2001..2a02b58 100644
--- a/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/functional/VideoEditorAPITest.java
+++ b/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/functional/VideoEditorAPITest.java
@@ -2590,12 +2590,15 @@
             + "H264_BP_1920x1080_30fps_1200Kbps_1_10.mp4";
         final int renderingMode = MediaItem.RENDERING_MODE_BLACK_BORDER;
         final MediaVideoItem mediaVideoItem1;
-        boolean flagForException = false;
+        // 1080p resolution is supported on some devices
+        // but not on other devices.
+        // So this test case is not generic and
+        // hence we always assert true
+        boolean flagForException = true;
         try {
             mediaVideoItem1 = mVideoEditorHelper.createMediaItem(mVideoEditor,
                 "m1", videoItemFileName1, renderingMode);
         } catch (IllegalArgumentException e) {
-            flagForException = true;
         }
         assertTrue("VideoContent 1920x1080", flagForException);
     }
diff --git a/packages/BackupRestoreConfirmation/AndroidManifest.xml b/packages/BackupRestoreConfirmation/AndroidManifest.xml
index 19848f6..f3feee8 100644
--- a/packages/BackupRestoreConfirmation/AndroidManifest.xml
+++ b/packages/BackupRestoreConfirmation/AndroidManifest.xml
@@ -25,6 +25,7 @@
                  android:permission="android.permission.CONFIRM_FULL_BACKUP" >
 
         <activity android:name=".BackupRestoreConfirmation" 
+                  android:title=""
                   android:windowSoftInputMode="stateAlwaysHidden"
                   android:excludeFromRecents="true"
                   android:exported="true" >
diff --git a/packages/BackupRestoreConfirmation/res/layout/confirm_restore.xml b/packages/BackupRestoreConfirmation/res/layout/confirm_restore.xml
index 3522e7c..2ee74fe 100644
--- a/packages/BackupRestoreConfirmation/res/layout/confirm_restore.xml
+++ b/packages/BackupRestoreConfirmation/res/layout/confirm_restore.xml
@@ -21,10 +21,10 @@
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
               android:layout_width="match_parent" 
               android:layout_height="match_parent"
-              android:orientation="vertical"
-              android:padding="16dp" >
+              android:orientation="vertical">
 
     <ScrollView 
+            android:padding="16dp"
             android:layout_height="0dp"
             android:layout_weight="1"
             android:layout_width="match_parent">
diff --git a/packages/BackupRestoreConfirmation/res/values/strings.xml b/packages/BackupRestoreConfirmation/res/values/strings.xml
index f022e57..e207a98 100644
--- a/packages/BackupRestoreConfirmation/res/values/strings.xml
+++ b/packages/BackupRestoreConfirmation/res/values/strings.xml
@@ -14,6 +14,10 @@
      limitations under the License.
 -->
 <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
+    <!-- Title of the activity when a full backup has been requested and must be confirmed -->
+    <string name="backup_confirm_title">Full backup</string>
+    <!-- Title of the activity when a full restore has been requested and must be confirmed -->
+    <string name="restore_confirm_title">Full restore</string>
 
     <!-- Text for message to user that a full backup has been requested, and must be confirmed. -->
     <string name="backup_confirm_text">A full backup of all data to a connected desktop computer has been requested.  Do you want to allow this to happen\?\n\nIf you did not request the backup yourself, do not allow the operation to proceed.</string>
diff --git a/packages/BackupRestoreConfirmation/src/com/android/backupconfirm/BackupRestoreConfirmation.java b/packages/BackupRestoreConfirmation/src/com/android/backupconfirm/BackupRestoreConfirmation.java
index 5448e0b..d053f29 100644
--- a/packages/BackupRestoreConfirmation/src/com/android/backupconfirm/BackupRestoreConfirmation.java
+++ b/packages/BackupRestoreConfirmation/src/com/android/backupconfirm/BackupRestoreConfirmation.java
@@ -131,10 +131,13 @@
         final String action = intent.getAction();
 
         final int layoutId;
+        final int titleId;
         if (action.equals(FullBackup.FULL_BACKUP_INTENT_ACTION)) {
             layoutId = R.layout.confirm_backup;
+            titleId = R.string.backup_confirm_title;
         } else if (action.equals(FullBackup.FULL_RESTORE_INTENT_ACTION)) {
             layoutId = R.layout.confirm_restore;
+            titleId = R.string.restore_confirm_title;
         } else {
             Slog.w(TAG, "Backup/restore confirmation activity launched with invalid action!");
             finish();
@@ -159,6 +162,7 @@
             mObserver.setHandler(mHandler);
         }
 
+        setTitle(titleId);
         setContentView(layoutId);
 
         // Same resource IDs for each layout variant (backup / restore)
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/KeyButtonView.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/KeyButtonView.java
index 840087c..3a06068 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/KeyButtonView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/KeyButtonView.java
@@ -16,12 +16,10 @@
 
 package com.android.systemui.statusbar.policy;
 
-import android.animation.Animator;
 import android.animation.AnimatorSet;
 import android.animation.ObjectAnimator;
 import android.content.Context;
 import android.content.res.TypedArray;
-import android.graphics.drawable.AnimationDrawable;
 import android.graphics.drawable.Drawable;
 import android.graphics.Canvas;
 import android.graphics.RectF;
@@ -29,7 +27,6 @@
 import android.os.SystemClock;
 import android.os.ServiceManager;
 import android.util.AttributeSet;
-import android.util.Slog;
 import android.view.accessibility.AccessibilityEvent;
 import android.view.HapticFeedbackConstants;
 import android.view.IWindowManager;
@@ -40,9 +37,7 @@
 import android.view.SoundEffectConstants;
 import android.view.View;
 import android.view.ViewConfiguration;
-import android.view.ViewGroup;
 import android.widget.ImageView;
-import android.widget.RemoteViews.RemoteView;
 
 import com.android.systemui.R;
 
@@ -53,9 +48,7 @@
 
     IWindowManager mWindowManager;
     long mDownTime;
-    boolean mSending;
     int mCode;
-    int mRepeat;
     int mTouchSlop;
     Drawable mGlowBG;
     float mGlowAlpha = 0f, mGlowScale = 1f, mDrawingAlpha = 1f;
@@ -67,12 +60,7 @@
             if (isPressed()) {
                 // Slog.d("KeyButtonView", "longpressed: " + this);
                 if (mCode != 0) {
-                    mRepeat++;
-                    sendEvent(KeyEvent.ACTION_DOWN,
-                            KeyEvent.FLAG_FROM_SYSTEM
-                            | KeyEvent.FLAG_VIRTUAL_HARD_KEY
-                            | KeyEvent.FLAG_LONG_PRESS);
-
+                    sendEvent(KeyEvent.ACTION_DOWN, KeyEvent.FLAG_LONG_PRESS);
                     sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
                 } else {
                     // Just an old-fashioned ImageView
@@ -217,64 +205,54 @@
             case MotionEvent.ACTION_DOWN:
                 //Slog.d("KeyButtonView", "press");
                 mDownTime = SystemClock.uptimeMillis();
-                mRepeat = 0;
-                mSending = true;
                 setPressed(true);
-                sendEvent(KeyEvent.ACTION_DOWN,
-                        KeyEvent.FLAG_FROM_SYSTEM | KeyEvent.FLAG_VIRTUAL_HARD_KEY, mDownTime);
+                if (mCode != 0) {
+                    sendEvent(KeyEvent.ACTION_DOWN, 0, mDownTime);
+                } else {
+                    // Provide the same haptic feedback that the system offers for virtual keys.
+                    performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
+                }
                 if (mSupportsLongpress) {
                     removeCallbacks(mCheckLongPress);
                     postDelayed(mCheckLongPress, ViewConfiguration.getLongPressTimeout());
-                } else {
-                    mSending = false;
-                    sendEvent(KeyEvent.ACTION_UP,
-                            KeyEvent.FLAG_FROM_SYSTEM | KeyEvent.FLAG_VIRTUAL_HARD_KEY, mDownTime);
-                    sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
-                    playSoundEffect(SoundEffectConstants.CLICK);
                 }
                 break;
             case MotionEvent.ACTION_MOVE:
-                if (mSending) {
-                    x = (int)ev.getX();
-                    y = (int)ev.getY();
-                    setPressed(x >= -mTouchSlop
-                            && x < getWidth() + mTouchSlop
-                            && y >= -mTouchSlop
-                            && y < getHeight() + mTouchSlop);
-                }
+                x = (int)ev.getX();
+                y = (int)ev.getY();
+                setPressed(x >= -mTouchSlop
+                        && x < getWidth() + mTouchSlop
+                        && y >= -mTouchSlop
+                        && y < getHeight() + mTouchSlop);
                 break;
             case MotionEvent.ACTION_CANCEL:
                 setPressed(false);
-                if (mSending) {
-                    mSending = false;
-                    sendEvent(KeyEvent.ACTION_UP,
-                            KeyEvent.FLAG_FROM_SYSTEM | KeyEvent.FLAG_VIRTUAL_HARD_KEY
-                                | KeyEvent.FLAG_CANCELED);
-                    if (mSupportsLongpress) {
-                        removeCallbacks(mCheckLongPress);
-                    }
+                if (mCode != 0) {
+                    sendEvent(KeyEvent.ACTION_UP, KeyEvent.FLAG_CANCELED);
+                }
+                if (mSupportsLongpress) {
+                    removeCallbacks(mCheckLongPress);
                 }
                 break;
             case MotionEvent.ACTION_UP:
                 final boolean doIt = isPressed();
                 setPressed(false);
-                if (mSending) {
-                    mSending = false;
-                    final int flags = KeyEvent.FLAG_FROM_SYSTEM | KeyEvent.FLAG_VIRTUAL_HARD_KEY;
-                    if (mSupportsLongpress) {
-                        removeCallbacks(mCheckLongPress);
-                    }
-
-                    if (mCode != 0) {
-                        if (doIt) {
-                            sendEvent(KeyEvent.ACTION_UP, flags);
-                            sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
-                            playSoundEffect(SoundEffectConstants.CLICK);
-                        }
+                if (mCode != 0) {
+                    if (doIt) {
+                        sendEvent(KeyEvent.ACTION_UP, 0);
+                        sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
+                        playSoundEffect(SoundEffectConstants.CLICK);
                     } else {
-                        // no key code, just a regular ImageView
-                        if (doIt) performClick();
+                        sendEvent(KeyEvent.ACTION_UP, KeyEvent.FLAG_CANCELED);
                     }
+                } else {
+                    // no key code, just a regular ImageView
+                    if (doIt) {
+                        performClick();
+                    }
+                }
+                if (mSupportsLongpress) {
+                    removeCallbacks(mCheckLongPress);
                 }
                 break;
         }
@@ -287,8 +265,11 @@
     }
 
     void sendEvent(int action, int flags, long when) {
-        final KeyEvent ev = new KeyEvent(mDownTime, when, action, mCode, mRepeat,
-                0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, flags, InputDevice.SOURCE_KEYBOARD);
+        final int repeatCount = (flags & KeyEvent.FLAG_LONG_PRESS) != 0 ? 1 : 0;
+        final KeyEvent ev = new KeyEvent(mDownTime, when, action, mCode, repeatCount,
+                0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0,
+                flags | KeyEvent.FLAG_FROM_SYSTEM | KeyEvent.FLAG_VIRTUAL_HARD_KEY,
+                InputDevice.SOURCE_KEYBOARD);
         try {
             //Slog.d(TAG, "injecting event " + ev);
             mWindowManager.injectInputEventNoWait(ev);
diff --git a/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java b/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
index 86671bd..9b51eb0 100755
--- a/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
+++ b/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
@@ -629,15 +629,6 @@
                 mContext.getContentResolver(), Settings.Secure.DEVICE_PROVISIONED, 0) != 0;
     }
 
-    /**
-     * When a home-key longpress expires, close other system windows and launch the recent apps
-     */
-    Runnable mHomeLongPress = new Runnable() {
-        public void run() {
-            handleLongPressOnHome();
-        }
-    };
-
     private void handleLongPressOnHome() {
         // We can't initialize this in init() since the configuration hasn't been loaded yet.
         if (mLongPressOnHomeBehavior < 0) {
@@ -1418,11 +1409,6 @@
         // it handle it, because that gives us the correct 5 second
         // timeout.
         if (keyCode == KeyEvent.KEYCODE_HOME) {
-            // Clear a pending HOME longpress if the user releases Home
-            if (!down) {
-                mHandler.removeCallbacks(mHomeLongPress);
-            }
-
             // If we have released the home key, and didn't do anything else
             // while it was pressed, then it is time to go home!
             if (mHomePressed && !down) {
@@ -1470,12 +1456,15 @@
                     }
                 }
             }
-            
-            if (down && repeatCount == 0) {
-                if (!keyguardOn) {
-                    mHandler.postDelayed(mHomeLongPress, ViewConfiguration.getGlobalActionKeyTimeout());
+
+            if (down) {
+                if (repeatCount == 0) {
+                    mHomePressed = true;
+                } else if ((event.getFlags() & KeyEvent.FLAG_LONG_PRESS) != 0) {
+                    if (!keyguardOn) {
+                        handleLongPressOnHome();
+                    }
                 }
-                mHomePressed = true;
             }
             return true;
         } else if (keyCode == KeyEvent.KEYCODE_MENU) {
@@ -2180,7 +2169,17 @@
                 if (topIsFullscreen) {
                     if (mStatusBarCanHide) {
                         if (DEBUG_LAYOUT) Log.v(TAG, "Hiding status bar");
-                        if (mStatusBar.hideLw(true)) changes |= FINISH_LAYOUT_REDO_LAYOUT;
+                        if (mStatusBar.hideLw(true)) {
+                            changes |= FINISH_LAYOUT_REDO_LAYOUT;
+
+                            mHandler.post(new Runnable() { public void run() {
+                                if (mStatusBarService != null) {
+                                    try {
+                                        mStatusBarService.collapse();
+                                    } catch (RemoteException ex) {}
+                                }
+                            }});
+                        }
                     } else if (localLOGV) {
                         Log.v(TAG, "Preventing status bar from hiding by policy");
                     }
@@ -2518,7 +2517,8 @@
                   + " screenIsOn=" + isScreenOn + " keyguardActive=" + keyguardActive);
         }
 
-        if (down && (policyFlags & WindowManagerPolicy.FLAG_VIRTUAL) != 0) {
+        if (down && (policyFlags & WindowManagerPolicy.FLAG_VIRTUAL) != 0
+                && event.getRepeatCount() == 0) {
             performHapticFeedbackLw(null, HapticFeedbackConstants.VIRTUAL_KEY, false);
         }
 
diff --git a/services/java/com/android/server/PowerManagerService.java b/services/java/com/android/server/PowerManagerService.java
index bbc26d6..bb21d81 100644
--- a/services/java/com/android/server/PowerManagerService.java
+++ b/services/java/com/android/server/PowerManagerService.java
@@ -235,6 +235,7 @@
     private boolean mPreventScreenOn;
     private int mScreenBrightnessOverride = -1;
     private int mButtonBrightnessOverride = -1;
+    private int mScreenBrightnessDim;
     private boolean mUseSoftwareAutoBrightness;
     private boolean mAutoBrightessEnabled;
     private int[] mAutoBrightnessLevels;
@@ -586,6 +587,9 @@
         mUnplugTurnsOnScreen = resources.getBoolean(
                 com.android.internal.R.bool.config_unplugTurnsOnScreen);
 
+        mScreenBrightnessDim = resources.getInteger(
+                com.android.internal.R.integer.config_screenBrightnessDim);
+
         // read settings for auto-brightness
         mUseSoftwareAutoBrightness = resources.getBoolean(
                 com.android.internal.R.bool.config_automatic_brightness_available);
@@ -1880,7 +1884,7 @@
                         nominalCurrentValue = preferredBrightness;
                         break;
                     case SCREEN_ON_BIT:
-                        nominalCurrentValue = Power.BRIGHTNESS_DIM;
+                        nominalCurrentValue = mScreenBrightnessDim;
                         break;
                     case 0:
                         nominalCurrentValue = Power.BRIGHTNESS_OFF;
@@ -1899,7 +1903,7 @@
                 // the scale is because the brightness ramp isn't linear and this biases
                 // it so the later parts take longer.
                 final float scale = 1.5f;
-                float ratio = (((float)Power.BRIGHTNESS_DIM)/preferredBrightness);
+                float ratio = (((float)mScreenBrightnessDim)/preferredBrightness);
                 if (ratio > 1.0f) ratio = 1.0f;
                 if ((newState & SCREEN_ON_BIT) == 0) {
                     if ((oldState & SCREEN_BRIGHT_BIT) != 0) {
@@ -1926,7 +1930,7 @@
                         // will then count going dim as turning off.
                         mScreenOffTime = SystemClock.elapsedRealtime();
                     }
-                    brightness = Power.BRIGHTNESS_DIM;
+                    brightness = mScreenBrightnessDim;
                 }
             }
             long identity = Binder.clearCallingIdentity();
@@ -1956,7 +1960,7 @@
             setLightBrightness(offMask, Power.BRIGHTNESS_OFF);
         }
         if (dimMask != 0) {
-            int brightness = Power.BRIGHTNESS_DIM;
+            int brightness = mScreenBrightnessDim;
             if ((newState & BATTERY_LOW_BIT) != 0 &&
                     brightness > Power.BRIGHTNESS_LOW_BATTERY) {
                 brightness = Power.BRIGHTNESS_LOW_BATTERY;
@@ -2122,7 +2126,7 @@
             final int brightness = Settings.System.getInt(mContext.getContentResolver(),
                                                           SCREEN_BRIGHTNESS);
              // Don't let applications turn the screen all the way off
-            return Math.max(brightness, Power.BRIGHTNESS_DIM);
+            return Math.max(brightness, mScreenBrightnessDim);
         } catch (SettingNotFoundException snfe) {
             return Power.BRIGHTNESS_ON;
         }
@@ -2810,7 +2814,7 @@
         mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
         // Don't let applications turn the screen all the way off
         synchronized (mLocks) {
-            brightness = Math.max(brightness, Power.BRIGHTNESS_DIM);
+            brightness = Math.max(brightness, mScreenBrightnessDim);
             mLcdLight.setBrightness(brightness);
             mKeyboardLight.setBrightness(mKeyboardVisible ? brightness : 0);
             mButtonLight.setBrightness(brightness);
diff --git a/services/java/com/android/server/accessibility/AccessibilityManagerService.java b/services/java/com/android/server/accessibility/AccessibilityManagerService.java
index 09ddc2f..1e4faad 100644
--- a/services/java/com/android/server/accessibility/AccessibilityManagerService.java
+++ b/services/java/com/android/server/accessibility/AccessibilityManagerService.java
@@ -225,7 +225,7 @@
                     return false;
                 }
             }
-            
+
             @Override
             public void onReceive(Context context, Intent intent) {
                 if (intent.getAction() == Intent.ACTION_BOOT_COMPLETED) {
@@ -697,6 +697,7 @@
     private void manageServicesLocked() {
         populateEnabledServicesLocked(mEnabledServices);
         updateServicesStateLocked(mInstalledServices, mEnabledServices);
+        disableAccessibilityIfNoEnabledServices(mEnabledServices);
     }
 
     /**
@@ -781,6 +782,19 @@
     }
 
     /**
+     * Disables accessibility if there are no enabled accessibility services which
+     * to consume the generated accessibility events.
+     *
+     * @param enabledServices The set of enabled services.
+     */
+    private void disableAccessibilityIfNoEnabledServices(Set<ComponentName> enabledServices) {
+        if (enabledServices.isEmpty()) {
+            Settings.Secure.putInt(mContext.getContentResolver(),
+                    Settings.Secure.ACCESSIBILITY_ENABLED, 0);
+        }
+    }
+
+    /**
      * Sends the state to the clients.
      */
     private void sendStateToClientsLocked() {
diff --git a/services/java/com/android/server/pm/PackageManagerService.java b/services/java/com/android/server/pm/PackageManagerService.java
index 4e5ca8e..9ebdd52 100644
--- a/services/java/com/android/server/pm/PackageManagerService.java
+++ b/services/java/com/android/server/pm/PackageManagerService.java
@@ -71,6 +71,7 @@
 import android.content.pm.Signature;
 import android.content.pm.UserInfo;
 import android.content.pm.ManifestDigest;
+import android.content.pm.VerifierDeviceIdentity;
 import android.net.Uri;
 import android.os.Binder;
 import android.os.Build;
@@ -8405,4 +8406,15 @@
         mUserManager.removeUser(userId);
         return true;
     }
+
+    @Override
+    public VerifierDeviceIdentity getVerifierDeviceIdentity() throws RemoteException {
+        mContext.enforceCallingOrSelfPermission(
+                android.Manifest.permission.PACKAGE_VERIFICATION_AGENT,
+                "Only package verification agents can read the verifier device identity");
+
+        synchronized (mPackages) {
+            return mSettings.getVerifierDeviceIdentityLPw();
+        }
+    }
 }
diff --git a/services/java/com/android/server/pm/Settings.java b/services/java/com/android/server/pm/Settings.java
index f270003..7cdb5b1 100644
--- a/services/java/com/android/server/pm/Settings.java
+++ b/services/java/com/android/server/pm/Settings.java
@@ -39,6 +39,7 @@
 import android.content.pm.PackageParser;
 import android.content.pm.PermissionInfo;
 import android.content.pm.Signature;
+import android.content.pm.VerifierDeviceIdentity;
 import android.os.Binder;
 import android.os.Environment;
 import android.os.FileUtils;
@@ -86,7 +87,10 @@
     // used to grant newer permissions one time during a system upgrade.
     int mInternalSdkPlatform;
     int mExternalSdkPlatform;
-    
+
+    /** Device identity for the purpose of package verification. */
+    private VerifierDeviceIdentity mVerifierDeviceIdentity;
+
     // The user's preferred activities associated with particular intent
     // filters.
     final IntentResolver<PreferredActivity, PreferredActivity> mPreferredActivities =
@@ -865,6 +869,12 @@
             serializer.attribute(null, "external", Integer.toString(mExternalSdkPlatform));
             serializer.endTag(null, "last-platform-version");
             
+            if (mVerifierDeviceIdentity != null) {
+                serializer.startTag(null, "verifier");
+                serializer.attribute(null, "device", mVerifierDeviceIdentity.toString());
+                serializer.endTag(null, "verifier");
+            }
+
             serializer.startTag(null, "permission-trees");
             for (BasePermission bp : mPermissionTrees.values()) {
                 writePermissionLPr(serializer, bp);
@@ -1280,6 +1290,14 @@
                         }
                     } catch (NumberFormatException e) {
                     }
+                } else if (tagName.equals("verifier")) {
+                    final String deviceIdentity = parser.getAttributeValue(null, "device");
+                    try {
+                        mVerifierDeviceIdentity = VerifierDeviceIdentity.parse(deviceIdentity);
+                    } catch (IllegalArgumentException e) {
+                        Slog.w(PackageManagerService.TAG, "Discard invalid verifier device id: "
+                                + e.getMessage());
+                    }
                 } else {
                     Slog.w(PackageManagerService.TAG, "Unknown element under <packages>: "
                             + parser.getName());
@@ -1894,6 +1912,16 @@
         return PackageManagerService.FIRST_APPLICATION_UID + N;
     }
 
+    public VerifierDeviceIdentity getVerifierDeviceIdentityLPw() {
+        if (mVerifierDeviceIdentity == null) {
+            mVerifierDeviceIdentity = VerifierDeviceIdentity.generate();
+
+            writeLPr();
+        }
+
+        return mVerifierDeviceIdentity;
+    }
+
     public PackageSetting getDisabledSystemPkgLPr(String name) {
         PackageSetting ps = mDisabledSysPackages.get(name);
         return ps;
diff --git a/telephony/java/com/android/internal/telephony/ApnSetting.java b/telephony/java/com/android/internal/telephony/ApnSetting.java
old mode 100644
new mode 100755
index b88bcf7..002ffad9
--- a/telephony/java/com/android/internal/telephony/ApnSetting.java
+++ b/telephony/java/com/android/internal/telephony/ApnSetting.java
@@ -38,12 +38,24 @@
     public final String numeric;
     public final String protocol;
     public final String roamingProtocol;
+    /**
+      * Current status of APN
+      * true : enabled APN, false : disabled APN.
+      */
+    public final boolean carrierEnabled;
+    /**
+      * Radio Access Technology info
+      * To check what values can hold, refer to ServiceState.java.
+      * This should be spread to other technologies,
+      * but currently only used for LTE(14) and EHRPD(13).
+      */
+    public final int bearer;
 
     public ApnSetting(int id, String numeric, String carrier, String apn,
             String proxy, String port,
             String mmsc, String mmsProxy, String mmsPort,
             String user, String password, int authType, String[] types,
-            String protocol, String roamingProtocol) {
+            String protocol, String roamingProtocol, boolean carrierEnabled, int bearer) {
         this.id = id;
         this.numeric = numeric;
         this.carrier = carrier;
@@ -59,6 +71,8 @@
         this.types = types;
         this.protocol = protocol;
         this.roamingProtocol = roamingProtocol;
+        this.carrierEnabled = carrierEnabled;
+        this.bearer = bearer;
     }
 
     /**
@@ -76,8 +90,8 @@
      *
      * v2 format:
      *   [ApnSettingV2] <carrier>, <apn>, <proxy>, <port>, <mmsc>, <mmsproxy>,
-     *   <mmsport>, <user>, <password, <authtype>, <mcc>, <mnc>,
-     *   <type>[| <type>...], <protocol>, <roaming_protocol>
+     *   <mmsport>, <user>, <password>, <authtype>, <mcc>, <mnc>,
+     *   <type>[| <type>...], <protocol>, <roaming_protocol>, <carrierEnabled>, <bearer>
      *
      * Note that the strings generated by toString() do not contain the username
      * and password and thus cannot be read by this method.
@@ -110,22 +124,32 @@
 
         String[] typeArray;
         String protocol, roamingProtocol;
+        boolean carrierEnabled;
+        int bearer;
         if (version == 1) {
             typeArray = new String[a.length - 13];
             System.arraycopy(a, 13, typeArray, 0, a.length - 13);
             protocol = RILConstants.SETUP_DATA_PROTOCOL_IP;
             roamingProtocol = RILConstants.SETUP_DATA_PROTOCOL_IP;
+            carrierEnabled = true;
+            bearer = 0;
         } else {
-            if (a.length < 16) {
+            if (a.length < 18) {
                 return null;
             }
             typeArray = a[13].split("\\s*\\|\\s*");
             protocol = a[14];
             roamingProtocol = a[15];
+            try {
+                carrierEnabled = Boolean.parseBoolean(a[16]);
+            } catch (Exception e) {
+                carrierEnabled = true;
+            }
+            bearer = Integer.parseInt(a[17]);
         }
 
         return new ApnSetting(-1,a[10]+a[11],a[0],a[1],a[2],a[3],a[7],a[8],
-                a[9],a[4],a[5],authType,typeArray,protocol,roamingProtocol);
+                a[9],a[4],a[5],authType,typeArray,protocol,roamingProtocol,carrierEnabled,bearer);
     }
 
     public String toString() {
@@ -149,6 +173,8 @@
         }
         sb.append(", ").append(protocol);
         sb.append(", ").append(roamingProtocol);
+        sb.append(", ").append(carrierEnabled);
+        sb.append(", ").append(bearer);
         return sb.toString();
     }
 
diff --git a/telephony/java/com/android/internal/telephony/cdma/CdmaDataConnectionTracker.java b/telephony/java/com/android/internal/telephony/cdma/CdmaDataConnectionTracker.java
index a728d0a..078e51d 100644
--- a/telephony/java/com/android/internal/telephony/cdma/CdmaDataConnectionTracker.java
+++ b/telephony/java/com/android/internal/telephony/cdma/CdmaDataConnectionTracker.java
@@ -338,7 +338,7 @@
             apnId = mDefaultApnId;
         }
         mActiveApn = new ApnSetting(apnId, "", "", "", "", "", "", "", "", "",
-                                    "", 0, types, "IP", "IP");
+                                    "", 0, types, "IP", "IP", true, 0);
         if (DBG) log("call conn.bringUp mActiveApn=" + mActiveApn);
 
         Message msg = obtainMessage();
diff --git a/telephony/java/com/android/internal/telephony/gsm/GsmDataConnectionTracker.java b/telephony/java/com/android/internal/telephony/gsm/GsmDataConnectionTracker.java
index e32270e..6d0b696 100644
--- a/telephony/java/com/android/internal/telephony/gsm/GsmDataConnectionTracker.java
+++ b/telephony/java/com/android/internal/telephony/gsm/GsmDataConnectionTracker.java
@@ -889,7 +889,10 @@
                         types,
                         cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.PROTOCOL)),
                         cursor.getString(cursor.getColumnIndexOrThrow(
-                                Telephony.Carriers.ROAMING_PROTOCOL)));
+                                Telephony.Carriers.ROAMING_PROTOCOL)),
+                        cursor.getInt(cursor.getColumnIndexOrThrow(
+                                Telephony.Carriers.CARRIER_ENABLED)) == 1,
+                        cursor.getInt(cursor.getColumnIndexOrThrow(Telephony.Carriers.BEARER)));
                 result.add(apn);
             } while (cursor.moveToNext());
         }
@@ -1982,6 +1985,9 @@
         String operator = mPhone.mIccRecords.getOperatorNumeric();
         if (operator != null) {
             String selection = "numeric = '" + operator + "'";
+            // query only enabled apn.
+            // carrier_enabled : 1 means enabled apn, 0 disabled apn.
+            selection += " and carrier_enabled = 1";
             if (DBG) log("createAllApnList: selection=" + selection);
 
             Cursor cursor = mPhone.getContext().getContentResolver().query(
@@ -2069,6 +2075,18 @@
     }
 
     /**
+     * Check current radio access technology is LTE or EHRPD.
+     *
+     * @param integer value of radio access technology
+     * @return true when current radio access technology is LTE or EHRPD
+     * @	   false when current radio access technology is not LTE or EHRPD
+     */
+    private boolean needToCheckApnBearer(int radioTech) {
+        return (radioTech == ServiceState.RADIO_TECHNOLOGY_LTE ||
+                radioTech == ServiceState.RADIO_TECHNOLOGY_EHRPD);
+    }
+
+    /**
      * Build a list of APNs to be used to create PDP's.
      *
      * @param requestedApnType
@@ -2088,6 +2106,9 @@
         }
 
         String operator = mPhone.mIccRecords.getOperatorNumeric();
+        int radioTech = mPhone.getServiceState().getRadioTechnology();
+        boolean needToCheckApnBearer = needToCheckApnBearer(radioTech);
+
         if (requestedApnType.equals(Phone.APN_TYPE_DEFAULT)) {
             if (canSetPreferApn && mPreferredApn != null) {
                 if (DBG) {
@@ -2095,9 +2116,15 @@
                         + mPreferredApn.numeric + ":" + mPreferredApn);
                 }
                 if (mPreferredApn.numeric.equals(operator)) {
-                    apnList.add(mPreferredApn);
-                    if (DBG) log("buildWaitingApns: X added preferred apnList=" + apnList);
-                    return apnList;
+                    if (!needToCheckApnBearer || mPreferredApn.bearer == radioTech) {
+                        apnList.add(mPreferredApn);
+                        if (DBG) log("buildWaitingApns: X added preferred apnList=" + apnList);
+                        return apnList;
+                    } else {
+                        if (DBG) log("buildWaitingApns: no preferred APN");
+                        setPreferredApn(-1);
+                        mPreferredApn = null;
+                    }
                 } else {
                     if (DBG) log("buildWaitingApns: no preferred APN");
                     setPreferredApn(-1);
@@ -2108,7 +2135,10 @@
         if (mAllApns != null) {
             for (ApnSetting apn : mAllApns) {
                 if (apn.canHandleType(requestedApnType)) {
-                    apnList.add(apn);
+                    if (!needToCheckApnBearer || apn.bearer == radioTech) {
+                        if (DBG) log("apn info : " +apn.toString());
+                        apnList.add(apn);
+                    }
                 }
             }
         } else {
diff --git a/telephony/tests/telephonytests/src/com/android/internal/telephony/ApnSettingTest.java b/telephony/tests/telephonytests/src/com/android/internal/telephony/ApnSettingTest.java
old mode 100644
new mode 100755
index 1032074..ac8c4c16
--- a/telephony/tests/telephonytests/src/com/android/internal/telephony/ApnSettingTest.java
+++ b/telephony/tests/telephonytests/src/com/android/internal/telephony/ApnSettingTest.java
@@ -44,6 +44,8 @@
         for (i = 0; i < a1.types.length; i++) {
             assertEquals(a1.types[i], a2.types[i]);
         }
+        assertEquals(a1.carrierEnabled, a2.carrierEnabled);
+        assertEquals(a1.bearer, a2.bearer);
     }
 
     @SmallTest
@@ -58,21 +60,21 @@
         testString = "Vodafone IT,web.omnitel.it,,,,,,,,,222,10,,DUN";
         expected_apn =  new ApnSetting(
                 -1, "22210", "Vodafone IT", "web.omnitel.it", "", "",
-                "", "", "", "", "", 0, dunTypes, "IP", "IP");
+                "", "", "", "", "", 0, dunTypes, "IP", "IP",true,0);
         assertApnSettingEqual(expected_apn, ApnSetting.fromString(testString));
 
         // A v2 string.
-        testString = "[ApnSettingV2] Name,apn,,,,,,,,,123,45,,mms|*,IPV6,IP";
+        testString = "[ApnSettingV2] Name,apn,,,,,,,,,123,45,,mms|*,IPV6,IP,true,14";
         expected_apn =  new ApnSetting(
                 -1, "12345", "Name", "apn", "", "",
-                "", "", "", "", "", 0, mmsTypes, "IPV6", "IP");
+                "", "", "", "", "", 0, mmsTypes, "IPV6", "IP",true,14);
         assertApnSettingEqual(expected_apn, ApnSetting.fromString(testString));
 
         // A v2 string with spaces.
-        testString = "[ApnSettingV2] Name,apn, ,,,,,,,,123,45,,mms|*,IPV4V6, IP";
+        testString = "[ApnSettingV2] Name,apn, ,,,,,,,,123,45,,mms|*,IPV4V6, IP,true,14";
         expected_apn =  new ApnSetting(
                 -1, "12345", "Name", "apn", "", "",
-                "", "", "", "", "", 0, mmsTypes, "IPV4V6", "IP");
+                "", "", "", "", "", 0, mmsTypes, "IPV4V6", "IP",true,14);
         assertApnSettingEqual(expected_apn, ApnSetting.fromString(testString));
 
         // Return null if insufficient fields given.
@@ -83,11 +85,11 @@
         assertEquals(null, ApnSetting.fromString(testString));
 
         // Parse (incorrect) V2 format without the tag as V1.
-        testString = "Name,apn,,,,,,,,,123, 45,,mms|*,IPV6";
+        testString = "Name,apn,,,,,,,,,123, 45,,mms|*,IPV6,true,14";
         String[] incorrectTypes = {"mms|*", "IPV6"};
         expected_apn =  new ApnSetting(
                 -1, "12345", "Name", "apn", "", "",
-                "", "", "", "", "", 0, incorrectTypes, "IP", "IP");
+                "", "", "", "", "", 0, incorrectTypes, "IP", "IP",true,14);
         assertApnSettingEqual(expected_apn, ApnSetting.fromString(testString));
     }
 
@@ -98,11 +100,10 @@
         ApnSetting apn =  new ApnSetting(
                 99, "12345", "Name", "apn", "proxy", "port",
                 "mmsc", "mmsproxy", "mmsport", "user", "password", 0,
-                types, "IPV4V6", "IP");
+                types, "IPV4V6", "IP", true, 14);
         String expected = "[ApnSettingV2] Name, 99, 12345, apn, proxy, " +
                 "mmsc, mmsproxy, mmsport, port, 0, default | *, " +
-                "IPV4V6, IP";
+                "IPV4V6, IP, true, 14";
         assertEquals(expected, apn.toString());
     }
 }
-
diff --git a/test-runner/src/android/test/mock/MockPackageManager.java b/test-runner/src/android/test/mock/MockPackageManager.java
index 501c219..f2fb36f 100644
--- a/test-runner/src/android/test/mock/MockPackageManager.java
+++ b/test-runner/src/android/test/mock/MockPackageManager.java
@@ -38,6 +38,7 @@
 import android.content.pm.ServiceInfo;
 import android.content.pm.UserInfo;
 import android.content.pm.ManifestDigest;
+import android.content.pm.VerifierDeviceIdentity;
 import android.content.res.Resources;
 import android.content.res.XmlResourceParser;
 import android.graphics.drawable.Drawable;
@@ -552,4 +553,12 @@
     public void verifyPendingInstall(int id, boolean verified, String failureMessage) {
         throw new UnsupportedOperationException();
     }
+
+    /**
+     * @hide
+     */
+    @Override
+    public VerifierDeviceIdentity getVerifierDeviceIdentity() {
+        throw new UnsupportedOperationException();
+    }
 }
diff --git a/voip/jni/rtp/AudioGroup.cpp b/voip/jni/rtp/AudioGroup.cpp
index 93c809e..459756d 100644
--- a/voip/jni/rtp/AudioGroup.cpp
+++ b/voip/jni/rtp/AudioGroup.cpp
@@ -28,6 +28,7 @@
 #include <arpa/inet.h>
 #include <netinet/in.h>
 
+// #define LOG_NDEBUG 0
 #define LOG_TAG "AudioGroup"
 #include <cutils/atomic.h>
 #include <cutils/properties.h>
@@ -62,9 +63,9 @@
 // a modulo operation on the index while accessing the array. However modulo can
 // be expensive on some platforms, such as ARM. Thus we round up the size of the
 // array to the nearest power of 2 and then use bitwise-and instead of modulo.
-// Currently we make it 512ms long and assume packet interval is 40ms or less.
-// The first 80ms is the place where samples get mixed. The rest 432ms is the
-// real jitter buffer. For a stream at 8000Hz it takes 8192 bytes. These numbers
+// Currently we make it 2048ms long and assume packet interval is 50ms or less.
+// The first 100ms is the place where samples get mixed. The rest is the real
+// jitter buffer. For a stream at 8000Hz it takes 32 kilobytes. These numbers
 // are chosen by experiments and each of them can be adjusted as needed.
 
 // Originally a stream does not send packets when it is receive-only or there is
@@ -84,9 +85,11 @@
 // + Resampling is not done yet, so streams in one group must use the same rate.
 //   For the first release only 8000Hz is supported.
 
-#define BUFFER_SIZE     512
-#define HISTORY_SIZE    80
-#define MEASURE_PERIOD  2000
+#define BUFFER_SIZE     2048
+#define HISTORY_SIZE    100
+#define MEASURE_BASE    100
+#define MEASURE_PERIOD  5000
+#define DTMF_PERIOD     200
 
 class AudioStream
 {
@@ -278,7 +281,7 @@
     if (mMode != RECEIVE_ONLY && mDtmfEvent != -1) {
         int duration = mTimestamp - mDtmfStart;
         // Make sure duration is reasonable.
-        if (duration >= 0 && duration < mSampleRate * 100) {
+        if (duration >= 0 && duration < mSampleRate * DTMF_PERIOD) {
             duration += mSampleCount;
             int32_t buffer[4] = {
                 htonl(mDtmfMagic | mSequence),
@@ -286,7 +289,7 @@
                 mSsrc,
                 htonl(mDtmfEvent | duration),
             };
-            if (duration >= mSampleRate * 100) {
+            if (duration >= mSampleRate * DTMF_PERIOD) {
                 buffer[3] |= htonl(1 << 23);
                 mDtmfEvent = -1;
             }
@@ -298,43 +301,39 @@
     }
 
     int32_t buffer[mSampleCount + 3];
+    bool data = false;
+    if (mMode != RECEIVE_ONLY) {
+        // Mix all other streams.
+        memset(buffer, 0, sizeof(buffer));
+        while (chain) {
+            if (chain != this) {
+                data |= chain->mix(buffer, tick - mInterval, tick, mSampleRate);
+            }
+            chain = chain->mNext;
+        }
+    }
+
     int16_t samples[mSampleCount];
-    if (mMode == RECEIVE_ONLY) {
+    if (data) {
+        // Saturate into 16 bits.
+        for (int i = 0; i < mSampleCount; ++i) {
+            int32_t sample = buffer[i];
+            if (sample < -32768) {
+                sample = -32768;
+            }
+            if (sample > 32767) {
+                sample = 32767;
+            }
+            samples[i] = sample;
+        }
+    } else {
         if ((mTick ^ mKeepAlive) >> 10 == 0) {
             return;
         }
         mKeepAlive = mTick;
         memset(samples, 0, sizeof(samples));
-    } else {
-        // Mix all other streams.
-        bool mixed = false;
-        memset(buffer, 0, sizeof(buffer));
-        while (chain) {
-            if (chain != this &&
-                chain->mix(buffer, tick - mInterval, tick, mSampleRate)) {
-                mixed = true;
-            }
-            chain = chain->mNext;
-        }
 
-        if (mixed) {
-            // Saturate into 16 bits.
-            for (int i = 0; i < mSampleCount; ++i) {
-                int32_t sample = buffer[i];
-                if (sample < -32768) {
-                    sample = -32768;
-                }
-                if (sample > 32767) {
-                    sample = 32767;
-                }
-                samples[i] = sample;
-            }
-        } else {
-            if ((mTick ^ mKeepAlive) >> 10 == 0) {
-                return;
-            }
-            mKeepAlive = mTick;
-            memset(samples, 0, sizeof(samples));
+        if (mMode != RECEIVE_ONLY) {
             LOGV("stream[%d] no data", mSocket);
         }
     }
@@ -380,19 +379,16 @@
         }
     }
 
-    // Adjust the jitter buffer if the latency keeps larger than two times of the
-    // packet interval in the past two seconds.
-    int score = mBufferTail - tick - mInterval * 2;
-    if (mLatencyScore > score) {
+    // Adjust the jitter buffer if the latency keeps larger than the threshold
+    // in the measurement period.
+    int score = mBufferTail - tick - MEASURE_BASE;
+    if (mLatencyScore > score || mLatencyScore <= 0) {
         mLatencyScore = score;
-    }
-    if (mLatencyScore <= 0) {
         mLatencyTimer = tick;
-        mLatencyScore = score;
     } else if (tick - mLatencyTimer >= MEASURE_PERIOD) {
         LOGV("stream[%d] reduces latency of %dms", mSocket, mLatencyScore);
         mBufferTail -= mLatencyScore;
-        mLatencyTimer = tick;
+        mLatencyScore = -1;
     }
 
     int count = (BUFFER_SIZE - (mBufferTail - mBufferHead)) * mSampleRate;