Add Flashlight blink on incoming calls

Change-Id: I531f196278cce9f22e10a2d17bb95f18b1bc3644
diff --git a/res/values/custom_arrays.xml b/res/values/custom_arrays.xml
index d858e75..8f1db3d 100644
--- a/res/values/custom_arrays.xml
+++ b/res/values/custom_arrays.xml
@@ -629,4 +629,19 @@
         <item>2</item>
         <item>3</item>
     </string-array>
+
+   <!-- Blink flashlight for incoming calls -->
+   <string-array name="flashlight_on_call_entries">
+      <item>@string/flashlight_on_call_disabled</item>
+      <item>@string/flashlight_on_call_not_dnd</item>
+      <item>@string/flashlight_on_call_dnd</item>
+      <item>@string/flashlight_on_call_always</item>
+   </string-array>
+    <string-array name="flashlight_on_call_values">
+       <item>0</item>
+       <item>1</item>
+       <item>2</item>
+       <item>3</item>
+   </string-array>
+
 </resources>
diff --git a/res/values/custom_strings.xml b/res/values/custom_strings.xml
index 3028747..9131be1 100644
--- a/res/values/custom_strings.xml
+++ b/res/values/custom_strings.xml
@@ -835,4 +835,14 @@
     <!-- AppOps -->
     <string name="app_ops_settings_title">App Ops</string>
 
+   <!-- Call options -->
+   <string name="calls_category">Calling options</string>
+
+   <!-- Blink flashlight for incoming calls -->
+    <string name="flashlight_on_call_title">Blink Flashlight on call</string>
+    <string name="flashlight_on_call_disabled">Disabled</string>
+    <string name="flashlight_on_call_not_dnd">Only in ringer mode</string>
+    <string name="flashlight_on_call_dnd">Only in DND mode</string>
+    <string name="flashlight_on_call_always">Always</string>
+
 </resources>
diff --git a/res/xml/more_settings.xml b/res/xml/more_settings.xml
index eafccfd..a99d905 100644
--- a/res/xml/more_settings.xml
+++ b/res/xml/more_settings.xml
@@ -44,6 +44,19 @@
 
         </PreferenceCategory>
 
+    <PreferenceCategory
+        android:key="calling_options"
+        android:title="@string/calls_category">
+
+        <!-- Flashlight on incoming calls -->
+        <ListPreference
+            android:key="flashlight_on_call"
+            android:icon="@drawable/ic_power_flashlight"
+            android:title="@string/flashlight_on_call_title"
+            android:entries="@array/flashlight_on_call_entries"
+            android:entryValues="@array/flashlight_on_call_values"
+            android:defaultValue="0" />
+
         <PreferenceCategory
             android:key="category_other"
             android:title="@string/other_category">
diff --git a/src/org/omnirom/omnigears/moresettings/MoreSettings.java b/src/org/omnirom/omnigears/moresettings/MoreSettings.java
index 5bcc3f1..71a7ec0 100644
--- a/src/org/omnirom/omnigears/moresettings/MoreSettings.java
+++ b/src/org/omnirom/omnigears/moresettings/MoreSettings.java
@@ -49,8 +49,10 @@
     private static final String TAG = "MoreSettings";
     private static final String KEY_SHOW_DASHBOARD_COLUMNS = "show_dashboard_columns";
     private static final String KEY_HIDE_DASHBOARD_SUMMARY = "hide_dashboard_summary";
+    private static final String PREF_FLASHLIGHT_ON_CALL = "flashlight_on_call";
 
     private SharedPreferences mAppPreferences;
+    private ListPreference mFlashOnCall;
 
     @Override
     public int getMetricsCategory() {
@@ -61,6 +63,14 @@
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         addPreferencesFromResource(R.xml.more_settings);
+        ContentResolver resolver = getActivity().getContentResolver();
+
+        mFlashOnCall = (ListPreference) findPreference(PREF_FLASHLIGHT_ON_CALL);
+        mFlashOnCall.setOnPreferenceChangeListener(this);
+        mFlashOnCall.setValue(Integer.toString(Settings.System.getInt(getActivity()
+                .getContentResolver(), Settings.System.FLASHLIGHT_ON_CALL,
+                0)));
+        mFlashOnCall.setSummary(mFlashOnCall.getEntry());
 
         mAppPreferences = getActivity().getSharedPreferences(SettingsActivity.APP_PREFERENCES_NAME,
                 Context.MODE_PRIVATE);
@@ -90,6 +100,14 @@
 
     @Override
     public boolean onPreferenceChange(Preference preference, Object newValue) {
+        if (preference == mFlashOnCall) {
+            int val = Integer.parseInt((String) newValue);
+            int index = mFlashOnCall.findIndexOfValue((String) newValue);
+            Settings.System.putInt(getActivity().getContentResolver(),
+                    Settings.System.FLASHLIGHT_ON_CALL, val);
+            mFlashOnCall.setSummary(mFlashOnCall.getEntries()[index]);
+            return true;
+        }
         return false;
     }