Settings: Allow choosing a custom vibration pattern [3/3]

Change-Id: I574da771ae90321f65d8ffddecd59db430105bb2
Signed-off-by: Pranav Vashi <neobuddy89@gmail.com>
This commit is contained in:
Ido Ben-Hur
2020-06-06 07:05:21 +03:00
committed by Joey
parent 9b3c789826
commit 5c1cea77b5
3 changed files with 115 additions and 6 deletions

View File

@@ -16,16 +16,20 @@
package com.android.settings.notification;
import android.content.ContentResolver;
import android.content.Context;
import android.provider.Settings;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settingslib.core.AbstractPreferenceController;
import org.evolution.settings.preferences.CustomSeekBarPreference;
/**
* This class allows choosing a vibration pattern while ringing
*/
@@ -33,8 +37,16 @@ public class VibrationPatternPreferenceController extends AbstractPreferenceCont
implements Preference.OnPreferenceChangeListener {
private static final String KEY_VIB_PATTERN = "vibration_pattern";
private static final String KEY_CUSTOM_VIB_CATEGORY = "custom_vibration_pattern";
private static final String KEY_CUSTOM_VIB1 = "custom_vibration_pattern1";
private static final String KEY_CUSTOM_VIB2 = "custom_vibration_pattern2";
private static final String KEY_CUSTOM_VIB3 = "custom_vibration_pattern3";
private ListPreference mVibPattern;
private PreferenceCategory mCustomVibCategory;
private CustomSeekBarPreference mCustomVib1;
private CustomSeekBarPreference mCustomVib2;
private CustomSeekBarPreference mCustomVib3;
public VibrationPatternPreferenceController(Context context) {
super(context);
@@ -59,14 +71,70 @@ public class VibrationPatternPreferenceController extends AbstractPreferenceCont
mVibPattern.setValueIndex(vibPattern);
mVibPattern.setSummary(mVibPattern.getEntries()[vibPattern]);
mVibPattern.setOnPreferenceChangeListener(this);
mCustomVibCategory = (PreferenceCategory) screen.findPreference(KEY_CUSTOM_VIB_CATEGORY);
mCustomVib1 = (CustomSeekBarPreference) screen.findPreference(KEY_CUSTOM_VIB1);
mCustomVib2 = (CustomSeekBarPreference) screen.findPreference(KEY_CUSTOM_VIB2);
mCustomVib3 = (CustomSeekBarPreference) screen.findPreference(KEY_CUSTOM_VIB3);
updateCustomVibVisibility(vibPattern == 5);
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
int vibPattern = Integer.valueOf((String) newValue);
Settings.System.putInt(mContext.getContentResolver(),
Settings.System.RINGTONE_VIBRATION_PATTERN, vibPattern);
mVibPattern.setSummary(mVibPattern.getEntries()[vibPattern]);
return true;
if (preference == mVibPattern) {
int vibPattern = Integer.valueOf((String) newValue);
Settings.System.putInt(mContext.getContentResolver(),
Settings.System.RINGTONE_VIBRATION_PATTERN, vibPattern);
mVibPattern.setSummary(mVibPattern.getEntries()[vibPattern]);
updateCustomVibVisibility(vibPattern == 5);
return true;
} else if (preference == mCustomVib1) {
updateCustomVib(0, (Integer) newValue);
return true;
} else if (preference == mCustomVib2) {
updateCustomVib(1, (Integer) newValue);
return true;
} else if (preference == mCustomVib3) {
updateCustomVib(2, (Integer) newValue);
return true;
}
return false;
}
private void updateCustomVibVisibility(boolean show) {
mCustomVibCategory.setVisible(show);
mCustomVib1.setVisible(show);
mCustomVib2.setVisible(show);
mCustomVib3.setVisible(show);
if (show) updateCustomVibPreferences();
}
private void updateCustomVibPreferences() {
String value = Settings.System.getString(mContext.getContentResolver(),
Settings.System.CUSTOM_RINGTONE_VIBRATION_PATTERN);
if (value != null) {
String[] customPattern = value.split(",", 3);
mCustomVib1.setValue(Integer.parseInt(customPattern[0]));
mCustomVib2.setValue(Integer.parseInt(customPattern[1]));
mCustomVib3.setValue(Integer.parseInt(customPattern[2]));
} else { // set default
mCustomVib1.setValue(0);
mCustomVib2.setValue(800);
mCustomVib3.setValue(800);
Settings.System.putString(mContext.getContentResolver(),
Settings.System.CUSTOM_RINGTONE_VIBRATION_PATTERN, "0,800,800");
}
mCustomVib1.setOnPreferenceChangeListener(this);
mCustomVib2.setOnPreferenceChangeListener(this);
mCustomVib3.setOnPreferenceChangeListener(this);
}
private void updateCustomVib(int index, int value) {
String[] customPattern = Settings.System.getString(mContext.getContentResolver(),
Settings.System.CUSTOM_RINGTONE_VIBRATION_PATTERN).split(",", 3);
customPattern[index] = String.valueOf(value);
Settings.System.putString(mContext.getContentResolver(),
Settings.System.CUSTOM_RINGTONE_VIBRATION_PATTERN, String.join(
",", customPattern[0], customPattern[1], customPattern[2]));
}
}