Android Open Source - MovisensGattSensorExample Time Preference






From Project

Back to project page MovisensGattSensorExample.

License

The source code is released under:

GNU General Public License

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

package com.movisens.gattsensorexample.activities;
//from  www .j a  v  a  2  s .  c  o m
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

import android.R.string;
import android.content.Context;
import android.content.res.TypedArray;
import android.preference.DialogPreference;
import android.text.format.DateFormat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TimePicker;

public class TimePreference extends DialogPreference {
  private Calendar calendar;
  private TimePicker picker = null;
  private SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm",
      Locale.US);

  public TimePreference(Context ctxt, AttributeSet attrs) {
    super(ctxt, attrs);

    setPositiveButtonText(string.ok);
    setNegativeButtonText(string.cancel);
    calendar = new GregorianCalendar();
  }

  @Override
  protected View onCreateDialogView() {
    picker = new TimePicker(getContext());

    return (picker);
  }

  @Override
  protected void onBindDialogView(View v) {
    super.onBindDialogView(v);
    picker.setIs24HourView(DateFormat.is24HourFormat(getContext()));
    picker.setCurrentHour(calendar.get(Calendar.HOUR_OF_DAY));
    picker.setCurrentMinute(calendar.get(Calendar.MINUTE));
  }

  @Override
  protected void onDialogClosed(boolean positiveResult) {
    super.onDialogClosed(positiveResult);

    if (positiveResult) {
      calendar.set(Calendar.HOUR_OF_DAY, picker.getCurrentHour());
      calendar.set(Calendar.MINUTE, picker.getCurrentMinute());

      CharSequence summary = getSummary();
      setSummary(summary);
      if (callChangeListener(summary)) {
        persistString(summary.toString());
        notifyChanged();
      }
    }
  }

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

  @Override
  protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
    String time = null;

    if (restoreValue) {
      if (defaultValue == null) {
        time = getPersistedString("00:00");
      } else {
        time = getPersistedString(defaultValue.toString());
      }
    } else {
      time = defaultValue.toString();
    }

    try {
      calendar.setTime(timeFormat.parse(time));
    } catch (ParseException e) {
      e.printStackTrace();
    }
  }

  @Override
  public CharSequence getSummary() {
    if (calendar == null) {
      return null;
    }
    return DateFormat.getTimeFormat(getContext()).format(
        new Date(calendar.getTimeInMillis()));
  }
}




Java Source Code List

com.movisens.gattsensorexample.activities.DeviceScanActivity.java
com.movisens.gattsensorexample.activities.Preferences.java
com.movisens.gattsensorexample.activities.SensorConnected.java
com.movisens.gattsensorexample.activities.SensorDisconnected.java
com.movisens.gattsensorexample.activities.StartActivity.java
com.movisens.gattsensorexample.activities.TimePreference.java
com.movisens.gattsensorexample.application.App.java
com.movisens.gattsensorexample.events.BLEEvent.java
com.movisens.gattsensorexample.events.MeasurementStatus.java
com.movisens.gattsensorexample.events.SensorStatusEvent.java
com.movisens.gattsensorexample.model.CurrentSensorData.java
com.movisens.gattsensorexample.receivers.SystemStateReceiver.java
com.movisens.gattsensorexample.receivers.UpdateAppReceiver.java
com.movisens.gattsensorexample.sensors.MovisensSensor.java
com.movisens.gattsensorexample.services.BluetoothLeService.java
com.movisens.gattsensorexample.services.SamplingService.java
com.movisens.gattsensorexample.utils.BleQueue.java
com.movisens.gattsensorexample.utils.BleUtils.java