Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.31 KB | None | 0 0
  1. package pl.cyrzan.projgrupowy.view;
  2.  
  3. /**
  4.  * Created by Patryk on 12.01.2017.
  5.  */
  6.  
  7. import android.app.DatePickerDialog;
  8. import android.app.DatePickerDialog.OnDateSetListener;
  9. import android.app.Dialog;
  10. import android.app.TimePickerDialog;
  11. import android.app.TimePickerDialog.OnTimeSetListener;
  12. import android.content.Context;
  13. import android.content.DialogInterface;
  14. import android.os.Bundle;
  15. import android.os.Parcelable;
  16. import android.support.annotation.NonNull;
  17. import android.support.annotation.Nullable;
  18. import android.support.v4.app.DialogFragment;
  19. import android.support.v4.app.FragmentActivity;
  20. import android.util.AttributeSet;
  21. import android.util.SparseArray;
  22. import android.view.LayoutInflater;
  23. import android.view.View;
  24. import android.widget.Button;
  25. import android.widget.DatePicker;
  26. import android.widget.LinearLayout;
  27. import android.widget.TextView;
  28. import android.widget.TimePicker;
  29. import android.widget.Toast;
  30.  
  31. import pl.cyrzan.projgrupowy.R;
  32.  
  33. import java.text.DateFormat;
  34. import java.util.Calendar;
  35. import java.util.Date;
  36.  
  37. import static android.text.format.DateFormat.getDateFormat;
  38. import static android.text.format.DateFormat.getTimeFormat;
  39. import static android.widget.Toast.LENGTH_SHORT;
  40. import static java.util.Calendar.DAY_OF_MONTH;
  41. import static java.util.Calendar.HOUR_OF_DAY;
  42. import static java.util.Calendar.MINUTE;
  43. import static java.util.Calendar.MONTH;
  44. import static java.util.Calendar.YEAR;
  45. import static java.util.concurrent.TimeUnit.MILLISECONDS;
  46.  
  47. public class TimeAndDateView extends LinearLayout
  48.         implements OnTimeSetListener, OnDateSetListener, OnNowClickListener {
  49.  
  50.     private final String SUPER_STATE = "superState";
  51.     private static final String DATE = "date";
  52.     private static final String NOW = "now";
  53.     private static final String TODAY = "today";
  54.     private final TimeAndDateViewHolder ui;
  55.     private Calendar calendar;
  56.     private boolean now = true, today = true;
  57.  
  58.     public TimeAndDateView(Context context, AttributeSet attr) {
  59.         super(context, attr);
  60.  
  61.         setOrientation(LinearLayout.VERTICAL);
  62.  
  63.         LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  64.         inflater.inflate(R.layout.time_and_date, this, true);
  65.         ui = new TimeAndDateViewHolder(this);
  66.  
  67.         // Initialize current Time and Date, display it in UI
  68.         reset();
  69.  
  70.         // Time
  71.         ui.time.setOnClickListener(new View.OnClickListener() {
  72.             @Override
  73.             public void onClick(View view) {
  74.                 TimePickerFragment newFragment = new TimePickerFragment();
  75.                 newFragment.setOnTimeSetListener(TimeAndDateView.this, TimeAndDateView.this);
  76.                 // show current time also in dialog if time set to now
  77.                 if(now) resetTime();
  78.                 Bundle bundle = new Bundle();
  79.                 bundle.putSerializable(DATE, calendar);
  80.                 newFragment.setArguments(bundle);
  81.                 newFragment.show(((FragmentActivity) getContext()).getSupportFragmentManager(), "timePicker");
  82.  
  83.             }
  84.         });
  85.         // set current time on long click
  86.         ui.time.setOnLongClickListener(new View.OnLongClickListener() {
  87.             @Override
  88.             public boolean onLongClick(View view) {
  89.                 resetTime();
  90.                 Toast.makeText(getContext(), R.string.current_time_set, LENGTH_SHORT).show();
  91.                 return true;
  92.             }
  93.         });
  94.  
  95.         // Date
  96.         ui.date.setOnClickListener(new View.OnClickListener() {
  97.             @Override
  98.             public void onClick(View view) {
  99.                 DatePickerFragment newFragment = new DatePickerFragment();
  100.                 newFragment.setOnDateSetListener(TimeAndDateView.this);
  101.                 // show current date also in dialog if set to today
  102.                 if(today) resetDate();
  103.                 Bundle bundle = new Bundle();
  104.                 bundle.putSerializable(DATE, calendar);
  105.                 newFragment.setArguments(bundle);
  106.                 newFragment.show(((FragmentActivity) getContext()).getSupportFragmentManager(), "datePicker");
  107.             }
  108.         });
  109.         // set current date on long click
  110.         ui.date.setOnLongClickListener(new View.OnLongClickListener() {
  111.             @Override
  112.             public boolean onLongClick(View view) {
  113.                 resetDate();
  114.                 Toast.makeText(getContext(), R.string.current_date_set, LENGTH_SHORT).show();
  115.                 return true;
  116.             }
  117.         });
  118.     }
  119.  
  120.     public TimeAndDateView(Context context) {
  121.         this(context, null);
  122.     }
  123.  
  124.     @Override
  125.     public Parcelable onSaveInstanceState() {
  126.         Bundle bundle = new Bundle();
  127.         bundle.putParcelable(SUPER_STATE, super.onSaveInstanceState());
  128.         bundle.putSerializable(DATE, calendar);
  129.         bundle.putBoolean(NOW, now);
  130.         bundle.putBoolean(TODAY, today);
  131.         return bundle;
  132.     }
  133.  
  134.     @Override
  135.     public void onRestoreInstanceState(Parcelable state) {
  136.         if(state instanceof Bundle) { // implicit null check
  137.             Bundle bundle = (Bundle) state;
  138.             calendar = (Calendar) bundle.getSerializable(DATE);
  139.             now = bundle.getBoolean(NOW);
  140.             today = bundle.getBoolean(TODAY);
  141.             updateTexts();
  142.             state = bundle.getParcelable(SUPER_STATE);
  143.         }
  144.         super.onRestoreInstanceState(state);
  145.     }
  146.  
  147.     @Override
  148.     protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
  149.         // Makes sure that the state of the child views are not saved
  150.         super.dispatchFreezeSelfOnly(container);
  151.     }
  152.  
  153.     @Override
  154.     protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
  155.         // Makes sure that the state of the child views are not restored
  156.         super.dispatchThawSelfOnly(container);
  157.     }
  158.  
  159.     @Override
  160.     public void onDateSet(@Nullable DatePicker view, int year, int month, int day) {
  161.         calendar.set(YEAR, year);
  162.         calendar.set(MONTH, month);
  163.         calendar.set(DAY_OF_MONTH, day);
  164.  
  165.         Calendar c = Calendar.getInstance();
  166.         today = c.get(YEAR) == year && c.get(MONTH) == month && c.get(DAY_OF_MONTH) == day;
  167.         updateTexts();
  168.     }
  169.  
  170.     @Override
  171.     public void onTimeSet(@Nullable TimePicker view, int hourOfDay, int minute) {
  172.         calendar.set(HOUR_OF_DAY, hourOfDay);
  173.         calendar.set(MINUTE, minute);
  174.  
  175.         // check if time can be considered "now"
  176.         Calendar c1 = Calendar.getInstance();
  177.         c1.set(HOUR_OF_DAY, hourOfDay);
  178.         c1.set(MINUTE, minute);
  179.         Calendar c2 = Calendar.getInstance();
  180.         now = MILLISECONDS.toMinutes(Math.abs(c1.getTimeInMillis() - c2.getTimeInMillis())) < 10;
  181.         updateTexts();
  182.     }
  183.  
  184.     public Date getDate() {
  185.         if(now) resetTime();
  186.         if(today) resetDate();
  187.         return calendar.getTime();
  188.     }
  189.  
  190.     public void setDate(Date date) {
  191.         Calendar tmp = Calendar.getInstance();
  192.         tmp.setTime(date);
  193.  
  194.         onTimeSet(null, tmp.get(HOUR_OF_DAY), tmp.get(MINUTE));
  195.         onDateSet(null, tmp.get(YEAR), tmp.get(MONTH), tmp.get(DAY_OF_MONTH));
  196.     }
  197.  
  198.     private void updateTexts() {
  199.         ui.timeText.setText(getTimeString());
  200.         ui.calendarText.setText(getDateString());
  201.     }
  202.  
  203.     private String getTimeString() {
  204.         if (now) {
  205.             return getContext().getString(R.string.now);
  206.         }
  207.         DateFormat tf = getTimeFormat(getContext().getApplicationContext());
  208.         return tf.format(calendar.getTime());
  209.     }
  210.  
  211.     private String getDateString() {
  212.         if(today) {
  213.             return getContext().getString(R.string.today);
  214.         }
  215.         DateFormat tf = getDateFormat(getContext().getApplicationContext());
  216.         return tf.format(calendar.getTime());
  217.     }
  218.  
  219.     private void addTime(int min) {
  220.         // remember day before adding
  221.         int day = calendar.get(DAY_OF_MONTH);
  222.  
  223.         // update time if it was set to now before
  224.         if(now) calendar = Calendar.getInstance();
  225.  
  226.         // add min minutes
  227.         calendar.add(MINUTE, min);
  228.  
  229.         // no more now, but maybe today?
  230.         now = false;
  231.         if (day != calendar.get(DAY_OF_MONTH)) {
  232.             today = false;
  233.         }
  234.  
  235.         // update text of buttons
  236.         updateTexts();
  237.     }
  238.  
  239.     private void resetTime() {
  240.         Calendar c = Calendar.getInstance();
  241.         calendar.set(HOUR_OF_DAY, c.get(HOUR_OF_DAY));
  242.         calendar.set(MINUTE, c.get(MINUTE));
  243.  
  244.         now = true;
  245.         ui.timeText.setText(getTimeString());
  246.     }
  247.  
  248.     private void resetDate() {
  249.         Calendar c = Calendar.getInstance();
  250.         calendar.set(YEAR, c.get(YEAR));
  251.         calendar.set(MONTH, c.get(MONTH));
  252.         calendar.set(DAY_OF_MONTH, c.get(DAY_OF_MONTH));
  253.  
  254.         today = true;
  255.         ui.calendarText.setText(getDateString());
  256.     }
  257.  
  258.     public void reset() {
  259.         calendar = Calendar.getInstance();
  260.         now = true;
  261.         today = true;
  262.         updateTexts();
  263.     }
  264.  
  265.     @Override
  266.     public void resetTimeFromDialog() {
  267.         reset();
  268.     }
  269.  
  270.     public static class TimeAndDateViewHolder {
  271.         public LinearLayout time;
  272.         private Button plus15;
  273.         public LinearLayout date;
  274.         public TextView timeText;
  275.         public TextView calendarText;
  276.  
  277.         private TimeAndDateViewHolder(View view) {
  278.             time = (LinearLayout) view.findViewById(R.id.timeLine);
  279.             plus15 = (Button) view.findViewById(R.id.plus15Button);
  280.             date = (LinearLayout) view.findViewById(R.id.calendarLine);
  281.             timeText = (TextView) view.findViewById(R.id.timeText);
  282.             calendarText = (TextView) view.findViewById(R.id.calendarText);
  283.         }
  284.     }
  285.  
  286.     public static class TimePickerFragment extends DialogFragment {
  287.  
  288.         OnTimeSetListener listener;
  289.         OnNowClickListener resetListener;
  290.  
  291.  
  292.         @NonNull
  293.         @Override
  294.         public Dialog onCreateDialog(Bundle savedInstanceState) {
  295.             Calendar calendar = (Calendar) getArguments().getSerializable(DATE);
  296.             if(calendar == null) calendar = Calendar.getInstance();
  297.  
  298.             int hour = calendar.get(HOUR_OF_DAY);
  299.             int minute = calendar.get(MINUTE);
  300.             TimePickerDialog tpd = new TimePickerDialog(getActivity(), listener, hour, minute,
  301.                     android.text.format.DateFormat.is24HourFormat(getActivity()));
  302.             tpd.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.cancel), tpd);
  303.             tpd.setButton(DialogInterface.BUTTON_NEUTRAL, getString(R.string.now), new DialogInterface.OnClickListener() {
  304.                 @Override
  305.                 public void onClick(DialogInterface dialogInterface, int i) {
  306.                     resetListener.resetTimeFromDialog();
  307.                 }
  308.             });
  309.             tpd.setButton(DialogInterface.BUTTON_POSITIVE, getString(R.string.ok), tpd);
  310.             return tpd;
  311.         }
  312.  
  313.         public void setOnTimeSetListener(OnTimeSetListener listener, OnNowClickListener resetListener) {
  314.             // TODO this needs to re-attach on configuration changes
  315.             this.listener = listener;
  316.             this.resetListener = resetListener;
  317.         }
  318.     }
  319.  
  320.     public static class DatePickerFragment extends DialogFragment {
  321.  
  322.         OnDateSetListener listener;
  323.  
  324.         @NonNull
  325.         @Override
  326.         public Dialog onCreateDialog(Bundle savedInstanceState) {
  327.             Calendar calendar = (Calendar) getArguments().getSerializable(DATE);
  328.             if(calendar == null) calendar = Calendar.getInstance();
  329.  
  330.             int year = calendar.get(YEAR);
  331.             int month = calendar.get(MONTH);
  332.             int day = calendar.get(DAY_OF_MONTH);
  333.             DatePickerDialog dpd = new DatePickerDialog(getActivity(), listener, year, month, day);
  334.             dpd.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.cancel), dpd);
  335.             dpd.setButton(DialogInterface.BUTTON_POSITIVE, getString(R.string.ok), dpd);
  336.             return dpd;
  337.         }
  338.  
  339.         public void setOnDateSetListener(OnDateSetListener listener) {
  340.             // TODO this needs to re-attach on configuration changes
  341.             this.listener = listener;
  342.         }
  343.  
  344.     }
  345.  
  346. }
  347.  
  348. interface OnNowClickListener {
  349.     public void resetTimeFromDialog();
  350. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement