Android Open Source - androidphotobackup Time Preference






From Project

Back to project page androidphotobackup.

License

The source code is released under:

Apache License

If you think the Android project androidphotobackup 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.brightsilence.dev.androidphotobackup;
//ww w .j a v  a  2  s.  co  m
// From http://stackoverflow.com/questions/5533078/timepicker-in-preferencescreen

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;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class TimePreference extends DialogPreference {
    private Calendar calendar;
    private TimePicker picker = null;

    public TimePreference(Context ctxt) {
        this(ctxt, null);
    }

    public TimePreference(Context ctxt, AttributeSet attrs) {
        this(ctxt, attrs, android.R.attr.dialogPreferenceStyle);
    }

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

        setPositiveButtonText(R.string.pref_string_set);
        setNegativeButtonText(R.string.pref_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.setCurrentHour(calendar.get(Calendar.HOUR_OF_DAY));
        picker.setCurrentMinute(calendar.get(Calendar.MINUTE));
    }

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

        if (positiveResult) {
            // Update to the current day - as we're only interested in the
            //  hour and minute components, this stops the actual calendar day
            //  becoming somewhat historic
            //  i.e. if the preference was 1st modified on the 1st June 2014,
            //  without this update it would forever refer to that day, even
            //  when the hour and minute components were changed.
            calendar.setTimeInMillis(System.currentTimeMillis());

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

            setSummary(getSummary());
            if (callChangeListener(calendar.getTimeInMillis())) {
                persistLong(calendar.getTimeInMillis());
                notifyChanged();
            }
        }
    }

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

    @Override
    protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {

        if (restoreValue) {
            if (defaultValue == null) {
                calendar.setTimeInMillis(getPersistedLong(System.currentTimeMillis()));
            } else {
                calendar.setTimeInMillis(Long.parseLong(getPersistedString((String) defaultValue)));
            }
        } else {
            if (defaultValue == null) {
                calendar.setTimeInMillis(System.currentTimeMillis());
            } else {
                calendar.setTimeInMillis(Long.parseLong((String) defaultValue));
            }
        }
        setSummary(getSummary());
    }

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




Java Source Code List

com.brightsilence.dev.androidphotobackup.ApplicationTest.java
com.brightsilence.dev.androidphotobackup.DropBoxWrapper.java
com.brightsilence.dev.androidphotobackup.PhotoBackupAlarmReceiver.java
com.brightsilence.dev.androidphotobackup.PhotoBackupServiceStarter.java
com.brightsilence.dev.androidphotobackup.PhotoBackupService.java
com.brightsilence.dev.androidphotobackup.PhotoBackupSettingsActivity.java
com.brightsilence.dev.androidphotobackup.TimePreference.java
com.brightsilence.dev.androidphotobackup.ZipInputStream.java