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