session-android/src/org/thoughtcrime/securesms/preferences/LedBlinkPatternListPreference.java

163 lines
5.5 KiB
Java
Raw Normal View History

/**
2011-12-20 18:20:44 +00:00
* Copyright (C) 2011 Whisper Systems
*
2011-12-20 18:20:44 +00:00
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
2011-12-20 18:20:44 +00:00
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.thoughtcrime.securesms.preferences;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Parcelable;
import android.preference.ListPreference;
import android.preference.PreferenceManager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
2011-12-20 18:20:44 +00:00
import android.widget.TextView;
import android.widget.Toast;
import org.thoughtcrime.securesms.ApplicationPreferencesActivity;
import org.thoughtcrime.securesms.R;
2011-12-20 18:20:44 +00:00
/**
* List preference for LED blink pattern notification.
*
2011-12-20 18:20:44 +00:00
* @author Moxie Marlinspike
*/
public class LedBlinkPatternListPreference extends ListPreference implements OnSeekBarChangeListener {
2011-12-20 18:20:44 +00:00
private Context context;
private SeekBar seekBarOn;
private SeekBar seekBarOff;
private TextView seekBarOnLabel;
private TextView seekBarOffLabel;
2011-12-20 18:20:44 +00:00
private boolean dialogInProgress;
public LedBlinkPatternListPreference(Context context) {
super(context);
this.context = context;
}
public LedBlinkPatternListPreference(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
@Override
protected void onDialogClosed(boolean positiveResult) {
2011-12-20 18:20:44 +00:00
super.onDialogClosed(positiveResult);
2011-12-20 18:20:44 +00:00
if (positiveResult) {
String blinkPattern = PreferenceManager.getDefaultSharedPreferences(context).getString(ApplicationPreferencesActivity.LED_BLINK_PREF, "500,2000");
if (blinkPattern.equals("custom")) showDialog();
}
}
private void initializeSeekBarValues() {
String patternString = PreferenceManager.getDefaultSharedPreferences(context).getString(ApplicationPreferencesActivity.LED_BLINK_PREF_CUSTOM, "500,2000");
String[] patternArray = patternString.split(",");
seekBarOn.setProgress(Integer.parseInt(patternArray[0]));
seekBarOff.setProgress(Integer.parseInt(patternArray[1]));
}
2011-12-20 18:20:44 +00:00
private void initializeDialog(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setIcon(android.R.drawable.ic_dialog_info);
builder.setTitle("Set Custom LED Blink Pattern");
builder.setView(view);
builder.setOnCancelListener(new CustomDialogCancelListener());
builder.setNegativeButton(android.R.string.cancel, new CustomDialogCancelListener());
builder.setPositiveButton(android.R.string.ok, new CustomDialogClickListener());
builder.show();
}
2011-12-20 18:20:44 +00:00
private void showDialog() {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.led_pattern_dialog, null);
this.seekBarOn = (SeekBar)view.findViewById(R.id.SeekBarOn);
this.seekBarOff = (SeekBar)view.findViewById(R.id.SeekBarOff);
this.seekBarOnLabel = (TextView)view.findViewById(R.id.SeekBarOnMsLabel);
this.seekBarOffLabel = (TextView)view.findViewById(R.id.SeekBarOffMsLabel);
this.seekBarOn.setOnSeekBarChangeListener(this);
this.seekBarOff.setOnSeekBarChangeListener(this);
initializeSeekBarValues();
initializeDialog(view);
dialogInProgress = true;
2011-12-20 18:20:44 +00:00
dialogInProgress = true;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
2011-12-20 18:20:44 +00:00
super.onRestoreInstanceState(state);
if (dialogInProgress) {
showDialog();
}
}
@Override
protected View onCreateDialogView() {
2011-12-20 18:20:44 +00:00
dialogInProgress = false;
return super.onCreateDialogView();
}
public void onProgressChanged(SeekBar seekbar, int progress, boolean fromTouch) {
if (seekbar.equals(seekBarOn)) {
seekBarOnLabel.setText(Integer.toString(progress));
} else if (seekbar.equals(seekBarOff)) {
seekBarOffLabel.setText(Integer.toString(progress));
}
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onStopTrackingTouch(SeekBar seekBar) {
}
private class CustomDialogCancelListener implements DialogInterface.OnClickListener, DialogInterface.OnCancelListener {
public void onClick(DialogInterface dialog, int which) {
dialogInProgress = false;
2011-12-20 18:20:44 +00:00
}
public void onCancel(DialogInterface dialog) {
dialogInProgress = false;
}
2011-12-20 18:20:44 +00:00
}
2011-12-20 18:20:44 +00:00
private class CustomDialogClickListener implements DialogInterface.OnClickListener {
public void onClick(DialogInterface dialog, int which) {
String pattern = seekBarOnLabel.getText() + "," + seekBarOffLabel.getText();
dialogInProgress = false;
2011-12-20 18:20:44 +00:00
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
preferences.edit().putString(ApplicationPreferencesActivity.LED_BLINK_PREF_CUSTOM, pattern).commit();
Toast.makeText(context, "Custom LED blink pattern set!", Toast.LENGTH_LONG).show();
}
2011-12-20 18:20:44 +00:00
}
2011-12-20 18:20:44 +00:00
}