package com.samknows.measurement.activity.components;
import com.samknows.libcore.R;
import com.samknows.measurement.util.TimeUtils;
import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.preference.DialogPreference;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.LinearLayout;
public class SeekBarPreference extends DialogPreference implements
SeekBar.OnSeekBarChangeListener {
private static final String androidns = "http://schemas.android.com/apk/res/android";
private SeekBar mSeekBar;
private TextView mSplashText, mValueText;
private final Context mContext;
private int initialValue;
private final String mDialogMessage;
private final String mPrefix;
private final int mDefault;
private int mMax;
private int mValue = 0;
public SeekBarPreference(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
mDialogMessage = attrs.getAttributeValue(androidns, "dialogMessage");
mPrefix = context.getString(attrs.getAttributeResourceValue(androidns, "text",0));
mDefault = attrs.getAttributeIntValue(androidns, "defaultValue", 0);
mMax = attrs.getAttributeIntValue(androidns, "max", 100);
}
@Override
protected void onDialogClosed(boolean positiveResult) {
if (!positiveResult) {
mSeekBar.setProgress(initialValue);
callChangeListener(initialValue);
}
}
@Override
protected View onCreateDialogView() {
LinearLayout.LayoutParams params;
LinearLayout layout = new LinearLayout(mContext);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setPadding(6, 6, 6, 6);
mSplashText = new TextView(mContext);
if (mDialogMessage != null)
mSplashText.setText(mDialogMessage);
layout.addView(mSplashText);
mValueText = new TextView(mContext);
mValueText.setGravity(Gravity.CENTER_HORIZONTAL);
mValueText.setTextSize(32);
params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layout.addView(mValueText, params);
mSeekBar = new SeekBar(mContext);
mSeekBar.setOnSeekBarChangeListener(this);
Button btn_Reset = new Button(mContext);
btn_Reset.setText(mContext.getString(R.string.set_default));
btn_Reset.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mSeekBar.setProgress(mDefault - 1);
callChangeListener(mDefault - 1);
}
});
layout.addView(mSeekBar, new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
layout.addView(btn_Reset);
if (shouldPersist())
mValue = getPersistedInt(mDefault);
mValue--;
initialValue = mValue;
mSeekBar.setMax(mMax);
mSeekBar.setProgress(mValue);
return layout;
}
@Override
protected void onBindDialogView(View v) {
super.onBindDialogView(v);
mSeekBar.setMax(mMax);
mSeekBar.setProgress(mValue);
}
@Override
protected void onSetInitialValue(boolean restore, Object defaultValue) {
super.onSetInitialValue(restore, defaultValue);
if (restore)
mValue = shouldPersist() ? getPersistedInt(mDefault) : 0;
else
mValue = (Integer) defaultValue;
}
public void onProgressChanged(SeekBar seek, int value, boolean fromTouch) {
value++;
String t = TimeUtils.getDayOfMonthFrom1AsStringWithNoSuffix(value);
mSeekBar.setContentDescription(t);
mValueText.setText( mPrefix == null ? t: mPrefix.concat(t));
if (shouldPersist())
persistInt(value);
callChangeListener(value);
}
public void onStartTrackingTouch(SeekBar seek) {
}
public void onStopTrackingTouch(SeekBar seek) {
}
public void setMax(int max) {
mMax = max;
}
public int getMax() {
return mMax;
}
public void setProgress(int progress) {
mValue = progress;
if (mSeekBar != null)
mSeekBar.setProgress(progress);
}
public int getProgress() {
return mValue;
}
}