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 31 public class ScaryWarningDialogFragment extends InstrumentedDialogFragment { 32 private static final String KEY_COMPONENT = "c"; 33 private static final String KEY_LABEL = "l"; 34 35 @Override getMetricsCategory()36 public int getMetricsCategory() { 37 return SettingsEnums.DIALOG_SERVICE_ACCESS_WARNING; 38 } 39 setServiceInfo(ComponentName cn, CharSequence label, Fragment target)40 public ScaryWarningDialogFragment setServiceInfo(ComponentName cn, CharSequence label, 41 Fragment target) { 42 Bundle args = new Bundle(); 43 args.putString(KEY_COMPONENT, cn.flattenToString()); 44 args.putCharSequence(KEY_LABEL, label); 45 setArguments(args); 46 setTargetFragment(target, 0); 47 return this; 48 } 49 50 @Override onCreateDialog(Bundle savedInstanceState)51 public Dialog onCreateDialog(Bundle savedInstanceState) { 52 final Bundle args = getArguments(); 53 final CharSequence label = args.getCharSequence(KEY_LABEL); 54 final ComponentName cn = ComponentName.unflattenFromString(args 55 .getString(KEY_COMPONENT)); 56 NotificationAccessDetails parent = (NotificationAccessDetails) getTargetFragment(); 57 58 final String title = getResources().getString( 59 R.string.notification_listener_security_warning_title, label); 60 final String summary = getResources().getString( 61 R.string.notification_listener_security_warning_summary, label); 62 return new AlertDialog.Builder(getContext()) 63 .setMessage(summary) 64 .setTitle(title) 65 .setCancelable(true) 66 .setPositiveButton(R.string.allow, 67 new DialogInterface.OnClickListener() { 68 @Override 69 public void onClick(DialogInterface dialog, int which) { 70 parent.enable(cn); 71 } 72 }) 73 .setNegativeButton(R.string.deny, 74 (dialog, id) -> { 75 // pass 76 }) 77 .create(); 78 } 79 }