Setup a flag to detect when the screen is on and we are in call mode.

Fixes b/6029570 Proximity turns screen on and off repeatedly for no reason, when device is flat

Change-Id: Ie727dd779bb957cd6da948dbe1584a4382d9935a
diff --git a/src/com/android/phone/InCallScreen.java b/src/com/android/phone/InCallScreen.java
index b46e770..26733ed 100755
--- a/src/com/android/phone/InCallScreen.java
+++ b/src/com/android/phone/InCallScreen.java
@@ -39,6 +39,7 @@
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.Message;
+import android.os.PowerManager;
 import android.os.SystemClock;
 import android.os.SystemProperties;
 import android.telephony.ServiceState;
@@ -241,6 +242,8 @@
     // current "activity lifecycle" state, we can remove these flags.
     private boolean mIsDestroyed = false;
     private boolean mIsForegroundActivity = false;
+    private boolean mIsForegroundActivityForProximity = false;
+    private PowerManager mPowerManager;
 
     // For use with Pause/Wait dialogs
     private String mPostDialStrAfterPause;
@@ -562,6 +565,7 @@
         super.onResume();
 
         mIsForegroundActivity = true;
+        mIsForegroundActivityForProximity = true;
 
         final InCallUiState inCallUiState = mApp.inCallUiState;
         if (VDBG) inCallUiState.dumpState();
@@ -776,6 +780,9 @@
         if (DBG) log("onPause()...");
         super.onPause();
 
+        if (mPowerManager.isScreenOn()) {
+            mIsForegroundActivityForProximity = false;
+        }
         mIsForegroundActivity = false;
 
         // Force a clear of the provider overlay' frame. Since the
@@ -1006,6 +1013,10 @@
         return mIsForegroundActivity;
     }
 
+    /* package */ boolean isForegroundActivityForProximity() {
+        return mIsForegroundActivityForProximity;
+    }
+
     /* package */ void updateKeyguardPolicy(boolean dismissKeyguard) {
         if (dismissKeyguard) {
             getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
@@ -1231,6 +1242,7 @@
         }
         // Finally, create the DTMFTwelveKeyDialer instance.
         mDialer = new DTMFTwelveKeyDialer(this, mDialerView);
+        mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
     }
 
     /**
diff --git a/src/com/android/phone/PhoneApp.java b/src/com/android/phone/PhoneApp.java
index d3edb18..e519102 100644
--- a/src/com/android/phone/PhoneApp.java
+++ b/src/com/android/phone/PhoneApp.java
@@ -740,6 +740,11 @@
         return mInCallScreen.isForegroundActivity();
     }
 
+    boolean isShowingCallScreenForProximity() {
+        if (mInCallScreen == null) return false;
+        return mInCallScreen.isForegroundActivityForProximity();
+    }
+
     /**
      * Dismisses the in-call UI.
      *
@@ -1189,7 +1194,7 @@
                 // proximity sensor goes negative.
                 boolean horizontal =
                         (mOrientation == AccelerometerListener.ORIENTATION_HORIZONTAL);
-                screenOnImmediately |= !isShowingCallScreen() && horizontal;
+                screenOnImmediately |= !isShowingCallScreenForProximity() && horizontal;
 
                 if (((state == Phone.State.OFFHOOK) || mBeginningCall) && !screenOnImmediately) {
                     // Phone is in use!  Arrange for the screen to turn off