Android Open Source - BatteryFu Time Picker Preference






From Project

Back to project page BatteryFu.

License

The source code is released under:

GNU General Public License

If you think the Android project BatteryFu 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

// Please note this must be the package if you want to use XML-based preferences
package android.preference;
 /*from  w  w w  .j  a  v a2  s .c  om*/
import java.text.Format;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TimePicker;
 
/**
 * A preference type that allows a user to choose a time
 */
public class TimePickerPreference extends DialogPreference implements
    TimePicker.OnTimeChangedListener {
  private int mHour = 0, mMinute = 0;

  /**
   * The validation expression for this preference
   */
  private static final String VALIDATION_EXPRESSION = "[0-2]*[0-9]:[0-5]*[0-9]";
 
  /**
   * The default value for this preference
   */
  private String defaultValue;
 
  /**
   * @param context
   * @param attrs
   */
  public TimePickerPreference(Context context, AttributeSet attrs) {
    super(context, attrs);
    initialize();
  }
 
  /**
   * @param context
   * @param attrs
   * @param defStyle
   */
  public TimePickerPreference(Context context, AttributeSet attrs,
      int defStyle) {
    super(context, attrs, defStyle);
    initialize();
  }
 
  /**
   * Initialize this preference
   */
  private void initialize() {
    setPersistent(true);
  }
 
  /*
   * (non-Javadoc)
   * 
   * @see android.preference.DialogPreference#onCreateDialogView()
   */
  @Override
  protected View onCreateDialogView() {
 
    TimePicker tp = new TimePicker(getContext());
    tp.setOnTimeChangedListener(this);
 
    int h = getHour();
    int m = getMinute();
    if (h >= 0 && m >= 0) {
      tp.setCurrentHour(h);
      tp.setCurrentMinute(m);
    }
 
    return tp;
  }
 
  /*
   * (non-Javadoc)
   * 
   * @see
   * android.widget.TimePicker.OnTimeChangedListener#onTimeChanged(android
   * .widget.TimePicker, int, int)
   */
  public void onTimeChanged(TimePicker view, int hour, int minute) {
    mHour = hour;
    mMinute = minute;
  }
  
  @Override
  public void onDialogClosed(boolean positiveResult) {
    if( positiveResult ) {
      if( isPersistent() ) {
        String result =  (mHour < 10 ? "0" + mHour : mHour) + ":" + (mMinute < 10 ? "0" + mMinute : mMinute);
        persistString(result);
        callChangeListener(result);
      }
    }
  }  
 
  /*
   * (non-Javadoc)
   * 
   * @see android.preference.Preference#setDefaultValue(java.lang.Object)
   */
  @Override
  public void setDefaultValue(Object defaultValue) {
    // BUG this method is never called if you use the 'android:defaultValue' attribute in your XML preference file, not sure why it isn't    
 
    super.setDefaultValue(defaultValue);
 
    if (!(defaultValue instanceof String)) {
      return;
    }
 
    if (!((String) defaultValue).matches(VALIDATION_EXPRESSION)) {
      return;
    }
 
    this.defaultValue = (String) defaultValue;
  }
 
  /**
   * Get the hour value (in 24 hour time)
   * 
   * @return The hour value, will be 0 to 23 (inclusive)
   */
  private int getHour() {
    String time = getPersistedString(this.defaultValue);
    if (time == null || !time.matches(VALIDATION_EXPRESSION)) {
      return -1;
    }
 
    return Integer.valueOf(time.split(":")[0]);
  }
 
  /**
   * Get the minute value
   * 
   * @return the minute value, will be 0 to 59 (inclusive)
   */
  private int getMinute() {
    String time = getPersistedString(this.defaultValue);
    if (time == null || !time.matches(VALIDATION_EXPRESSION)) {
      return -1;
    }
 
    return Integer.valueOf(time.split(":")[1]);
  }
}




Java Source Code List

android.preference.TimePickerPreference.java
com.koushikdutta.widgets.ActivityBaseFragment.java
com.koushikdutta.widgets.ActivityBase.java
com.koushikdutta.widgets.AnimatedView.java
com.koushikdutta.widgets.ListContentAdapter.java
com.koushikdutta.widgets.ListContentFragment.java
com.koushikdutta.widgets.ListItem.java
com.koushikdutta.widgets.SeparatedListAdapter.java
com.tobykurien.android.UtilsConstants.java
com.tobykurien.android.UtilsDebug.java
com.tobykurien.android.Utils.java
com.tobykurien.batteryfu.BattServiceInfo.java
com.tobykurien.batteryfu.BatteryFu.java
com.tobykurien.batteryfu.BatteryMinder.java
com.tobykurien.batteryfu.DataToggler.java
com.tobykurien.batteryfu.GeneralReceiver.java
com.tobykurien.batteryfu.MainFunctions.java
com.tobykurien.batteryfu.ModeSelect.java
com.tobykurien.batteryfu.ScreenService.java
com.tobykurien.batteryfu.Settings.java
com.tobykurien.batteryfu.ToggleWidget.java
com.tobykurien.batteryfu.compat.Api17.java
com.tobykurien.batteryfu.compat.Api3.java
com.tobykurien.batteryfu.data_switcher.APNDroidSwitcher.java
com.tobykurien.batteryfu.data_switcher.APNSwitcher.java
com.tobykurien.batteryfu.data_switcher.GingerbreadSwitcher.java
com.tobykurien.batteryfu.data_switcher.ICSSwitcher.java
com.tobykurien.batteryfu.data_switcher.MobileDataSwitcher.java