123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- package de.tudarmstadt.informatik.hostage.ui2.dialog;
- import android.annotation.SuppressLint;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.AlertDialog.Builder;
- import android.app.Dialog;
- import android.app.DialogFragment;
- import android.content.DialogInterface;
- import android.content.res.Configuration;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.DatePicker;
- import android.widget.DatePicker.OnDateChangedListener;
- import android.widget.TimePicker;
- import android.widget.TimePicker.OnTimeChangedListener;
- import java.util.Calendar;
- import de.tudarmstadt.informatik.hostage.R;
- import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
- /**
- * Created by Julien on 16.02.14.
- */
- @SuppressLint("ValidFragment")
- public class DateTimeDialogFragment extends DialogFragment implements OnDateChangedListener, OnTimeChangedListener {
- // Define constants for date-time picker.
- public final static int DATE_PICKER = 1;
- public final static int TIME_PICKER = 2;
- public final static int DATE_TIME_PICKER = 3;
- // DatePicker reference
- public DatePicker datePicker;
- // TimePicker reference
- public TimePicker timePicker;
- // Calendar reference
- private Calendar mCalendar;
- // Define activity
- private Activity activity;
- // Define Dialog type
- private int DialogType;
- // Define Dialog view
- private View mView;
- // Constructor start
- public DateTimeDialogFragment(Activity activity) {
- this(activity, DATE_TIME_PICKER);
- }
- /**
- * Constructor
- * @param activity the activity
- * @param DialogType, what kind of dialog it is (TIME_PICKER, DATE_PICKER, DATE_TIME_PICKER)
- */
- public DateTimeDialogFragment(Activity activity, int DialogType) {
- this.activity = activity;
- this.DialogType = DialogType;
- LayoutInflater inflater = activity.getLayoutInflater();
- mView = inflater.inflate(R.layout.date_time_dialog, null);
- this.setupRootView(mView);
- }
- @Override
- public void onConfigurationChanged(Configuration newConfig){
- super.onConfigurationChanged(newConfig);
- LayoutInflater inflater = LayoutInflater.from(this.activity);
- ViewGroup container = (ViewGroup) this.mView.getParent();
- container.removeView(this.mView);
- mView = inflater.inflate(R.layout.date_time_dialog, null);
- container.addView(mView);
- this.setupRootView(mView);
- }
- /**
- * Configure the root view in here.
- * @param mView, root view
- */
- private void setupRootView(View mView){
- mCalendar = Calendar.getInstance();
- datePicker = (DatePicker) mView.findViewById(R.id.DatePicker);
- datePicker.init(mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH), this);
- timePicker = (TimePicker) mView.findViewById(R.id.TimePicker);
- setIs24HourView(true);
- setCalendarViewShown(false);
- switch (DialogType) {
- case DATE_PICKER:
- timePicker.setVisibility(View.GONE);
- break;
- case TIME_PICKER:
- datePicker.setVisibility(View.GONE);
- break;
- }
- }
- /**
- * Set the current date.
- * @param timeInMillis, date in milliseconds
- */
- public void setDate(long timeInMillis){
- Calendar calendar = Calendar.getInstance();
- calendar.setTimeInMillis (timeInMillis);
- int year = calendar.get(Calendar.YEAR) ;
- int month = calendar.get(Calendar.MONTH);
- int day = calendar.get(Calendar.DATE);
- int hour = calendar.get(Calendar.HOUR);
- int min = calendar.get(Calendar.MINUTE);
- datePicker.updateDate(year, month, day);
- timePicker.setCurrentHour(hour);
- timePicker.setCurrentMinute(min);
- }
- /**
- * Returns the current selected date.
- * @return long, date in milliseconds
- */
- public long getDate(){
-
- int day = datePicker.getDayOfMonth();
- int month = datePicker.getMonth();
- int year = datePicker.getYear();
-
- int hourOfDay = timePicker.getCurrentHour();
- int minute = timePicker.getCurrentMinute();
- Calendar calendar = Calendar.getInstance();
- calendar.set(year, month, day, hourOfDay, minute);
- return calendar.getTime().getTime();
- }
- /**
- * The listener which will be called if the user tapped "cancel" or "ok"
- */
- public interface DateTimeDialogFragmentListener {
- /**
- * Called if the user tapped "ok"
- * @param dialog {@link DateTimeDialogFragment DateTimeDialogFragment}
- */
- public void onDateTimePickerPositiveClick(DateTimeDialogFragment dialog);
- /**
- * Called if the user tapped "cancel"
- * @param dialog {@link DateTimeDialogFragment DateTimeDialogFragment}
- */
- public void onDateTimePickerNegativeClick(DateTimeDialogFragment dialog);
- }
- private DateTimeDialogFragmentListener mListener;
- /**
- * Set the user interaction listener.
- * @param listener DateTimeDialogFragmentListener
- */
- public void setDateChangeListener(DateTimeDialogFragmentListener listener){
- this.mListener = listener;
- }
- @Override
- public void onAttach(Activity activity) {
- super.onAttach(activity);
- try {
- if (this.mListener == null){
- if (activity.getClass().equals(MainActivity.class)){
- mListener = (DateTimeDialogFragmentListener) (((MainActivity)activity).getDisplayedFragment());
- } else {
- mListener = (DateTimeDialogFragmentListener) activity;
- }
- }
- } catch (ClassCastException e) {
- throw new ClassCastException(activity.toString()
- + " must implement DateTimeDialogListener");
- }
- }
- @Override
- public Dialog onCreateDialog(Bundle savedInstanceState) {
- Builder builder = new AlertDialog.Builder(activity);
- builder.setView(mView);
- builder.setMessage(activity.getString(R.string.rec_set_date))
- .setPositiveButton(activity.getString(R.string.rec_set),
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- mListener
- .onDateTimePickerPositiveClick(DateTimeDialogFragment.this);
- }
- })
- .setNegativeButton(activity.getString(R.string.rec_cancel),
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- mListener
- .onDateTimePickerNegativeClick(DateTimeDialogFragment.this);
- DateTimeDialogFragment.this.getDialog().cancel();
- }
- });
- return builder.create();
- }
- @Override
- public void onActivityCreated(Bundle savedInstanceState) {
- super.onActivityCreated(savedInstanceState);
- timePicker.setOnTimeChangedListener(this);
- }
- /**
- * Returns the value of the given field after computing the field values by calling complete() first.
- * @param field int
- * @return int
- */
- public int get(final int field) {
- return mCalendar.get(field);
- }
- /**
- * Set the time picker style.
- * @param is24HourView
- */
- public void setIs24HourView(boolean is24HourView) {
- timePicker.setIs24HourView(is24HourView);
- }
- /**
- * Show / hide the calendar view of the DatePicker.
- * @param calendarView boolean
- */
- public void setCalendarViewShown(boolean calendarView) {
- datePicker.setCalendarViewShown(calendarView);
- }
- /**
- * Handles date change event.
- * @param view DatePicker
- * @param year changed year
- * @param monthOfYear changed month of Year
- * @param dayOfMonth changed day of Month
- */
- public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
- mCalendar.set(year, monthOfYear, dayOfMonth, mCalendar.get(Calendar.HOUR_OF_DAY), mCalendar.get(Calendar.MINUTE));
- }
- /**
- * Handles on time changed events.
- * @param view TimePicker
- * @param hourOfDay changed hour
- * @param minute changed minute
- */
- public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
- mCalendar.set(mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH), hourOfDay, minute);
- }
- // MAYBE NEED IN FUTURE DEVELOPMENT
- // UNUSED
- // public long getDateTimeMillis() {
- // return mCalendar.getTimeInMillis();
- // }
- // public boolean CalendarViewShown() {
- // return datePicker.getCalendarViewShown();
- // }
- // public boolean is24HourView() {
- // return timePicker.is24HourView();
- // }
- // public void updateDate(int year, int monthOfYear, int dayOfMonth) {
- // datePicker.updateDate(year, monthOfYear, dayOfMonth);
- // }
- //
- // public void updateTime(int currentHour, int currentMinute) {
- // timePicker.setCurrentHour(currentHour);
- // timePicker.setCurrentMinute(currentMinute);
- // }
- //
- // public String getDateTime() {
- // DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(activity);
- // return dateFormat.format(mCalendar.getTime());
- // }
- }
|