Android Open Source - SpartanTimeLapseRecorder Seek Bar Preference






From Project

Back to project page SpartanTimeLapseRecorder.

License

The source code is released under:

GNU General Public License

If you think the Android project SpartanTimeLapseRecorder listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Spartan Time Lapse Recorder - Minimalistic android time lapse recording app
 * Copyright (C) 2014  Andreas Rohner/*from   w  ww.  j  ava  2  s.  co  m*/
 *
 * 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.
 *
 * 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 at.andreasrohner.spartantimelapserec.preference;

import android.content.Context;
import android.content.res.TypedArray;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;
import at.andreasrohner.spartantimelapserec.R;

public class SeekBarPreference extends DialogPreference implements
    SeekBar.OnSeekBarChangeListener {
  private static final String NAMESPACE = "http://schemas.android.com/apk/res/android";
  private static final int DEFAULT_VALUE = 0;

  private TextView mValueDisp;
  private SeekBar mSeekBar;
  private String mSuffix;
  private int mMaxValue, mMinValue, mValue = DEFAULT_VALUE;
  private OnFormatOutputValueListener mFormatListener;
  private float mLog;
  private int mStickyValue;

  public SeekBarPreference(Context context, AttributeSet attrs) {

    super(context, attrs);

    int resId = attrs.getAttributeResourceValue(null, "suffix", 0);
    if (resId == 0)
      mSuffix = attrs.getAttributeValue(null, "suffix");
    else
      mSuffix = context.getString(resId);

    mMaxValue = attrs.getAttributeIntValue(NAMESPACE, "max", 100);
    mMinValue = attrs.getAttributeIntValue(null, "min", 0);
    mLog = attrs.getAttributeFloatValue(null, "log", 0);
    mStickyValue = attrs.getAttributeIntValue(null, "stickyValue", 0);

    setDialogLayoutResource(R.layout.dialog_seekbar_preference);
  }

  public interface OnFormatOutputValueListener {
    String onFormatOutputValue(int value, String suffix);
  }

  public void setOnFormatOutputValueListener(
      OnFormatOutputValueListener listener) {
    mFormatListener = listener;
  }

  public void setMaxValue(int value) {
    mMaxValue = value;
    if (mSeekBar != null)
      mSeekBar.setMax(mMaxValue - mMinValue);
  }

  public void setMinValue(int value) {
    mMinValue = value;
  }

  private int getLogValue(int val) {
    if (mLog > 1)
      return (int) Math.round(((Math.log(val) / Math.log(mLog)) * 1000));
    return val;
  }

  private int getPowValue(int val) {
    if (mLog > 1) {
      val = (int) Math.round(Math.pow(mLog, ((double) val) / 1000));
      if (mStickyValue > 0) {
        int mod = val % mStickyValue;
        int area = 15 * mStickyValue / 100;
        if (val > mStickyValue) {
          if (mod < area || val > mStickyValue * 8)
            val -= mod;
          else if (mod > mStickyValue - area)
            val = val - mod + mStickyValue;
        }
      }
    }
    return val;
  }

  @Override
  protected void onBindDialogView(View v) {
    super.onBindDialogView(v);

    mValue = getPersistedInt(DEFAULT_VALUE);

    mValueDisp = (TextView) v
        .findViewById(R.id.dialog_seekbar_preference_value);

    mSeekBar = (SeekBar) v
        .findViewById(R.id.dialog_seekbar_preference_control);
    mSeekBar.setMax(getLogValue(mMaxValue - mMinValue));
    mSeekBar.setOnSeekBarChangeListener(this);
    mSeekBar.setProgress(getLogValue(mValue - mMinValue));
  }

  @Override
  protected void onDialogClosed(boolean positiveResult) {
    if (positiveResult) {
      persistInt(mValue);
    }
  }

  @Override
  protected void onSetInitialValue(boolean restorePersistedValue,
      Object defaultValue) {
    if (restorePersistedValue) {
      // Restore existing state
      mValue = getPersistedInt(DEFAULT_VALUE);
    } else {
      // Set default state from the XML attribute
      mValue = (Integer) defaultValue;
      persistInt(mValue);
    }
  }

  @Override
  protected Object onGetDefaultValue(TypedArray a, int index) {
    return a.getInteger(index, DEFAULT_VALUE);
  }

  @Override
  public void onProgressChanged(SeekBar seek, int value, boolean fromTouch) {
    value = getPowValue(value);
    value += mMinValue;
    mValue = value;

    String text;
    if (mFormatListener != null) {
      text = mFormatListener.onFormatOutputValue(value, mSuffix);
    } else {
      text = String.valueOf(value);
      if (mSuffix != null)
        text += " " + mSuffix;
    }
    if (mValueDisp != null)
      mValueDisp.setText(text);
  }

  @Override
  public void onStartTrackingTouch(SeekBar arg0) {
  }

  @Override
  public void onStopTrackingTouch(SeekBar arg0) {
  }
}




Java Source Code List

at.andreasrohner.spartantimelapserec.BackgroundService.java
at.andreasrohner.spartantimelapserec.DeviceStatusReceiver.java
at.andreasrohner.spartantimelapserec.MainActivity.java
at.andreasrohner.spartantimelapserec.PowerSavingReceiver.java
at.andreasrohner.spartantimelapserec.PreviewActivity.java
at.andreasrohner.spartantimelapserec.ScheduleReceiver.java
at.andreasrohner.spartantimelapserec.SettingsActivity.java
at.andreasrohner.spartantimelapserec.SettingsCommon.java
at.andreasrohner.spartantimelapserec.SettingsFragment.java
at.andreasrohner.spartantimelapserec.data.RecMode.java
at.andreasrohner.spartantimelapserec.data.RecSettings.java
at.andreasrohner.spartantimelapserec.preference.DateTimePreference.java
at.andreasrohner.spartantimelapserec.preference.IconArrayAdapter.java
at.andreasrohner.spartantimelapserec.preference.IconListPreference.java
at.andreasrohner.spartantimelapserec.preference.NoKBEditTextPreference.java
at.andreasrohner.spartantimelapserec.preference.SeekBarPreference.java
at.andreasrohner.spartantimelapserec.recorder.ImageRecorder.java
at.andreasrohner.spartantimelapserec.recorder.PowerSavingImageRecorder.java
at.andreasrohner.spartantimelapserec.recorder.Recorder.java
at.andreasrohner.spartantimelapserec.recorder.VideoRecorder.java
at.andreasrohner.spartantimelapserec.recorder.VideoTimeLapseRecorder.java
at.andreasrohner.spartantimelapserec.sensor.CameraSettings.java
at.andreasrohner.spartantimelapserec.sensor.MuteShutter.java
at.andreasrohner.spartantimelapserec.sensor.OrientationSensor.java