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.KeyEvent;
23import android.view.LayoutInflater;
24import android.view.View;
25import android.view.ViewGroup;
26import android.view.WindowManager;
27import android.view.inputmethod.EditorInfo;
28import android.widget.Button;
29import android.widget.EditText;
30import android.widget.TextView;
31import android.widget.TextView.OnEditorActionListener;
32
33import com.android.deskclock.provider.Alarm;
34import com.android.deskclock.timer.TimerObj;
35
36/**
37 * DialogFragment to edit label.
38 */
39public class LabelDialogFragment extends DialogFragment {
40
41    private static final String KEY_LABEL = "label";
42    private static final String KEY_ALARM = "alarm";
43    private static final String KEY_TIMER = "timer";
44    private static final String KEY_TAG = "tag";
45
46    private EditText mLabelBox;
47
48    public static LabelDialogFragment newInstance(Alarm alarm, String label, String tag) {
49        final LabelDialogFragment frag = new LabelDialogFragment();
50        Bundle args = new Bundle();
51        args.putString(KEY_LABEL, label);
52        args.putParcelable(KEY_ALARM, alarm);
53        args.putString(KEY_TAG, tag);
54        frag.setArguments(args);
55        return frag;
56    }
57
58    public static LabelDialogFragment newInstance(TimerObj timer, String label, String tag) {
59        final LabelDialogFragment frag = new LabelDialogFragment();
60        Bundle args = new Bundle();
61        args.putString(KEY_LABEL, label);
62        args.putParcelable(KEY_TIMER, timer);
63        args.putString(KEY_TAG, tag);
64        frag.setArguments(args);
65        return frag;
66    }
67
68    @Override
69    public void onCreate(Bundle savedInstanceState) {
70        super.onCreate(savedInstanceState);
71        setStyle(DialogFragment.STYLE_NO_TITLE, 0);
72    }
73
74    @Override
75    public View onCreateView(LayoutInflater inflater, ViewGroup container,
76            Bundle savedInstanceState) {
77        Bundle bundle = getArguments();
78        final String label = bundle.getString(KEY_LABEL);
79        final Alarm alarm = bundle.getParcelable(KEY_ALARM);
80        final TimerObj timer = bundle.getParcelable(KEY_TIMER);
81        final String tag = bundle.getString(KEY_TAG);
82
83        final View view = inflater.inflate(R.layout.label_dialog, container, false);
84
85        mLabelBox = (EditText) view.findViewById(R.id.labelBox);
86        mLabelBox.setText(label);
87        mLabelBox.setOnEditorActionListener(new OnEditorActionListener() {
88            @Override
89            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
90                if (actionId == EditorInfo.IME_ACTION_DONE) {
91                    set(alarm, timer, tag);
92                    return true;
93                }
94                return false;
95            }
96        });
97
98        final Button cancelButton = (Button) view.findViewById(R.id.cancelButton);
99        cancelButton.setOnClickListener(new View.OnClickListener() {
100            @Override
101            public void onClick(View view) {
102                dismiss();
103            }
104        });
105
106        final Button setButton = (Button) view.findViewById(R.id.setButton);
107        setButton.setOnClickListener(new View.OnClickListener() {
108            @Override
109            public void onClick(View view) {
110                set(alarm, timer, tag);
111            }
112        });
113
114        getDialog().getWindow().setSoftInputMode(
115                WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
116
117        return view;
118    }
119
120    private void set(Alarm alarm, TimerObj timer, String tag) {
121        String label = mLabelBox.getText().toString();
122        if (label.trim().length() == 0) {
123            // Don't allow user to input label with only whitespace.
124            label = "";
125        }
126
127        if (alarm != null) {
128            set(alarm, tag, label);
129        } else if (timer != null) {
130            set(timer, tag, label);
131        } else {
132            Log.e("No alarm or timer available.");
133        }
134    }
135
136    private void set(Alarm alarm, String tag, String label) {
137        final Activity activity = getActivity();
138        // TODO just pass in a listener in newInstance()
139        if (activity instanceof AlarmLabelDialogHandler) {
140            ((DeskClock) getActivity()).onDialogLabelSet(alarm, label, tag);
141        } else {
142            Log.e("Error! Activities that use LabelDialogFragment must implement "
143                    + "AlarmLabelDialogHandler");
144        }
145        dismiss();
146    }
147
148    private void set(TimerObj timer, String tag, String label) {
149        final Activity activity = getActivity();
150        // TODO just pass in a listener in newInstance()
151        if (activity instanceof TimerLabelDialogHandler){
152            ((DeskClock) getActivity()).onDialogLabelSet(timer, label, tag);
153        } else {
154            Log.e("Error! Activities that use LabelDialogFragment must implement "
155                    + "AlarmLabelDialogHandler or TimerLabelDialogHandler");
156        }
157        dismiss();
158    }
159
160    interface AlarmLabelDialogHandler {
161        void onDialogLabelSet(Alarm alarm, String label, String tag);
162    }
163
164    interface TimerLabelDialogHandler {
165        void onDialogLabelSet(TimerObj timer, String label, String tag);
166    }
167}
168