Merge change I204e4787 into eclair

* changes:
  Fix updating Bluetooth icon on status bar and for Wifi.
diff --git a/core/jni/android_media_AudioRecord.cpp b/core/jni/android_media_AudioRecord.cpp
index 0be996d..d7485ae 100644
--- a/core/jni/android_media_AudioRecord.cpp
+++ b/core/jni/android_media_AudioRecord.cpp
@@ -224,17 +224,17 @@
 
 
 // ----------------------------------------------------------------------------
-static void
+static int
 android_media_AudioRecord_start(JNIEnv *env, jobject thiz)
 {
     AudioRecord *lpRecorder = 
             (AudioRecord *)env->GetIntField(thiz, javaAudioRecordFields.nativeRecorderInJavaObj);
     if (lpRecorder == NULL ) {
         jniThrowException(env, "java/lang/IllegalStateException", NULL);
-        return;
+        return AUDIORECORD_ERROR;
     }
     
-    lpRecorder->start();
+    return android_media_translateRecorderErrorCode(lpRecorder->start());
 }
 
 
@@ -482,7 +482,7 @@
 // ----------------------------------------------------------------------------
 static JNINativeMethod gMethods[] = {
     // name,               signature,  funcPtr
-    {"native_start",         "()V",    (void *)android_media_AudioRecord_start},
+    {"native_start",         "()I",    (void *)android_media_AudioRecord_start},
     {"native_stop",          "()V",    (void *)android_media_AudioRecord_stop},
     {"native_setup",         "(Ljava/lang/Object;IIIII)I",
                                        (void *)android_media_AudioRecord_setup},
diff --git a/graphics/java/android/renderscript/Allocation.java b/graphics/java/android/renderscript/Allocation.java
index 957f2dd..b6ac14a 100644
--- a/graphics/java/android/renderscript/Allocation.java
+++ b/graphics/java/android/renderscript/Allocation.java
@@ -40,27 +40,36 @@
     }
 
     public void uploadToTexture(int baseMipLevel) {
+        mRS.validate();
+        mRS.validateSurface();
         mRS.nAllocationUploadToTexture(mID, baseMipLevel);
     }
 
     public void uploadToBufferObject() {
+        mRS.validate();
+        mRS.validateSurface();
         mRS.nAllocationUploadToBufferObject(mID);
     }
 
     public void data(int[] d) {
+        mRS.validate();
         subData1D(0, mType.getElementCount(), d);
     }
     public void data(short[] d) {
+        mRS.validate();
         subData1D(0, mType.getElementCount(), d);
     }
     public void data(byte[] d) {
+        mRS.validate();
         subData1D(0, mType.getElementCount(), d);
     }
     public void data(float[] d) {
+        mRS.validate();
         subData1D(0, mType.getElementCount(), d);
     }
 
     private void data1DChecks(int off, int count, int len, int dataSize) {
+        mRS.validate();
         if((off < 0) || (count < 1) || ((off + count) > mType.getElementCount())) {
             throw new IllegalArgumentException("Offset or Count out of bounds.");
         }
@@ -93,30 +102,37 @@
 
 
     public void subData2D(int xoff, int yoff, int w, int h, int[] d) {
+        mRS.validate();
         mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d, d.length * 4);
     }
 
     public void subData2D(int xoff, int yoff, int w, int h, float[] d) {
+        mRS.validate();
         mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d, d.length * 4);
     }
 
     public void readData(int[] d) {
+        mRS.validate();
         mRS.nAllocationRead(mID, d);
     }
 
     public void readData(float[] d) {
+        mRS.validate();
         mRS.nAllocationRead(mID, d);
     }
 
     public void data(Object o) {
+        mRS.validate();
         mRS.nAllocationSubDataFromObject(mID, mType, 0, o);
     }
 
     public void read(Object o) {
+        mRS.validate();
         mRS.nAllocationSubReadFromObject(mID, mType, 0, o);
     }
 
     public void subData(int offset, Object o) {
+        mRS.validate();
         mRS.nAllocationSubDataFromObject(mID, mType, offset, o);
     }
 
@@ -127,27 +143,33 @@
         }
 
         public void setConstraint(Dimension dim, int value) {
+            mRS.validate();
             mRS.nAdapter1DSetConstraint(mID, dim.mID, value);
         }
 
         public void data(int[] d) {
+            mRS.validate();
             mRS.nAdapter1DData(mID, d);
         }
 
         public void data(float[] d) {
+            mRS.validate();
             mRS.nAdapter1DData(mID, d);
         }
 
         public void subData(int off, int count, int[] d) {
+            mRS.validate();
             mRS.nAdapter1DSubData(mID, off, count, d);
         }
 
         public void subData(int off, int count, float[] d) {
+            mRS.validate();
             mRS.nAdapter1DSubData(mID, off, count, d);
         }
     }
 
     public Adapter1D createAdapter1D() {
+        mRS.validate();
         int id = mRS.nAdapter1DCreate();
         if (id != 0) {
             mRS.nAdapter1DBindAllocation(id, mID);
@@ -163,27 +185,33 @@
         }
 
         public void setConstraint(Dimension dim, int value) {
+            mRS.validate();
             mRS.nAdapter2DSetConstraint(mID, dim.mID, value);
         }
 
         public void data(int[] d) {
+            mRS.validate();
             mRS.nAdapter2DData(mID, d);
         }
 
         public void data(float[] d) {
+            mRS.validate();
             mRS.nAdapter2DData(mID, d);
         }
 
         public void subData(int xoff, int yoff, int w, int h, int[] d) {
+            mRS.validate();
             mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d);
         }
 
         public void subData(int xoff, int yoff, int w, int h, float[] d) {
+            mRS.validate();
             mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d);
         }
     }
 
     public Adapter2D createAdapter2D() {
+        mRS.validate();
         int id = mRS.nAdapter2DCreate();
         if (id != 0) {
             mRS.nAdapter2DBindAllocation(id, mID);
@@ -202,6 +230,7 @@
     static public Allocation createTyped(RenderScript rs, Type type)
         throws IllegalArgumentException {
 
+        rs.validate();
         if(type.mID == 0) {
             throw new IllegalStateException("Bad Type");
         }
@@ -212,6 +241,7 @@
     static public Allocation createSized(RenderScript rs, Element e, int count)
         throws IllegalArgumentException {
 
+        rs.validate();
         Type.Builder b = new Type.Builder(rs, e);
         b.add(Dimension.X, count);
         Type t = b.create();
@@ -226,6 +256,7 @@
     static public Allocation createFromBitmap(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
         throws IllegalArgumentException {
 
+        rs.validate();
         int id = rs.nAllocationCreateFromBitmap(dstFmt.mID, genMips, b);
         return new Allocation(id, rs, null);
     }
@@ -233,6 +264,7 @@
     static public Allocation createFromBitmapBoxed(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
         throws IllegalArgumentException {
 
+        rs.validate();
         int id = rs.nAllocationCreateFromBitmapBoxed(dstFmt.mID, genMips, b);
         return new Allocation(id, rs, null);
     }
@@ -240,6 +272,7 @@
     static public Allocation createFromBitmapResource(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
         throws IllegalArgumentException {
 
+        rs.validate();
         InputStream is = null;
         try {
             final TypedValue value = new TypedValue();
diff --git a/graphics/java/android/renderscript/Element.java b/graphics/java/android/renderscript/Element.java
index 73d8266..ee9b098 100644
--- a/graphics/java/android/renderscript/Element.java
+++ b/graphics/java/android/renderscript/Element.java
@@ -284,6 +284,7 @@
     }
 
     public static Element createFromClass(RenderScript rs, Class c) {
+        rs.validate();
         Field[] fields = c.getFields();
         Builder b = new Builder(rs);
 
@@ -322,6 +323,7 @@
     }
 
     void init() {
+        mRS.validate();
         internalCreate(mRS, this);
     }
 
@@ -483,6 +485,7 @@
         }
 
         public Element create() {
+            mRS.validate();
             Element e = new Element(mRS, mEntryCount);
             java.lang.System.arraycopy(mEntries, 0, e.mEntries, 0, mEntryCount);
             e.init();
diff --git a/graphics/java/android/renderscript/Light.java b/graphics/java/android/renderscript/Light.java
index 115ae03..aab656f 100644
--- a/graphics/java/android/renderscript/Light.java
+++ b/graphics/java/android/renderscript/Light.java
@@ -30,10 +30,12 @@
     }
 
     public void setColor(float r, float g, float b) {
+        mRS.validate();
         mRS.nLightSetColor(mID, r, g, b);
     }
 
     public void setPosition(float x, float y, float z) {
+        mRS.validate();
         mRS.nLightSetPosition(mID, x, y, z);
     }
 
@@ -65,6 +67,7 @@
         }
 
         public Light create() {
+            mRS.validate();
             return internalCreate(mRS, this);
         }
     }
diff --git a/graphics/java/android/renderscript/ProgramFragment.java b/graphics/java/android/renderscript/ProgramFragment.java
index 392d93d..1a72578 100644
--- a/graphics/java/android/renderscript/ProgramFragment.java
+++ b/graphics/java/android/renderscript/ProgramFragment.java
@@ -47,6 +47,7 @@
 
     public void bindTexture(Allocation va, int slot)
         throws IllegalArgumentException {
+        mRS.validate();
         if((slot < 0) || (slot >= MAX_SLOT)) {
             throw new IllegalArgumentException("Slot ID out of range.");
         }
@@ -56,6 +57,7 @@
 
     public void bindSampler(Sampler vs, int slot)
         throws IllegalArgumentException {
+        mRS.validate();
         if((slot < 0) || (slot >= MAX_SLOT)) {
             throw new IllegalArgumentException("Slot ID out of range.");
         }
@@ -149,6 +151,7 @@
         }
 
         public ProgramFragment create() {
+            mRS.validate();
             return internalCreate(mRS, this);
         }
     }
diff --git a/graphics/java/android/renderscript/ProgramRaster.java b/graphics/java/android/renderscript/ProgramRaster.java
index ab327f1..56f9bf4 100644
--- a/graphics/java/android/renderscript/ProgramRaster.java
+++ b/graphics/java/android/renderscript/ProgramRaster.java
@@ -46,11 +46,13 @@
     }
 
     public void setLineWidth(float w) {
+        mRS.validate();
         mLineWidth = w;
         mRS.nProgramRasterSetLineWidth(mID, w);
     }
 
     public void setPointSize(float s) {
+        mRS.validate();
         mPointSize = s;
         mRS.nProgramRasterSetPointSize(mID, s);
     }
@@ -98,6 +100,7 @@
         }
 
         public ProgramRaster create() {
+            mRS.validate();
             return internalCreate(mRS, this);
         }
     }
diff --git a/graphics/java/android/renderscript/ProgramStore.java b/graphics/java/android/renderscript/ProgramStore.java
index 5cbe1b2..69be245 100644
--- a/graphics/java/android/renderscript/ProgramStore.java
+++ b/graphics/java/android/renderscript/ProgramStore.java
@@ -162,6 +162,7 @@
         }
 
         public ProgramStore create() {
+            mRS.validate();
             return internalCreate(mRS, this);
         }
     }
diff --git a/graphics/java/android/renderscript/ProgramVertex.java b/graphics/java/android/renderscript/ProgramVertex.java
index ddb23ac..ba97d5b 100644
--- a/graphics/java/android/renderscript/ProgramVertex.java
+++ b/graphics/java/android/renderscript/ProgramVertex.java
@@ -34,6 +34,7 @@
     }
 
     public void bindAllocation(MatrixAllocation va) {
+        mRS.validate();
         mRS.nProgramVertexBindAllocation(mID, va.mAlloc.mID);
     }
 
@@ -88,6 +89,7 @@
         }
 
         public ProgramVertex create() {
+            mRS.validate();
             return internalCreate(mRS, this);
         }
     }
diff --git a/graphics/java/android/renderscript/RenderScript.java b/graphics/java/android/renderscript/RenderScript.java
index c42f647..0d8b675 100644
--- a/graphics/java/android/renderscript/RenderScript.java
+++ b/graphics/java/android/renderscript/RenderScript.java
@@ -242,6 +242,18 @@
         }
     }
 
+    void validate() {
+        if (mContext == 0) {
+            throw new IllegalStateException("Calling RS with no Context active.");
+        }
+    }
+
+    void validateSurface() {
+        if (mSurface == null) {
+            throw new IllegalStateException("Uploading data to GL with no surface.");
+        }
+    }
+
     public void contextSetPriority(Priority p) {
         nContextSetPriority(p.mID);
     }
diff --git a/graphics/java/android/renderscript/Sampler.java b/graphics/java/android/renderscript/Sampler.java
index 5e0b110..625a576 100644
--- a/graphics/java/android/renderscript/Sampler.java
+++ b/graphics/java/android/renderscript/Sampler.java
@@ -100,6 +100,7 @@
         }
 
         public Sampler create() {
+            mRS.validate();
             return internalCreate(mRS, this);
         }
     }
diff --git a/graphics/java/android/renderscript/Script.java b/graphics/java/android/renderscript/Script.java
index 35791a3..57ccfa3 100644
--- a/graphics/java/android/renderscript/Script.java
+++ b/graphics/java/android/renderscript/Script.java
@@ -48,22 +48,27 @@
     }
 
     public void bindAllocation(Allocation va, int slot) {
+        mRS.validate();
         mRS.nScriptBindAllocation(mID, va.mID, slot);
     }
 
     public void setClearColor(float r, float g, float b, float a) {
+        mRS.validate();
         mRS.nScriptSetClearColor(mID, r, g, b, a);
     }
 
     public void setClearDepth(float d) {
+        mRS.validate();
         mRS.nScriptSetClearDepth(mID, d);
     }
 
     public void setClearStencil(int stencil) {
+        mRS.validate();
         mRS.nScriptSetClearStencil(mID, stencil);
     }
 
     public void setTimeZone(String timeZone) {
+        mRS.validate();
         try {
             mRS.nScriptSetTimeZone(mID, timeZone.getBytes("UTF-8"));
         } catch (java.io.UnsupportedEncodingException e) {
diff --git a/graphics/java/android/renderscript/SimpleMesh.java b/graphics/java/android/renderscript/SimpleMesh.java
index 3d10e3b..f45074e 100644
--- a/graphics/java/android/renderscript/SimpleMesh.java
+++ b/graphics/java/android/renderscript/SimpleMesh.java
@@ -35,18 +35,22 @@
     }
 
     public void bindVertexAllocation(Allocation a, int slot) {
+        mRS.validate();
         mRS.nSimpleMeshBindVertex(mID, a.mID, slot);
     }
 
     public void bindIndexAllocation(Allocation a) {
+        mRS.validate();
         mRS.nSimpleMeshBindIndex(mID, a.mID);
     }
 
     public Allocation createVertexAllocation(int slot) {
+        mRS.validate();
         return Allocation.createTyped(mRS, mVertexTypes[slot]);
     }
 
     public Allocation createIndexAllocation() {
+        mRS.validate();
         return Allocation.createTyped(mRS, mIndexType);
     }
 
@@ -162,6 +166,7 @@
         }
 
         public SimpleMesh create() {
+            mRS.validate();
             SimpleMesh sm = internalCreate(mRS, this);
             sm.mVertexTypes = new Type[mVertexTypeCount];
             for(int ct=0; ct < mVertexTypeCount; ct++) {
diff --git a/libs/audioflinger/AudioFlinger.cpp b/libs/audioflinger/AudioFlinger.cpp
index 9ec8de5..2024cc0 100644
--- a/libs/audioflinger/AudioFlinger.cpp
+++ b/libs/audioflinger/AudioFlinger.cpp
@@ -1111,7 +1111,6 @@
 {
     track->mState = TrackBase::TERMINATED;
     if (mActiveTracks.indexOf(track) < 0) {
-        LOGV("remove track (%d) and delete from mixer", track->name());
         mTracks.remove(track);
         deleteTrackName_l(track->name());
     }
@@ -1511,6 +1510,7 @@
 // deleteTrackName_l() must be called with ThreadBase::mLock held
 void AudioFlinger::MixerThread::deleteTrackName_l(int name)
 {
+    LOGV("remove track (%d) and delete from mixer", name);
     mAudioMixer->deleteTrackName(name);
 }
 
@@ -1922,6 +1922,9 @@
 
 AudioFlinger::DuplicatingThread::~DuplicatingThread()
 {
+    for (size_t i = 0; i < mOutputTracks.size(); i++) {
+        mOutputTracks[i]->destroy();
+    }
     mOutputTracks.clear();
 }
 
@@ -2044,17 +2047,6 @@
         outputTracks.clear();
     }
 
-    { // scope for the mLock
-
-        Mutex::Autolock _l(mLock);
-        if (!mStandby) {
-            LOGV("DuplicatingThread() exiting out of standby");
-            for (size_t i = 0; i < mOutputTracks.size(); i++) {
-                mOutputTracks[i]->destroy();
-            }
-        }
-    }
-
     return false;
 }
 
@@ -3086,23 +3078,34 @@
             }
             if (mActiveTrack != 0) {
                 if (mActiveTrack->mState == TrackBase::PAUSING) {
+                    if (!mStandby) {
+                        mInput->standby();
+                        mStandby = true;
+                    }
                     mActiveTrack.clear();
                     mStartStopCond.broadcast();
                 } else if (mActiveTrack->mState == TrackBase::RESUMING) {
-                    mRsmpInIndex = mFrameCount;
                     if (mReqChannelCount != mActiveTrack->channelCount()) {
                         mActiveTrack.clear();
-                    } else {
-                        mActiveTrack->mState = TrackBase::ACTIVE;
+                        mStartStopCond.broadcast();
+                    } else if (mBytesRead != 0) {
+                        // record start succeeds only if first read from audio input
+                        // succeeds
+                        if (mBytesRead > 0) {
+                            mActiveTrack->mState = TrackBase::ACTIVE;
+                        } else {
+                            mActiveTrack.clear();
+                        }
+                        mStartStopCond.broadcast();
                     }
-                    mStartStopCond.broadcast();
+                    mStandby = false;
                 }
-                mStandby = false;
             }
         }
 
         if (mActiveTrack != 0) {
-            if (mActiveTrack->mState != TrackBase::ACTIVE) {
+            if (mActiveTrack->mState != TrackBase::ACTIVE &&
+                mActiveTrack->mState != TrackBase::RESUMING) {
                 usleep(5000);
                 continue;
             }
@@ -3140,18 +3143,19 @@
                             }
                         }
                         if (framesOut && mFrameCount == mRsmpInIndex) {
-                            ssize_t bytesRead;
                             if (framesOut == mFrameCount &&
                                 (mChannelCount == mReqChannelCount || mFormat != AudioSystem::PCM_16_BIT)) {
-                                bytesRead = mInput->read(buffer.raw, mInputBytes);
+                                mBytesRead = mInput->read(buffer.raw, mInputBytes);
                                 framesOut = 0;
                             } else {
-                                bytesRead = mInput->read(mRsmpInBuffer, mInputBytes);
+                                mBytesRead = mInput->read(mRsmpInBuffer, mInputBytes);
                                 mRsmpInIndex = 0;
                             }
-                            if (bytesRead < 0) {
+                            if (mBytesRead < 0) {
                                 LOGE("Error reading audio input");
-                                sleep(1);
+                                if (mActiveTrack->mState == TrackBase::ACTIVE) {
+                                    sleep(1);
+                                }
                                 mRsmpInIndex = mFrameCount;
                                 framesOut = 0;
                                 buffer.frameCount = 0;
@@ -3220,7 +3224,7 @@
             if (recordTrack != mActiveTrack.get()) {
                 status = -EBUSY;
             } else if (mActiveTrack->mState == TrackBase::PAUSING) {
-                mActiveTrack->mState = TrackBase::RESUMING;
+                mActiveTrack->mState = TrackBase::ACTIVE;
             }
             return status;
         }
@@ -3235,6 +3239,8 @@
             return status;
         }
         mActiveTrack->mState = TrackBase::RESUMING;
+        mRsmpInIndex = mFrameCount;
+        mBytesRead = 0;
         // signal thread to start
         LOGV("Signal record thread");
         mWaitWorkCV.signal();
@@ -3275,6 +3281,7 @@
                 mLock.unlock();
                 AudioSystem::stopInput(mId);
                 mLock.lock();
+                LOGV("Record stopped OK");
             }
         }
     }
@@ -3325,10 +3332,12 @@
     int channelCount;
 
     if (framesReady == 0) {
-        ssize_t bytesRead = mInput->read(mRsmpInBuffer, mInputBytes);
-        if (bytesRead < 0) {
+        mBytesRead = mInput->read(mRsmpInBuffer, mInputBytes);
+        if (mBytesRead < 0) {
             LOGE("RecordThread::getNextBuffer() Error reading audio input");
-            sleep(1);
+            if (mActiveTrack->mState == TrackBase::ACTIVE) {
+                sleep(1);
+            }
             buffer->raw = 0;
             buffer->frameCount = 0;
             return NOT_ENOUGH_DATA;
@@ -3546,9 +3555,11 @@
         if (pFormat) *pFormat = format;
         if (pChannels) *pChannels = channels;
         if (pLatencyMs) *pLatencyMs = thread->latency();
+
+        return mNextThreadId;
     }
 
-    return mNextThreadId;
+    return 0;
 }
 
 int AudioFlinger::openDuplicateOutput(int output1, int output2)
@@ -3694,9 +3705,11 @@
         if (pChannels) *pChannels = reqChannels;
 
         input->standby();
+
+        return mNextThreadId;
     }
 
-    return mNextThreadId;
+    return 0;
 }
 
 status_t AudioFlinger::closeInput(int input)
diff --git a/libs/audioflinger/AudioFlinger.h b/libs/audioflinger/AudioFlinger.h
index 5a17294..8c29da85 100644
--- a/libs/audioflinger/AudioFlinger.h
+++ b/libs/audioflinger/AudioFlinger.h
@@ -744,6 +744,7 @@
                 size_t                              mInputBytes;
                 int                                 mReqChannelCount;
                 uint32_t                            mReqSampleRate;
+                ssize_t                             mBytesRead;
     };
 
     class RecordHandle : public android::BnAudioRecord {
diff --git a/libs/surfaceflinger/Android.mk b/libs/surfaceflinger/Android.mk
index eb51c22..b3fed58 100644
--- a/libs/surfaceflinger/Android.mk
+++ b/libs/surfaceflinger/Android.mk
@@ -22,9 +22,6 @@
 ifeq ($(TARGET_BOARD_PLATFORM), msm7k)
 	LOCAL_CFLAGS += -DDIM_WITH_TEXTURE
 endif
-ifeq ($(TARGET_BOARD_PLATFORM), qsd8k)
-	LOCAL_CFLAGS += -DDIM_WITH_TEXTURE
-endif
 
 # need "-lrt" on Linux simulator to pick up clock_gettime
 ifeq ($(TARGET_SIMULATOR),true)
diff --git a/media/java/android/media/AudioRecord.java b/media/java/android/media/AudioRecord.java
index 7a47157..c48eaad 100644
--- a/media/java/android/media/AudioRecord.java
+++ b/media/java/android/media/AudioRecord.java
@@ -498,8 +498,9 @@
 
         // start recording
         synchronized(mRecordingStateLock) {
-            native_start();
-            mRecordingState = RECORDSTATE_RECORDING;
+            if (native_start() == SUCCESS) {
+                mRecordingState = RECORDSTATE_RECORDING;
+            }
         }
     }
 
@@ -764,7 +765,7 @@
     
     private native final void native_release();
 
-    private native final void native_start();  
+    private native final int native_start();
 
     private native final void native_stop();
 
diff --git a/media/libmedia/AudioRecord.cpp b/media/libmedia/AudioRecord.cpp
index c3828f0..f4165ff 100644
--- a/media/libmedia/AudioRecord.cpp
+++ b/media/libmedia/AudioRecord.cpp
@@ -125,7 +125,7 @@
     audio_io_handle_t input = AudioSystem::getInput(inputSource,
                                     sampleRate, format, channels, (AudioSystem::audio_in_acoustics)flags);
     if (input == 0) {
-        LOGE("Could not get audio output for stream type %d", inputSource);
+        LOGE("Could not get audio input for record source %d", inputSource);
         return BAD_VALUE;
     }
 
@@ -539,7 +539,6 @@
         return BAD_VALUE;
     }
 
-    LOGV("read size: %d", userSize);
 
     do {
 
diff --git a/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/performance/MediaPlayerPerformance.java b/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/performance/MediaPlayerPerformance.java
index 6e3f5e3..4cdc06d 100644
--- a/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/performance/MediaPlayerPerformance.java
+++ b/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/performance/MediaPlayerPerformance.java
@@ -315,16 +315,19 @@
         mEndMemory = getMediaserverVsize();
         Log.v(TAG, "End Memory " + mEndMemory);
         output.write("End Memory :" + mEndMemory + "\n");
-        //Write the total memory different into the output file
-        output.write("The total diff = " + (mEndMemory - startMemory));
+        int memDiff = mEndMemory - startMemory;
+        if (memDiff < 0)
+            memDiff = 0;
+        else
+            output.write("The total diff = " + memDiff);
         output.write("\n\n");
-        //mediaserver crash
-        if (startPid != mEndPid){
+        // mediaserver crash
+        if (startPid != mEndPid) {
             output.write("mediaserver died. Test failed\n");
             return false;
         }
         //memory leak greter than the tolerant
-        if ((mEndMemory - startMemory) > MAX_ACCEPTED_MEMORY_LEAK_KB )
+        if (memDiff > MAX_ACCEPTED_MEMORY_LEAK_KB )
             return false;
         return true;
     }
@@ -345,13 +348,11 @@
         File h263MemoryOut = new File(MEDIA_MEMORY_OUTPUT);
         Writer output = new BufferedWriter(new FileWriter(h263MemoryOut, true));
         output.write("H263 Video Playback Only\n");
+        mStartMemory = getMediaserverVsize();
+        output.write("Start memory : " + mStartMemory + "\n");
+        Log.v(TAG, "first mem : " + mStartMemory);
         for (int i = 0; i < NUM_STRESS_LOOP; i++) {
             mediaStressPlayback(MediaNames.VIDEO_HIGHRES_H263);
-            if (i == 0) {
-                mStartMemory = getMediaserverVsize();
-                output.write("Start memory : " + mStartMemory + "\n");
-                Log.v(TAG, "first mem : " + mStartMemory);
-            }
             getMemoryWriteToLog(output);
         }
         output.write("\n");
@@ -369,12 +370,11 @@
         File h264MemoryOut = new File(MEDIA_MEMORY_OUTPUT);
         Writer output = new BufferedWriter(new FileWriter(h264MemoryOut, true));
         output.write("H264 Video Playback only\n");
+        mStartMemory = getMediaserverVsize();
+        output.write("Start memory : " + mStartMemory + "\n");
+        Log.v(TAG, "first mem : " + mStartMemory);
         for (int i = 0; i < NUM_STRESS_LOOP; i++) {
             mediaStressPlayback(MediaNames.VIDEO_H264_AMR);
-            if (i == 0) {
-              mStartMemory = getMediaserverVsize();
-              output.write("Start memory : " + mStartMemory + "\n");
-            }
             getMemoryWriteToLog(output);
         }
         output.write("\n");
@@ -392,12 +392,11 @@
             File wmvMemoryOut = new File(MEDIA_MEMORY_OUTPUT);
             Writer output = new BufferedWriter(new FileWriter(wmvMemoryOut, true));
             output.write("WMV video playback only\n");
+            mStartMemory = getMediaserverVsize();
+            output.write("Start memory : " + mStartMemory + "\n");
+            Log.v(TAG, "first mem : " + mStartMemory);
             for (int i = 0; i < NUM_STRESS_LOOP; i++) {
                 mediaStressPlayback(MediaNames.VIDEO_WMV);
-                if (i == 0) {
-                    mStartMemory = getMediaserverVsize();
-                    output.write("Start memory : " + mStartMemory + "\n");
-                }
                 getMemoryWriteToLog(output);
             }
             output.write("\n");
@@ -416,13 +415,13 @@
         File videoH263RecordOnlyMemoryOut = new File(MEDIA_MEMORY_OUTPUT);
         Writer output = new BufferedWriter(new FileWriter(videoH263RecordOnlyMemoryOut, true));
         output.write("H263 video record only\n");
+        mStartMemory = getMediaserverVsize();
+        output.write("Start memory : " + mStartMemory + "\n");
+        Log.v(TAG, "first mem : " + mStartMemory);
+
         for (int i = 0; i < NUM_STRESS_LOOP; i++) {
             stressVideoRecord(20, 352, 288, MediaRecorder.VideoEncoder.H263,
                     MediaRecorder.OutputFormat.MPEG_4, MediaNames.RECORDED_VIDEO_3GP, true);
-            if (i == 0) {
-              mStartMemory = getMediaserverVsize();
-              output.write("Start memory : " + mStartMemory + "\n");
-            }
             getMemoryWriteToLog(output);
         }
         output.write("\n");
@@ -440,13 +439,13 @@
         File videoMp4RecordOnlyMemoryOut = new File(MEDIA_MEMORY_OUTPUT);
         Writer output = new BufferedWriter(new FileWriter(videoMp4RecordOnlyMemoryOut, true));
         output.write("MPEG4 video record only\n");
+        mStartMemory = getMediaserverVsize();
+        output.write("Start memory : " + mStartMemory + "\n");
+        Log.v(TAG, "first mem : " + mStartMemory);
+
         for (int i = 0; i < NUM_STRESS_LOOP; i++) {
             stressVideoRecord(20, 352, 288, MediaRecorder.VideoEncoder.MPEG_4_SP,
                     MediaRecorder.OutputFormat.MPEG_4, MediaNames.RECORDED_VIDEO_3GP, true);
-            if (i == 0) {
-              mStartMemory = getMediaserverVsize();
-              output.write("Start memory : " + mStartMemory + "\n");
-            }
             getMemoryWriteToLog(output);
         }
         output.write("\n");
@@ -465,13 +464,13 @@
         File videoRecordAudioMemoryOut = new File(MEDIA_MEMORY_OUTPUT);
         Writer output = new BufferedWriter(new FileWriter(videoRecordAudioMemoryOut, true));
         output.write("Audio and h263 video record\n");
+        mStartMemory = getMediaserverVsize();
+        output.write("Start memory : " + mStartMemory + "\n");
+        Log.v(TAG, "first mem : " + mStartMemory);
+
         for (int i = 0; i < NUM_STRESS_LOOP; i++) {
             stressVideoRecord(20, 352, 288, MediaRecorder.VideoEncoder.H263,
                     MediaRecorder.OutputFormat.MPEG_4, MediaNames.RECORDED_VIDEO_3GP, false);
-            if (i == 0) {
-              mStartMemory = getMediaserverVsize();
-              output.write("Start memory : " + mStartMemory + "\n");
-            }
             getMemoryWriteToLog(output);
         }
         output.write("\n");
@@ -489,12 +488,12 @@
         File audioOnlyMemoryOut = new File(MEDIA_MEMORY_OUTPUT);
         Writer output = new BufferedWriter(new FileWriter(audioOnlyMemoryOut, true));
         output.write("Audio record only\n");
+        mStartMemory = getMediaserverVsize();
+        output.write("Start memory : " + mStartMemory + "\n");
+        Log.v(TAG, "first mem : " + mStartMemory);
+
         for (int i = 0; i < NUM_STRESS_LOOP; i++) {
             stressAudioRecord(MediaNames.RECORDER_OUTPUT);
-            if (i == 0) {
-              mStartMemory = getMediaserverVsize();
-              output.write("Start memory : " + mStartMemory + "\n");
-            }
             getMemoryWriteToLog(output);
         }
         output.write("\n");
diff --git a/services/java/com/android/server/ConnectivityService.java b/services/java/com/android/server/ConnectivityService.java
index 78215b0..9b41f83 100644
--- a/services/java/com/android/server/ConnectivityService.java
+++ b/services/java/com/android/server/ConnectivityService.java
@@ -103,7 +103,7 @@
     private boolean mSystemReady;
     private ArrayList<Intent> mDeferredBroadcasts;
 
-    private class NetworkAttributes {
+    private static class NetworkAttributes {
         /**
          * Class for holding settings read from resources.
          */
@@ -111,6 +111,7 @@
         public int mType;
         public int mRadio;
         public int mPriority;
+        public NetworkInfo.State mLastState;
         public NetworkAttributes(String init) {
             String fragments[] = init.split(",");
             mName = fragments[0].toLowerCase();
@@ -131,6 +132,7 @@
                 mType = ConnectivityManager.TYPE_MOBILE_HIPRI;
             }
             mPriority = Integer.parseInt(fragments[2]);
+            mLastState = NetworkInfo.State.UNKNOWN;
         }
         public boolean isDefault() {
             return (mType == mRadio);
@@ -138,7 +140,7 @@
     }
     NetworkAttributes[] mNetAttributes;
 
-    private class RadioAttributes {
+    private static class RadioAttributes {
         public String mName;
         public int mPriority;
         public int mSimultaneity;
@@ -1214,9 +1216,22 @@
             switch (msg.what) {
                 case NetworkStateTracker.EVENT_STATE_CHANGED:
                     info = (NetworkInfo) msg.obj;
+                    int type = info.getType();
+                    NetworkInfo.State state = info.getState();
+                    if(mNetAttributes[type].mLastState == state) {
+                        if (DBG) {
+                            // TODO - remove this after we validate the dropping doesn't break anything
+                            Log.d(TAG, "Dropping ConnectivityChange for " +
+                                    info.getTypeName() +": " +
+                                    state + "/" + info.getDetailedState());
+                        }
+                        return;
+                    }
+                    mNetAttributes[type].mLastState = state;
+
                     if (DBG) Log.d(TAG, "ConnectivityChange for " +
                             info.getTypeName() + ": " +
-                            info.getState() + "/" + info.getDetailedState());
+                            state + "/" + info.getDetailedState());
 
                     // Connectivity state changed:
                     // [31-13] Reserved for future use
@@ -1234,10 +1249,9 @@
                     if (info.getDetailedState() ==
                             NetworkInfo.DetailedState.FAILED) {
                         handleConnectionFailure(info);
-                    } else if (info.getState() ==
-                            NetworkInfo.State.DISCONNECTED) {
+                    } else if (state == NetworkInfo.State.DISCONNECTED) {
                         handleDisconnect(info);
-                    } else if (info.getState() == NetworkInfo.State.SUSPENDED) {
+                    } else if (state == NetworkInfo.State.SUSPENDED) {
                         // TODO: need to think this over.
                         // the logic here is, handle SUSPENDED the same as
                         // DISCONNECTED. The only difference being we are
@@ -1246,7 +1260,7 @@
                         // opportunity to handle DISCONNECTED and SUSPENDED
                         // differently, or not.
                         handleDisconnect(info);
-                    } else if (info.getState() == NetworkInfo.State.CONNECTED) {
+                    } else if (state == NetworkInfo.State.CONNECTED) {
                         handleConnect(info);
                     }
                     break;