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