1/*
2 * Copyright (C) 2012 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
17package com.android.deskclock;
18
19import android.app.Activity;
20import android.app.DialogFragment;
21import android.os.Bundle;
22import android.view.LayoutInflater;
23import android.view.View;
24import android.view.ViewGroup;
25import android.widget.Button;
26
27/**
28 * Dialog to set alarm time.
29 */
30public class AlarmTimePickerDialogFragment extends DialogFragment {
31
32    private static final String KEY_ALARM = "alarm";
33
34    private Button mSet, mCancel;
35    private TimePicker mPicker;
36
37    public static AlarmTimePickerDialogFragment newInstance(Alarm alarm) {
38        final AlarmTimePickerDialogFragment frag = new AlarmTimePickerDialogFragment();
39        Bundle args = new Bundle();
40        args.putParcelable(KEY_ALARM, alarm);
41        frag.setArguments(args);
42        return frag;
43    }
44
45    @Override
46    public void onSaveInstanceState(Bundle outState) {
47        super.onSaveInstanceState(outState);
48    }
49
50    @Override
51    public void onCreate(Bundle savedInstanceState) {
52        super.onCreate(savedInstanceState);
53        setStyle(DialogFragment.STYLE_NO_TITLE, 0);
54    }
55
56    @Override
57    public View onCreateView(LayoutInflater inflater, ViewGroup container,
58            Bundle savedInstanceState) {
59        final Alarm alarm = getArguments().getParcelable(KEY_ALARM);
60
61        View v = inflater.inflate(R.layout.time_picker_dialog, null);
62        mSet = (Button) v.findViewById(R.id.set_button);
63        mCancel = (Button) v.findViewById(R.id.cancel_button);
64        mCancel.setOnClickListener(new View.OnClickListener() {
65            @Override
66            public void onClick(View view) {
67                dismiss();
68            }
69        });
70        mPicker = (TimePicker) v.findViewById(R.id.time_picker);
71        mPicker.setSetButton(mSet);
72        mSet.setOnClickListener(new View.OnClickListener() {
73            @Override
74            public void onClick(View view) {
75                final Activity activity = getActivity();
76                if (activity instanceof AlarmTimePickerDialogHandler) {
77                    final AlarmTimePickerDialogHandler act =
78                            (AlarmTimePickerDialogHandler) activity;
79                    act.onDialogTimeSet(alarm, mPicker.getHours(), mPicker.getMinutes());
80                } else {
81                    Log.e("Error! Activities that use AlarmTimePickerDialogFragment must implement "
82                            + "AlarmTimePickerDialogHandler");
83                }
84                dismiss();
85            }
86        });
87
88        return v;
89    }
90
91    interface AlarmTimePickerDialogHandler {
92        void onDialogTimeSet(Alarm alarm, int hourOfDay, int minute);
93    }
94}
95