ZenRuleNameDialog.java revision 39b467482d1bf256a111c757e9b7621c6f523271
1/*
2 * Copyright (C) 2015 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.settings.notification;
18
19import android.app.AlertDialog;
20import android.content.Context;
21import android.content.DialogInterface;
22import android.view.LayoutInflater;
23import android.view.View;
24import android.widget.EditText;
25
26import com.android.settings.R;
27
28public abstract class ZenRuleNameDialog {
29    private static final String TAG = "ZenRuleNameDialog";
30    private static final boolean DEBUG = ZenModeSettings.DEBUG;
31
32    private final AlertDialog mDialog;
33    private final EditText mEditText;
34    private final String mOriginalRuleName;
35    private final boolean mIsNew;
36
37    public ZenRuleNameDialog(Context context, String ruleName) {
38        mIsNew = ruleName == null;
39        mOriginalRuleName = ruleName;
40        final View v = LayoutInflater.from(context).inflate(R.layout.zen_rule_name, null, false);
41        mEditText = (EditText) v.findViewById(R.id.rule_name);
42        if (!mIsNew) {
43            mEditText.setText(ruleName);
44        }
45        mEditText.setSelectAllOnFocus(true);
46
47        mDialog = new AlertDialog.Builder(context)
48                .setTitle(mIsNew ? R.string.zen_mode_add_rule : R.string.zen_mode_rule_name)
49                .setView(v)
50                .setPositiveButton(R.string.okay, new DialogInterface.OnClickListener() {
51                    @Override
52                    public void onClick(DialogInterface dialog, int which) {
53                        final String newName = trimmedText();
54                        if (!mIsNew && mOriginalRuleName != null
55                                && mOriginalRuleName.equalsIgnoreCase(newName)) {
56                            return;  // no change to an existing rule, just dismiss
57                        }
58                        onOk(newName);
59                    }
60                })
61                .setNegativeButton(R.string.cancel, null)
62                .create();
63    }
64
65    abstract public void onOk(String ruleName);
66
67    public void show() {
68        mDialog.show();
69    }
70
71    private String trimmedText() {
72        return mEditText.getText() == null ? null : mEditText.getText().toString().trim();
73    }
74}
75