ZenRuleNameDialog.java revision 25cb8f06914538f337be66d9a784834a7373b58a
145fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock/*
245fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock * Copyright (C) 2015 The Android Open Source Project
345fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock *
445fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock * Licensed under the Apache License, Version 2.0 (the "License");
545fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock * you may not use this file except in compliance with the License.
645fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock * You may obtain a copy of the License at
745fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock *
845fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock *      http://www.apache.org/licenses/LICENSE-2.0
945fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock *
1045fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock * Unless required by applicable law or agreed to in writing, software
1145fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock * distributed under the License is distributed on an "AS IS" BASIS,
1245fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1345fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock * See the License for the specific language governing permissions and
1445fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock * limitations under the License.
1545fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock */
1645fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock
1745fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlockpackage com.android.settings.notification;
1845fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock
1945fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlockimport android.app.AlertDialog;
207d8a9aead53f817c9df14cc5feaae223a8a834e8Julia Reynoldsimport android.app.AutomaticZenRule;
2145fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlockimport android.content.Context;
2245fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlockimport android.content.DialogInterface;
237b4d1e2cb8684dc1bece3cfe4edfb43b73a00565John Spurlockimport android.content.res.ColorStateList;
2445fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlockimport android.text.Editable;
2545fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlockimport android.text.TextUtils;
2645fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlockimport android.text.TextWatcher;
2745fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlockimport android.util.ArraySet;
287b4d1e2cb8684dc1bece3cfe4edfb43b73a00565John Spurlockimport android.util.TypedValue;
2945fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlockimport android.view.LayoutInflater;
3045fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlockimport android.view.View;
3145fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlockimport android.widget.EditText;
3245fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock
3345fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlockimport com.android.settings.R;
3445fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock
35c96a5dcbfc3f37f71e8b3e8b962f9571b1e7ceafJohn Spurlockimport java.util.List;
36c96a5dcbfc3f37f71e8b3e8b962f9571b1e7ceafJohn Spurlock
3745fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlockpublic abstract class ZenRuleNameDialog {
38f7748e4ae15e49f9f4a9cc15a372e9a6d3c35c56Julia Reynolds    private static final String TAG = "ZenRuleNameDialog";
39c96a5dcbfc3f37f71e8b3e8b962f9571b1e7ceafJohn Spurlock    private static final boolean DEBUG = ZenModeSettings.DEBUG;
40c96a5dcbfc3f37f71e8b3e8b962f9571b1e7ceafJohn Spurlock
4145fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock    private final AlertDialog mDialog;
4245fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock    private final EditText mEditText;
43c5bab237f577d743f78db923b0449566d34fd1d4John Spurlock    private final String mOriginalRuleName;
44f57bad7d5b0f9044231fc52351ed27e14013f491John Spurlock    private final boolean mIsNew;
4545fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock
4625cb8f06914538f337be66d9a784834a7373b58aJulia Reynolds    public ZenRuleNameDialog(Context context, String ruleName) {
47f57bad7d5b0f9044231fc52351ed27e14013f491John Spurlock        mIsNew = ruleName == null;
48c5bab237f577d743f78db923b0449566d34fd1d4John Spurlock        mOriginalRuleName = ruleName;
4945fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock        final View v = LayoutInflater.from(context).inflate(R.layout.zen_rule_name, null, false);
5045fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock        mEditText = (EditText) v.findViewById(R.id.rule_name);
51f57bad7d5b0f9044231fc52351ed27e14013f491John Spurlock        if (!mIsNew) {
52c96a5dcbfc3f37f71e8b3e8b962f9571b1e7ceafJohn Spurlock            mEditText.setText(ruleName);
53c96a5dcbfc3f37f71e8b3e8b962f9571b1e7ceafJohn Spurlock        }
5445fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock        mEditText.setSelectAllOnFocus(true);
55f7748e4ae15e49f9f4a9cc15a372e9a6d3c35c56Julia Reynolds
5645fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock        mDialog = new AlertDialog.Builder(context)
57f57bad7d5b0f9044231fc52351ed27e14013f491John Spurlock                .setTitle(mIsNew ? R.string.zen_mode_add_rule : R.string.zen_mode_rule_name)
5845fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock                .setView(v)
5945fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock                .setPositiveButton(R.string.okay, new DialogInterface.OnClickListener() {
6045fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock                    @Override
6145fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock                    public void onClick(DialogInterface dialog, int which) {
62c5bab237f577d743f78db923b0449566d34fd1d4John Spurlock                        final String newName = trimmedText();
63c5bab237f577d743f78db923b0449566d34fd1d4John Spurlock                        if (!mIsNew && mOriginalRuleName != null
64c5bab237f577d743f78db923b0449566d34fd1d4John Spurlock                                && mOriginalRuleName.equalsIgnoreCase(newName)) {
65c5bab237f577d743f78db923b0449566d34fd1d4John Spurlock                            return;  // no change to an existing rule, just dismiss
66c5bab237f577d743f78db923b0449566d34fd1d4John Spurlock                        }
67f7748e4ae15e49f9f4a9cc15a372e9a6d3c35c56Julia Reynolds                        onOk(newName);
6845fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock                    }
6945fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock                })
7045fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock                .setNegativeButton(R.string.cancel, null)
7145fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock                .create();
7245fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock    }
7345fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock
74f7748e4ae15e49f9f4a9cc15a372e9a6d3c35c56Julia Reynolds    abstract public void onOk(String ruleName);
7545fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock
7645fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock    public void show() {
7745fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock        mDialog.show();
787d8a9aead53f817c9df14cc5feaae223a8a834e8Julia Reynolds    }
797d8a9aead53f817c9df14cc5feaae223a8a834e8Julia Reynolds
80c96a5dcbfc3f37f71e8b3e8b962f9571b1e7ceafJohn Spurlock    private String trimmedText() {
81c96a5dcbfc3f37f71e8b3e8b962f9571b1e7ceafJohn Spurlock        return mEditText.getText() == null ? null : mEditText.getText().toString().trim();
82c96a5dcbfc3f37f71e8b3e8b962f9571b1e7ceafJohn Spurlock    }
8325cb8f06914538f337be66d9a784834a7373b58aJulia Reynolds}
84