1 /*
2  * Copyright (C) 2014 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;
18 
19 import android.app.Activity;
20 import android.app.Dialog;
21 import android.app.DialogFragment;
22 import android.app.TimePickerDialog;
23 import android.app.TimePickerDialog.OnTimeSetListener;
24 import android.os.Bundle;
25 import android.text.format.DateFormat;
26 
27 import com.android.deskclock.provider.Alarm;
28 
29 import java.util.Calendar;
30 
31 public class TimePickerFragment extends DialogFragment {
32 
33     private Alarm mAlarm;
34     private OnTimeSetListener mListener;
35 
36     @Override
onCreateDialog(Bundle savedInstanceState)37     public Dialog onCreateDialog(Bundle savedInstanceState) {
38         final int hour, minute;
39         if (mAlarm == null) {
40             final Calendar c = Calendar.getInstance();
41             hour = c.get(Calendar.HOUR_OF_DAY);
42             minute = c.get(Calendar.MINUTE);
43         } else {
44             hour = mAlarm.hour;
45             minute = mAlarm.minutes;
46         }
47 
48         return new TimePickerDialog(getActivity(), R.style.TimePickerTheme, mListener, hour, minute,
49                 DateFormat.is24HourFormat(getActivity()));
50     }
51 
52     @Override
onAttach(Activity activity)53     public void onAttach(Activity activity) {
54         super.onAttach(activity);
55         if (getTargetFragment() instanceof OnTimeSetListener) {
56             setOnTimeSetListener((OnTimeSetListener) getTargetFragment());
57         }
58     }
59 
setOnTimeSetListener(OnTimeSetListener listener)60     public void setOnTimeSetListener(OnTimeSetListener listener) {
61         mListener = listener;
62     }
63 
setAlarm(Alarm alarm)64     public void setAlarm(Alarm alarm) {
65         mAlarm = alarm;
66     }
67 }
68