DateTimeDialogFragment.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. package de.tudarmstadt.informatik.hostage.ui2.dialog;
  2. import android.annotation.SuppressLint;
  3. import android.app.Activity;
  4. import android.app.AlertDialog;
  5. import android.app.AlertDialog.Builder;
  6. import android.app.Dialog;
  7. import android.app.DialogFragment;
  8. import android.content.DialogInterface;
  9. import android.content.res.Configuration;
  10. import android.os.Bundle;
  11. import android.view.LayoutInflater;
  12. import android.view.View;
  13. import android.view.ViewGroup;
  14. import android.widget.DatePicker;
  15. import android.widget.DatePicker.OnDateChangedListener;
  16. import android.widget.TimePicker;
  17. import android.widget.TimePicker.OnTimeChangedListener;
  18. import java.util.Calendar;
  19. import de.tudarmstadt.informatik.hostage.R;
  20. import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
  21. /**
  22. * Created by Julien on 16.02.14.
  23. */
  24. @SuppressLint("ValidFragment")
  25. public class DateTimeDialogFragment extends DialogFragment implements OnDateChangedListener, OnTimeChangedListener {
  26. // Define constants for date-time picker.
  27. public final static int DATE_PICKER = 1;
  28. public final static int TIME_PICKER = 2;
  29. public final static int DATE_TIME_PICKER = 3;
  30. // DatePicker reference
  31. public DatePicker datePicker;
  32. // TimePicker reference
  33. public TimePicker timePicker;
  34. // Calendar reference
  35. private Calendar mCalendar;
  36. // Define activity
  37. private Activity activity;
  38. // Define Dialog type
  39. private int DialogType;
  40. // Define Dialog view
  41. private View mView;
  42. // Constructor start
  43. public DateTimeDialogFragment(Activity activity) {
  44. this(activity, DATE_TIME_PICKER);
  45. }
  46. /**
  47. * Constructor
  48. * @param activity the activity
  49. * @param DialogType, what kind of dialog it is (TIME_PICKER, DATE_PICKER, DATE_TIME_PICKER)
  50. */
  51. public DateTimeDialogFragment(Activity activity, int DialogType) {
  52. this.activity = activity;
  53. this.DialogType = DialogType;
  54. LayoutInflater inflater = activity.getLayoutInflater();
  55. mView = inflater.inflate(R.layout.date_time_dialog, null);
  56. this.setupRootView(mView);
  57. }
  58. @Override
  59. public void onConfigurationChanged(Configuration newConfig){
  60. super.onConfigurationChanged(newConfig);
  61. LayoutInflater inflater = LayoutInflater.from(this.activity);
  62. ViewGroup container = (ViewGroup) this.mView.getParent();
  63. container.removeView(this.mView);
  64. mView = inflater.inflate(R.layout.date_time_dialog, null);
  65. container.addView(mView);
  66. this.setupRootView(mView);
  67. }
  68. /**
  69. * Configure the root view in here.
  70. * @param mView, root view
  71. */
  72. private void setupRootView(View mView){
  73. mCalendar = Calendar.getInstance();
  74. datePicker = (DatePicker) mView.findViewById(R.id.DatePicker);
  75. datePicker.init(mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH), this);
  76. timePicker = (TimePicker) mView.findViewById(R.id.TimePicker);
  77. setIs24HourView(true);
  78. setCalendarViewShown(false);
  79. switch (DialogType) {
  80. case DATE_PICKER:
  81. timePicker.setVisibility(View.GONE);
  82. break;
  83. case TIME_PICKER:
  84. datePicker.setVisibility(View.GONE);
  85. break;
  86. }
  87. }
  88. /**
  89. * Set the current date.
  90. * @param timeInMillis, date in milliseconds
  91. */
  92. public void setDate(long timeInMillis){
  93. Calendar calendar = Calendar.getInstance();
  94. calendar.setTimeInMillis (timeInMillis);
  95. int year = calendar.get(Calendar.YEAR) ;
  96. int month = calendar.get(Calendar.MONTH);
  97. int day = calendar.get(Calendar.DATE);
  98. int hour = calendar.get(Calendar.HOUR);
  99. int min = calendar.get(Calendar.MINUTE);
  100. datePicker.updateDate(year, month, day);
  101. timePicker.setCurrentHour(hour);
  102. timePicker.setCurrentMinute(min);
  103. }
  104. /**
  105. * Returns the current selected date.
  106. * @return long, date in milliseconds
  107. */
  108. public long getDate(){
  109. int day = datePicker.getDayOfMonth();
  110. int month = datePicker.getMonth();
  111. int year = datePicker.getYear();
  112. int hourOfDay = timePicker.getCurrentHour();
  113. int minute = timePicker.getCurrentMinute();
  114. Calendar calendar = Calendar.getInstance();
  115. calendar.set(year, month, day, hourOfDay, minute);
  116. return calendar.getTime().getTime();
  117. }
  118. /**
  119. * The listener which will be called if the user tapped "cancel" or "ok"
  120. */
  121. public interface DateTimeDialogFragmentListener {
  122. /**
  123. * Called if the user tapped "ok"
  124. * @param dialog {@link DateTimeDialogFragment DateTimeDialogFragment}
  125. */
  126. public void onDateTimePickerPositiveClick(DateTimeDialogFragment dialog);
  127. /**
  128. * Called if the user tapped "cancel"
  129. * @param dialog {@link DateTimeDialogFragment DateTimeDialogFragment}
  130. */
  131. public void onDateTimePickerNegativeClick(DateTimeDialogFragment dialog);
  132. }
  133. private DateTimeDialogFragmentListener mListener;
  134. /**
  135. * Set the user interaction listener.
  136. * @param listener DateTimeDialogFragmentListener
  137. */
  138. public void setDateChangeListener(DateTimeDialogFragmentListener listener){
  139. this.mListener = listener;
  140. }
  141. @Override
  142. public void onAttach(Activity activity) {
  143. super.onAttach(activity);
  144. try {
  145. if (this.mListener == null){
  146. if (activity.getClass().equals(MainActivity.class)){
  147. mListener = (DateTimeDialogFragmentListener) (((MainActivity)activity).getDisplayedFragment());
  148. } else {
  149. mListener = (DateTimeDialogFragmentListener) activity;
  150. }
  151. }
  152. } catch (ClassCastException e) {
  153. throw new ClassCastException(activity.toString()
  154. + " must implement DateTimeDialogListener");
  155. }
  156. }
  157. @Override
  158. public Dialog onCreateDialog(Bundle savedInstanceState) {
  159. Builder builder = new AlertDialog.Builder(activity);
  160. builder.setView(mView);
  161. builder.setMessage(activity.getString(R.string.rec_set_date))
  162. .setPositiveButton(activity.getString(R.string.rec_set),
  163. new DialogInterface.OnClickListener() {
  164. public void onClick(DialogInterface dialog, int id) {
  165. mListener
  166. .onDateTimePickerPositiveClick(DateTimeDialogFragment.this);
  167. }
  168. })
  169. .setNegativeButton(activity.getString(R.string.rec_cancel),
  170. new DialogInterface.OnClickListener() {
  171. public void onClick(DialogInterface dialog, int id) {
  172. mListener
  173. .onDateTimePickerNegativeClick(DateTimeDialogFragment.this);
  174. DateTimeDialogFragment.this.getDialog().cancel();
  175. }
  176. });
  177. return builder.create();
  178. }
  179. @Override
  180. public void onActivityCreated(Bundle savedInstanceState) {
  181. super.onActivityCreated(savedInstanceState);
  182. timePicker.setOnTimeChangedListener(this);
  183. }
  184. /**
  185. * Returns the value of the given field after computing the field values by calling complete() first.
  186. * @param field int
  187. * @return int
  188. */
  189. public int get(final int field) {
  190. return mCalendar.get(field);
  191. }
  192. /**
  193. * Set the time picker style.
  194. * @param is24HourView
  195. */
  196. public void setIs24HourView(boolean is24HourView) {
  197. timePicker.setIs24HourView(is24HourView);
  198. }
  199. /**
  200. * Show / hide the calendar view of the DatePicker.
  201. * @param calendarView boolean
  202. */
  203. public void setCalendarViewShown(boolean calendarView) {
  204. datePicker.setCalendarViewShown(calendarView);
  205. }
  206. /**
  207. * Handles date change event.
  208. * @param view DatePicker
  209. * @param year changed year
  210. * @param monthOfYear changed month of Year
  211. * @param dayOfMonth changed day of Month
  212. */
  213. public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
  214. mCalendar.set(year, monthOfYear, dayOfMonth, mCalendar.get(Calendar.HOUR_OF_DAY), mCalendar.get(Calendar.MINUTE));
  215. }
  216. /**
  217. * Handles on time changed events.
  218. * @param view TimePicker
  219. * @param hourOfDay changed hour
  220. * @param minute changed minute
  221. */
  222. public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
  223. mCalendar.set(mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH), hourOfDay, minute);
  224. }
  225. // MAYBE NEED IN FUTURE DEVELOPMENT
  226. // UNUSED
  227. // public long getDateTimeMillis() {
  228. // return mCalendar.getTimeInMillis();
  229. // }
  230. // public boolean CalendarViewShown() {
  231. // return datePicker.getCalendarViewShown();
  232. // }
  233. // public boolean is24HourView() {
  234. // return timePicker.is24HourView();
  235. // }
  236. // public void updateDate(int year, int monthOfYear, int dayOfMonth) {
  237. // datePicker.updateDate(year, monthOfYear, dayOfMonth);
  238. // }
  239. //
  240. // public void updateTime(int currentHour, int currentMinute) {
  241. // timePicker.setCurrentHour(currentHour);
  242. // timePicker.setCurrentMinute(currentMinute);
  243. // }
  244. //
  245. // public String getDateTime() {
  246. // DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(activity);
  247. // return dateFormat.format(mCalendar.getTime());
  248. // }
  249. }