package com.leorz.smartringtone.config;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Typeface;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import com.leorz.smartringtone.R;
import com.leorz.smartringtone.Setting;
public class SeekBarPref2 extends Preference implements OnSeekBarChangeListener {
SeekBar bar;
private String unitString;
private Context context;
public static int maximum = 60;
public static int interval = 1;
private float oldValue = 10;
private TextView monitorBox;
public SeekBarPref2(Context context) {
super(context);
this.context = context;
}
public SeekBarPref2(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
public SeekBarPref2(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
}
protected void onBindView(View view) {
// TODO Auto-generated method stub
unitString = Setting.unitString;
layout = (LinearLayout) view.findViewById(R.id.Layoutseekbar);
TextView textview = (TextView) view.findViewById(R.id.TextView01);
textview.setText(getTitle());
textview.setTextSize(16);
textview.setTextColor(Color.WHITE);
textview.setTypeface(Typeface.SANS_SERIF);
bar = (SeekBar) view.findViewById(R.id.SeekBar01);
bar.setMax(maximum);
String key = getKey().toString();
System.out.println("OOOOOO###" + key);
bar.setProgress(Integer.valueOf(getPersistedString(String
.valueOf((int) this.oldValue))));
System.out.println("O****O###"
+ getPersistedString(String.valueOf((int) this.oldValue)));
bar.setOnSeekBarChangeListener(this);
monitorBox = (TextView) view.findViewById(R.id.monitorBox);
monitorBox.setTextColor(Color.WHITE);
monitorBox.setTypeface(Typeface.MONOSPACE);
monitorBox.setText(Integer.valueOf(getPersistedString(String
.valueOf((int) this.oldValue))) + unitString);
System.out.println("O****O###"
+ getPersistedString(String.valueOf((int) this.oldValue)));
monitorBox.setTextSize(20);
super.onBindView(view);
}
LinearLayout layout;
protected View onCreateView(ViewGroup parent) {
unitString = Setting.unitString;
View v = LayoutInflater.from(SeekBarPref2.this.context).inflate(
R.layout.seekbar2, parent, false);
return v;
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
if (!fromUser)
return;
// progress = Math.round(((float) progress) / interval) * interval;
// if (progress < 10) // 10 SeekBar10 10
//
// {
//
// progress = 10;
// } else if (progress > 72) // 72 SeekBar72
// // SeekBar()
//
// {
// progress = 72;
// }
if (!callChangeListener(progress)) {
seekBar.setProgress(this.bar.getProgress());
return;
}
// seekBar.setProgress(progress);
oldValue = progress;
monitorBox.setText(this.bar.getProgress() + unitString);
// this.monitorBox.setTextSize(progress); // monitorBox
// SeekBar
// updatePreference(progress);
}
public void notifyChanged() {
super.notifyChanged();
// monitorBox.setText(bar.getProgress() + unitString);
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onStopTrackingTouch(SeekBar seekBar) {
updatePreference(seekBar.getProgress());
notifyChanged();
}
protected Object onGetDefaultValue(TypedArray ta, int index) {
int dValue = (int) ta.getInt(index, 10);
return validateValue(dValue);
}
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
int temp = restoreValue ? Integer.valueOf(getPersistedString("10"))
: Integer.valueOf(defaultValue.toString());
if (!restoreValue)
this.persistString(String.valueOf(temp));
this.oldValue = temp;
}
private int validateValue(int value) {
// if (value > 72)
// value = 72;
//
// else if (value < 10)
// value = 10;
// if (value % interval != 0)
// value = Math.round(((float) value) / interval) * interval;
return value;
}
private void updatePreference(int newValue) {
SharedPreferences.Editor editor = getEditor();
editor.putString(getKey(), String.valueOf(newValue));
editor.commit();
// notifyChanged();
}
}
|