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.Context; 22 import android.content.pm.PackageManager; 23 import android.graphics.drawable.Drawable; 24 import android.os.Bundle; 25 import android.view.LayoutInflater; 26 import android.view.View; 27 import android.widget.Button; 28 import android.widget.ImageView; 29 import android.widget.TextView; 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 38 public class ScaryWarningDialogFragment extends InstrumentedDialogFragment { 39 private static final String KEY_COMPONENT = "c"; 40 private static final String KEY_LABEL = "l"; 41 42 @Override getMetricsCategory()43 public int getMetricsCategory() { 44 return SettingsEnums.DIALOG_NOTIFICATION_ACCESS_GRANT; 45 } 46 setServiceInfo(ComponentName cn, CharSequence label, Fragment target)47 public ScaryWarningDialogFragment setServiceInfo(ComponentName cn, CharSequence label, 48 Fragment target) { 49 Bundle args = new Bundle(); 50 args.putString(KEY_COMPONENT, cn.flattenToString()); 51 args.putCharSequence(KEY_LABEL, label); 52 setArguments(args); 53 setTargetFragment(target, 0); 54 return this; 55 } 56 57 @Override onCreateDialog(Bundle savedInstanceState)58 public Dialog onCreateDialog(Bundle savedInstanceState) { 59 final Bundle args = getArguments(); 60 final CharSequence label = args.getCharSequence(KEY_LABEL); 61 final ComponentName cn = ComponentName.unflattenFromString(args 62 .getString(KEY_COMPONENT)); 63 NotificationAccessDetails parent = (NotificationAccessDetails) getTargetFragment(); 64 65 return new AlertDialog.Builder(getContext()) 66 .setView(getDialogView(getContext(), label, parent, cn)) 67 .setCancelable(true) 68 .create(); 69 } 70 getDialogView(Context context, CharSequence label, NotificationAccessDetails parent, ComponentName cn)71 private View getDialogView(Context context, CharSequence label, 72 NotificationAccessDetails parent, ComponentName cn) { 73 LayoutInflater inflater = (LayoutInflater) context.getSystemService( 74 Context.LAYOUT_INFLATER_SERVICE); 75 76 View content = inflater.inflate(R.layout.enable_nls_dialog_content, null); 77 78 Drawable icon = null; 79 try { 80 icon = context.getPackageManager().getApplicationIcon(cn.getPackageName()); 81 } catch (PackageManager.NameNotFoundException e) { 82 } 83 84 ImageView appIcon = content.findViewById(R.id.app_icon); 85 if (icon != null) { 86 appIcon.setImageDrawable(icon); 87 } else { 88 appIcon.setVisibility(View.GONE); 89 } 90 91 final String title = context.getResources().getString( 92 R.string.notification_listener_security_warning_title, label); 93 ((TextView) content.findViewById(R.id.title)).setText(title); 94 95 final String prompt = context.getResources().getString( 96 R.string.nls_warning_prompt, label); 97 ((TextView) content.findViewById(R.id.prompt)).setText(prompt); 98 99 Button allowButton = content.findViewById(R.id.allow_button); 100 allowButton.setOnClickListener((view) -> { 101 parent.enable(cn); 102 dismiss(); 103 }); 104 Button denyButton = content.findViewById(R.id.deny_button); 105 denyButton.setOnClickListener((view) -> { 106 dismiss(); 107 }); 108 return content; 109 } 110 }