1 /*
2  * Copyright (C) 2019 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 package com.android.settings.applications.specialaccess.notificationaccess;
17 
18 import android.app.Dialog;
19 import android.app.settings.SettingsEnums;
20 import android.content.ComponentName;
21 import android.content.DialogInterface;
22 import android.os.Bundle;
23 
24 import androidx.appcompat.app.AlertDialog;
25 import androidx.fragment.app.Fragment;
26 
27 import com.android.settings.R;
28 import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
29 
30 public class FriendlyWarningDialogFragment extends InstrumentedDialogFragment {
31     static final String KEY_COMPONENT = "c";
32     static final String KEY_LABEL = "l";
33 
setServiceInfo(ComponentName cn, CharSequence label, Fragment target)34     public FriendlyWarningDialogFragment setServiceInfo(ComponentName cn, CharSequence label,
35             Fragment target) {
36         Bundle args = new Bundle();
37         args.putString(KEY_COMPONENT, cn.flattenToString());
38         args.putCharSequence(KEY_LABEL, label);
39         setArguments(args);
40         setTargetFragment(target, 0);
41         return this;
42     }
43 
44     @Override
getMetricsCategory()45     public int getMetricsCategory() {
46         return SettingsEnums.DIALOG_DISABLE_NOTIFICATION_ACCESS;
47     }
48 
49     @Override
onCreateDialog(Bundle savedInstanceState)50     public Dialog onCreateDialog(Bundle savedInstanceState) {
51         final Bundle args = getArguments();
52         final CharSequence label = args.getCharSequence(KEY_LABEL);
53         final ComponentName cn = ComponentName.unflattenFromString(args
54                 .getString(KEY_COMPONENT));
55         NotificationAccessDetails parent = (NotificationAccessDetails) getTargetFragment();
56 
57         final String summary = getResources().getString(
58                 R.string.notification_listener_disable_warning_summary, label);
59         return new AlertDialog.Builder(getContext())
60                 .setMessage(summary)
61                 .setCancelable(true)
62                 .setPositiveButton(R.string.notification_listener_disable_warning_confirm,
63                         new DialogInterface.OnClickListener() {
64                             @Override
65                             public void onClick(DialogInterface dialog, int which) {
66                                 parent.disable(cn);
67                             }
68                         })
69                 .setNegativeButton(R.string.notification_listener_disable_warning_cancel,
70                         (dialog, id) -> {
71                             // pass
72                         })
73                 .create();
74     }
75 }
76