Merge change 25050 into eclair

* changes:
  Fix issue 2118464: cannot play ring tones and notifications after disconnecting BT headset while in call.
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/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/IHardwareService.aidl b/core/java/android/os/IHardwareService.aidl
index aebcb3c..a6ef6473 100755
--- a/core/java/android/os/IHardwareService.aidl
+++ b/core/java/android/os/IHardwareService.aidl
@@ -32,6 +32,9 @@
     // sets the brightness of the backlights (screen, keyboard, button) 0-255
     void setBacklights(int brightness);
 
+    // enables or disables automatic brightness mode
+    void setAutoBrightness(boolean on);
+
     // for the phone
     void setAttentionLight(boolean on);
 }
diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java
index 688f377..0bbd1fc 100644
--- a/core/java/android/provider/Settings.java
+++ b/core/java/android/provider/Settings.java
@@ -1061,6 +1061,12 @@
         public static final String SCREEN_BRIGHTNESS = "screen_brightness";
 
         /**
+         * Control whether to enable automatic brightness mode.
+         * @hide
+         */
+        public static final String SCREEN_BRIGHTNESS_MODE = "screen_brightness_mode";
+
+        /**
          * Control whether the process CPU usage meter should be shown.
          */
         public static final String SHOW_PROCESSES = "show_processes";
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/core/res/res/values/config.xml b/core/res/res/values/config.xml
index abb575c..117e139 100644
--- a/core/res/res/values/config.xml
+++ b/core/res/res/values/config.xml
@@ -81,4 +81,6 @@
         <item>30</item>
     </integer-array>
     
+    <!-- Flag indicating whether the device supports automatic brightness mode. -->
+    <bool name="config_automatic_brightness_available">false</bool>
 </resources>
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/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);
diff --git a/packages/SettingsProvider/res/values/defaults.xml b/packages/SettingsProvider/res/values/defaults.xml
index 6b20445..d5f1c61 100644
--- a/packages/SettingsProvider/res/values/defaults.xml
+++ b/packages/SettingsProvider/res/values/defaults.xml
@@ -27,6 +27,7 @@
     <bool name="def_accelerometer_rotation">true</bool>
     <!-- Default screen brightness, from 0 to 255.  102 is 40%. -->
     <integer name="def_screen_brightness">102</integer>
+    <bool name="def_screen_brightness_automatic_mode">false</bool>
     <fraction name="def_window_animation_scale">100%</fraction>
     <fraction name="def_window_transition_scale">0%</fraction>
     
diff --git a/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java b/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java
index 2524a30..f99eb58 100644
--- a/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java
+++ b/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java
@@ -71,7 +71,7 @@
     // database gets upgraded properly. At a minimum, please confirm that 'upgradeVersion'
     // is properly propagated through your change.  Not doing so will result in a loss of user
     // settings.
-    private static final int DATABASE_VERSION = 39;
+    private static final int DATABASE_VERSION = 40;
 
     private Context mContext;
 
@@ -465,6 +465,22 @@
             upgradeVersion = 39;
         }
 
+        if (upgradeVersion == 39) {
+            db.beginTransaction();
+            try {
+                String value =
+                        mContext.getResources().getBoolean(
+                        R.bool.def_screen_brightness_automatic_mode) ? "1" : "0";
+                db.execSQL("INSERT OR IGNORE INTO system(name,value) values('" +
+                        Settings.System.SCREEN_BRIGHTNESS_MODE + "','" + value + "');");
+                db.setTransactionSuccessful();
+            } finally {
+                db.endTransaction();
+            }
+
+            upgradeVersion = 40;
+        }
+
         if (upgradeVersion != currentVersion) {
             Log.w(TAG, "Got stuck trying to upgrade from version " + upgradeVersion
                     + ", must wipe the settings provider");
@@ -701,6 +717,9 @@
         loadIntegerSetting(stmt, Settings.System.SCREEN_BRIGHTNESS,
                 R.integer.def_screen_brightness);
 
+        loadBooleanSetting(stmt, Settings.System.SCREEN_BRIGHTNESS_MODE,
+                R.bool.def_screen_brightness_automatic_mode);
+
         loadDefaultAnimationSettings(stmt);
 
         loadBooleanSetting(stmt, Settings.System.ACCELEROMETER_ROTATION,
diff --git a/services/java/com/android/server/HardwareService.java b/services/java/com/android/server/HardwareService.java
index 6ac72e0..01daae3 100755
--- a/services/java/com/android/server/HardwareService.java
+++ b/services/java/com/android/server/HardwareService.java
@@ -59,6 +59,8 @@
     private boolean mAttentionLightOn;
     private boolean mPulsing;
 
+    private boolean mAutoBrightnessAvailable;
+
     private class Vibration implements IBinder.DeathRecipient {
         private final IBinder mToken;
         private final long    mTimeout;
@@ -129,6 +131,9 @@
         IntentFilter filter = new IntentFilter();
         filter.addAction(Intent.ACTION_SCREEN_OFF);
         context.registerReceiver(mIntentReceiver, filter);
+
+        mAutoBrightnessAvailable = context.getResources().getBoolean(
+                com.android.internal.R.bool.config_automatic_brightness_available);
     }
 
     protected void finalize() throws Throwable {
@@ -302,6 +307,20 @@
         setLight_native(mNativePointer, light, color, mode, onMS, offMS);
     }
 
+    public void setAutoBrightness(boolean on) {
+        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.HARDWARE_TEST)
+                != PackageManager.PERMISSION_GRANTED) {
+            throw new SecurityException("Requires HARDWARE_TEST permission");
+        }
+        setAutoBrightness_UNCHECKED(on);
+    }
+
+    void setAutoBrightness_UNCHECKED(boolean on) {
+        if (mAutoBrightnessAvailable) {
+            setAutoBrightness_native(mNativePointer, on);
+        }
+    }
+
     public void setAttentionLight(boolean on) {
         // Not worthy of a permission.  We shouldn't have a flashlight permission.
         synchronized (this) {
@@ -502,6 +521,7 @@
     private static native int init_native();
     private static native void finalize_native(int ptr);
 
+    private static native void setAutoBrightness_native(int ptr, boolean automatic);
     private static native void setLight_native(int ptr, int light, int color, int mode,
             int onMS, int offMS);
 
diff --git a/services/java/com/android/server/PowerManagerService.java b/services/java/com/android/server/PowerManagerService.java
index ba65f01..2951384 100644
--- a/services/java/com/android/server/PowerManagerService.java
+++ b/services/java/com/android/server/PowerManagerService.java
@@ -52,6 +52,7 @@
 import android.view.WindowManagerPolicy;
 import static android.provider.Settings.System.DIM_SCREEN;
 import static android.provider.Settings.System.SCREEN_BRIGHTNESS;
+import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE;
 import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
 import static android.provider.Settings.System.STAY_ON_WHILE_PLUGGED_IN;
 
@@ -87,6 +88,9 @@
     private static final int LONG_KEYLIGHT_DELAY = 6000;        // t+6 sec
     private static final int LONG_DIM_TIME = 7000;              // t+N-5 sec
 
+    // trigger proximity if distance is less than 5 cm
+    private static final float PROXIMITY_THRESHOLD = 5.0f;
+
     // Cached Gservices settings; see updateGservicesValues()
     private int mShortKeylightDelay = SHORT_KEYLIGHT_DELAY_DEFAULT;
 
@@ -442,7 +446,11 @@
 
         // turn everything on
         setPowerState(ALL_BRIGHT);
-        
+
+        // set auto brightness mode to user setting
+        boolean brightnessMode = Settings.System.getInt(resolver, SCREEN_BRIGHTNESS_MODE, 1) != 0;
+        mHardware.setAutoBrightness_UNCHECKED(brightnessMode);
+
         synchronized (mHandlerThread) {
             mInitComplete = true;
             mHandlerThread.notifyAll();
@@ -2079,18 +2087,19 @@
     public void onSensorChanged(SensorEvent event) {
         long milliseconds = event.timestamp / 1000000;
         synchronized (mLocks) {
-            if (event.values[0] == 0.0) {
+            float distance = event.values[0];
+            if (distance >= 0.0 && distance < PROXIMITY_THRESHOLD) {
                 if (mSpew) {
-                    Log.d(TAG, "onSensorChanged: proximity active");
+                    Log.d(TAG, "onSensorChanged: proximity active, distance: " + distance);
                 }
                 goToSleepLocked(milliseconds);
                 mProximitySensorActive = true;
             } else {
-                // proximity sensor negative events user activity.
+                // proximity sensor negative events trigger as user activity.
                 // temporarily set mUserActivityAllowed to true so this will work
                 // even when the keyguard is on.
                 if (mSpew) {
-                    Log.d(TAG, "onSensorChanged: proximity inactive");
+                    Log.d(TAG, "onSensorChanged: proximity inactive, distance: " + distance);
                 }
                 mProximitySensorActive = false;
                 boolean savedActivityAllowed = mUserActivityAllowed;
diff --git a/services/jni/com_android_server_HardwareService.cpp b/services/jni/com_android_server_HardwareService.cpp
index 22d4bd8..a17e29f 100644
--- a/services/jni/com_android_server_HardwareService.cpp
+++ b/services/jni/com_android_server_HardwareService.cpp
@@ -100,6 +100,18 @@
     free(devices);
 }
 
+static void setAutoBrightness_native(JNIEnv *env, jobject clazz, int ptr,
+        jboolean automatic)
+{
+    Devices* devices = (Devices*)ptr;
+
+    if (devices->lights[LIGHT_INDEX_BACKLIGHT] == NULL) {
+        return;
+    }
+
+    devices->lights[LIGHT_INDEX_BACKLIGHT]->set_als_mode(automatic ? 0 : 1);
+}
+
 static void setLight_native(JNIEnv *env, jobject clazz, int ptr,
         int light, int colorARGB, int flashMode, int onMS, int offMS)
 {
@@ -134,6 +146,7 @@
 static JNINativeMethod method_table[] = {
     { "init_native", "()I", (void*)init_native },
     { "finalize_native", "(I)V", (void*)finalize_native },
+    { "setAutoBrightness_native", "(IZ)V", (void*)setAutoBrightness_native },
     { "setLight_native", "(IIIIII)V", (void*)setLight_native },
     { "vibratorOn", "(J)V", (void*)vibratorOn },
     { "vibratorOff", "()V", (void*)vibratorOff }