Dialer: Add option to disable proximity sensor

Provide a switch for end user to disable proximity sensor
while calling/incall.

Co-authored-by: Pranav Vashi <neobuddy89@gmail.com>
Signed-off-by: Pranav Vashi <neobuddy89@gmail.com>
Signed-off-by: Pranav Temkar <pranavtemkar@gmail.com>
Signed-off-by: Jis G Jacob <studiokeys@blissroms.org>
diff --git a/java/com/android/dialer/app/res/values/cr_strings.xml b/java/com/android/dialer/app/res/values/cr_strings.xml
index 7c446d3..1cda93a 100644
--- a/java/com/android/dialer/app/res/values/cr_strings.xml
+++ b/java/com/android/dialer/app/res/values/cr_strings.xml
@@ -19,4 +19,9 @@
     <!-- Call recording -->
     <string name="auto_call_recording_title">Auto call recording</string>
     <string name="auto_call_recording_summary">Automatic recording of incoming and outgoing calls</string>
+
+    <!-- Disable Proximity Options -->
+    <string name="disable_proximity_sensor_title">Disable proximity sensor</string>
+    <string name="disable_proximity_sensor_summary">Do not turn off touchscreen and display in call based on proximity sensor</string>
+    <string name="sensor_settings_title">Sensors</string>
 </resources>
diff --git a/java/com/android/dialer/app/res/xml/sensor_settings.xml b/java/com/android/dialer/app/res/xml/sensor_settings.xml
new file mode 100644
index 0000000..7433d5a
--- /dev/null
+++ b/java/com/android/dialer/app/res/xml/sensor_settings.xml
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<!--
+  ~ Copyright (C) 2014 The Android Open Source Project
+  ~
+  ~ Licensed under the Apache License, Version 2.0 (the "License");
+  ~ you may not use this file except in compliance with the License.
+  ~ You may obtain a copy of the License at
+  ~
+  ~      http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License
+  -->
+
+<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
+
+  <SwitchPreferenceCompat
+    android:defaultValue="false"
+    android:key="disable_proximity_sensor_key"
+    android:title="@string/disable_proximity_sensor_title"
+    android:summary="@string/disable_proximity_sensor_summary" />
+
+</PreferenceScreen>
diff --git a/java/com/android/dialer/app/settings/DialerSettingsActivity.java b/java/com/android/dialer/app/settings/DialerSettingsActivity.java
index b4e1782..b7465f6 100644
--- a/java/com/android/dialer/app/settings/DialerSettingsActivity.java
+++ b/java/com/android/dialer/app/settings/DialerSettingsActivity.java
@@ -16,10 +16,13 @@
  */
 package com.android.dialer.app.settings;
 
+import static android.hardware.Sensor.TYPE_PROXIMITY;
+
 import android.annotation.SuppressLint;
 import android.content.Context;
 import android.content.Intent;
 import android.content.SharedPreferences;
+import android.hardware.SensorManager;
 import android.net.Uri;
 import android.os.Bundle;
 import android.os.UserManager;
@@ -136,6 +139,14 @@
       soundSettings.setIconSpaceReserved(false);
       getPreferenceScreen().addPreference(soundSettings);
 
+      if (showSensorOptions()) {
+        Preference sensorSettings = new Preference(getContext());
+        sensorSettings.setTitle(R.string.sensor_settings_title);
+        sensorSettings.setFragment(SensorSettingsFragment.class.getName());
+        sensorSettings.setIconSpaceReserved(false);
+        getPreferenceScreen().addPreference(sensorSettings);
+      }
+
       Preference quickResponseSettings = new Preference(getContext());
       Intent quickResponseSettingsIntent =
               new Intent(TelecomManager.ACTION_SHOW_RESPOND_VIA_SMS_SETTINGS);
@@ -304,5 +315,10 @@
     private boolean isPrimaryUser() {
       return requireContext().getSystemService(UserManager.class).isSystemUser();
     }
+
+    private boolean showSensorOptions() {
+      SensorManager sm = (SensorManager) requireContext().getSystemService(Context.SENSOR_SERVICE);
+      return sm.getDefaultSensor(TYPE_PROXIMITY) != null;
+    }
   }
 }
diff --git a/java/com/android/dialer/app/settings/SensorSettingsFragment.java b/java/com/android/dialer/app/settings/SensorSettingsFragment.java
new file mode 100644
index 0000000..3be01f0
--- /dev/null
+++ b/java/com/android/dialer/app/settings/SensorSettingsFragment.java
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package com.android.dialer.app.settings;
+
+import android.os.Bundle;
+import android.preference.PreferenceFragment;
+import com.android.dialer.app.R;
+
+public class SensorSettingsFragment extends PreferenceFragment {
+
+  @Override
+  public void onCreate(Bundle savedInstanceState) {
+    super.onCreate(savedInstanceState);
+    addPreferencesFromResource(R.xml.sensor_settings);
+  }
+}
diff --git a/java/com/android/incallui/ProximitySensor.java b/java/com/android/incallui/ProximitySensor.java
index c02ebf7..e4db765 100644
--- a/java/com/android/incallui/ProximitySensor.java
+++ b/java/com/android/incallui/ProximitySensor.java
@@ -18,6 +18,7 @@
 package com.android.incallui;
 
 import android.content.Context;
+import android.content.SharedPreferences;
 import android.hardware.display.DisplayManager;
 import android.hardware.display.DisplayManager.DisplayListener;
 import android.os.PowerManager;
@@ -34,6 +35,7 @@
 import com.android.incallui.audiomode.AudioModeProvider.AudioModeListener;
 import com.android.incallui.call.CallList;
 import com.android.incallui.call.DialerCall;
+import android.preference.PreferenceManager;
 
 /**
  * Class manages the proximity sensor for the in-call UI. We enable the proximity sensor while the
@@ -46,6 +48,7 @@
     implements AccelerometerListener.OrientationListener, InCallStateListener, AudioModeListener {
 
   private static final String TAG = ProximitySensor.class.getSimpleName();
+  private static final String PREF_KEY_DISABLE_PROXI_SENSOR = "disable_proximity_sensor_key";
 
   private final PowerManager powerManager;
   private final PowerManager.WakeLock proximityWakeLock;
@@ -59,18 +62,26 @@
   private boolean isAttemptingVideoCall;
   private boolean isVideoCall;
   private boolean isRttCall;
+  private SharedPreferences mPrefs;
 
   public ProximitySensor(
       @NonNull Context context,
       @NonNull AudioModeProvider audioModeProvider,
       @NonNull AccelerometerListener accelerometerListener) {
     Trace.beginSection("ProximitySensor.Constructor");
+
+    mPrefs = PreferenceManager.getDefaultSharedPreferences(context);
+    final boolean mIsProximitySensorDisabled = mPrefs.getBoolean(PREF_KEY_DISABLE_PROXI_SENSOR, false);
+
     powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
-    if (powerManager.isWakeLockLevelSupported(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK)) {
+    if (powerManager.isWakeLockLevelSupported(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK)
+          && !mIsProximitySensorDisabled) {
       proximityWakeLock =
           powerManager.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, TAG);
+    } else if (mIsProximitySensorDisabled) {
+      turnOffProximitySensor(true); // Ensure the wakelock is released before destroying it.
+      proximityWakeLock = null;
     } else {
-      LogUtil.i("ProximitySensor.constructor", "Device does not support proximity wake lock.");
       proximityWakeLock = null;
     }
     this.accelerometerListener = accelerometerListener;
@@ -210,6 +221,12 @@
     Trace.beginSection("ProximitySensor.updateProximitySensorMode");
     final int audioRoute = audioModeProvider.getAudioState().getRoute();
 
+    final boolean mIsProximitySensorDisabled = mPrefs.getBoolean(PREF_KEY_DISABLE_PROXI_SENSOR, false);
+
+    if (mIsProximitySensorDisabled) {
+        return;
+    }
+
     boolean screenOnImmediately =
         (CallAudioState.ROUTE_WIRED_HEADSET == audioRoute
             || CallAudioState.ROUTE_SPEAKER == audioRoute