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;
2045fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlockimport android.content.Context;
2145fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlockimport android.content.DialogInterface;
22472526e53f4d88bf8817b2618769881b02b6e8fdJulia Reynoldsimport android.text.TextUtils;
2345fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlockimport android.view.LayoutInflater;
2445fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlockimport android.view.View;
2545fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlockimport android.widget.EditText;
2645fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock
2745fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlockimport com.android.settings.R;
2845fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock
2945fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlockpublic abstract class ZenRuleNameDialog {
30f7748e4ae15e49f9f4a9cc15a372e9a6d3c35c56Julia Reynolds    private static final String TAG = "ZenRuleNameDialog";
31c96a5dcbfc3f37f71e8b3e8b962f9571b1e7ceafJohn Spurlock    private static final boolean DEBUG = ZenModeSettings.DEBUG;
32c96a5dcbfc3f37f71e8b3e8b962f9571b1e7ceafJohn Spurlock
3345fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock    private final AlertDialog mDialog;
3445fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock    private final EditText mEditText;
351d33d59be29c719f8003458373585397a781dc77Julia Reynolds    private final CharSequence mOriginalRuleName;
36f57bad7d5b0f9044231fc52351ed27e14013f491John Spurlock    private final boolean mIsNew;
3745fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock
381d33d59be29c719f8003458373585397a781dc77Julia Reynolds    public ZenRuleNameDialog(Context context, CharSequence ruleName) {
39f57bad7d5b0f9044231fc52351ed27e14013f491John Spurlock        mIsNew = ruleName == null;
40c5bab237f577d743f78db923b0449566d34fd1d4John Spurlock        mOriginalRuleName = ruleName;
4145fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock        final View v = LayoutInflater.from(context).inflate(R.layout.zen_rule_name, null, false);
4245fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock        mEditText = (EditText) v.findViewById(R.id.rule_name);
43f57bad7d5b0f9044231fc52351ed27e14013f491John Spurlock        if (!mIsNew) {
44c96a5dcbfc3f37f71e8b3e8b962f9571b1e7ceafJohn Spurlock            mEditText.setText(ruleName);
45c96a5dcbfc3f37f71e8b3e8b962f9571b1e7ceafJohn Spurlock        }
4645fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock        mEditText.setSelectAllOnFocus(true);
47f7748e4ae15e49f9f4a9cc15a372e9a6d3c35c56Julia Reynolds
4845fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock        mDialog = new AlertDialog.Builder(context)
49f57bad7d5b0f9044231fc52351ed27e14013f491John Spurlock                .setTitle(mIsNew ? R.string.zen_mode_add_rule : R.string.zen_mode_rule_name)
5045fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock                .setView(v)
5145fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock                .setPositiveButton(R.string.okay, new DialogInterface.OnClickListener() {
5245fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock                    @Override
5345fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock                    public void onClick(DialogInterface dialog, int which) {
54c5bab237f577d743f78db923b0449566d34fd1d4John Spurlock                        final String newName = trimmedText();
55472526e53f4d88bf8817b2618769881b02b6e8fdJulia Reynolds                        if (TextUtils.isEmpty(newName)) {
56472526e53f4d88bf8817b2618769881b02b6e8fdJulia Reynolds                            return;
57472526e53f4d88bf8817b2618769881b02b6e8fdJulia Reynolds                        }
58c5bab237f577d743f78db923b0449566d34fd1d4John Spurlock                        if (!mIsNew && mOriginalRuleName != null
591d33d59be29c719f8003458373585397a781dc77Julia Reynolds                                && mOriginalRuleName.equals(newName)) {
60c5bab237f577d743f78db923b0449566d34fd1d4John Spurlock                            return;  // no change to an existing rule, just dismiss
61c5bab237f577d743f78db923b0449566d34fd1d4John Spurlock                        }
62f7748e4ae15e49f9f4a9cc15a372e9a6d3c35c56Julia Reynolds                        onOk(newName);
6345fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock                    }
6445fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock                })
6545fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock                .setNegativeButton(R.string.cancel, null)
6645fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock                .create();
6745fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock    }
6845fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock
69f7748e4ae15e49f9f4a9cc15a372e9a6d3c35c56Julia Reynolds    abstract public void onOk(String ruleName);
7045fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock
7145fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock    public void show() {
7245fa140b8c6846b4546fdeabebf989ae9102cebbJohn Spurlock        mDialog.show();
737d8a9aead53f817c9df14cc5feaae223a8a834e8Julia Reynolds    }
747d8a9aead53f817c9df14cc5feaae223a8a834e8Julia Reynolds
75c96a5dcbfc3f37f71e8b3e8b962f9571b1e7ceafJohn Spurlock    private String trimmedText() {
76c96a5dcbfc3f37f71e8b3e8b962f9571b1e7ceafJohn Spurlock        return mEditText.getText() == null ? null : mEditText.getText().toString().trim();
77c96a5dcbfc3f37f71e8b3e8b962f9571b1e7ceafJohn Spurlock    }
7825cb8f06914538f337be66d9a784834a7373b58aJulia Reynolds}
79