Fix race condition in PhoneApp.updateProximitySensorMode()

updateProximitySensorMode can be called from both the main thread and the
CallNotifier InCallTonePlayer at the same time, which was resulting in
WakeLock under-locked exceptions when disconnecting calls in automated tests.

Fixes bug b/2183174 (RuntimeException: WakeLock under-locked PhoneApp)

Change-Id: I6172a2c500ddeab2772aeb3d7534b3a4a4a1f09c
Signed-off-by: Mike Lockwood <lockwood@android.com>
diff --git a/src/com/android/phone/PhoneApp.java b/src/com/android/phone/PhoneApp.java
index 9297223..192cbcd 100755
--- a/src/com/android/phone/PhoneApp.java
+++ b/src/com/android/phone/PhoneApp.java
@@ -1100,30 +1100,34 @@
         if (VDBG) Log.d(LOG_TAG, "updateProximitySensorMode: state = " + state);
 
         if (proximitySensorModeEnabled()) {
-            if (((state == Phone.State.OFFHOOK) || mBeginningCall)
-                    && !(isHeadsetPlugged()
-                    || PhoneUtils.isSpeakerOn(this)
-                    || ((mBtHandsfree != null) && mBtHandsfree.isAudioOn())
-                    || mIsHardKeyboardOpen)) {
-                // Phone is in use!  Arrange for the screen to turn off
-                // automatically when the sensor detects a close object.
-                if (!mProximityWakeLock.isHeld()) {
-                    if (DBG) Log.d(LOG_TAG, "updateProximitySensorMode: acquiring...");
-                    mProximityWakeLock.acquire();
-                    // disable keyguard while we are using the proximity sensor
-                    disableKeyguard();
+            synchronized (mProximityWakeLock) {
+                if (((state == Phone.State.OFFHOOK) || mBeginningCall)
+                        && !(isHeadsetPlugged()
+                        || PhoneUtils.isSpeakerOn(this)
+                        || ((mBtHandsfree != null) && mBtHandsfree.isAudioOn())
+                        || mIsHardKeyboardOpen)) {
+                    // Phone is in use!  Arrange for the screen to turn off
+                    // automatically when the sensor detects a close object.
+                    if (!mProximityWakeLock.isHeld()) {
+                        if (DBG) Log.d(LOG_TAG, "updateProximitySensorMode: acquiring...");
+                        mProximityWakeLock.acquire();
+                        // disable keyguard while we are using the proximity sensor
+                        disableKeyguard();
+                    } else {
+                        if (VDBG) Log.d(LOG_TAG, "updateProximitySensorMode: lock already held.");
+                    }
                 } else {
-                    if (VDBG) Log.d(LOG_TAG, "updateProximitySensorMode: lock already held.");
-                }
-            } else {
-                // Phone is either idle, or ringing.  We don't want any
-                // special proximity sensor behavior in either case.
-                if (mProximityWakeLock.isHeld()) {
-                    if (DBG) Log.d(LOG_TAG, "updateProximitySensorMode: releasing...");
-                    mProximityWakeLock.release();
-                    reenableKeyguard();
-                } else {
-                    if (VDBG) Log.d(LOG_TAG, "updateProximitySensorMode: lock already released.");
+                    // Phone is either idle, or ringing.  We don't want any
+                    // special proximity sensor behavior in either case.
+                    if (mProximityWakeLock.isHeld()) {
+                        if (DBG) Log.d(LOG_TAG, "updateProximitySensorMode: releasing...");
+                        mProximityWakeLock.release();
+                        reenableKeyguard();
+                    } else {
+                        if (VDBG) {
+                            Log.d(LOG_TAG, "updateProximitySensorMode: lock already released.");
+                        }
+                    }
                 }
             }
         }