Add back autoanswer option when bt or wired headset connected

Forward port to P

Change-Id: I6151e800bc33d58f89b938d3b6f3dcce253f3d3a
Signed-off-by: DennySPB <dennyspb@gmail.com>
diff --git a/java/com/android/dialer/app/res/values/bliss_arrays.xml b/java/com/android/dialer/app/res/values/bliss_arrays.xml
index 7a5adb2..c07caae 100644
--- a/java/com/android/dialer/app/res/values/bliss_arrays.xml
+++ b/java/com/android/dialer/app/res/values/bliss_arrays.xml
@@ -36,4 +36,29 @@
         <item>4000</item>
         <item>5000</item>
     </string-array>
+
+    <string-array name="auto_answer_entries" translatable="false">
+        <item>100</item>
+        <item>500</item>
+        <item>1000</item>
+        <item>1500</item>
+        <item>2000</item>
+        <item>2500</item>
+        <item>3000</item>
+        <item>4000</item>
+	<item>5000</item>
+    </string-array>
+
+    <!-- Proximity speaker delay values. -->
+    <string-array name="auto_answer_values" translatable="false">
+        <item>100</item>
+        <item>500</item>
+        <item>1000</item>
+        <item>1500</item>
+        <item>2000</item>
+        <item>2500</item>
+        <item>3000</item>
+        <item>4000</item>
+        <item>5000</item>
+    </string-array>
 </resources>
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 4160f11..2425df2 100644
--- a/java/com/android/dialer/app/res/values/bliss_strings.xml
+++ b/java/com/android/dialer/app/res/values/bliss_strings.xml
@@ -36,4 +36,9 @@
     <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>
 
+    <string name="auto_answer_call_title">Autoanswer</string>
+    <string name="auto_answer_call_summary">Use autoanswer with connected BT or Wired headset</string>
+    <string name="auto_answer_delay_title">Autoanswer delay</string>
+    <string name="auto_answer_dialog_title">Autoanswer delay in (ms)</string>
+    <string name="auto_answer_delay_summary">Autoanswer will be activated in <xliff:g id="number">%d</xliff:g> ms</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 dc89d75..dda8e2a 100644
--- a/java/com/android/dialer/app/res/xml/speaker_settings.xml
+++ b/java/com/android/dialer/app/res/xml/speaker_settings.xml
@@ -47,4 +47,20 @@
         android:summary="@string/prox_auto_answer_incall_only_summary"
         android:persistent="false"
         android:defaultValue="false" />
+
+    <SwitchPreference
+        android:key="auto_answer_call_key"
+        android:title="@string/auto_answer_call_title"
+        android:summary="@string/auto_answer_call_summary"
+        android:persistent="false"
+        android:defaultValue="false" />
+
+    <ListPreference
+        android:key="auto_answer_delay"
+        android:title="@string/auto_answer_delay_title"
+        android:dialogTitle="@string/auto_answer_dialog_title"
+        android:entries="@array/auto_answer_entries"
+        android:entryValues="@array/auto_answer_values"
+        android:persistent="false"
+        android:dependency="auto_answer_call_key" />
 </PreferenceScreen>
diff --git a/java/com/android/dialer/app/settings/SpeakerSettingsFragment.java b/java/com/android/dialer/app/settings/SpeakerSettingsFragment.java
index 8488b1c..2a3ad7f 100644
--- a/java/com/android/dialer/app/settings/SpeakerSettingsFragment.java
+++ b/java/com/android/dialer/app/settings/SpeakerSettingsFragment.java
@@ -35,11 +35,16 @@
     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 static final String AUTO_ANSWER_CALL_KEY  = "auto_answer_call_key";
+    private static final String AUTO_ANSWER_DELAY  = "auto_answer_delay";
 
     private SwitchPreference mProxSpeaker;
     private ListPreference mProxSpeakerDelay;
+    private ListPreference mAutoAnswerDelay;
+
     private SwitchPreference mProxSpeakerIncallOnly;
     private SwitchPreference mProxAnswer;
+    private SwitchPreference mAutoAnswer;
 
     @Override
     public void onCreate(Bundle savedInstanceState) {
@@ -48,6 +53,11 @@
         addPreferencesFromResource(R.xml.speaker_settings);
         final ContentResolver resolver = getActivity().getContentResolver();
 
+	mAutoAnswer = (SwitchPreference) findPreference(AUTO_ANSWER_CALL_KEY);
+        mAutoAnswer.setChecked(Settings.System.getInt(resolver,
+                Settings.System.AUTO_ANSWER_CALL_KEY, 0) == 1);
+        mAutoAnswer.setOnPreferenceChangeListener(this);
+
         mProxSpeaker = (SwitchPreference) findPreference(PROXIMITY_AUTO_SPEAKER);
         mProxSpeaker.setChecked(Settings.System.getInt(resolver,
                 Settings.System.PROXIMITY_AUTO_SPEAKER, 0) == 1);
@@ -60,6 +70,13 @@
         mProxSpeakerDelay.setOnPreferenceChangeListener(this);
         updateProximityDelaySummary(proxDelay);
 
+	mAutoAnswerDelay = (ListPreference) findPreference(AUTO_ANSWER_DELAY);
+        int ansDelay = Settings.System.getInt(resolver,
+                Settings.System.AUTO_ANSWER_DELAY, 100);
+        mAutoAnswerDelay.setValue(String.valueOf(ansDelay));
+        mAutoAnswerDelay.setOnPreferenceChangeListener(this);
+        updateAnswerDelaySummary(ansDelay);
+
         mProxSpeakerIncallOnly = (SwitchPreference) findPreference(PROXIMITY_AUTO_SPEAKER_INCALL_ONLY);
         mProxSpeakerIncallOnly.setChecked(Settings.System.getInt(resolver,
                 Settings.System.PROXIMITY_AUTO_SPEAKER_INCALL_ONLY, 0) == 1);
@@ -79,11 +96,20 @@
             Settings.System.putInt(resolver, Settings.System.PROXIMITY_AUTO_SPEAKER,
                     ((Boolean) newValue) ? 1 : 0);
             return true;
+	} else if (preference == mAutoAnswer) {
+            Settings.System.putInt(resolver, Settings.System.AUTO_ANSWER_CALL_KEY,
+                    ((Boolean) newValue) ? 1 : 0);
+            return true;
         } else if (preference == mProxSpeakerDelay) {
             int proxDelay = Integer.valueOf((String) newValue);
             Settings.System.putInt(resolver, Settings.System.PROXIMITY_AUTO_SPEAKER_DELAY, proxDelay);
             updateProximityDelaySummary(proxDelay);
             return true;
+	} else if (preference == mAutoAnswerDelay) {
+            int ansDelay = Integer.valueOf((String) newValue);
+            Settings.System.putInt(resolver, Settings.System.AUTO_ANSWER_DELAY, ansDelay);
+            updateAnswerDelaySummary(ansDelay);
+            return true;
         } else if (preference == mProxSpeakerIncallOnly) {
             Settings.System.putInt(resolver, Settings.System.PROXIMITY_AUTO_SPEAKER_INCALL_ONLY,
                     ((Boolean) newValue) ? 1 : 0);
@@ -100,4 +126,10 @@
         String summary = getResources().getString(R.string.prox_auto_speaker_delay_summary, value);
         mProxSpeakerDelay.setSummary(summary);
     }
+
+    private void updateAnswerDelaySummary(int value) {
+        String summary = getResources().getString(R.string.auto_answer_delay_summary, value);
+        mAutoAnswerDelay.setSummary(summary);
+    }
+
 }
diff --git a/java/com/android/incallui/ProximitySensor.java b/java/com/android/incallui/ProximitySensor.java
index 9637103..c5948c2 100644
--- a/java/com/android/incallui/ProximitySensor.java
+++ b/java/com/android/incallui/ProximitySensor.java
@@ -71,6 +71,7 @@
   private boolean isProxSensorNear = false;
   private boolean isProxSensorFar = true;
   private int proxSpeakerDelay = 3000;
+  private int answerDelay = 5000;
   private boolean isPhoneOffhook = false;
   private boolean dialpadVisible;
   private boolean isAttemptingVideoCall;
@@ -82,7 +83,9 @@
 
   private static final int SENSOR_SENSITIVITY = 4;
 
-   private final Handler handler = new Handler();
+  private final Handler handler = new Handler();
+  private final Handler handlerAnswer = new Handler();
+
    private final Runnable activateSpeaker = new Runnable() {
     @Override
     public void run() {
@@ -90,6 +93,15 @@
     }
    };
 
+   private final Runnable answerCall = new Runnable() {
+    @Override
+    public void run() {
+	if (hasIncomingCall) {
+	    telecomManager.acceptRingingCall();
+	}
+    }
+   };
+
   public ProximitySensor(
       @NonNull Context context,
       @NonNull AudioModeProvider audioModeProvider,
@@ -378,11 +390,20 @@
     }
 
  private void answerProx(boolean isNear) {
+    handlerAnswer.removeCallbacks(answerCall);
+    final int audioRoute = audioModeProvider.getAudioState().getRoute();
     final boolean proxIncallAnswPref =
                 (Settings.System.getInt(mContext.getContentResolver(),
                 Settings.System.PROXIMITY_AUTO_ANSWER_INCALL_ONLY, 0) == 1);
+    answerDelay = Settings.System.getInt(mContext.getContentResolver(),
+                Settings.System.AUTO_ANSWER_DELAY, 5000);
     if (isNear && telecomManager != null && !isScreenReallyOff() && proxIncallAnswPref) {
     telecomManager.acceptRingingCall();
+	if (Settings.System.getInt(mContext.getContentResolver(),
+                Settings.System.AUTO_ANSWER_CALL_KEY, 0) == 1 && (audioRoute == CallAudioState.ROUTE_WIRED_HEADSET
+								  || audioRoute == CallAudioState.ROUTE_BLUETOOTH)) {
+        handlerAnswer.postDelayed(answerCall, answerDelay);
+	}
     }
  }