1 /*
2  * Copyright (c) 2017 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.DialogInterface;
22 import android.os.Bundle;
23 import android.text.BidiFormatter;
24 import android.view.View;
25 
26 import androidx.appcompat.app.AlertDialog;
27 import androidx.fragment.app.Fragment;
28 
29 import com.android.settings.R;
30 import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
31 
32 public class ZenDeleteRuleDialog extends InstrumentedDialogFragment {
33     protected static final String TAG = "ZenDeleteRuleDialog";
34     private static final String EXTRA_ZEN_RULE_NAME = "zen_rule_name";
35     private static final String EXTRA_ZEN_RULE_ID = "zen_rule_id";
36     protected static PositiveClickListener mPositiveClickListener;
37 
38     /**
39      * The interface we expect a listener to implement.
40      */
41     public interface PositiveClickListener {
onOk(String id)42         void onOk(String id);
43     }
44 
show(Fragment parent, String ruleName, String id, PositiveClickListener listener)45     public static void show(Fragment parent, String ruleName, String id, PositiveClickListener
46             listener) {
47         final BidiFormatter bidi = BidiFormatter.getInstance();
48         final Bundle args = new Bundle();
49         args.putString(EXTRA_ZEN_RULE_NAME, bidi.unicodeWrap(ruleName));
50         args.putString(EXTRA_ZEN_RULE_ID, id);
51         mPositiveClickListener = listener;
52 
53         ZenDeleteRuleDialog dialog = new ZenDeleteRuleDialog();
54         dialog.setArguments(args);
55         dialog.setTargetFragment(parent, 0);
56         dialog.show(parent.getFragmentManager(), TAG);
57     }
58 
59     @Override
getMetricsCategory()60     public int getMetricsCategory() {
61         return SettingsEnums.NOTIFICATION_ZEN_MODE_DELETE_RULE_DIALOG;
62     }
63 
64     @Override
onCreateDialog(Bundle savedInstanceState)65     public Dialog onCreateDialog(Bundle savedInstanceState) {
66         Bundle arguments = getArguments();
67         String ruleName = arguments.getString(EXTRA_ZEN_RULE_NAME);
68         String id = arguments.getString(EXTRA_ZEN_RULE_ID);
69 
70         final AlertDialog dialog = new AlertDialog.Builder(getContext())
71                 .setMessage(getString(R.string.zen_mode_delete_rule_confirmation, ruleName))
72                 .setNegativeButton(R.string.cancel, null)
73                 .setPositiveButton(R.string.zen_mode_delete_rule_button,
74                         new DialogInterface.OnClickListener() {
75                     @Override
76                     public void onClick(DialogInterface dialog, int which) {
77                         if (arguments != null) {
78                             mPositiveClickListener.onOk(id);
79                         }
80                     }
81                 }).create();
82         final View messageView = dialog.findViewById(android.R.id.message);
83         if (messageView != null) {
84             messageView.setTextDirection(View.TEXT_DIRECTION_LOCALE);
85         }
86         return dialog;
87     }
88 
89 }
90