LineageParts: Add reset button to ApplicationLightPreference

*) Add a reset button to ApplicationLightPreference dialog.

*) Button is not shown by default but can be enabled by
   calling setDefaultValues(color, onMs, offMs) on an
   instance of the preference.

*) Enable reset capability for each battery light and the
   default notification, missed call and voicemail lights.

Change-Id: Iab1dcad048de838bb9015c62a4171b002141f09a
diff --git a/src/org/lineageos/lineageparts/notificationlight/ApplicationLightPreference.java b/src/org/lineageos/lineageparts/notificationlight/ApplicationLightPreference.java
index 7d25360..ec4cf57 100644
--- a/src/org/lineageos/lineageparts/notificationlight/ApplicationLightPreference.java
+++ b/src/org/lineageos/lineageparts/notificationlight/ApplicationLightPreference.java
@@ -50,6 +50,11 @@
     private int mOffValue;
     private boolean mOnOffChangeable;
 
+    private boolean mHasDefaults;
+    private int mDefaultColorValue;
+    private int mDefaultOnValue;
+    private int mDefaultOffValue;
+
     private LightSettingsDialog mDialog;
 
     public interface ItemLongClickListener {
@@ -94,6 +99,7 @@
         mOnValue = onValue;
         mOffValue = offValue;
         mOnOffChangeable = onOffChangeable;
+        mHasDefaults = false;
 
         setWidgetLayoutResource(R.layout.preference_application_light);
     }
@@ -169,6 +175,20 @@
     }
 
     @Override
+    protected boolean onDismissDialog(LightSettingsDialog dialog, int which) {
+        if (which == DialogInterface.BUTTON_NEUTRAL) {
+            // Reset to previously supplied defaults
+            mDialog.setColor(mDefaultColorValue);
+            if (mOnOffChangeable) {
+                mDialog.setPulseSpeedOn(mDefaultOnValue);
+                mDialog.setPulseSpeedOff(mDefaultOffValue);
+            }
+            return false;
+        }
+        return true;
+    }
+
+    @Override
     protected void onDialogClosed(boolean positiveResult) {
         if (positiveResult) {
             mColorValue = mDialog.getColor() & 0x00FFFFFF; // strip alpha, led does not support it
@@ -194,6 +214,11 @@
         mDialog.setButton(AlertDialog.BUTTON_NEGATIVE,
                 getContext().getResources().getString(R.string.cancel),
                 (DialogInterface.OnClickListener) null);
+        if (mHasDefaults) {
+            mDialog.setButton(AlertDialog.BUTTON_NEUTRAL,
+                    getContext().getResources().getString(R.string.reset),
+                    (DialogInterface.OnClickListener) null);
+        }
 
         return mDialog;
     }
@@ -254,6 +279,13 @@
         mOnOffChangeable = value;
     }
 
+    public void setDefaultValues(int color, int onValue, int offValue) {
+        mDefaultColorValue = color;
+        mDefaultOnValue = onValue;
+        mDefaultOffValue = offValue;
+        mHasDefaults = true;
+    }
+
     /**
      * Utility methods
      */
diff --git a/src/org/lineageos/lineageparts/notificationlight/BatteryLightSettings.java b/src/org/lineageos/lineageparts/notificationlight/BatteryLightSettings.java
index 3281229..5d4397c 100644
--- a/src/org/lineageos/lineageparts/notificationlight/BatteryLightSettings.java
+++ b/src/org/lineageos/lineageparts/notificationlight/BatteryLightSettings.java
@@ -50,6 +50,9 @@
     private ApplicationLightPreference mFullColorPref;
     private LineageSystemSettingSwitchPreference mLightEnabledPref;
     private LineageSystemSettingSwitchPreference mPulseEnabledPref;
+    private int mDefaultLowColor;
+    private int mDefaultMediumColor;
+    private int mDefaultFullColor;
 
     private static final int MENU_RESET = Menu.FIRST;
 
@@ -58,6 +61,7 @@
         super.onActivityCreated(savedInstanceState);
 
         final Context context = getContext();
+        final Resources res = getResources();
 
         addPreferencesFromResource(R.xml.battery_light_settings);
         getActivity().getActionBar().setTitle(R.string.battery_light_title);
@@ -79,15 +83,25 @@
         if (LightsCapabilities.supports(context, LightsCapabilities.LIGHTS_RGB_BATTERY_LED)) {
             setHasOptionsMenu(true);
 
+            mDefaultLowColor = res.getInteger(
+                    com.android.internal.R.integer.config_notificationsBatteryLowARGB);
+            mDefaultMediumColor = res.getInteger(
+                    com.android.internal.R.integer.config_notificationsBatteryMediumARGB);
+            mDefaultFullColor = res.getInteger(
+                    com.android.internal.R.integer.config_notificationsBatteryFullARGB);
+
             // Low, Medium and full color preferences
             mLowColorPref = (ApplicationLightPreference) prefSet.findPreference(LOW_COLOR_PREF);
             mLowColorPref.setOnPreferenceChangeListener(this);
+            mLowColorPref.setDefaultValues(mDefaultLowColor, 0, 0);
 
             mMediumColorPref = (ApplicationLightPreference) prefSet.findPreference(MEDIUM_COLOR_PREF);
             mMediumColorPref.setOnPreferenceChangeListener(this);
+            mMediumColorPref.setDefaultValues(mDefaultMediumColor, 0, 0);
 
             mFullColorPref = (ApplicationLightPreference) prefSet.findPreference(FULL_COLOR_PREF);
             mFullColorPref.setOnPreferenceChangeListener(this);
+            mFullColorPref.setDefaultValues(mDefaultFullColor, 0, 0);
         } else {
             prefSet.removePreference(prefSet.findPreference("colors_list"));
             resetColors();
@@ -99,10 +113,10 @@
     @Override
     public void onResume() {
         super.onResume();
-        refreshDefault();
+        refreshColors();
     }
 
-    private void refreshDefault() {
+    private void refreshColors() {
         ContentResolver resolver = getActivity().getContentResolver();
         Resources res = getResources();
 
@@ -166,16 +180,15 @@
 
     protected void resetColors() {
         ContentResolver resolver = getActivity().getContentResolver();
-        Resources res = getResources();
 
         // Reset to the framework default colors
         LineageSettings.System.putInt(resolver, LineageSettings.System.BATTERY_LIGHT_LOW_COLOR,
-                res.getInteger(com.android.internal.R.integer.config_notificationsBatteryLowARGB));
+                mDefaultLowColor);
         LineageSettings.System.putInt(resolver, LineageSettings.System.BATTERY_LIGHT_MEDIUM_COLOR,
-                res.getInteger(com.android.internal.R.integer.config_notificationsBatteryMediumARGB));
+                mDefaultMediumColor);
         LineageSettings.System.putInt(resolver, LineageSettings.System.BATTERY_LIGHT_FULL_COLOR,
-                res.getInteger(com.android.internal.R.integer.config_notificationsBatteryFullARGB));
-        refreshDefault();
+                mDefaultFullColor);
+        refreshColors();
     }
 
     protected void resetToDefaults() {
diff --git a/src/org/lineageos/lineageparts/notificationlight/LightSettingsDialog.java b/src/org/lineageos/lineageparts/notificationlight/LightSettingsDialog.java
index 77ab5ae..18d029e 100644
--- a/src/org/lineageos/lineageparts/notificationlight/LightSettingsDialog.java
+++ b/src/org/lineageos/lineageparts/notificationlight/LightSettingsDialog.java
@@ -65,6 +65,8 @@
 
     private EditText mHexColorInput;
     private ColorPanelView mNewColor;
+    private PulseSpeedAdapter mPulseSpeedAdapterOn;
+    private PulseSpeedAdapter mPulseSpeedAdapterOff;
     private Spinner mPulseSpeedOn;
     private Spinner mPulseSpeedOff;
     private LayoutInflater mInflater;
@@ -147,19 +149,19 @@
         mHexColorInput.setOnFocusChangeListener(this);
 
         if (onOffChangeable) {
-            PulseSpeedAdapter pulseSpeedAdapter = new PulseSpeedAdapter(
+            mPulseSpeedAdapterOn = new PulseSpeedAdapter(
                     R.array.notification_pulse_length_entries,
                     R.array.notification_pulse_length_values,
                     speedOn);
-            mPulseSpeedOn.setAdapter(pulseSpeedAdapter);
-            mPulseSpeedOn.setSelection(pulseSpeedAdapter.getTimePosition(speedOn));
+            mPulseSpeedOn.setAdapter(mPulseSpeedAdapterOn);
+            mPulseSpeedOn.setSelection(mPulseSpeedAdapterOn.getTimePosition(speedOn));
             mPulseSpeedOn.setOnItemSelectedListener(mPulseSelectionListener);
 
-            pulseSpeedAdapter = new PulseSpeedAdapter(R.array.notification_pulse_speed_entries,
+            mPulseSpeedAdapterOff = new PulseSpeedAdapter(R.array.notification_pulse_speed_entries,
                     R.array.notification_pulse_speed_values,
                     speedOff);
-            mPulseSpeedOff.setAdapter(pulseSpeedAdapter);
-            mPulseSpeedOff.setSelection(pulseSpeedAdapter.getTimePosition(speedOff));
+            mPulseSpeedOff.setAdapter(mPulseSpeedAdapterOff);
+            mPulseSpeedOff.setSelection(mPulseSpeedAdapterOff.getTimePosition(speedOff));
             mPulseSpeedOff.setOnItemSelectedListener(mPulseSelectionListener);
         } else {
             View speedSettingsGroup = layout.findViewById(R.id.speed_title_view);
@@ -248,6 +250,10 @@
         return mColorPicker.getColor();
     }
 
+    public void setColor(int color) {
+        mColorPicker.setColor(color, true);
+    }
+
     @SuppressWarnings("unchecked")
     public int getPulseSpeedOn() {
         if (mPulseSpeedOn.isEnabled()) {
@@ -257,12 +263,20 @@
         }
     }
 
+    public void setPulseSpeedOn(int speedOn) {
+        mPulseSpeedOn.setSelection(mPulseSpeedAdapterOn.getTimePosition(speedOn));
+    }
+
     @SuppressWarnings("unchecked")
     public int getPulseSpeedOff() {
         // return 0 if 'Always on' is selected
         return getPulseSpeedOn() == 1 ? 0 : ((Pair<String, Integer>) mPulseSpeedOff.getSelectedItem()).second;
     }
 
+    public void setPulseSpeedOff(int speedOff) {
+        mPulseSpeedOff.setSelection(mPulseSpeedAdapterOff.getTimePosition(speedOff));
+    }
+
     private Handler mLedHandler = new Handler() {
         public void handleMessage(Message msg) {
             updateLed();
diff --git a/src/org/lineageos/lineageparts/notificationlight/NotificationLightSettings.java b/src/org/lineageos/lineageparts/notificationlight/NotificationLightSettings.java
index 2d444e1..8b427b2 100644
--- a/src/org/lineageos/lineageparts/notificationlight/NotificationLightSettings.java
+++ b/src/org/lineageos/lineageparts/notificationlight/NotificationLightSettings.java
@@ -145,6 +145,7 @@
         } else {
             mCustomEnabledPref.setOnPreferenceChangeListener(this);
             mDefaultPref.setOnPreferenceChangeListener(this);
+            mDefaultPref.setDefaultValues(mDefaultColor, mDefaultLedOn, mDefaultLedOff);
         }
 
         // Missed call and Voicemail preferences should only show on devices with a voice capabilities
@@ -155,9 +156,11 @@
         } else {
             mCallPref = (ApplicationLightPreference) findPreference(MISSED_CALL_PREF);
             mCallPref.setOnPreferenceChangeListener(this);
+            mCallPref.setDefaultValues(mDefaultColor, mDefaultLedOn, mDefaultLedOff);
 
             mVoicemailPref = (ApplicationLightPreference) findPreference(VOICEMAIL_PREF);
             mVoicemailPref.setOnPreferenceChangeListener(this);
+            mVoicemailPref.setDefaultValues(mDefaultColor, mDefaultLedOn, mDefaultLedOff);
         }
 
         if (!mLedCanPulse && !mMultiColorLed) {