Merge change 25012 into eclair

* changes:
  PowerManagerService: Use 5 cm threshold for the proximity sensor.
diff --git a/camera/libcameraservice/CameraHardwareStub.cpp b/camera/libcameraservice/CameraHardwareStub.cpp
index 24496bb..35f4846 100644
--- a/camera/libcameraservice/CameraHardwareStub.cpp
+++ b/camera/libcameraservice/CameraHardwareStub.cpp
@@ -265,6 +265,11 @@
     return NO_ERROR;
 }
 
+status_t CameraHardwareStub::cancelAutoFocus()
+{
+    return NO_ERROR;
+}
+
 /*static*/ int CameraHardwareStub::beginPictureThread(void *cookie)
 {
     CameraHardwareStub *c = (CameraHardwareStub *)cookie;
diff --git a/camera/libcameraservice/CameraHardwareStub.h b/camera/libcameraservice/CameraHardwareStub.h
index 000906a..f957fa8 100644
--- a/camera/libcameraservice/CameraHardwareStub.h
+++ b/camera/libcameraservice/CameraHardwareStub.h
@@ -51,6 +51,7 @@
     virtual void        releaseRecordingFrame(const sp<IMemory>& mem);
 
     virtual status_t    autoFocus();
+    virtual status_t    cancelAutoFocus();
     virtual status_t    takePicture();
     virtual status_t    cancelPicture();
     virtual status_t    dump(int fd, const Vector<String16>& args) const;
diff --git a/camera/libcameraservice/CameraService.cpp b/camera/libcameraservice/CameraService.cpp
index f425f6b..bab7d08 100644
--- a/camera/libcameraservice/CameraService.cpp
+++ b/camera/libcameraservice/CameraService.cpp
@@ -798,7 +798,6 @@
 }
 #endif
 
-// take a picture - image is returned in callback
 status_t CameraService::Client::autoFocus()
 {
     LOGD("autoFocus (pid %d)", getCallingPid());
@@ -815,6 +814,22 @@
     return mHardware->autoFocus();
 }
 
+status_t CameraService::Client::cancelAutoFocus()
+{
+    LOGD("cancelAutoFocus (pid %d)", getCallingPid());
+
+    Mutex::Autolock lock(mLock);
+    status_t result = checkPid();
+    if (result != NO_ERROR) return result;
+
+    if (mHardware == 0) {
+        LOGE("mHardware is NULL, returning.");
+        return INVALID_OPERATION;
+    }
+
+    return mHardware->cancelAutoFocus();
+}
+
 // take a picture - image is returned in callback
 status_t CameraService::Client::takePicture()
 {
diff --git a/camera/libcameraservice/CameraService.h b/camera/libcameraservice/CameraService.h
index f8c7216..0a909cf 100644
--- a/camera/libcameraservice/CameraService.h
+++ b/camera/libcameraservice/CameraService.h
@@ -110,6 +110,9 @@
         // auto focus
         virtual status_t        autoFocus();
 
+        // cancel auto focus
+        virtual status_t        cancelAutoFocus();
+
         // take a picture - returns an IMemory (ref-counted mmap)
         virtual status_t        takePicture();
 
diff --git a/core/java/android/app/Activity.java b/core/java/android/app/Activity.java
index be243a5..545db17 100644
--- a/core/java/android/app/Activity.java
+++ b/core/java/android/app/Activity.java
@@ -34,6 +34,7 @@
 import android.graphics.drawable.Drawable;
 import android.media.AudioManager;
 import android.net.Uri;
+import android.os.Build;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.IBinder;
@@ -1752,8 +1753,16 @@
      * 
      * <p>If the focused view didn't want this event, this method is called.
      *
-     * <p>The default implementation sets up state to call
-     * {@link #onKeyLongPress}, and does other default key handling
+     * <p>The default implementation takes care of {@link KeyEvent#KEYCODE_BACK}
+     * by calling {@link #onBackPressed()}, though the behavior varies based
+     * on the application compatibility mode: for
+     * {@link android.os.Build.VERSION_CODES#ECLAIR} or later applications,
+     * it will set up the dispatch to call {@link #onKeyUp} where the action
+     * will be performed; for earlier applications, it will perform the
+     * action immediately in on-down, as those versions of the platform
+     * behaved.
+     * 
+     * <p>Other additional default key handling may be performed
      * if configured with {@link #setDefaultKeyMode}.
      * 
      * @return Return <code>true</code> to prevent this event from being propagated
@@ -1764,7 +1773,12 @@
      */
     public boolean onKeyDown(int keyCode, KeyEvent event)  {
         if (keyCode == KeyEvent.KEYCODE_BACK) {
-            event.startTracking();
+            if (getApplicationInfo().targetSdkVersion
+                    >= Build.VERSION_CODES.ECLAIR) {
+                event.startTracking();
+            } else {
+                onBackPressed();
+            }
             return true;
         }
         
@@ -1841,10 +1855,13 @@
      * @see KeyEvent
      */
     public boolean onKeyUp(int keyCode, KeyEvent event) {
-        if (keyCode == KeyEvent.KEYCODE_BACK && event.isTracking()
-                && !event.isCanceled()) {
-            onBackPressed();
-            return true;
+        if (getApplicationInfo().targetSdkVersion
+                >= Build.VERSION_CODES.ECLAIR) {
+            if (keyCode == KeyEvent.KEYCODE_BACK && event.isTracking()
+                    && !event.isCanceled()) {
+                onBackPressed();
+                return true;
+            }
         }
         return false;
     }
@@ -2016,11 +2033,14 @@
      */
     public boolean dispatchKeyEvent(KeyEvent event) {
         onUserInteraction();
-        if (getWindow().superDispatchKeyEvent(event)) {
+        Window win = getWindow();
+        if (win.superDispatchKeyEvent(event)) {
             return true;
         }
-        return event.dispatch(this, mDecor != null
-                ? mDecor.getKeyDispatcherState() : null, this);
+        View decor = mDecor;
+        if (decor == null) decor = win.getDecorView();
+        return event.dispatch(this, decor != null
+                ? decor.getKeyDispatcherState() : null, this);
     }
 
     /**
diff --git a/core/java/android/app/SearchDialog.java b/core/java/android/app/SearchDialog.java
index 62dc9b2..75a90c4 100644
--- a/core/java/android/app/SearchDialog.java
+++ b/core/java/android/app/SearchDialog.java
@@ -51,7 +51,6 @@
 import android.view.ContextThemeWrapper;
 import android.view.Gravity;
 import android.view.KeyEvent;
-import android.view.Menu;
 import android.view.MotionEvent;
 import android.view.View;
 import android.view.ViewConfiguration;
@@ -1684,7 +1683,6 @@
     public static class SearchAutoComplete extends AutoCompleteTextView {
 
         private int mThreshold;
-        private int mLastKeyDown;
         private SearchDialog mSearchDialog;
         
         public SearchAutoComplete(Context context) {
@@ -1765,26 +1763,26 @@
          */
         @Override
         public boolean onKeyPreIme(int keyCode, KeyEvent event) {
-            mLastKeyDown = keyCode;
             if (mSearchDialog.mSearchable == null) {
                 return false;
             }
             if (keyCode == KeyEvent.KEYCODE_BACK) {
                 if (event.getAction() == KeyEvent.ACTION_DOWN
                         && event.getRepeatCount() == 0) {
-                    // We releae the back key, might we want to do
+                    // We release the back key, might we want to do
                     // something before the IME?
                     if (mSearchDialog.backToPreviousComponent(false)) {
+                        getKeyDispatcherState().startTracking(event, this);
                         return true;
                     }
                     if (isInputMethodNotNeeded() ||
                             (isEmpty() && getDropDownChildCount() >= getAdapterCount())) {
+                        getKeyDispatcherState().startTracking(event, this);
                         return true;
                     }
-                    mLastKeyDown = 0;
                     return false; // will dismiss soft keyboard if necessary
                 } else if (event.getAction() == KeyEvent.ACTION_UP
-                        && mLastKeyDown == keyCode && !event.isCanceled()) {
+                        && event.isTracking() && !event.isCanceled()) {
                     if (mSearchDialog.backToPreviousComponent(true)) {
                         return true;
                     }
@@ -1815,8 +1813,10 @@
     
     protected boolean handleBackKey(int keyCode, KeyEvent event) {
         if (keyCode == KeyEvent.KEYCODE_BACK) {
-            if (event.getAction() == KeyEvent.ACTION_DOWN) {
+            if (event.getAction() == KeyEvent.ACTION_DOWN
+                    && event.getRepeatCount() == 0) {
                 // Consume the event, to get an up at which point we execute.
+                event.startTracking();
                 return true;
             }
             if (event.getAction() == KeyEvent.ACTION_UP && event.isTracking()
diff --git a/core/java/android/hardware/Camera.java b/core/java/android/hardware/Camera.java
index 04daa1c..9991600 100644
--- a/core/java/android/hardware/Camera.java
+++ b/core/java/android/hardware/Camera.java
@@ -383,6 +383,20 @@
     private native final void native_autoFocus();
 
     /**
+     * Cancels auto-focus function. If the auto-focus is still in progress,
+     * this function will cancel it. Whether the auto-focus is in progress
+     * or not, this function will return the focus position to the default.
+     * If the camera does not support auto-focus, this is a no-op.
+     * @hide
+     */
+    public final void cancelAutoFocus()
+    {
+        mAutoFocusCallback = null;
+        native_cancelAutoFocus();
+    }
+    private native final void native_cancelAutoFocus();
+
+    /**
      * An interface which contains a callback for the shutter closing after taking a picture.
      */
     public interface ShutterCallback
@@ -1338,5 +1352,3 @@
         }
     };
 }
-
-
diff --git a/core/java/android/os/Build.java b/core/java/android/os/Build.java
index 6c2a27a..b4778fe 100644
--- a/core/java/android/os/Build.java
+++ b/core/java/android/os/Build.java
@@ -143,6 +143,9 @@
          * Service.onStartCommand} function will return the new
          * {@link android.app.Service#START_STICKY} behavior instead of the
          * old compatibility {@link android.app.Service#START_STICKY_COMPATIBILITY}.
+         * <li> The {@link android.app.Activity} class will now execute back
+         * key presses on the key up instead of key down, to be able to detect
+         * canceled presses from virtual keys.
          * </ul>
          */
         public static final int ECLAIR = CUR_DEVELOPMENT;
diff --git a/core/java/android/view/KeyEvent.java b/core/java/android/view/KeyEvent.java
index daa4b29..d4f9787 100644
--- a/core/java/android/view/KeyEvent.java
+++ b/core/java/android/view/KeyEvent.java
@@ -18,6 +18,7 @@
 
 import android.os.Parcel;
 import android.os.Parcelable;
+import android.util.Log;
 import android.util.SparseIntArray;
 import android.view.KeyCharacterMap;
 import android.view.KeyCharacterMap.KeyData;
@@ -319,6 +320,9 @@
         return KeyCharacterMap.getDeadChar(accent, c);
     }
     
+    static final boolean DEBUG = false;
+    static final String TAG = "KeyEvent";
+    
     private int mMetaState;
     private int mAction;
     private int mKeyCode;
@@ -1028,13 +1032,17 @@
         switch (mAction) {
             case ACTION_DOWN: {
                 mFlags &= ~FLAG_START_TRACKING;
+                if (DEBUG) Log.v(TAG, "Key down to " + target + " in " + state
+                        + ": " + this);
                 boolean res = receiver.onKeyDown(mKeyCode, this);
                 if (state != null) {
                     if (res && mRepeatCount == 0 && (mFlags&FLAG_START_TRACKING) != 0) {
+                        if (DEBUG) Log.v(TAG, "  Start tracking!");
                         state.startTracking(this, target);
                     } else if (isLongPress() && state.isTracking(this)) {
                         try {
                             if (receiver.onKeyLongPress(mKeyCode, this)) {
+                                if (DEBUG) Log.v(TAG, "  Clear from long press!");
                                 state.performedLongPress(this);
                                 res = true;
                             }
@@ -1045,6 +1053,8 @@
                 return res;
             }
             case ACTION_UP:
+                if (DEBUG) Log.v(TAG, "Key up to " + target + " in " + state
+                        + ": " + this);
                 if (state != null) {
                     state.handleUpEvent(this);
                 }
@@ -1085,6 +1095,7 @@
          * Reset back to initial state.
          */
         public void reset() {
+            if (DEBUG) Log.v(TAG, "Reset: " + this);
             mDownKeyCode = 0;
             mDownTarget = null;
             mActiveLongPresses.clear();
@@ -1095,6 +1106,7 @@
          */
         public void reset(Object target) {
             if (mDownTarget == target) {
+                if (DEBUG) Log.v(TAG, "Reset in " + target + ": " + this);
                 mDownKeyCode = 0;
                 mDownTarget = null;
             }
@@ -1115,6 +1127,7 @@
                 throw new IllegalArgumentException(
                         "Can only start tracking on a down event");
             }
+            if (DEBUG) Log.v(TAG, "Start trackingt in " + target + ": " + this);
             mDownKeyCode = event.getKeyCode();
             mDownTarget = target;
         }
@@ -1145,12 +1158,15 @@
          */
         public void handleUpEvent(KeyEvent event) {
             final int keyCode = event.getKeyCode();
+            if (DEBUG) Log.v(TAG, "Handle key up " + event + ": " + this);
             int index = mActiveLongPresses.indexOfKey(keyCode);
             if (index >= 0) {
+                if (DEBUG) Log.v(TAG, "  Index: " + index);
                 event.mFlags |= FLAG_CANCELED | FLAG_CANCELED_LONG_PRESS;
                 mActiveLongPresses.removeAt(index);
             }
             if (mDownKeyCode == keyCode) {
+                if (DEBUG) Log.v(TAG, "  Tracking!");
                 event.mFlags |= FLAG_TRACKING;
                 mDownKeyCode = 0;
                 mDownTarget = null;
diff --git a/core/java/android/widget/AbsListView.java b/core/java/android/widget/AbsListView.java
index bed2a7a..b242b58 100644
--- a/core/java/android/widget/AbsListView.java
+++ b/core/java/android/widget/AbsListView.java
@@ -2946,7 +2946,9 @@
             break;
         case KeyEvent.KEYCODE_BACK:
             if (mFiltered && mPopup != null && mPopup.isShowing()) {
-                if (event.getAction() == KeyEvent.ACTION_DOWN) {
+                if (event.getAction() == KeyEvent.ACTION_DOWN
+                        && event.getRepeatCount() == 0) {
+                    getKeyDispatcherState().startTracking(event, this);
                     handled = true;
                 } else if (event.getAction() == KeyEvent.ACTION_UP
                         && event.isTracking() && !event.isCanceled()) {
diff --git a/core/java/android/widget/AutoCompleteTextView.java b/core/java/android/widget/AutoCompleteTextView.java
index 7891d3c..4566c4c 100644
--- a/core/java/android/widget/AutoCompleteTextView.java
+++ b/core/java/android/widget/AutoCompleteTextView.java
@@ -132,8 +132,6 @@
 
     private AutoCompleteTextView.PassThroughClickListener mPassThroughClickListener;
 
-    private int mDownKeyCode;
-    
     public AutoCompleteTextView(Context context) {
         this(context, null);
     }
@@ -605,19 +603,18 @@
 
     @Override
     public boolean onKeyPreIme(int keyCode, KeyEvent event) {
-        if (event.getAction() == KeyEvent.ACTION_DOWN
-                && event.getRepeatCount() == 0) {
-            mDownKeyCode = keyCode;
-        }
-        if (isPopupShowing()) {
+        if (keyCode == KeyEvent.KEYCODE_BACK && isPopupShowing()
+                && !mDropDownAlwaysVisible) {
             // special case for the back key, we do not even try to send it
             // to the drop down list but instead, consume it immediately
-            if (keyCode == KeyEvent.KEYCODE_BACK && !mDropDownAlwaysVisible) {
-                if (event.getAction() == KeyEvent.ACTION_UP
-                        && mDownKeyCode == keyCode && !event.isCanceled()) {
-                    dismissDropDown();
-                    return true;
-                }
+            if (event.getAction() == KeyEvent.ACTION_DOWN
+                    && event.getRepeatCount() == 0) {
+                getKeyDispatcherState().startTracking(event, this);
+                return true;
+            } else if (event.getAction() == KeyEvent.ACTION_UP
+                    && event.isTracking() && !event.isCanceled()) {
+                dismissDropDown();
+                return true;
             }
         }
         return super.onKeyPreIme(keyCode, event);
@@ -1026,7 +1023,6 @@
     @Override
     public void onWindowFocusChanged(boolean hasWindowFocus) {
         super.onWindowFocusChanged(hasWindowFocus);
-        mDownKeyCode = 0;
         performValidation();
         if (!hasWindowFocus && !mDropDownAlwaysVisible) {
             dismissDropDown();
@@ -1036,7 +1032,6 @@
     @Override
     protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
         super.onFocusChanged(focused, direction, previouslyFocusedRect);
-        mDownKeyCode = 0;
         performValidation();
         if (!focused && !mDropDownAlwaysVisible) {
             dismissDropDown();
diff --git a/core/java/android/widget/PopupWindow.java b/core/java/android/widget/PopupWindow.java
index 548dee9..d86b674 100644
--- a/core/java/android/widget/PopupWindow.java
+++ b/core/java/android/widget/PopupWindow.java
@@ -1323,8 +1323,16 @@
         @Override
         public boolean dispatchKeyEvent(KeyEvent event) {
             if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
-                dismiss();
-                return true;
+                if (event.getAction() == KeyEvent.ACTION_DOWN
+                        && event.getRepeatCount() == 0) {
+                    getKeyDispatcherState().startTracking(event, this);
+                    return true;
+                } else if (event.getAction() == KeyEvent.ACTION_UP
+                        && event.isTracking() && !event.isCanceled()) {
+                    dismiss();
+                    return true;
+                }
+                return super.dispatchKeyEvent(event);
             } else {
                 return super.dispatchKeyEvent(event);
             }
diff --git a/core/java/android/widget/ZoomButtonsController.java b/core/java/android/widget/ZoomButtonsController.java
index a41e2e3..760b8ff 100644
--- a/core/java/android/widget/ZoomButtonsController.java
+++ b/core/java/android/widget/ZoomButtonsController.java
@@ -476,7 +476,21 @@
         if (isInterestingKey(keyCode)) {
 
             if (keyCode == KeyEvent.KEYCODE_BACK) {
-                setVisible(false);
+                if (event.getAction() == KeyEvent.ACTION_DOWN
+                        && event.getRepeatCount() == 0) {
+                    if (mOwnerView != null) {
+                        KeyEvent.DispatcherState ds = mOwnerView.getKeyDispatcherState();
+                        if (ds != null) {
+                            ds.startTracking(event, this);
+                        }
+                    }
+                    return true;
+                } else if (event.getAction() == KeyEvent.ACTION_UP
+                        && event.isTracking() && !event.isCanceled()) {
+                    setVisible(false);
+                    return true;
+                }
+                
             } else {
                 dismissControlsDelayed(ZOOM_CONTROLS_TIMEOUT);
             }
diff --git a/core/java/com/android/internal/view/menu/MenuDialogHelper.java b/core/java/com/android/internal/view/menu/MenuDialogHelper.java
index 88f7b2f..70f040a 100644
--- a/core/java/com/android/internal/view/menu/MenuDialogHelper.java
+++ b/core/java/com/android/internal/view/menu/MenuDialogHelper.java
@@ -22,6 +22,7 @@
 import android.os.IBinder;
 import android.view.KeyEvent;
 import android.view.View;
+import android.view.Window;
 import android.view.WindowManager;
 import android.widget.ListAdapter;
 
@@ -86,18 +87,26 @@
     }
     
     public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
-        /*
-         * Close menu on key down (more responsive, and there's no way to cancel
-         * a key press so no point having it on key up. Note: This is also
-         * needed because when a top-level menu item that shows a submenu is
-         * invoked by chording, this onKey method will be called with the menu
-         * up event.
-         */
-        if (event.getAction() == KeyEvent.ACTION_DOWN && (keyCode == KeyEvent.KEYCODE_MENU)
-                || (keyCode == KeyEvent.KEYCODE_BACK)) {
-            mMenu.close(true);
-            dialog.dismiss();
-            return true;
+        if (keyCode == KeyEvent.KEYCODE_MENU || keyCode == KeyEvent.KEYCODE_BACK) {
+            if (event.getAction() == KeyEvent.ACTION_DOWN
+                    && event.getRepeatCount() == 0) {
+                Window win = mDialog.getWindow();
+                if (win != null) {
+                    View decor = win.getDecorView();
+                    if (decor != null) {
+                        KeyEvent.DispatcherState ds = decor.getKeyDispatcherState();
+                        if (ds != null) {
+                            ds.startTracking(event, this);
+                            return true;
+                        }
+                    }
+                }
+            } else if (event.getAction() == KeyEvent.ACTION_UP
+                    && event.isTracking() && !event.isCanceled()) {
+                mMenu.close(true);
+                dialog.dismiss();
+                return true;
+            }
         }
 
         // Menu shortcut matching
diff --git a/core/jni/android_hardware_Camera.cpp b/core/jni/android_hardware_Camera.cpp
index 8a312d9..ce2b10c 100644
--- a/core/jni/android_hardware_Camera.cpp
+++ b/core/jni/android_hardware_Camera.cpp
@@ -327,6 +327,18 @@
     }
 }
 
+static void android_hardware_Camera_cancelAutoFocus(JNIEnv *env, jobject thiz)
+{
+    LOGV("cancelAutoFocus");
+    JNICameraContext* context;
+    sp<Camera> c = get_native_camera(env, thiz, &context);
+    if (c == 0) return;
+
+    if (c->cancelAutoFocus() != NO_ERROR) {
+        jniThrowException(env, "java/lang/RuntimeException", "cancelAutoFocus failed");
+    }
+}
+
 static void android_hardware_Camera_takePicture(JNIEnv *env, jobject thiz)
 {
     LOGV("takePicture");
@@ -422,6 +434,9 @@
   { "native_autoFocus",
     "()V",
     (void *)android_hardware_Camera_autoFocus },
+  { "native_cancelAutoFocus",
+    "()V",
+    (void *)android_hardware_Camera_cancelAutoFocus },
   { "native_takePicture",
     "()V",
     (void *)android_hardware_Camera_takePicture },
diff --git a/include/ui/Camera.h b/include/ui/Camera.h
index ae6e255..9ceb8fd 100644
--- a/include/ui/Camera.h
+++ b/include/ui/Camera.h
@@ -143,6 +143,9 @@
             // autoFocus - status returned from callback
             status_t    autoFocus();
 
+            // cancel auto focus
+            status_t    cancelAutoFocus();
+
             // take a picture - picture returned from callback
             status_t    takePicture();
 
diff --git a/include/ui/CameraHardwareInterface.h b/include/ui/CameraHardwareInterface.h
index 535f70e..5fbb7d8 100644
--- a/include/ui/CameraHardwareInterface.h
+++ b/include/ui/CameraHardwareInterface.h
@@ -161,6 +161,14 @@
     virtual status_t    autoFocus() = 0;
 
     /**
+     * Cancels auto-focus function. If the auto-focus is still in progress,
+     * this function will cancel it. Whether the auto-focus is in progress
+     * or not, this function will return the focus position to the default.
+     * If the camera does not support auto-focus, this is a no-op.
+     */
+    virtual status_t    cancelAutoFocus() = 0;
+
+    /**
      * Take a picture.
      */
     virtual status_t    takePicture() = 0;
diff --git a/include/ui/ICamera.h b/include/ui/ICamera.h
index 1df7914..7595e36 100644
--- a/include/ui/ICamera.h
+++ b/include/ui/ICamera.h
@@ -76,6 +76,9 @@
     // auto focus
     virtual status_t        autoFocus() = 0;
 
+    // cancel auto focus
+    virtual status_t        cancelAutoFocus() = 0;
+
     // take a picture
     virtual status_t        takePicture() = 0;
 
diff --git a/libs/audioflinger/AudioFlinger.cpp b/libs/audioflinger/AudioFlinger.cpp
index 790a655..d9be007 100644
--- a/libs/audioflinger/AudioFlinger.cpp
+++ b/libs/audioflinger/AudioFlinger.cpp
@@ -1239,7 +1239,7 @@
                 // active tracks were late. Sleep a little bit to give
                 // them another chance. If we're too late, write 0s to audio
                 // hardware to avoid underrun.
-                if (sleepTime < kMaxBufferRecoveryInUsecs) {
+                if (mBytesWritten == 0 || sleepTime < kMaxBufferRecoveryInUsecs) {
                     usleep(kBufferRecoveryInUsecs);
                 } else {
                     memset (curBuf, 0, mixBufferSize);
@@ -1741,7 +1741,8 @@
                 standbyTime = systemTime() + kStandbyTimeInNsecs;
             } else {
                 sleepTime += kBufferRecoveryInUsecs;
-                if (sleepTime < kMaxBufferRecoveryInUsecs) {
+                if (mBytesWritten == 0 || !AudioSystem::isLinearPCM(mFormat) ||
+                    sleepTime < kMaxBufferRecoveryInUsecs) {
                     usleep(kBufferRecoveryInUsecs);
                 } else {
                     memset (mMixBuffer, 0, mFrameCount * mFrameSize);
diff --git a/libs/ui/Camera.cpp b/libs/ui/Camera.cpp
index 12a7725..0c6d340 100644
--- a/libs/ui/Camera.cpp
+++ b/libs/ui/Camera.cpp
@@ -242,6 +242,14 @@
     return c->autoFocus();
 }
 
+status_t Camera::cancelAutoFocus()
+{
+    LOGV("cancelAutoFocus");
+    sp <ICamera> c = mCamera;
+    if (c == 0) return NO_INIT;
+    return c->cancelAutoFocus();
+}
+
 // take a picture
 status_t Camera::takePicture()
 {
diff --git a/libs/ui/ICamera.cpp b/libs/ui/ICamera.cpp
index 805c2ca..fd7e084 100644
--- a/libs/ui/ICamera.cpp
+++ b/libs/ui/ICamera.cpp
@@ -32,6 +32,7 @@
     START_PREVIEW,
     STOP_PREVIEW,
     AUTO_FOCUS,
+    CANCEL_AUTO_FOCUS,
     TAKE_PICTURE,
     SET_PARAMETERS,
     GET_PARAMETERS,
@@ -162,6 +163,17 @@
         return ret;
     }
 
+    // cancel focus
+    status_t cancelAutoFocus()
+    {
+        LOGV("cancelAutoFocus");
+        Parcel data, reply;
+        data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
+        remote()->transact(CANCEL_AUTO_FOCUS, data, &reply);
+        status_t ret = reply.readInt32();
+        return ret;
+    }
+
     // take a picture - returns an IMemory (ref-counted mmap)
     status_t takePicture()
     {
@@ -294,6 +306,12 @@
             reply->writeInt32(autoFocus());
             return NO_ERROR;
         } break;
+        case CANCEL_AUTO_FOCUS: {
+            LOGV("CANCEL_AUTO_FOCUS");
+            CHECK_INTERFACE(ICamera, data, reply);
+            reply->writeInt32(cancelAutoFocus());
+            return NO_ERROR;
+        } break;
         case TAKE_PICTURE: {
             LOGV("TAKE_PICTURE");
             CHECK_INTERFACE(ICamera, data, reply);