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