de.uni_koblenz_landau.apow.dialogs.TimeDialogFragment.java Source code

Java tutorial

Introduction

Here is the source code for de.uni_koblenz_landau.apow.dialogs.TimeDialogFragment.java

Source

/**
 * Apow - a mobile EHR Management System for low-resource environments
 * in developing countries, exemplified by rural Ghana
 * Copyright (C) 2014 Martin Landua
 *
 * 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 de.uni_koblenz_landau.apow.dialogs;

import java.util.Calendar;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.text.format.DateFormat;
import android.widget.TimePicker;

/**
 * Implements a Dialog to pick a time.
 * @author Martin Landua
 *
 */
public class TimeDialogFragment extends DialogFragment {

    private static Calendar mCalendar;
    private static DateDialogListener mListener;
    private static String mTarget;

    public static TimeDialogFragment newInstance(Calendar date) {
        TimeDialogFragment dialog = new TimeDialogFragment();

        mCalendar = Calendar.getInstance();
        mCalendar = date;
        return dialog;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        setRetainInstance(true);
        mTarget = this.getTag();
        return new TimePickerDialog(getActivity(), timeSetListener, mCalendar.get(Calendar.HOUR_OF_DAY),
                mCalendar.get(Calendar.MINUTE), DateFormat.is24HourFormat(getActivity()));
    }

    public void setListener(DateDialogListener listener) {
        mListener = listener;
    }

    private final TimePickerDialog.OnTimeSetListener timeSetListener = new TimePickerDialog.OnTimeSetListener() {

        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            mCalendar = Calendar.getInstance();
            mCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
            mCalendar.set(Calendar.MINUTE, minute);

            // Callback to listener
            mListener.dateSet(mCalendar, mTarget);
        }
    };

    @Override
    public void onDestroyView() {
        if (getDialog() != null && getRetainInstance()) {
            getDialog().setDismissMessage(null);
        }
        super.onDestroyView();
    }
}