LabelDialogFragment.java revision 2a781815f822566b23f0cdef5404b179775fac1d
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.Dialog;
21import android.app.DialogFragment;
22import android.content.Context;
23import android.content.DialogInterface;
24import android.content.res.ColorStateList;
25import android.graphics.Color;
26import android.os.Bundle;
27import android.support.annotation.NonNull;
28import android.support.v7.app.AlertDialog;
29import android.support.v7.widget.AppCompatEditText;
30import android.text.Editable;
31import android.text.InputType;
32import android.text.TextUtils;
33import android.text.TextWatcher;
34import android.view.KeyEvent;
35import android.view.WindowManager;
36import android.view.inputmethod.EditorInfo;
37import android.widget.TextView;
38
39import com.android.deskclock.provider.Alarm;
40import com.android.deskclock.timer.TimerObj;
41
42/**
43 * DialogFragment to edit label.
44 */
45public class LabelDialogFragment extends DialogFragment {
46
47    private static final String KEY_LABEL = "label";
48    private static final String KEY_ALARM = "alarm";
49    private static final String KEY_TIMER = "timer";
50    private static final String KEY_TAG = "tag";
51
52    private AppCompatEditText mLabelBox;
53
54    public static LabelDialogFragment newInstance(Alarm alarm, String label, String tag) {
55        final Bundle args = new Bundle();
56        args.putString(KEY_LABEL, label);
57        args.putParcelable(KEY_ALARM, alarm);
58        args.putString(KEY_TAG, tag);
59
60        final LabelDialogFragment frag = new LabelDialogFragment();
61        frag.setArguments(args);
62        return frag;
63    }
64
65    public static LabelDialogFragment newInstance(TimerObj timer, String label, String tag) {
66        final Bundle args = new Bundle();
67        args.putString(KEY_LABEL, label);
68        args.putParcelable(KEY_TIMER, timer);
69        args.putString(KEY_TAG, tag);
70
71        final LabelDialogFragment frag = new LabelDialogFragment();
72        frag.setArguments(args);
73        return frag;
74    }
75
76    @Override
77    public void onSaveInstanceState(@NonNull Bundle outState) {
78        super.onSaveInstanceState(outState);
79        outState.putString(KEY_LABEL, mLabelBox.getText().toString());
80    }
81
82    @Override
83    public Dialog onCreateDialog(Bundle savedInstanceState) {
84        final Bundle bundle = getArguments();
85        final Alarm alarm = bundle.getParcelable(KEY_ALARM);
86        final TimerObj timer = bundle.getParcelable(KEY_TIMER);
87        final String tag = bundle.getString(KEY_TAG);
88        final String label = savedInstanceState != null ?
89                savedInstanceState.getString(KEY_LABEL) : bundle.getString(KEY_LABEL);
90
91        final Context context = getActivity();
92        final int colorAccent = Utils.obtainStyledColor(context, R.attr.colorAccent, Color.RED);
93        final int colorControlNormal =
94                Utils.obtainStyledColor(context, R.attr.colorControlNormal, Color.WHITE);
95
96        mLabelBox = new AppCompatEditText(context);
97        mLabelBox.setOnEditorActionListener(new TextView.OnEditorActionListener() {
98            @Override
99            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
100                if (actionId == EditorInfo.IME_ACTION_DONE) {
101                    set(alarm, timer, tag);
102                    return true;
103                }
104                return false;
105            }
106        });
107        mLabelBox.addTextChangedListener(new TextWatcher() {
108            @Override
109            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
110            }
111
112            @Override
113            public void onTextChanged(CharSequence s, int start, int before, int count) {
114                final int color = TextUtils.isEmpty(s) ? colorControlNormal : colorAccent;
115                mLabelBox.setSupportBackgroundTintList(ColorStateList.valueOf(color));
116            }
117
118            @Override
119            public void afterTextChanged(Editable editable) {
120            }
121        });
122        mLabelBox.setSingleLine();
123        mLabelBox.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
124        mLabelBox.setText(label);
125        mLabelBox.selectAll();
126
127        final int padding = getResources().getDimensionPixelSize(R.dimen.label_edittext_padding);
128        final AlertDialog alertDialog = new AlertDialog.Builder(context)
129                .setView(mLabelBox, padding, 0, padding, 0)
130                .setPositiveButton(R.string.time_picker_set, new DialogInterface.OnClickListener() {
131                    @Override
132                    public void onClick(DialogInterface dialog, int which) {
133                        set(alarm, timer, tag);
134                    }
135                })
136                .setNegativeButton(R.string.time_picker_cancel,
137                        new DialogInterface.OnClickListener() {
138                            @Override
139                            public void onClick(DialogInterface dialog, int which) {
140                                dismiss();
141                            }
142                })
143                .setMessage(R.string.label)
144                .create();
145
146        alertDialog.getWindow().setSoftInputMode(
147                WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
148        return alertDialog;
149    }
150
151    private void set(Alarm alarm, TimerObj timer, String tag) {
152        String label = mLabelBox.getText().toString();
153        if (label.trim().isEmpty()) {
154            // Don't allow user to input label with only whitespace.
155            label = "";
156        }
157
158        if (alarm != null) {
159            set(alarm, tag, label);
160        } else if (timer != null) {
161            set(timer, tag, label);
162        } else {
163            LogUtils.e("No alarm or timer available.");
164        }
165    }
166
167    private void set(Alarm alarm, String tag, String label) {
168        final Activity activity = getActivity();
169        if (activity instanceof AlarmLabelDialogHandler) {
170            ((AlarmLabelDialogHandler) activity).onDialogLabelSet(alarm, label, tag);
171        } else {
172            LogUtils.e("Error! Activities that use LabelDialogFragment must implement "
173                    + "AlarmLabelDialogHandler or TimerLabelDialogHandler");
174        }
175        dismiss();
176    }
177
178    private void set(TimerObj timer, String tag, String label) {
179        final Activity activity = getActivity();
180        if (activity instanceof TimerLabelDialogHandler){
181            ((TimerLabelDialogHandler) activity).onDialogLabelSet(timer, label, tag);
182        } else {
183            LogUtils.e("Error! Activities that use LabelDialogFragment must implement "
184                    + "AlarmLabelDialogHandler or TimerLabelDialogHandler");
185        }
186        dismiss();
187    }
188
189    interface AlarmLabelDialogHandler {
190        void onDialogLabelSet(Alarm alarm, String label, String tag);
191    }
192
193    interface TimerLabelDialogHandler {
194        void onDialogLabelSet(TimerObj timer, String label, String tag);
195    }
196}
197