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.app.Dialog;
     21 import android.app.Fragment;
     22 import android.content.Context;
     23 import android.content.DialogInterface;
     24 import android.net.Uri;
     25 import android.os.Bundle;
     26 import android.service.notification.ZenModeConfig;
     27 import android.text.TextUtils;
     28 import android.view.LayoutInflater;
     29 import android.view.View;
     30 import android.widget.EditText;
     31 
     32 import com.android.internal.logging.nano.MetricsProto;
     33 import com.android.settings.R;
     34 import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
     35 
     36 public class ZenRuleNameDialog extends InstrumentedDialogFragment {
     37     protected static final String TAG = "ZenRuleNameDialog";
     38     private static final String EXTRA_ZEN_RULE_NAME = "zen_rule_name";
     39     private static final String EXTRA_CONDITION_ID = "extra_zen_condition_id";
     40     protected static PositiveClickListener mPositiveClickListener;
     41 
     42     @Override
     43     public int getMetricsCategory() {
     44         return MetricsProto.MetricsEvent.NOTIFICATION_ZEN_MODE_RULE_NAME_DIALOG;
     45     }
     46 
     47     /**
     48      * The interface we expect a listener to implement.
     49      */
     50     public interface PositiveClickListener {
     51         void onOk(String newName, Fragment parent);
     52     }
     53 
     54     public static void show(Fragment parent, String ruleName, Uri conditionId, PositiveClickListener
     55             listener) {
     56         final Bundle args = new Bundle();
     57         args.putString(EXTRA_ZEN_RULE_NAME, ruleName);
     58         args.putParcelable(EXTRA_CONDITION_ID, conditionId);
     59         mPositiveClickListener = listener;
     60 
     61         ZenRuleNameDialog dialog = new ZenRuleNameDialog();
     62         dialog.setArguments(args);
     63         dialog.setTargetFragment(parent, 0);
     64         dialog.show(parent.getFragmentManager(), TAG);
     65     }
     66 
     67     @Override
     68     public Dialog onCreateDialog(Bundle savedInstanceState) {
     69         Bundle arguments = getArguments();
     70         Uri conditionId = arguments.getParcelable(EXTRA_CONDITION_ID);
     71         String ruleName = arguments.getString(EXTRA_ZEN_RULE_NAME);
     72 
     73         boolean isNew = ruleName == null;
     74         CharSequence originalRuleName = ruleName;
     75         Context context = getContext();
     76         final View v = LayoutInflater.from(context).inflate(R.layout.zen_rule_name, null,
     77                 false);
     78         EditText editText = (EditText) v.findViewById(R.id.zen_mode_rule_name);
     79         if (!isNew) {
     80             // set text to current rule name
     81             editText.setText(ruleName);
     82             // move cursor to end of text
     83             editText.setSelection(editText.getText().length());
     84         }
     85         editText.setSelectAllOnFocus(true);
     86         return new AlertDialog.Builder(context)
     87                 .setTitle(getTitleResource(conditionId, isNew))
     88                 .setView(v)
     89                 .setPositiveButton(isNew ? R.string.zen_mode_add : R.string.okay,
     90                         new DialogInterface.OnClickListener() {
     91                             @Override
     92                             public void onClick(DialogInterface dialog, int which) {
     93                                 final String newName = trimmedText(editText);
     94                                 if (TextUtils.isEmpty(newName)) {
     95                                     return;
     96                                 }
     97                                 if (!isNew && originalRuleName != null
     98                                         && originalRuleName.equals(newName)) {
     99                                     return;  // no change to an existing rule, just dismiss
    100                                 }
    101                                mPositiveClickListener.onOk(newName, getTargetFragment());
    102                             }
    103                         })
    104                 .setNegativeButton(R.string.cancel, null)
    105                 .create();
    106     }
    107 
    108     private String trimmedText(EditText editText) {
    109         return editText.getText() == null ? null : editText.getText().toString().trim();
    110     }
    111 
    112     private int getTitleResource(Uri conditionId, boolean isNew) {
    113         final boolean isEvent = ZenModeConfig.isValidEventConditionId(conditionId);
    114         final boolean isTime = ZenModeConfig.isValidScheduleConditionId(conditionId);
    115         int titleResource =  R.string.zen_mode_rule_name;
    116         if (isNew) {
    117             if (isEvent) {
    118                 titleResource = R.string.zen_mode_add_event_rule;
    119             } else if (isTime) {
    120                 titleResource = R.string.zen_mode_add_time_rule;
    121             }
    122         }
    123         return titleResource;
    124     }
    125 }
    126