Add back proximity autoanswer feature

Forward port to P

Change-Id: I613986b63adc6a3ba73ace208bc0ac0589435b8d
Signed-off-by: DennySPB <dennyspb@gmail.com>
diff --git a/java/com/android/dialer/app/res/values/bliss_strings.xml b/java/com/android/dialer/app/res/values/bliss_strings.xml
index 23616c9..4160f11 100644
--- a/java/com/android/dialer/app/res/values/bliss_strings.xml
+++ b/java/com/android/dialer/app/res/values/bliss_strings.xml
@@ -33,4 +33,7 @@
     <string name="prox_auto_speaker_incall_only_summary_on">On outgoing calls, listener will only become active after the receiving party answers</string>
     <string name="prox_auto_speaker_incall_only_summary_off">Listener will be active during all offhook call states (default)</string>
 
+    <string name="prox_auto_answer_incall_only_title">Auto answer</string>
+    <string name="prox_auto_answer_incall_only_summary">Use proximity sensor for autoanswer</string>
+
 </resources>
diff --git a/java/com/android/dialer/app/res/xml/speaker_settings.xml b/java/com/android/dialer/app/res/xml/speaker_settings.xml
index c442f83..dc89d75 100644
--- a/java/com/android/dialer/app/res/xml/speaker_settings.xml
+++ b/java/com/android/dialer/app/res/xml/speaker_settings.xml
@@ -41,4 +41,10 @@
         android:defaultValue="false"
         android:dependency="proximity_auto_speaker" />
 
+    <SwitchPreference
+        android:key="proximity_auto_answer_incall_only"
+        android:title="@string/prox_auto_answer_incall_only_title"
+        android:summary="@string/prox_auto_answer_incall_only_summary"
+        android:persistent="false"
+        android:defaultValue="false" />
 </PreferenceScreen>
diff --git a/java/com/android/dialer/app/settings/SpeakerSettingsFragment.java b/java/com/android/dialer/app/settings/SpeakerSettingsFragment.java
index ff3e7ff..8488b1c 100644
--- a/java/com/android/dialer/app/settings/SpeakerSettingsFragment.java
+++ b/java/com/android/dialer/app/settings/SpeakerSettingsFragment.java
@@ -34,10 +34,12 @@
     private static final String PROXIMITY_AUTO_SPEAKER  = "proximity_auto_speaker";
     private static final String PROXIMITY_AUTO_SPEAKER_DELAY  = "proximity_auto_speaker_delay";
     private static final String PROXIMITY_AUTO_SPEAKER_INCALL_ONLY  = "proximity_auto_speaker_incall_only";
+    private static final String PROXIMITY_AUTO_ANSWER_INCALL_ONLY  = "proximity_auto_answer_incall_only";
 
     private SwitchPreference mProxSpeaker;
     private ListPreference mProxSpeakerDelay;
     private SwitchPreference mProxSpeakerIncallOnly;
+    private SwitchPreference mProxAnswer;
 
     @Override
     public void onCreate(Bundle savedInstanceState) {
@@ -62,6 +64,11 @@
         mProxSpeakerIncallOnly.setChecked(Settings.System.getInt(resolver,
                 Settings.System.PROXIMITY_AUTO_SPEAKER_INCALL_ONLY, 0) == 1);
         mProxSpeakerIncallOnly.setOnPreferenceChangeListener(this);
+
+	mProxAnswer = (SwitchPreference) findPreference(PROXIMITY_AUTO_ANSWER_INCALL_ONLY);
+        mProxAnswer.setChecked(Settings.System.getInt(resolver,
+                Settings.System.PROXIMITY_AUTO_ANSWER_INCALL_ONLY, 0) == 1);
+        mProxAnswer.setOnPreferenceChangeListener(this);
     }
 
     @Override
@@ -81,7 +88,11 @@
             Settings.System.putInt(resolver, Settings.System.PROXIMITY_AUTO_SPEAKER_INCALL_ONLY,
                     ((Boolean) newValue) ? 1 : 0);
             return true;
-        }
+        } else if (preference == mProxAnswer) {
+            Settings.System.putInt(resolver, Settings.System.PROXIMITY_AUTO_ANSWER_INCALL_ONLY,
+                    ((Boolean) newValue) ? 1 : 0);
+            return true;
+	}
         return false;
     }
 
diff --git a/java/com/android/incallui/ProximitySensor.java b/java/com/android/incallui/ProximitySensor.java
index c423f1c..9637103 100644
--- a/java/com/android/incallui/ProximitySensor.java
+++ b/java/com/android/incallui/ProximitySensor.java
@@ -40,6 +40,7 @@
 import com.android.incallui.call.CallList;
 import com.android.incallui.call.DialerCall;
 import android.preference.PreferenceManager;
+import android.telecom.TelecomManager;
 
 /**
  * Class manages the proximity sensor for the in-call UI. We enable the proximity sensor while the
@@ -65,7 +66,9 @@
   private boolean uiShowing = false;
   private boolean hasIncomingCall = false;
   private boolean isPhoneOutgoing = false;
+  private boolean isPhoneRinging = false;
   private boolean proximitySpeaker = false;
+  private boolean isProxSensorNear = false;
   private boolean isProxSensorFar = true;
   private int proxSpeakerDelay = 3000;
   private boolean isPhoneOffhook = false;
@@ -75,6 +78,10 @@
   private boolean isRttCall;
   private SharedPreferences mPrefs;
   private Context mContext;
+  private final TelecomManager telecomManager;
+
+  private static final int SENSOR_SENSITIVITY = 4;
+
    private final Handler handler = new Handler();
    private final Runnable activateSpeaker = new Runnable() {
     @Override
@@ -94,6 +101,7 @@
     final boolean mIsProximitySensorDisabled = mPrefs.getBoolean(PREF_KEY_DISABLE_PROXI_SENSOR, false);
 
     powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
+    telecomManager = (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE);
     if (powerManager.isWakeLockLevelSupported(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK)
           && !mIsProximitySensorDisabled) {
       proximityWakeLock =
@@ -180,7 +188,8 @@
       setProxSpeaker(isProxSensorFar);
     }
      if (hasIncomingCall) {
-
+      updateProxRing();
+      answerProx(isProxSensorNear);
       updateProximitySensorMode();
     }
   }
@@ -199,8 +208,14 @@
       isProxSensorFar = false;
     } else {
       isProxSensorFar = true;
+      isProxSensorNear = false;
     }
+    if (event.values[0] <= SENSOR_SENSITIVITY ) {
+            isProxSensorNear = true;
+        }
+     Log.i(this, "Proximity sensor changed");
      setProxSpeaker(isProxSensorFar);
+     answerProx(isProxSensorNear);
   }
    @Override
   public void onAccuracyChanged(Sensor sensor, int accuracy) {
@@ -350,6 +365,27 @@
       }
     }
   }
+
+ private void updateProxRing() {
+        if (sensor != null && proxSensor != null) {
+            if (hasIncomingCall) {
+                sensor.registerListener(this, proxSensor,
+                        SensorManager.SENSOR_DELAY_NORMAL);
+            } else {
+                sensor.unregisterListener(this);
+            }
+        }
+    }
+
+ private void answerProx(boolean isNear) {
+    final boolean proxIncallAnswPref =
+                (Settings.System.getInt(mContext.getContentResolver(),
+                Settings.System.PROXIMITY_AUTO_ANSWER_INCALL_ONLY, 0) == 1);
+    if (isNear && telecomManager != null && !isScreenReallyOff() && proxIncallAnswPref) {
+    telecomManager.acceptRingingCall();
+    }
+ }
+
    private void setProxSpeaker(final boolean speaker) {
     // remove any pending audio changes scheduled
     handler.removeCallbacks(activateSpeaker);