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 
17 package com.android.settings.applications.specialaccess.zenaccess;
18 
19 import android.app.Dialog;
20 import android.app.settings.SettingsEnums;
21 import android.os.Bundle;
22 import android.text.TextUtils;
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 /**
31  * Warning dialog when allowing zen access warning about the privileges being granted.
32  */
33 public class ScaryWarningDialogFragment extends InstrumentedDialogFragment {
34     static final String KEY_PKG = "p";
35     static final String KEY_LABEL = "l";
36 
37     @Override
getMetricsCategory()38     public int getMetricsCategory() {
39         return SettingsEnums.DIALOG_ZEN_ACCESS_GRANT;
40     }
41 
setPkgInfo(String pkg, CharSequence label, Fragment target)42     public ScaryWarningDialogFragment setPkgInfo(String pkg, CharSequence label, Fragment target) {
43         Bundle args = new Bundle();
44         args.putString(KEY_PKG, pkg);
45         args.putString(KEY_LABEL, TextUtils.isEmpty(label) ? pkg : label.toString());
46         setTargetFragment(target, 0);
47         setArguments(args);
48         return this;
49     }
50 
51     @Override
onCreateDialog(Bundle savedInstanceState)52     public Dialog onCreateDialog(Bundle savedInstanceState) {
53         super.onCreate(savedInstanceState);
54         final Bundle args = getArguments();
55         final String pkg = args.getString(KEY_PKG);
56         final String label = args.getString(KEY_LABEL);
57 
58         final String title = getResources().getString(R.string.zen_access_warning_dialog_title,
59                 label);
60         final String summary = getResources()
61                 .getString(R.string.zen_access_warning_dialog_summary);
62 
63         ZenAccessDetails parent = (ZenAccessDetails) getTargetFragment();
64 
65         return new AlertDialog.Builder(getContext())
66                 .setMessage(summary)
67                 .setTitle(title)
68                 .setCancelable(true)
69                 .setPositiveButton(R.string.allow,
70                         (dialog, id) -> {
71                             ZenAccessController.setAccess(getContext(), pkg, true);
72                             parent.refreshUi();
73                         })
74                 .setNegativeButton(R.string.deny,
75                         (dialog, id) -> {
76                             // pass
77                         })
78                 .create();
79     }
80 }
81