Merge "unhide API for synchronous audio capture."
diff --git a/api/current.txt b/api/current.txt
index 6ba5cb6..f61eb30 100644
--- a/api/current.txt
+++ b/api/current.txt
@@ -12496,10 +12496,14 @@
     method public void resolveService(android.net.nsd.NsdManager.Channel, java.lang.String, java.lang.String, android.net.nsd.NsdManager.DnsSdResolveListener);
     method public void stopServiceDiscovery(android.net.nsd.NsdManager.Channel, android.net.nsd.NsdManager.ActionListener);
     method public void unregisterService(android.net.nsd.NsdManager.Channel, int, android.net.nsd.NsdManager.ActionListener);
+    field public static final java.lang.String ACTION_NSD_STATE_CHANGED = "android.net.nsd.STATE_CHANGED";
     field public static final int ALREADY_ACTIVE = 3; // 0x3
     field public static final int BUSY = 2; // 0x2
     field public static final int ERROR = 0; // 0x0
+    field public static final java.lang.String EXTRA_NSD_STATE = "nsd_state";
     field public static final int MAX_REGS_REACHED = 4; // 0x4
+    field public static final int NSD_STATE_DISABLED = 1; // 0x1
+    field public static final int NSD_STATE_ENABLED = 2; // 0x2
     field public static final int UNSUPPORTED = 1; // 0x1
   }
 
diff --git a/core/java/android/app/INotificationManager.aidl b/core/java/android/app/INotificationManager.aidl
index 4d5238c..6f95e26 100644
--- a/core/java/android/app/INotificationManager.aidl
+++ b/core/java/android/app/INotificationManager.aidl
@@ -34,5 +34,8 @@
     void cancelToast(String pkg, ITransientNotification callback);
     void enqueueNotificationWithTag(String pkg, String tag, int id, in Notification notification, inout int[] idReceived);
     void cancelNotificationWithTag(String pkg, String tag, int id);
+
+    void setNotificationsEnabledForPackage(String pkg, boolean enabled);
+    boolean areNotificationsEnabledForPackage(String pkg);
 }
 
diff --git a/core/java/android/content/ClipboardManager.java b/core/java/android/content/ClipboardManager.java
index 800d0d2..dfd1820 100644
--- a/core/java/android/content/ClipboardManager.java
+++ b/core/java/android/content/ClipboardManager.java
@@ -77,7 +77,20 @@
         }
     };
 
+    /**
+     * Defines a listener callback that is invoked when the primary clip on the clipboard changes.
+     * Objects that want to register a listener call
+     * {@link android.content.ClipboardManager#addPrimaryClipChangedListener(OnPrimaryClipChangedListener)
+     * addPrimaryClipChangedListener()} with an
+     * object that implements OnPrimaryClipChangedListener.
+     *
+     */
     public interface OnPrimaryClipChangedListener {
+
+        /**
+         * Callback that is invoked by {@link android.content.ClipboardManager} when the primary
+         * clip changes.
+         */
         void onPrimaryClipChanged();
     }
 
diff --git a/core/java/android/net/nsd/INsdManager.aidl b/core/java/android/net/nsd/INsdManager.aidl
index 077a675..3361a7b 100644
--- a/core/java/android/net/nsd/INsdManager.aidl
+++ b/core/java/android/net/nsd/INsdManager.aidl
@@ -26,4 +26,5 @@
 interface INsdManager
 {
     Messenger getMessenger();
+    void setEnabled(boolean enable);
 }
diff --git a/core/java/android/net/nsd/NsdManager.java b/core/java/android/net/nsd/NsdManager.java
index dac8d20..ecf5e20 100644
--- a/core/java/android/net/nsd/NsdManager.java
+++ b/core/java/android/net/nsd/NsdManager.java
@@ -16,6 +16,8 @@
 
 package android.net.nsd;
 
+import android.annotation.SdkConstant;
+import android.annotation.SdkConstant.SdkConstantType;
 import android.content.Context;
 import android.os.Binder;
 import android.os.IBinder;
@@ -133,6 +135,40 @@
     private static final String TAG = "NsdManager";
     INsdManager mService;
 
+    /**
+     * Broadcast intent action to indicate whether network service discovery is
+     * enabled or disabled. An extra {@link #EXTRA_NSD_STATE} provides the state
+     * information as int.
+     *
+     * @see #EXTRA_NSD_STATE
+     */
+    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
+    public static final String ACTION_NSD_STATE_CHANGED =
+        "android.net.nsd.STATE_CHANGED";
+
+    /**
+     * The lookup key for an int that indicates whether network service discovery is enabled
+     * or disabled. Retrieve it with {@link android.content.Intent#getIntExtra(String,int)}.
+     *
+     * @see #NSD_STATE_DISABLED
+     * @see #NSD_STATE_ENABLED
+     */
+    public static final String EXTRA_NSD_STATE = "nsd_state";
+
+    /**
+     * Network service discovery is disabled
+     *
+     * @see #NSD_STATE_CHANGED_ACTION
+     */
+    public static final int NSD_STATE_DISABLED = 1;
+
+    /**
+     * Network service discovery is enabled
+     *
+     * @see #NSD_STATE_CHANGED_ACTION
+     */
+    public static final int NSD_STATE_ENABLED = 2;
+
     private static final int BASE = Protocol.BASE_NSD_MANAGER;
 
     /** @hide */
@@ -188,6 +224,12 @@
     /** @hide */
     public static final int STOP_RESOLVE_SUCCEEDED                  = BASE + 23;
 
+    /** @hide */
+    public static final int ENABLE                                  = BASE + 24;
+    /** @hide */
+    public static final int DISABLE                                 = BASE + 25;
+
+
     /**
      * Create a new Nsd instance. Applications use
      * {@link android.content.Context#getSystemService Context.getSystemService()} to retrieve
@@ -312,8 +354,8 @@
         private DnsSdResolveListener mDnsSdResolveListener;
         private ActionListener mDnsSdStopResolveListener;
 
-        AsyncChannel mAsyncChannel;
-        ServiceHandler mHandler;
+        private AsyncChannel mAsyncChannel;
+        private ServiceHandler mHandler;
         class ServiceHandler extends Handler {
             ServiceHandler(Looper looper) {
                 super(looper);
@@ -594,6 +636,13 @@
         c.mAsyncChannel.sendMessage(STOP_RESOLVE);
     }
 
+    /** Internal use only @hide */
+    public void setEnabled(boolean enabled) {
+        try {
+            mService.setEnabled(enabled);
+        } catch (RemoteException e) { }
+    }
+
     /**
      * Get a reference to NetworkService handler. This is used to establish
      * an AsyncChannel communication with the service
diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java
index 6dfbb2f..3a5fdd1 100644
--- a/core/java/android/provider/Settings.java
+++ b/core/java/android/provider/Settings.java
@@ -3277,6 +3277,12 @@
             "wifi_mobile_data_transition_wakelock_timeout_ms";
 
         /**
+         * Whether network service discovery is enabled.
+         * @hide
+         */
+        public static final String NSD_ON = "nsd_on";
+
+        /**
          * Whether background data usage is allowed by the user. See
          * ConnectivityManager for more info.
          */
diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java
index bd054bc..7e5fe63 100644
--- a/core/java/android/view/View.java
+++ b/core/java/android/view/View.java
@@ -12297,7 +12297,7 @@
         final int flags = parent.mGroupFlags;
         final boolean initialized = a.isInitialized();
         if (!initialized) {
-            a.initialize(mRight - mLeft, mBottom - mTop, getWidth(), getHeight());
+            a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
             a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
             onAnimationStart();
         }
diff --git a/core/java/android/widget/AbsSeekBar.java b/core/java/android/widget/AbsSeekBar.java
index ca5648a..ae68794 100644
--- a/core/java/android/widget/AbsSeekBar.java
+++ b/core/java/android/widget/AbsSeekBar.java
@@ -123,7 +123,7 @@
         invalidate();
         if (needUpdate) {
             updateThumbPos(getWidth(), getHeight());
-            if (thumb.isStateful()) {
+            if (thumb != null && thumb.isStateful()) {
                 // Note that if the states are different this won't work.
                 // For now, let's consider that an app bug.
                 int[] state = getDrawableState();
diff --git a/core/java/android/widget/ExpandableListConnector.java b/core/java/android/widget/ExpandableListConnector.java
index 2ff6b70..bda64ba 100644
--- a/core/java/android/widget/ExpandableListConnector.java
+++ b/core/java/android/widget/ExpandableListConnector.java
@@ -372,7 +372,8 @@
 
     @Override
     public boolean isEnabled(int flatListPos) {
-        final ExpandableListPosition pos = getUnflattenedPos(flatListPos).position;
+        final PositionMetadata metadata = getUnflattenedPos(flatListPos);
+        final ExpandableListPosition pos = metadata.position;
         
         boolean retValue;
         if (pos.type == ExpandableListPosition.CHILD) {
@@ -382,7 +383,7 @@
             retValue = true;
         }
         
-        pos.recycle();
+        metadata.recycle();
         
         return retValue;
     }
@@ -461,7 +462,8 @@
 
     @Override
     public int getItemViewType(int flatListPos) {
-        final ExpandableListPosition pos = getUnflattenedPos(flatListPos).position;
+        final PositionMetadata metadata = getUnflattenedPos(flatListPos);
+        final ExpandableListPosition pos = metadata.position;
 
         int retValue;
         if (mExpandableListAdapter instanceof HeterogeneousExpandableList) {
@@ -481,7 +483,7 @@
             }
         }
         
-        pos.recycle();
+        metadata.recycle();
         
         return retValue;
     }
@@ -590,8 +592,10 @@
      * @param groupPos position of the group to collapse
      */
     boolean collapseGroup(int groupPos) {
-        PositionMetadata pm = getFlattenedPos(ExpandableListPosition.obtain(
-                ExpandableListPosition.GROUP, groupPos, -1, -1)); 
+        ExpandableListPosition elGroupPos = ExpandableListPosition.obtain(
+                ExpandableListPosition.GROUP, groupPos, -1, -1);
+        PositionMetadata pm = getFlattenedPos(elGroupPos);
+        elGroupPos.recycle();
         if (pm == null) return false;
         
         boolean retValue = collapseGroup(pm);
@@ -631,8 +635,10 @@
      * @param groupPos the group to be expanded
      */
     boolean expandGroup(int groupPos) {
-        PositionMetadata pm = getFlattenedPos(ExpandableListPosition.obtain(
-                ExpandableListPosition.GROUP, groupPos, -1, -1));
+        ExpandableListPosition elGroupPos = ExpandableListPosition.obtain(
+                ExpandableListPosition.GROUP, groupPos, -1, -1);
+        PositionMetadata pm = getFlattenedPos(elGroupPos);
+        elGroupPos.recycle();
         boolean retValue = expandGroup(pm);
         pm.recycle();
         return retValue;
@@ -971,7 +977,10 @@
         public int groupInsertIndex;
         
         private void resetState() {
-            position = null;
+            if (position != null) {
+                position.recycle();
+                position = null;
+            }
             groupMetadata = null;
             groupInsertIndex = 0;
         }
@@ -1005,6 +1014,7 @@
         }
         
         public void recycle() {
+            resetState();
             synchronized (sPool) {
                 if (sPool.size() < MAX_POOL_SIZE) {
                     sPool.add(this);
diff --git a/core/java/android/widget/ExpandableListPosition.java b/core/java/android/widget/ExpandableListPosition.java
index e8d6113..bb68da6 100644
--- a/core/java/android/widget/ExpandableListPosition.java
+++ b/core/java/android/widget/ExpandableListPosition.java
@@ -125,6 +125,10 @@
         return elp;
     }
     
+    /**
+     * Do not call this unless you obtained this via ExpandableListPosition.obtain().
+     * PositionMetadata will handle recycling its own children.
+     */
     public void recycle() {
         synchronized (sPool) {
             if (sPool.size() < MAX_POOL_SIZE) {
diff --git a/core/java/android/widget/ExpandableListView.java b/core/java/android/widget/ExpandableListView.java
index c2d8bda..a746370 100644
--- a/core/java/android/widget/ExpandableListView.java
+++ b/core/java/android/widget/ExpandableListView.java
@@ -326,7 +326,6 @@
                     indicator.draw(canvas);
                 }
             }
-            
             pos.recycle();
         }
 
@@ -613,8 +612,10 @@
      *         was already expanded, this will return false)
      */
     public boolean expandGroup(int groupPos, boolean animate) {
-        PositionMetadata pm = mConnector.getFlattenedPos(ExpandableListPosition.obtain(
-                ExpandableListPosition.GROUP, groupPos, -1, -1));
+        ExpandableListPosition elGroupPos = ExpandableListPosition.obtain(
+                ExpandableListPosition.GROUP, groupPos, -1, -1);
+        PositionMetadata pm = mConnector.getFlattenedPos(elGroupPos);
+        elGroupPos.recycle();
         boolean retValue = mConnector.expandGroup(pm);
 
         if (mOnGroupExpandListener != null) {
@@ -776,8 +777,10 @@
      * @return The flat list position for the given child or group.
      */
     public int getFlatListPosition(long packedPosition) {
-        PositionMetadata pm = mConnector.getFlattenedPos(ExpandableListPosition
-                .obtainPosition(packedPosition));
+        ExpandableListPosition elPackedPos = ExpandableListPosition
+                .obtainPosition(packedPosition);
+        PositionMetadata pm = mConnector.getFlattenedPos(elPackedPos);
+        elPackedPos.recycle();
         final int flatListPosition = pm.position.flatListPos;
         pm.recycle();
         return getAbsoluteFlatPosition(flatListPosition);
@@ -988,11 +991,11 @@
         final int adjustedPosition = getFlatPositionForConnector(flatListPosition);
         PositionMetadata pm = mConnector.getUnflattenedPos(adjustedPosition);
         ExpandableListPosition pos = pm.position;
-        pm.recycle();
         
         id = getChildOrGroupId(pos);
         long packedPosition = pos.getPackedPosition();
-        pos.recycle();
+
+        pm.recycle();
         
         return new ExpandableListContextMenuInfo(view, packedPosition, id);
     }
diff --git a/core/java/com/android/internal/util/AsyncChannel.java b/core/java/com/android/internal/util/AsyncChannel.java
index 0c5d5ef..d1c2d2e 100644
--- a/core/java/com/android/internal/util/AsyncChannel.java
+++ b/core/java/com/android/internal/util/AsyncChannel.java
@@ -150,7 +150,7 @@
      */
     public static final int CMD_CHANNEL_DISCONNECTED = BASE + 4;
 
-    private static final int CMD_TO_STRING_COUNT = CMD_CHANNEL_DISCONNECTED + 1;
+    private static final int CMD_TO_STRING_COUNT = CMD_CHANNEL_DISCONNECTED - BASE + 1;
     private static String[] sCmdToString = new String[CMD_TO_STRING_COUNT];
     static {
         sCmdToString[CMD_CHANNEL_HALF_CONNECTED - BASE] = "CMD_CHANNEL_HALF_CONNECTED";
diff --git a/media/java/android/media/audiofx/Visualizer.java b/media/java/android/media/audiofx/Visualizer.java
index 91d0add..47c0d57 100755
--- a/media/java/android/media/audiofx/Visualizer.java
+++ b/media/java/android/media/audiofx/Visualizer.java
@@ -81,6 +81,20 @@
      */
     public static final int STATE_ENABLED   = 2;
 
+    // to keep in sync with system/media/audio_effects/include/audio_effects/effect_visualizer.h
+    /**
+     * @hide
+     * Defines a capture mode where amplification is applied based on the content of the captured
+     * data. This is the default Visualizer mode, and is suitable for music visualization.
+     */
+    public static final int SCALING_MODE_NORMALIZED = 0;
+    /**
+     * @hide
+     * Defines a capture mode where the playback volume will affect (scale) the range of the
+     * captured data. A low playback volume will lead to low sample and fft values, and vice-versa.
+     */
+    public static final int SCALING_MODE_AS_PLAYED = 1;
+
     // to keep in sync with frameworks/base/media/jni/audioeffect/android_media_Visualizer.cpp
     private static final int NATIVE_EVENT_PCM_CAPTURE = 0;
     private static final int NATIVE_EVENT_FFT_CAPTURE = 1;
@@ -302,6 +316,44 @@
     }
 
     /**
+     * @hide
+     * Set the type of scaling applied on the captured visualization data.
+     * @param mode see {@link #SCALING_MODE_NORMALIZED}
+     *     and {@link #SCALING_MODE_AS_PLAYED}
+     * @return {@link #SUCCESS} in case of success,
+     *     {@link #ERROR_BAD_VALUE} in case of failure.
+     * @throws IllegalStateException
+     */
+    public int setScalingMode(int mode)
+    throws IllegalStateException {
+        synchronized (mStateLock) {
+            if (mState == STATE_UNINITIALIZED) {
+                throw(new IllegalStateException("setScalingMode() called in wrong state: "
+                        + mState));
+            }
+            return native_setScalingMode(mode);
+        }
+    }
+
+    /**
+     * @hide
+     * Returns the current scaling mode on the captured visualization data.
+     * @return the scaling mode, see {@link #SCALING_MODE_NORMALIZED}
+     *     and {@link #SCALING_MODE_AS_PLAYED}.
+     * @throws IllegalStateException
+     */
+    public int getScalingMode()
+    throws IllegalStateException {
+        synchronized (mStateLock) {
+            if (mState == STATE_UNINITIALIZED) {
+                throw(new IllegalStateException("getScalingMode() called in wrong state: "
+                        + mState));
+            }
+            return native_getScalingMode();
+        }
+    }
+
+    /**
      * Returns the sampling rate of the captured audio.
      * @return the sampling rate in milliHertz.
      */
@@ -588,6 +640,10 @@
 
     private native final int native_getCaptureSize();
 
+    private native final int native_setScalingMode(int mode);
+
+    private native final int native_getScalingMode();
+
     private native final int native_getSamplingRate();
 
     private native final int native_getWaveForm(byte[] waveform);
diff --git a/media/jni/audioeffect/android_media_Visualizer.cpp b/media/jni/audioeffect/android_media_Visualizer.cpp
index f015afb..c2655c7 100644
--- a/media/jni/audioeffect/android_media_Visualizer.cpp
+++ b/media/jni/audioeffect/android_media_Visualizer.cpp
@@ -491,6 +491,27 @@
 }
 
 static jint
+android_media_visualizer_native_setScalingMode(JNIEnv *env, jobject thiz, jint mode)
+{
+    Visualizer* lpVisualizer = getVisualizer(env, thiz);
+    if (lpVisualizer == NULL) {
+        return VISUALIZER_ERROR_NO_INIT;
+    }
+
+    return translateError(lpVisualizer->setScalingMode(mode));
+}
+
+static jint
+android_media_visualizer_native_getScalingMode(JNIEnv *env, jobject thiz)
+{
+    Visualizer* lpVisualizer = getVisualizer(env, thiz);
+    if (lpVisualizer == NULL) {
+        return -1;
+    }
+    return lpVisualizer->getScalingMode();
+}
+
+static jint
 android_media_visualizer_native_getSamplingRate(JNIEnv *env, jobject thiz)
 {
     Visualizer* lpVisualizer = getVisualizer(env, thiz);
@@ -582,6 +603,8 @@
     {"getMaxCaptureRate",        "()I",   (void *)android_media_visualizer_native_getMaxCaptureRate},
     {"native_setCaptureSize",    "(I)I",  (void *)android_media_visualizer_native_setCaptureSize},
     {"native_getCaptureSize",    "()I",   (void *)android_media_visualizer_native_getCaptureSize},
+    {"native_setScalingMode",    "(I)I",  (void *)android_media_visualizer_native_setScalingMode},
+    {"native_getScalingMode",    "()I",   (void *)android_media_visualizer_native_getScalingMode},
     {"native_getSamplingRate",   "()I",   (void *)android_media_visualizer_native_getSamplingRate},
     {"native_getWaveForm",       "([B)I", (void *)android_media_visualizer_native_getWaveForm},
     {"native_getFft",            "([B)I", (void *)android_media_visualizer_native_getFft},
diff --git a/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/functional/audio/MediaVisualizerTest.java b/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/functional/audio/MediaVisualizerTest.java
index b0bf654..abf85d7 100644
--- a/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/functional/audio/MediaVisualizerTest.java
+++ b/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/functional/audio/MediaVisualizerTest.java
@@ -188,6 +188,37 @@
         assertTrue(msg, result);
     }
 
+    //Test case 1.2: check scaling mode
+    @LargeTest
+    public void test1_2ScalingMode() throws Exception {
+        boolean result = false;
+        String msg = "test1_2ScalingMode()";
+        getVisualizer(0);
+        try {
+            int res = mVisualizer.setScalingMode(Visualizer.SCALING_MODE_AS_PLAYED);
+            assertEquals(msg + ": setting SCALING_MODE_AS_PLAYED failed",
+                    res, Visualizer.SUCCESS);
+            int mode = mVisualizer.getScalingMode();
+            assertEquals(msg + ": setting SCALING_MODE_AS_PLAYED didn't stick",
+                    mode, Visualizer.SCALING_MODE_AS_PLAYED);
+
+            res = mVisualizer.setScalingMode(Visualizer.SCALING_MODE_NORMALIZED);
+            assertEquals(msg + ": setting SCALING_MODE_NORMALIZED failed",
+                    res, Visualizer.SUCCESS);
+            mode = mVisualizer.getScalingMode();
+            assertEquals(msg + ": setting SCALING_MODE_NORMALIZED didn't stick",
+                    mode, Visualizer.SCALING_MODE_NORMALIZED);
+
+            result = true;
+        } catch (IllegalStateException e) {
+            msg = msg.concat("IllegalStateException");
+            loge(msg, "set/get parameter() called in wrong state: " + e);
+        } finally {
+            releaseVisualizer();
+        }
+        assertTrue(msg, result);
+    }
+
     //-----------------------------------------------------------------
     // 2 - check capture
     //----------------------------------
@@ -403,6 +434,91 @@
         assertTrue(msg, result);
     }
 
+    //Test case 2.2: test capture in polling mode with volume scaling
+    @LargeTest
+    public void test2_2PollingCaptureVolumeScaling() throws Exception {
+        // test that when playing a sound, the energy measured with Visualizer in
+        //   SCALING_MODE_AS_PLAYED mode decreases when lowering the volume
+        boolean result = false;
+        String msg = "test2_2PollingCaptureVolumeScaling()";
+        AudioEffect vc = null;
+        MediaPlayer mp = null;
+        AudioManager am = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
+        int ringerMode = am.getRingerMode();
+        am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
+        final int volMax = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
+        am.setStreamVolume(AudioManager.STREAM_MUSIC, volMax, 0);
+
+        try {
+            // test setup not related to tested functionality:
+            // creating a volume controller on output mix ensures that ro.audio.silent mutes
+            // audio after the effects and not before
+            vc = new AudioEffect(
+                                AudioEffect.EFFECT_TYPE_NULL,
+                                VOLUME_EFFECT_UUID,
+                                0,
+                                0);
+            vc.setEnabled(true);
+
+            mp = new MediaPlayer();
+            mp.setDataSource(MediaNames.SINE_200_1000);
+            mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
+            getVisualizer(mp.getAudioSessionId());
+
+            // verify we successfully set the Visualizer in SCALING_MODE_AS_PLAYED mode
+            mVisualizer.setScalingMode(Visualizer.SCALING_MODE_AS_PLAYED);
+            assertTrue(msg + " get volume scaling doesn't return SCALING_MODE_AS_PLAYED",
+                    mVisualizer.getScalingMode() == Visualizer.SCALING_MODE_AS_PLAYED);
+            mVisualizer.setEnabled(true);
+            mp.prepare();
+            mp.start();
+            Thread.sleep(500);
+
+            // check capture on sound with music volume at max
+            byte[] data = new byte[mVisualizer.getCaptureSize()];
+            mVisualizer.getWaveForm(data);
+            int energyAtVolMax = computeEnergy(data, true);
+            assertTrue(msg +": getWaveForm reads insufficient level",
+                    energyAtVolMax > 0);
+            log(msg, " engergy at max volume = "+energyAtVolMax);
+
+            // check capture on sound with music volume lowered from max
+            am.setStreamVolume(AudioManager.STREAM_MUSIC, (volMax * 2) / 3, 0);
+            Thread.sleep(500);
+            mVisualizer.getWaveForm(data);
+            int energyAtLowerVol = computeEnergy(data, true);
+            assertTrue(msg +": getWaveForm at lower volume reads insufficient level",
+                    energyAtLowerVol > 0);
+            log(msg, "energy at lower volume = "+energyAtLowerVol);
+            assertTrue(msg +": getWaveForm didn't report lower energy when volume decreases",
+                    energyAtVolMax > energyAtLowerVol);
+
+            result = true;
+        } catch (IllegalArgumentException e) {
+            msg = msg.concat(": IllegalArgumentException");
+            loge(msg, " hit exception " + e);
+        } catch (UnsupportedOperationException e) {
+            msg = msg.concat(": UnsupportedOperationException");
+            loge(msg, " hit exception " + e);
+        } catch (IllegalStateException e) {
+            msg = msg.concat("IllegalStateException");
+            loge(msg, " hit exception " + e);
+        } catch (InterruptedException e) {
+            loge(msg, " sleep() interrupted");
+        }
+        finally {
+            releaseVisualizer();
+            if (mp != null) {
+                mp.release();
+            }
+            if (vc != null) {
+                vc.release();
+            }
+            am.setRingerMode(ringerMode);
+        }
+        assertTrue(msg, result);
+    }
+
     //-----------------------------------------------------------------
     // private methods
     //----------------------------------
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
index b7becf3..da98c80 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
@@ -218,11 +218,6 @@
 
     private int mNavigationIconHints = 0;
 
-    // TODO(dsandler): codify this stuff in NotificationManager's header somewhere
-    private int mDisplayMinScore             = Notification.PRIORITY_LOW * 10;
-    private int mIntruderMinScore            = Notification.PRIORITY_HIGH * 10;
-    private int mIntruderInImmersiveMinScore = Notification.PRIORITY_HIGH * 10 + 5;
-    
     private class ExpandedDialog extends Dialog {
         ExpandedDialog(Context context) {
             super(context, com.android.internal.R.style.Theme_Translucent_NoTitleBar);
@@ -2132,30 +2127,6 @@
         vib.vibrate(250);
     }
 
-    public int getScoreThreshold() {
-        return mDisplayMinScore;
-    }
-
-    public void setScoreThreshold(int score) {
-        // XXX HAX
-        if (mDisplayMinScore != score) {
-            this.mDisplayMinScore = score;
-            applyScoreThreshold();
-        }
-    }
-    
-    private void applyScoreThreshold() {
-        int N = mNotificationData.size();
-        for (int i=0; i<N; i++) {
-            NotificationData.Entry entry = mNotificationData.get(i);
-            int vis = (entry.notification.score < mDisplayMinScore)
-                ? View.GONE
-                : View.VISIBLE;
-            entry.row.setVisibility(vis);
-            entry.icon.setVisibility(vis);
-        }
-    }
-
     Runnable mStartTracing = new Runnable() {
         public void run() {
             vibrate();
diff --git a/services/java/com/android/server/NotificationManagerService.java b/services/java/com/android/server/NotificationManagerService.java
index b22be76..1ba7e79 100755
--- a/services/java/com/android/server/NotificationManagerService.java
+++ b/services/java/com/android/server/NotificationManagerService.java
@@ -16,7 +16,9 @@
 
 package com.android.server;
 
+import com.android.internal.os.AtomicFile;
 import com.android.internal.statusbar.StatusBarNotification;
+import com.android.internal.util.FastXmlSerializer;
 
 import android.app.ActivityManagerNative;
 import android.app.IActivityManager;
@@ -37,9 +39,10 @@
 import android.content.res.Resources;
 import android.database.ContentObserver;
 import android.media.AudioManager;
+import android.net.NetworkPolicy;
+import android.net.NetworkTemplate;
 import android.net.Uri;
 import android.os.Binder;
-import android.os.Bundle;
 import android.os.Handler;
 import android.os.IBinder;
 import android.os.Message;
@@ -53,14 +56,36 @@
 import android.util.EventLog;
 import android.util.Log;
 import android.util.Slog;
+import android.util.Xml;
 import android.view.accessibility.AccessibilityEvent;
 import android.view.accessibility.AccessibilityManager;
 import android.widget.Toast;
 
+import java.io.File;
 import java.io.FileDescriptor;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
 import java.io.PrintWriter;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.HashSet;
+
+import libcore.io.IoUtils;
+
+import org.xmlpull.v1.XmlPullParser;
+import org.xmlpull.v1.XmlPullParserException;
+import org.xmlpull.v1.XmlSerializer;
+
+import static android.net.NetworkPolicyManager.POLICY_NONE;
+import static com.android.server.net.NetworkPolicyManagerService.XmlUtils.writeBooleanAttribute;
+import static com.android.server.net.NetworkPolicyManagerService.XmlUtils.writeIntAttribute;
+import static com.android.server.net.NetworkPolicyManagerService.XmlUtils.writeLongAttribute;
+import static org.xmlpull.v1.XmlPullParser.END_DOCUMENT;
+import static org.xmlpull.v1.XmlPullParser.END_TAG;
+import static org.xmlpull.v1.XmlPullParser.START_TAG;
+
 
 /** {@hide} */
 public class NotificationManagerService extends INotificationManager.Stub
@@ -81,6 +106,13 @@
     private static final int DEFAULT_STREAM_TYPE = AudioManager.STREAM_NOTIFICATION;
     private static final boolean SCORE_ONGOING_HIGHER = false;
 
+    private static final int JUNK_SCORE = -1000;
+    private static final int NOTIFICATION_PRIORITY_MULTIPLIER = 10;
+    private static final int SCORE_DISPLAY_THRESHOLD = Notification.PRIORITY_MIN * NOTIFICATION_PRIORITY_MULTIPLIER;
+
+    private static final boolean ENABLE_BLOCKED_NOTIFICATIONS = true;
+    private static final boolean ENABLE_BLOCKED_TOASTS = true;
+
     final Context mContext;
     final IActivityManager mAm;
     final IBinder mForegroundToken = new Binder();
@@ -115,6 +147,144 @@
     private ArrayList<NotificationRecord> mLights = new ArrayList<NotificationRecord>();
     private NotificationRecord mLedNotification;
 
+    // Notification control database. For now just contains disabled packages.
+    private AtomicFile mPolicyFile;
+    private HashSet<String> mBlockedPackages = new HashSet<String>();
+
+    private static final int DB_VERSION = 1;
+
+    private static final String TAG_BODY = "notification-policy";
+    private static final String ATTR_VERSION = "version";
+
+    private static final String TAG_BLOCKED_PKGS = "blocked-packages";
+    private static final String TAG_PACKAGE = "package";
+    private static final String ATTR_NAME = "name";
+
+    private void loadBlockDb() {
+        synchronized(mBlockedPackages) {
+            if (mPolicyFile == null) {
+                File dir = new File("/data/system");
+                mPolicyFile = new AtomicFile(new File(dir, "notification_policy.xml"));
+
+                mBlockedPackages.clear();
+
+                FileInputStream infile = null;
+                try {
+                    infile = mPolicyFile.openRead();
+                    final XmlPullParser parser = Xml.newPullParser();
+                    parser.setInput(infile, null);
+
+                    int type;
+                    String tag;
+                    int version = DB_VERSION;
+                    while ((type = parser.next()) != END_DOCUMENT) {
+                        tag = parser.getName();
+                        if (type == START_TAG) {
+                            if (TAG_BODY.equals(tag)) {
+                                version = Integer.parseInt(parser.getAttributeValue(null, ATTR_VERSION));
+                            } else if (TAG_BLOCKED_PKGS.equals(tag)) {
+                                while ((type = parser.next()) != END_DOCUMENT) {
+                                    tag = parser.getName();
+                                    if (TAG_PACKAGE.equals(tag)) {
+                                        mBlockedPackages.add(parser.getAttributeValue(null, ATTR_NAME));
+                                    } else if (TAG_BLOCKED_PKGS.equals(tag) && type == END_TAG) {
+                                        break;
+                                    }
+                                }
+                            }
+                        }
+                    }
+                } catch (FileNotFoundException e) {
+                    // No data yet
+                } catch (IOException e) {
+                    Log.wtf(TAG, "Unable to read blocked notifications database", e);
+                } catch (NumberFormatException e) {
+                    Log.wtf(TAG, "Unable to parse blocked notifications database", e);
+                } catch (XmlPullParserException e) {
+                    Log.wtf(TAG, "Unable to parse blocked notifications database", e);
+                } finally {
+                    IoUtils.closeQuietly(infile);
+                }
+            }
+        }
+    }
+
+    private void writeBlockDb() {
+        synchronized(mBlockedPackages) {
+            FileOutputStream outfile = null;
+            try {
+                outfile = mPolicyFile.startWrite();
+
+                XmlSerializer out = new FastXmlSerializer();
+                out.setOutput(outfile, "utf-8");
+
+                out.startDocument(null, true);
+
+                out.startTag(null, TAG_BODY); {
+                    out.attribute(null, ATTR_VERSION, String.valueOf(DB_VERSION));
+                    out.startTag(null, TAG_BLOCKED_PKGS); {
+                        // write all known network policies
+                        for (String pkg : mBlockedPackages) {
+                            out.startTag(null, TAG_PACKAGE); {
+                                out.attribute(null, ATTR_NAME, pkg);
+                            } out.endTag(null, TAG_PACKAGE);
+                        }
+                    } out.endTag(null, TAG_BLOCKED_PKGS);
+                } out.endTag(null, TAG_BODY);
+
+                out.endDocument();
+
+                mPolicyFile.finishWrite(outfile);
+            } catch (IOException e) {
+                if (outfile != null) {
+                    mPolicyFile.failWrite(outfile);
+                }
+            }
+        }
+    }
+
+    public boolean areNotificationsEnabledForPackage(String pkg) {
+        checkCallerIsSystem();
+        return areNotificationsEnabledForPackageInt(pkg);
+    }
+
+    // Unchecked. Not exposed via Binder, but can be called in the course of enqueue*().
+    private boolean areNotificationsEnabledForPackageInt(String pkg) {
+        final boolean enabled = !mBlockedPackages.contains(pkg);
+        if (DBG) {
+            Slog.v(TAG, "notifications are " + (enabled?"en":"dis") + "abled for " + pkg);
+        }
+        return enabled;
+    }
+
+    public void setNotificationsEnabledForPackage(String pkg, boolean enabled) {
+        checkCallerIsSystem();
+        if (DBG) {
+            Slog.v(TAG, (enabled?"en":"dis") + "abling notifications for " + pkg);
+        }
+        if (enabled) {
+            mBlockedPackages.remove(pkg);
+        } else {
+            mBlockedPackages.add(pkg);
+
+            // Now, cancel any outstanding notifications that are part of a just-disabled app
+            if (ENABLE_BLOCKED_NOTIFICATIONS) {
+                synchronized (mNotificationList) {
+                    final int N = mNotificationList.size();
+                    for (int i=0; i<N; i++) {
+                        final NotificationRecord r = mNotificationList.get(i);
+                        if (r.pkg.equals(pkg)) {
+                            cancelNotificationLocked(r, false);
+                        }
+                    }
+                }
+            }
+            // Don't bother canceling toasts, they'll go away soon enough.
+        }
+        writeBlockDb();
+    }
+
+
     private static String idDebugString(Context baseContext, String packageName, int id) {
         Context c = null;
 
@@ -405,6 +575,8 @@
         mToastQueue = new ArrayList<ToastRecord>();
         mHandler = new WorkerHandler();
 
+        loadBlockDb();
+
         mStatusBar = statusBar;
         statusBar.setNotificationCallbacks(mNotificationCallbacks);
 
@@ -465,6 +637,13 @@
             return ;
         }
 
+        final boolean isSystemToast = ("android".equals(pkg));
+
+        if (ENABLE_BLOCKED_TOASTS && !isSystemToast && !areNotificationsEnabledForPackageInt(pkg)) {
+            Slog.e(TAG, "Suppressing toast from package " + pkg + " by user request.");
+            return;
+        }
+
         synchronized (mToastQueue) {
             int callingPid = Binder.getCallingPid();
             long callingId = Binder.clearCallingIdentity();
@@ -479,7 +658,7 @@
                 } else {
                     // Limit the number of toasts that any given package except the android
                     // package can enqueue.  Prevents DOS attacks and deals with leaks.
-                    if (!"android".equals(pkg)) {
+                    if (!isSystemToast) {
                         int count = 0;
                         final int N = mToastQueue.size();
                         for (int i=0; i<N; i++) {
@@ -675,11 +854,15 @@
     public void enqueueNotificationInternal(String pkg, int callingUid, int callingPid,
             String tag, int id, Notification notification, int[] idOut)
     {
-        checkIncomingCall(pkg);
+        if (DBG) {
+            Slog.v(TAG, "enqueueNotificationInternal: pkg=" + pkg + " id=" + id + " notification=" + notification);
+        }
+        checkCallerIsSystemOrSameApp(pkg);
+        final boolean isSystemNotification = ("android".equals(pkg));
 
         // Limit the number of notifications that any given package except the android
         // package can enqueue.  Prevents DOS attacks and deals with leaks.
-        if (!"android".equals(pkg)) {
+        if (!isSystemNotification) {
             synchronized (mNotificationList) {
                 int count = 0;
                 final int N = mNotificationList.size();
@@ -717,7 +900,7 @@
         }
 
         // === Scoring ===
-        
+
         // 0. Sanitize inputs
         notification.priority = clamp(notification.priority, Notification.PRIORITY_MIN, Notification.PRIORITY_MAX);
         // Migrate notification flags to scores
@@ -726,19 +909,27 @@
         } else if (SCORE_ONGOING_HIGHER && 0 != (notification.flags & Notification.FLAG_ONGOING_EVENT)) {
             if (notification.priority < Notification.PRIORITY_HIGH) notification.priority = Notification.PRIORITY_HIGH;
         }
-        
+
         // 1. initial score: buckets of 10, around the app 
-        int score = notification.priority * 10; //[-20..20]
+        int score = notification.priority * NOTIFICATION_PRIORITY_MULTIPLIER; //[-20..20]
 
-        // 2. Consult oracles (external heuristics)
-        // TODO(dsandler): oracles
+        // 2. Consult external heuristics (TBD)
 
-        // 3. Apply local heuristics & overrides
+        // 3. Apply local rules
 
         // blocked apps
-        // TODO(dsandler): add block db
-        if (pkg.startsWith("com.test.spammer.")) {
-            score = -1000;
+        if (ENABLE_BLOCKED_NOTIFICATIONS && !isSystemNotification && !areNotificationsEnabledForPackageInt(pkg)) {
+            score = JUNK_SCORE;
+            Slog.e(TAG, "Suppressing notification from package " + pkg + " by user request.");
+        }
+
+        if (DBG) {
+            Slog.v(TAG, "Assigned score=" + score + " to " + notification);
+        }
+
+        if (score < SCORE_DISPLAY_THRESHOLD) {
+            // Notification will be blocked because the score is too low.
+            return;
         }
 
         synchronized (mNotificationList) {
@@ -1030,7 +1221,7 @@
     }
 
     public void cancelNotificationWithTag(String pkg, String tag, int id) {
-        checkIncomingCall(pkg);
+        checkCallerIsSystemOrSameApp(pkg);
         // Don't allow client applications to cancel foreground service notis.
         cancelNotification(pkg, tag, id, 0,
                 Binder.getCallingUid() == Process.SYSTEM_UID
@@ -1038,14 +1229,22 @@
     }
 
     public void cancelAllNotifications(String pkg) {
-        checkIncomingCall(pkg);
+        checkCallerIsSystemOrSameApp(pkg);
 
         // Calling from user space, don't allow the canceling of actively
         // running foreground services.
         cancelAllNotificationsInt(pkg, 0, Notification.FLAG_FOREGROUND_SERVICE, true);
     }
 
-    void checkIncomingCall(String pkg) {
+    void checkCallerIsSystem() {
+        int uid = Binder.getCallingUid();
+        if (uid == Process.SYSTEM_UID || uid == 0) {
+            return;
+        }
+        throw new SecurityException("Disallowed call for uid " + uid);
+    }
+
+    void checkCallerIsSystemOrSameApp(String pkg) {
         int uid = Binder.getCallingUid();
         if (uid == Process.SYSTEM_UID || uid == 0) {
             return;
diff --git a/services/java/com/android/server/NsdService.java b/services/java/com/android/server/NsdService.java
index 8014e27..839fbe2 100644
--- a/services/java/com/android/server/NsdService.java
+++ b/services/java/com/android/server/NsdService.java
@@ -17,6 +17,8 @@
 package com.android.server;
 
 import android.content.Context;
+import android.content.ContentResolver;
+import android.content.Intent;
 import android.content.pm.PackageManager;
 import android.net.nsd.DnsSdServiceInfo;
 import android.net.nsd.DnsSdTxtRecord;
@@ -28,6 +30,7 @@
 import android.os.Message;
 import android.os.Messenger;
 import android.os.IBinder;
+import android.provider.Settings;
 import android.util.Slog;
 
 import java.io.FileDescriptor;
@@ -41,6 +44,9 @@
 import com.android.internal.app.IBatteryStats;
 import com.android.internal.telephony.TelephonyIntents;
 import com.android.internal.util.AsyncChannel;
+import com.android.internal.util.Protocol;
+import com.android.internal.util.State;
+import com.android.internal.util.StateMachine;
 import com.android.server.am.BatteryStatsService;
 import com.android.server.NativeDaemonConnector.Command;
 import com.android.internal.R;
@@ -58,6 +64,8 @@
     private static final boolean DBG = true;
 
     private Context mContext;
+    private ContentResolver mContentResolver;
+    private NsdStateMachine mNsdStateMachine;
 
     /**
      * Clients receiving asynchronous messages
@@ -69,189 +77,342 @@
     private int INVALID_ID = 0;
     private int mUniqueId = 1;
 
-    /**
-     * Handles client(app) connections
-     */
-    private class AsyncServiceHandler extends Handler {
+    private static final int BASE = Protocol.BASE_NSD_MANAGER;
+    private static final int CMD_TO_STRING_COUNT = NsdManager.STOP_RESOLVE - BASE + 1;
+    private static String[] sCmdToString = new String[CMD_TO_STRING_COUNT];
 
-        AsyncServiceHandler(android.os.Looper looper) {
-            super(looper);
-        }
+    static {
+        sCmdToString[NsdManager.DISCOVER_SERVICES - BASE] = "DISCOVER";
+        sCmdToString[NsdManager.STOP_DISCOVERY - BASE] = "STOP-DISCOVER";
+        sCmdToString[NsdManager.REGISTER_SERVICE - BASE] = "REGISTER";
+        sCmdToString[NsdManager.UNREGISTER_SERVICE - BASE] = "UNREGISTER";
+        sCmdToString[NsdManager.RESOLVE_SERVICE - BASE] = "RESOLVE";
+        sCmdToString[NsdManager.STOP_RESOLVE - BASE] = "STOP-RESOLVE";
+    }
 
-        @Override
-        public void handleMessage(Message msg) {
-            ClientInfo clientInfo;
-            DnsSdServiceInfo servInfo;
-            switch (msg.what) {
-                case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED:
-                    if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) {
-                        AsyncChannel c = (AsyncChannel) msg.obj;
-                        if (DBG) Slog.d(TAG, "New client listening to asynchronous messages");
-                        c.sendMessage(AsyncChannel.CMD_CHANNEL_FULLY_CONNECTED);
-                        ClientInfo cInfo = new ClientInfo(c, msg.replyTo);
-                        if (mClients.size() == 0) {
-                            startMDnsDaemon();
-                        }
-                        mClients.put(msg.replyTo, cInfo);
-                    } else {
-                        Slog.e(TAG, "Client connection failure, error=" + msg.arg1);
-                    }
-                    break;
-                case AsyncChannel.CMD_CHANNEL_DISCONNECTED:
-                    if (msg.arg1 == AsyncChannel.STATUS_SEND_UNSUCCESSFUL) {
-                        Slog.e(TAG, "Send failed, client connection lost");
-                    } else {
-                        if (DBG) Slog.d(TAG, "Client connection lost with reason: " + msg.arg1);
-                    }
-                    mClients.remove(msg.replyTo);
-                    if (mClients.size() == 0) {
-                        stopMDnsDaemon();
-                    }
-                    break;
-                case AsyncChannel.CMD_CHANNEL_FULL_CONNECTION:
-                    AsyncChannel ac = new AsyncChannel();
-                    ac.connect(mContext, this, msg.replyTo);
-                    break;
-                case NsdManager.DISCOVER_SERVICES:
-                    if (DBG) Slog.d(TAG, "Discover services");
-                    servInfo = (DnsSdServiceInfo) msg.obj;
-                    clientInfo = mClients.get(msg.replyTo);
-                    if (clientInfo.mDiscoveryId != INVALID_ID) {
-                        //discovery already in progress
-                        if (DBG) Slog.d(TAG, "discovery in progress");
-                        mReplyChannel.replyToMessage(msg, NsdManager.DISCOVER_SERVICES_FAILED,
-                                NsdManager.ALREADY_ACTIVE);
-                        break;
-                    }
-                    clientInfo.mDiscoveryId = getUniqueId();
-                    if (discoverServices(clientInfo.mDiscoveryId, servInfo.getServiceType())) {
-                        mReplyChannel.replyToMessage(msg, NsdManager.DISCOVER_SERVICES_STARTED);
-                    } else {
-                        mReplyChannel.replyToMessage(msg, NsdManager.DISCOVER_SERVICES_FAILED,
-                                NsdManager.ERROR);
-                        clientInfo.mDiscoveryId = INVALID_ID;
-                    }
-                    break;
-                case NsdManager.STOP_DISCOVERY:
-                    if (DBG) Slog.d(TAG, "Stop service discovery");
-                    clientInfo = mClients.get(msg.replyTo);
-                    if (clientInfo.mDiscoveryId == INVALID_ID) {
-                        //already stopped
-                        if (DBG) Slog.d(TAG, "discovery already stopped");
-                        mReplyChannel.replyToMessage(msg, NsdManager.STOP_DISCOVERY_FAILED,
-                                NsdManager.ALREADY_ACTIVE);
-                        break;
-                    }
-                    if (stopServiceDiscovery(clientInfo.mDiscoveryId)) {
-                        clientInfo.mDiscoveryId = INVALID_ID;
-                        mReplyChannel.replyToMessage(msg, NsdManager.STOP_DISCOVERY_SUCCEEDED);
-                    } else {
-                        mReplyChannel.replyToMessage(msg, NsdManager.STOP_DISCOVERY_FAILED,
-                                NsdManager.ERROR);
-                    }
-                    break;
-                case NsdManager.REGISTER_SERVICE:
-                    if (DBG) Slog.d(TAG, "Register service");
-                    clientInfo = mClients.get(msg.replyTo);
-                    if (clientInfo.mRegisteredIds.size() >= ClientInfo.MAX_REG) {
-                        if (DBG) Slog.d(TAG, "register service exceeds limit");
-                        mReplyChannel.replyToMessage(msg, NsdManager.REGISTER_SERVICE_FAILED,
-                                NsdManager.MAX_REGS_REACHED);
-                    }
-
-                    int id = getUniqueId();
-                    if (registerService(id, (DnsSdServiceInfo) msg.obj)) {
-                        clientInfo.mRegisteredIds.add(id);
-                    } else {
-                        mReplyChannel.replyToMessage(msg, NsdManager.REGISTER_SERVICE_FAILED,
-                                NsdManager.ERROR);
-                    }
-                    break;
-                case NsdManager.UNREGISTER_SERVICE:
-                    if (DBG) Slog.d(TAG, "unregister service");
-                    clientInfo = mClients.get(msg.replyTo);
-                    int regId = msg.arg1;
-                    if (clientInfo.mRegisteredIds.remove(new Integer(regId)) &&
-                            unregisterService(regId)) {
-                        mReplyChannel.replyToMessage(msg, NsdManager.UNREGISTER_SERVICE_SUCCEEDED);
-                    } else {
-                        mReplyChannel.replyToMessage(msg, NsdManager.UNREGISTER_SERVICE_FAILED,
-                                NsdManager.ERROR);
-                    }
-                    break;
-                case NsdManager.UPDATE_SERVICE:
-                    if (DBG) Slog.d(TAG, "Update service");
-                    //TODO: implement
-                    mReplyChannel.replyToMessage(msg, NsdManager.UPDATE_SERVICE_FAILED);
-                    break;
-                case NsdManager.RESOLVE_SERVICE:
-                    if (DBG) Slog.d(TAG, "Resolve service");
-                    servInfo = (DnsSdServiceInfo) msg.obj;
-                    clientInfo = mClients.get(msg.replyTo);
-                    if (clientInfo.mResolveId != INVALID_ID) {
-                        //first cancel existing resolve
-                        stopResolveService(clientInfo.mResolveId);
-                    }
-
-                    clientInfo.mResolveId = getUniqueId();
-                    if (!resolveService(clientInfo.mResolveId, servInfo)) {
-                        mReplyChannel.replyToMessage(msg, NsdManager.RESOLVE_SERVICE_FAILED,
-                                NsdManager.ERROR);
-                        clientInfo.mResolveId = INVALID_ID;
-                    }
-                    break;
-                case NsdManager.STOP_RESOLVE:
-                    if (DBG) Slog.d(TAG, "Stop resolve");
-                    clientInfo = mClients.get(msg.replyTo);
-                    if (clientInfo.mResolveId == INVALID_ID) {
-                        //already stopped
-                        if (DBG) Slog.d(TAG, "resolve already stopped");
-                        mReplyChannel.replyToMessage(msg, NsdManager.STOP_RESOLVE_FAILED,
-                                NsdManager.ALREADY_ACTIVE);
-                        break;
-                    }
-                    if (stopResolveService(clientInfo.mResolveId)) {
-                        clientInfo.mResolveId = INVALID_ID;
-                        mReplyChannel.replyToMessage(msg, NsdManager.STOP_RESOLVE_SUCCEEDED);
-                    } else {
-                        mReplyChannel.replyToMessage(msg, NsdManager.STOP_RESOLVE_FAILED,
-                                NsdManager.ERROR);
-                    }
-                    break;
-                default:
-                    Slog.d(TAG, "NsdServicehandler.handleMessage ignoring msg=" + msg);
-                    break;
-            }
+    private static String cmdToString(int cmd) {
+        cmd -= BASE;
+        if ((cmd >= 0) && (cmd < sCmdToString.length)) {
+            return sCmdToString[cmd];
+        } else {
+            return null;
         }
     }
-    private AsyncServiceHandler mAsyncServiceHandler;
+
+    private class NsdStateMachine extends StateMachine {
+
+        private DefaultState mDefaultState = new DefaultState();
+        private DisabledState mDisabledState = new DisabledState();
+        private EnabledState mEnabledState = new EnabledState();
+
+        @Override
+        protected String getMessageInfo(Message msg) {
+            return cmdToString(msg.what);
+        }
+
+        NsdStateMachine(String name) {
+            super(name);
+            addState(mDefaultState);
+                addState(mDisabledState, mDefaultState);
+                addState(mEnabledState, mDefaultState);
+            if (isNsdEnabled()) {
+                setInitialState(mEnabledState);
+            } else {
+                setInitialState(mDisabledState);
+            }
+            setProcessedMessagesSize(25);
+        }
+
+        class DefaultState extends State {
+            @Override
+            public boolean processMessage(Message msg) {
+                switch (msg.what) {
+                    case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED:
+                        if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) {
+                            AsyncChannel c = (AsyncChannel) msg.obj;
+                            if (DBG) Slog.d(TAG, "New client listening to asynchronous messages");
+                            c.sendMessage(AsyncChannel.CMD_CHANNEL_FULLY_CONNECTED);
+                            ClientInfo cInfo = new ClientInfo(c, msg.replyTo);
+                            mClients.put(msg.replyTo, cInfo);
+                        } else {
+                            Slog.e(TAG, "Client connection failure, error=" + msg.arg1);
+                        }
+                        break;
+                    case AsyncChannel.CMD_CHANNEL_DISCONNECTED:
+                        if (msg.arg1 == AsyncChannel.STATUS_SEND_UNSUCCESSFUL) {
+                            Slog.e(TAG, "Send failed, client connection lost");
+                        } else {
+                            if (DBG) Slog.d(TAG, "Client connection lost with reason: " + msg.arg1);
+                        }
+                        mClients.remove(msg.replyTo);
+                        break;
+                    case AsyncChannel.CMD_CHANNEL_FULL_CONNECTION:
+                        AsyncChannel ac = new AsyncChannel();
+                        ac.connect(mContext, getHandler(), msg.replyTo);
+                        break;
+                    case NsdManager.DISCOVER_SERVICES:
+                        mReplyChannel.replyToMessage(msg, NsdManager.DISCOVER_SERVICES_FAILED,
+                                NsdManager.BUSY);
+                       break;
+                    case NsdManager.STOP_DISCOVERY:
+                            mReplyChannel.replyToMessage(msg, NsdManager.STOP_DISCOVERY_FAILED,
+                                    NsdManager.ERROR);
+                        break;
+                    case NsdManager.REGISTER_SERVICE:
+                        mReplyChannel.replyToMessage(msg, NsdManager.REGISTER_SERVICE_FAILED,
+                                NsdManager.ERROR);
+                        break;
+                    case NsdManager.UNREGISTER_SERVICE:
+                        mReplyChannel.replyToMessage(msg, NsdManager.UNREGISTER_SERVICE_FAILED,
+                                NsdManager.ERROR);
+                        break;
+                    case NsdManager.RESOLVE_SERVICE:
+                        mReplyChannel.replyToMessage(msg, NsdManager.RESOLVE_SERVICE_FAILED,
+                                NsdManager.ERROR);
+                        break;
+                    case NsdManager.STOP_RESOLVE:
+                        mReplyChannel.replyToMessage(msg, NsdManager.STOP_RESOLVE_FAILED,
+                                NsdManager.ERROR);
+                        break;
+                    default:
+                        Slog.e(TAG, "Unhandled " + msg);
+                        return NOT_HANDLED;
+                }
+                return HANDLED;
+            }
+        }
+
+        class DisabledState extends State {
+            @Override
+            public void enter() {
+                sendNsdStateChangeBroadcast(false);
+            }
+
+            @Override
+            public boolean processMessage(Message msg) {
+                switch (msg.what) {
+                    case NsdManager.ENABLE:
+                        transitionTo(mEnabledState);
+                        break;
+                    default:
+                        return NOT_HANDLED;
+                }
+                return HANDLED;
+            }
+        }
+
+        class EnabledState extends State {
+            @Override
+            public void enter() {
+                sendNsdStateChangeBroadcast(true);
+                if (mClients.size() > 0) {
+                    startMDnsDaemon();
+                }
+            }
+
+            @Override
+            public void exit() {
+                if (mClients.size() > 0) {
+                    stopMDnsDaemon();
+                }
+            }
+
+            @Override
+            public boolean processMessage(Message msg) {
+                ClientInfo clientInfo;
+                DnsSdServiceInfo servInfo;
+                boolean result = HANDLED;
+                switch (msg.what) {
+                  case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED:
+                        //First client
+                        if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL &&
+                                mClients.size() == 0) {
+                            startMDnsDaemon();
+                        }
+                        result = NOT_HANDLED;
+                        break;
+                    case AsyncChannel.CMD_CHANNEL_DISCONNECTED:
+                        //Last client
+                        if (mClients.size() == 1) {
+                            stopMDnsDaemon();
+                        }
+                        result = NOT_HANDLED;
+                        break;
+                    case NsdManager.DISABLE:
+                        //TODO: cleanup clients
+                        transitionTo(mDisabledState);
+                        break;
+                    case NsdManager.DISCOVER_SERVICES:
+                        if (DBG) Slog.d(TAG, "Discover services");
+                        servInfo = (DnsSdServiceInfo) msg.obj;
+                        clientInfo = mClients.get(msg.replyTo);
+                        if (clientInfo.mDiscoveryId != INVALID_ID) {
+                            //discovery already in progress
+                            if (DBG) Slog.d(TAG, "discovery in progress");
+                            mReplyChannel.replyToMessage(msg, NsdManager.DISCOVER_SERVICES_FAILED,
+                                    NsdManager.ALREADY_ACTIVE);
+                            break;
+                        }
+                        clientInfo.mDiscoveryId = getUniqueId();
+                        if (discoverServices(clientInfo.mDiscoveryId, servInfo.getServiceType())) {
+                            mReplyChannel.replyToMessage(msg, NsdManager.DISCOVER_SERVICES_STARTED);
+                        } else {
+                            mReplyChannel.replyToMessage(msg, NsdManager.DISCOVER_SERVICES_FAILED,
+                                    NsdManager.ERROR);
+                            clientInfo.mDiscoveryId = INVALID_ID;
+                        }
+                        break;
+                    case NsdManager.STOP_DISCOVERY:
+                        if (DBG) Slog.d(TAG, "Stop service discovery");
+                        clientInfo = mClients.get(msg.replyTo);
+                        if (clientInfo.mDiscoveryId == INVALID_ID) {
+                            //already stopped
+                            if (DBG) Slog.d(TAG, "discovery already stopped");
+                            mReplyChannel.replyToMessage(msg, NsdManager.STOP_DISCOVERY_FAILED,
+                                    NsdManager.ALREADY_ACTIVE);
+                            break;
+                        }
+                        if (stopServiceDiscovery(clientInfo.mDiscoveryId)) {
+                            clientInfo.mDiscoveryId = INVALID_ID;
+                            mReplyChannel.replyToMessage(msg, NsdManager.STOP_DISCOVERY_SUCCEEDED);
+                        } else {
+                            mReplyChannel.replyToMessage(msg, NsdManager.STOP_DISCOVERY_FAILED,
+                                    NsdManager.ERROR);
+                        }
+                        break;
+                    case NsdManager.REGISTER_SERVICE:
+                        if (DBG) Slog.d(TAG, "Register service");
+                        clientInfo = mClients.get(msg.replyTo);
+                        if (clientInfo.mRegisteredIds.size() >= ClientInfo.MAX_REG) {
+                            if (DBG) Slog.d(TAG, "register service exceeds limit");
+                            mReplyChannel.replyToMessage(msg, NsdManager.REGISTER_SERVICE_FAILED,
+                                    NsdManager.MAX_REGS_REACHED);
+                        }
+
+                        int id = getUniqueId();
+                        if (registerService(id, (DnsSdServiceInfo) msg.obj)) {
+                            clientInfo.mRegisteredIds.add(id);
+                        } else {
+                            mReplyChannel.replyToMessage(msg, NsdManager.REGISTER_SERVICE_FAILED,
+                                    NsdManager.ERROR);
+                        }
+                        break;
+                    case NsdManager.UNREGISTER_SERVICE:
+                        if (DBG) Slog.d(TAG, "unregister service");
+                        clientInfo = mClients.get(msg.replyTo);
+                        int regId = msg.arg1;
+                        if (clientInfo.mRegisteredIds.remove(new Integer(regId)) &&
+                                unregisterService(regId)) {
+                            mReplyChannel.replyToMessage(msg,
+                                    NsdManager.UNREGISTER_SERVICE_SUCCEEDED);
+                        } else {
+                            mReplyChannel.replyToMessage(msg, NsdManager.UNREGISTER_SERVICE_FAILED,
+                                    NsdManager.ERROR);
+                        }
+                        break;
+                    case NsdManager.UPDATE_SERVICE:
+                        if (DBG) Slog.d(TAG, "Update service");
+                        //TODO: implement
+                        mReplyChannel.replyToMessage(msg, NsdManager.UPDATE_SERVICE_FAILED);
+                        break;
+                    case NsdManager.RESOLVE_SERVICE:
+                        if (DBG) Slog.d(TAG, "Resolve service");
+                        servInfo = (DnsSdServiceInfo) msg.obj;
+                        clientInfo = mClients.get(msg.replyTo);
+                        if (clientInfo.mResolveId != INVALID_ID) {
+                            //first cancel existing resolve
+                            stopResolveService(clientInfo.mResolveId);
+                        }
+
+                        clientInfo.mResolveId = getUniqueId();
+                        if (!resolveService(clientInfo.mResolveId, servInfo)) {
+                            mReplyChannel.replyToMessage(msg, NsdManager.RESOLVE_SERVICE_FAILED,
+                                    NsdManager.ERROR);
+                            clientInfo.mResolveId = INVALID_ID;
+                        }
+                        break;
+                    case NsdManager.STOP_RESOLVE:
+                        if (DBG) Slog.d(TAG, "Stop resolve");
+                        clientInfo = mClients.get(msg.replyTo);
+                        if (clientInfo.mResolveId == INVALID_ID) {
+                            //already stopped
+                            if (DBG) Slog.d(TAG, "resolve already stopped");
+                            mReplyChannel.replyToMessage(msg, NsdManager.STOP_RESOLVE_FAILED,
+                                    NsdManager.ALREADY_ACTIVE);
+                            break;
+                        }
+                        if (stopResolveService(clientInfo.mResolveId)) {
+                            clientInfo.mResolveId = INVALID_ID;
+                            mReplyChannel.replyToMessage(msg, NsdManager.STOP_RESOLVE_SUCCEEDED);
+                        } else {
+                            mReplyChannel.replyToMessage(msg, NsdManager.STOP_RESOLVE_FAILED,
+                                    NsdManager.ERROR);
+                        }
+                        break;
+                    default:
+                        result = NOT_HANDLED;
+                        break;
+                }
+                return result;
+            }
+       }
+    }
 
     private NativeDaemonConnector mNativeConnector;
     private final CountDownLatch mNativeDaemonConnected = new CountDownLatch(1);
 
     private NsdService(Context context) {
         mContext = context;
-
-        HandlerThread nsdThread = new HandlerThread("NsdService");
-        nsdThread.start();
-        mAsyncServiceHandler = new AsyncServiceHandler(nsdThread.getLooper());
+        mContentResolver = context.getContentResolver();
 
         mNativeConnector = new NativeDaemonConnector(new NativeCallbackReceiver(), "mdns", 10,
                 MDNS_TAG, 25);
+
+        mNsdStateMachine = new NsdStateMachine(TAG);
+        mNsdStateMachine.start();
+
         Thread th = new Thread(mNativeConnector, MDNS_TAG);
         th.start();
     }
 
     public static NsdService create(Context context) throws InterruptedException {
         NsdService service = new NsdService(context);
-        /* service.mNativeDaemonConnected.await(); */
+        service.mNativeDaemonConnected.await();
         return service;
     }
 
     public Messenger getMessenger() {
         mContext.enforceCallingOrSelfPermission(android.Manifest.permission.INTERNET,
             "NsdService");
-        return new Messenger(mAsyncServiceHandler);
+        return new Messenger(mNsdStateMachine.getHandler());
+    }
+
+    public void setEnabled(boolean enable) {
+        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL,
+                "NsdService");
+        Settings.Secure.putInt(mContentResolver, Settings.Secure.NSD_ON, enable ? 1 : 0);
+        if (enable) {
+            mNsdStateMachine.sendMessage(NsdManager.ENABLE);
+        } else {
+            mNsdStateMachine.sendMessage(NsdManager.DISABLE);
+        }
+    }
+
+    private void sendNsdStateChangeBroadcast(boolean enabled) {
+        final Intent intent = new Intent(NsdManager.NSD_STATE_CHANGED_ACTION);
+        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
+        if (enabled) {
+            intent.putExtra(NsdManager.EXTRA_NSD_STATE, NsdManager.NSD_STATE_ENABLED);
+        } else {
+            intent.putExtra(NsdManager.EXTRA_NSD_STATE, NsdManager.NSD_STATE_DISABLED);
+        }
+        mContext.sendStickyBroadcast(intent);
+    }
+
+    private boolean isNsdEnabled() {
+        boolean ret = Settings.Secure.getInt(mContentResolver, Settings.Secure.NSD_ON, 1) == 1;
+        if (DBG) Slog.d(TAG, "Network service discovery enabled " + ret);
+        return ret;
     }
 
     private int getUniqueId() {
@@ -522,7 +683,7 @@
     }
 
     @Override
-    protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
+    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
         if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
                 != PackageManager.PERMISSION_GRANTED) {
             pw.println("Permission Denial: can't dump ServiceDiscoverService from from pid="
@@ -531,7 +692,12 @@
             return;
         }
 
-        pw.println("Internal state:");
+        for (ClientInfo client : mClients.values()) {
+            pw.println("Client Info");
+            pw.println(client);
+        }
+
+        mNsdStateMachine.dump(fd, pw, args);
     }
 
     private ClientInfo getClientByDiscovery(int discoveryId) {
@@ -579,5 +745,19 @@
             mDiscoveryId = mResolveId = INVALID_ID;
             if (DBG) Slog.d(TAG, "New client, channel: " + c + " messenger: " + m);
         }
+
+        @Override
+        public String toString() {
+            StringBuffer sb = new StringBuffer();
+            sb.append("mChannel ").append(mChannel).append("\n");
+            sb.append("mMessenger ").append(mMessenger).append("\n");
+            sb.append("mDiscoveryId ").append(mDiscoveryId).append("\n");
+            sb.append("mResolveId ").append(mResolveId).append("\n");
+            sb.append("mResolvedService ").append(mResolvedService).append("\n");
+            for(int regId : mRegisteredIds) {
+                sb.append("regId ").append(regId).append("\n");
+            }
+            return sb.toString();
+        }
     }
 }
diff --git a/services/java/com/android/server/wm/WindowStateAnimator.java b/services/java/com/android/server/wm/WindowStateAnimator.java
index 3a51afe..0cebee7 100644
--- a/services/java/com/android/server/wm/WindowStateAnimator.java
+++ b/services/java/com/android/server/wm/WindowStateAnimator.java
@@ -978,7 +978,7 @@
 
         setSurfaceBoundaries(recoveringMemory);
 
-        if (w.mAttachedHidden || !w.isReadyForDisplay() || !w.isDrawnLw()) {
+        if (w.mAttachedHidden || !w.isReadyForDisplay()) {
             if (!mLastHidden) {
                 //dump();
                 mLastHidden = true;
@@ -1136,17 +1136,15 @@
                     + " animating=" + mAnimating
                     + " tok animating="
                     + (mWin.mAppToken != null ? mWin.mAppToken.mAppAnimator.animating : false));
-            if (!showSurfaceRobustlyLocked()) {
-                return false;
-            }
 
             mService.enableScreenIfNeededLocked();
 
             applyEnterAnimationLocked();
 
+            // Force the show in the next prepareSurfaceLocked() call.
             mLastAlpha = -1;
-            mLastHidden = false;
             mDrawState = HAS_DRAWN;
+            mService.scheduleAnimationLocked();
 
             int i = mWin.mChildWindows.size();
             while (i > 0) {
diff --git a/telephony/java/com/android/internal/telephony/DataConnection.java b/telephony/java/com/android/internal/telephony/DataConnection.java
index a124c7f..da03f76 100644
--- a/telephony/java/com/android/internal/telephony/DataConnection.java
+++ b/telephony/java/com/android/internal/telephony/DataConnection.java
@@ -209,7 +209,7 @@
     protected static final int EVENT_RIL_CONNECTED = BASE + 5;
     protected static final int EVENT_DISCONNECT_ALL = BASE + 6;
 
-    private static final int CMD_TO_STRING_COUNT = EVENT_DISCONNECT_ALL + 1;
+    private static final int CMD_TO_STRING_COUNT = EVENT_DISCONNECT_ALL - BASE + 1;
     private static String[] sCmdToString = new String[CMD_TO_STRING_COUNT];
     static {
         sCmdToString[EVENT_CONNECT - BASE] = "EVENT_CONNECT";
diff --git a/telephony/java/com/android/internal/telephony/DataConnectionAc.java b/telephony/java/com/android/internal/telephony/DataConnectionAc.java
index 4744ff0..96419ae 100644
--- a/telephony/java/com/android/internal/telephony/DataConnectionAc.java
+++ b/telephony/java/com/android/internal/telephony/DataConnectionAc.java
@@ -82,7 +82,7 @@
     public static final int REQ_GET_RECONNECT_INTENT = BASE + 26;
     public static final int RSP_GET_RECONNECT_INTENT = BASE + 27;
 
-    private static final int CMD_TO_STRING_COUNT = RSP_GET_RECONNECT_INTENT + 1;
+    private static final int CMD_TO_STRING_COUNT = RSP_GET_RECONNECT_INTENT - BASE + 1;
     private static String[] sCmdToString = new String[CMD_TO_STRING_COUNT];
     static {
         sCmdToString[REQ_IS_INACTIVE - BASE] = "REQ_IS_INACTIVE";