Home | History | Annotate | Download | only in alarms
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.deskclock.alarms;
     18 
     19 import android.app.Dialog;
     20 import android.app.DialogFragment;
     21 import android.app.Fragment;
     22 import android.app.FragmentManager;
     23 import android.app.TimePickerDialog;
     24 import android.content.Context;
     25 import android.content.DialogInterface;
     26 import android.os.Bundle;
     27 import android.support.v7.app.AlertDialog;
     28 import android.text.format.DateFormat;
     29 import android.widget.TimePicker;
     30 
     31 import com.android.deskclock.Utils;
     32 
     33 import java.util.Calendar;
     34 
     35 /**
     36  * DialogFragment used to show TimePicker.
     37  */
     38 public class TimePickerDialogFragment extends DialogFragment {
     39 
     40     /**
     41      * Tag for timer picker fragment in FragmentManager.
     42      */
     43     private static final String TAG = "TimePickerDialogFragment";
     44 
     45     private static final String ARG_HOUR = TAG + "_hour";
     46     private static final String ARG_MINUTE = TAG + "_minute";
     47 
     48     @Override
     49     @SuppressWarnings("deprecation")
     50     public Dialog onCreateDialog(Bundle savedInstanceState) {
     51         final OnTimeSetListener listener = ((OnTimeSetListener) getParentFragment());
     52 
     53         final Calendar now = Calendar.getInstance();
     54         final Bundle args = getArguments() == null ? Bundle.EMPTY : getArguments();
     55         final int hour = args.getInt(ARG_HOUR, now.get(Calendar.HOUR_OF_DAY));
     56         final int minute = args.getInt(ARG_MINUTE, now.get(Calendar.MINUTE));
     57 
     58         if (Utils.isLOrLater()) {
     59             final Context context = getActivity();
     60             return new TimePickerDialog(context, new TimePickerDialog.OnTimeSetListener() {
     61                 @Override
     62                 public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
     63                     listener.onTimeSet(TimePickerDialogFragment.this, hourOfDay, minute);
     64                 }
     65             }, hour, minute, DateFormat.is24HourFormat(context));
     66         } else {
     67             final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
     68             final Context context = builder.getContext();
     69 
     70             final TimePicker timePicker = new TimePicker(context);
     71             timePicker.setCurrentHour(hour);
     72             timePicker.setCurrentMinute(minute);
     73             timePicker.setIs24HourView(DateFormat.is24HourFormat(context));
     74 
     75             return builder.setView(timePicker)
     76                     .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
     77                         @Override
     78                         public void onClick(DialogInterface dialog, int which) {
     79                             listener.onTimeSet(TimePickerDialogFragment.this,
     80                                     timePicker.getCurrentHour(), timePicker.getCurrentMinute());
     81                         }
     82                     }).setNegativeButton(android.R.string.cancel, null /* listener */)
     83                     .create();
     84         }
     85     }
     86 
     87     public static void show(Fragment fragment) {
     88         show(fragment, -1 /* hour */, -1 /* minute */);
     89     }
     90 
     91     public static void show(Fragment parentFragment, int hourOfDay, int minute) {
     92         if (!(parentFragment instanceof OnTimeSetListener)) {
     93             throw new IllegalArgumentException("Fragment must implement OnTimeSetListener");
     94         }
     95 
     96         final FragmentManager manager = parentFragment.getChildFragmentManager();
     97         if (manager == null || manager.isDestroyed()) {
     98             return;
     99         }
    100 
    101         // Make sure the dialog isn't already added.
    102         removeTimeEditDialog(manager);
    103 
    104         final TimePickerDialogFragment fragment = new TimePickerDialogFragment();
    105 
    106         final Bundle args = new Bundle();
    107         if (hourOfDay >= 0 && hourOfDay < 24) {
    108             args.putInt(ARG_HOUR, hourOfDay);
    109         }
    110         if (minute >= 0 && minute < 60) {
    111             args.putInt(ARG_MINUTE, minute);
    112         }
    113 
    114         fragment.setArguments(args);
    115         fragment.show(manager, TAG);
    116     }
    117 
    118     public static void removeTimeEditDialog(FragmentManager manager) {
    119         if (manager != null) {
    120             final Fragment prev = manager.findFragmentByTag(TAG);
    121             if (prev != null) {
    122                 manager.beginTransaction().remove(prev).commit();
    123             }
    124         }
    125     }
    126 
    127     /**
    128      * The callback interface used to indicate the user is done filling in the time (e.g. they
    129      * clicked on the 'OK' button).
    130      */
    131     public interface OnTimeSetListener {
    132         /**
    133          * Called when the user is done setting a new time and the dialog has closed.
    134          *
    135          * @param fragment  the fragment associated with this listener
    136          * @param hourOfDay the hour that was set
    137          * @param minute    the minute that was set
    138          */
    139         void onTimeSet(TimePickerDialogFragment fragment, int hourOfDay, int minute);
    140     }
    141 }
    142