[2/2] OmniGears: Fix pimp up ActiveDisplay

Base part: https://gerrit.omnirom.org/#/c/3380/

Change-Id: Ief2aa6802ad25a86b49d06bab1b9e34140dc5db5
diff --git a/res/values/custom_arrays.xml b/res/values/custom_arrays.xml
index 023880e..c673416 100644
--- a/res/values/custom_arrays.xml
+++ b/res/values/custom_arrays.xml
@@ -146,6 +146,24 @@
         <item>18000000</item>
     </string-array>
 
+    <string-array name="ad_threshold_entries" translatable="false">
+        <item>@string/ad_threshold_0s</item>
+        <item>@string/ad_threshold_1s</item>
+        <item>@string/ad_threshold_2s</item>
+        <item>@string/ad_threshold_3s</item>
+        <item>@string/ad_threshold_4s</item>
+        <item>@string/ad_threshold_5s</item>
+    </string-array>
+
+    <string-array name="ad_threshold_values" translatable="false">
+        <item>0</item>
+        <item>1000</item>
+        <item>2000</item>
+        <item>3000</item>
+        <item>4000</item>
+        <item>5000</item>
+    </string-array>
+
     <string-array name="ad_timeout_entries" translatable="false">
         <item>@string/ad_timeout_3s</item>
         <item>@string/ad_timeout_5s</item>
diff --git a/res/values/custom_strings.xml b/res/values/custom_strings.xml
index 27718e5..847c3f9 100644
--- a/res/values/custom_strings.xml
+++ b/res/values/custom_strings.xml
@@ -227,6 +227,7 @@
     <string name="ad_pocket_mode_summary">Show active display notifications when you take the device out of your pocket.</string>
     <string name="ad_redisplay_title">Redisplay notifications</string>
     <string name="ad_timeout_title">Display timeout</string>
+    <string name="ad_threshold_title">Threshold to proximity</string>
     <string name="ad_brightness_title">Notification brightness</string>
     <string name="ad_customizations_title">Customizations</string>
     <string name="ad_excluded_apps_title">Excluded applications</string>
@@ -238,6 +239,8 @@
     <string name="ad_show_date_summary">Show the date above the clock</string>
     <string name="ad_show_ampm_summary">Show AM/PM indicator next to time when using 12hour format</string>
     <string name="ad_sunlight_mode_summary">Invert the colors when in areas with bright light such as direct sunlight.</string>
+    <string name="ad_turnoff_mode_title">Turn off display</string>
+    <string name="ad_turnoff_mode_summary">Turn off display if the device gets pocketed again and was turned on by Active Display.</string>
     <string name="ad_redisplay_never">Never</string>
     <string name="ad_redisplay_1m">1 minute</string>
     <string name="ad_redisplay_5m">5 minutes</string>
@@ -252,6 +255,12 @@
     <string name="ad_timeout_10s">10 seconds</string>
     <string name="ad_timeout_15s">15 seconds</string>
     <string name="ad_timeout_25s">25 seconds</string>
+    <string name="ad_threshold_0s">Instant</string>
+    <string name="ad_threshold_1s">1 second</string>
+    <string name="ad_threshold_2s">2 seconds</string>
+    <string name="ad_threshold_3s">3 seconds</string>
+    <string name="ad_threshold_4s">4 seconds</string>
+    <string name="ad_threshold_5s">5 seconds</string>
     <string name="ad_pocket_mode_off">Off</string>
     <string name="ad_pocket_mode_notifications">Notifications only</string>
     <string name="ad_pocket_mode_always">Always</string>
diff --git a/res/xml/active_display_settings.xml b/res/xml/active_display_settings.xml
index 325c84e..cc5945b 100644
--- a/res/xml/active_display_settings.xml
+++ b/res/xml/active_display_settings.xml
@@ -95,6 +95,22 @@
             android:summary="@string/ad_show_ampm_summary"
             android:dependency="ad_enable"/>
 
+        <CheckBoxPreference
+            android:key="ad_turnoff_mode"
+            android:persistent="true"
+            android:title="@string/ad_turnoff_mode_title"
+            android:summary="@string/ad_turnoff_mode_summary"
+            android:dependency="ad_enable"/>
+
+        <ListPreference
+            android:key="ad_threshold"
+            android:persistent="false"
+            android:dialogTitle="@string/ad_threshold_title"
+            android:title="@string/ad_threshold_title"
+            android:entries="@array/ad_threshold_entries"
+            android:entryValues="@array/ad_threshold_values"
+            android:dependency="ad_enable"/>
+
         <ListPreference
             android:key="ad_timeout"
             android:persistent="false"
diff --git a/src/org/omnirom/omnigears/chameleonos/ActiveDisplaySettings.java b/src/org/omnirom/omnigears/chameleonos/ActiveDisplaySettings.java
index bf03a9f..4d0bed6 100644
--- a/src/org/omnirom/omnigears/chameleonos/ActiveDisplaySettings.java
+++ b/src/org/omnirom/omnigears/chameleonos/ActiveDisplaySettings.java
@@ -53,6 +53,8 @@
     private static final String KEY_SHOW_AMPM = "ad_show_ampm";
     private static final String KEY_BRIGHTNESS = "ad_brightness";
     private static final String KEY_TIMEOUT = "ad_timeout";
+    private static final String KEY_THRESHOLD = "ad_threshold";
+    private static final String KEY_TURNOFF_MODE = "ad_turnoff_mode";
 
     private SwitchPreference mEnabledPref;
     private CheckBoxPreference mShowTextPref;
@@ -66,6 +68,8 @@
     private CheckBoxPreference mShowAmPmPref;
     private SeekBarPreference mBrightnessLevel;
     private ListPreference mDisplayTimeout;
+    private ListPreference mProximityThreshold;
+    private CheckBoxPreference mTurnOffModePref;
 
     @Override
     public void onCreate(Bundle savedInstanceState) {
@@ -138,6 +142,17 @@
                 Settings.System.ACTIVE_DISPLAY_TIMEOUT, 8000L);
         mDisplayTimeout.setValue(String.valueOf(timeout));
         updateTimeoutSummary(timeout);
+
+        mProximityThreshold = (ListPreference) prefSet.findPreference(KEY_THRESHOLD);
+        mProximityThreshold.setOnPreferenceChangeListener(this);
+        long threshold = Settings.System.getLong(getContentResolver(),
+                Settings.System.ACTIVE_DISPLAY_THRESHOLD, 5000L);
+        mProximityThreshold.setValue(String.valueOf(threshold));
+        updateThresholdSummary(threshold);
+
+        mTurnOffModePref = (CheckBoxPreference) findPreference(KEY_TURNOFF_MODE);
+        mTurnOffModePref.setChecked((Settings.System.getInt(getContentResolver(),
+                Settings.System.ACTIVE_DISPLAY_TURNOFF_MODE, 0) == 1));
     }
 
     public boolean onPreferenceChange(Preference preference, Object newValue) {
@@ -166,6 +181,10 @@
             long timeout = Integer.valueOf((String) newValue);
             updateTimeoutSummary(timeout);
             return true;
+        } else if (preference == mProximityThreshold) {
+            long threshold = Integer.valueOf((String) newValue);
+            updateThresholdSummary(threshold);
+            return true;
         }
         return false;
     }
@@ -204,6 +223,11 @@
             Settings.System.putInt(getContentResolver(),
                     Settings.System.ACTIVE_DISPLAY_SHOW_AMPM,
                     value ? 1 : 0);
+        } else if (preference == mTurnOffModePref) {
+            value = mTurnOffModePref.isChecked();
+            Settings.System.putInt(getContentResolver(),
+                    Settings.System.ACTIVE_DISPLAY_TURNOFF_MODE,
+                    value ? 1 : 0);
         } else {
             return super.onPreferenceTreeClick(preferenceScreen, preference);
         }
@@ -233,6 +257,15 @@
         }
     }
 
+    private void updateThresholdSummary(long value) {
+        try {
+            mProximityThreshold.setSummary(mProximityThreshold.getEntries()[mProximityThreshold.findIndexOfValue("" + value)]);
+            Settings.System.putLong(getContentResolver(),
+                    Settings.System.ACTIVE_DISPLAY_THRESHOLD, value);
+        } catch (ArrayIndexOutOfBoundsException e) {
+        }
+    }
+
     private boolean hasProximitySensor() {
         SensorManager sm = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);
         return sm.getDefaultSensor(TYPE_PROXIMITY) != null;