1 /* 2 * Copyright (C) 2017 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 18 package com.android.settings.notification; 19 20 import static com.android.internal.notification.NotificationAccessConfirmationActivityContract 21 .EXTRA_COMPONENT_NAME; 22 import static com.android.internal.notification.NotificationAccessConfirmationActivityContract 23 .EXTRA_PACKAGE_TITLE; 24 import static com.android.internal.notification.NotificationAccessConfirmationActivityContract 25 .EXTRA_USER_ID; 26 27 import android.Manifest; 28 import android.annotation.Nullable; 29 import android.app.Activity; 30 import android.app.NotificationManager; 31 import android.content.ComponentName; 32 import android.content.Context; 33 import android.content.DialogInterface; 34 import android.content.pm.PackageManager; 35 import android.content.pm.ServiceInfo; 36 import android.os.Bundle; 37 import android.os.UserHandle; 38 import android.util.Slog; 39 import android.view.WindowManager; 40 import android.view.accessibility.AccessibilityEvent; 41 42 import com.android.internal.app.AlertActivity; 43 import com.android.internal.app.AlertController; 44 import com.android.settings.R; 45 46 /** @hide */ 47 public class NotificationAccessConfirmationActivity extends Activity 48 implements DialogInterface { 49 50 private static final boolean DEBUG = false; 51 private static final String LOG_TAG = "NotificationAccessConfirmationActivity"; 52 53 private int mUserId; 54 private ComponentName mComponentName; 55 private NotificationManager mNm; 56 57 @Override onCreate(@ullable Bundle savedInstanceState)58 protected void onCreate(@Nullable Bundle savedInstanceState) { 59 super.onCreate(savedInstanceState); 60 61 mNm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 62 63 mComponentName = getIntent().getParcelableExtra(EXTRA_COMPONENT_NAME); 64 mUserId = getIntent().getIntExtra(EXTRA_USER_ID, UserHandle.USER_NULL); 65 String pkgTitle = getIntent().getStringExtra(EXTRA_PACKAGE_TITLE); 66 67 AlertController.AlertParams p = new AlertController.AlertParams(this); 68 p.mTitle = getString( 69 R.string.notification_listener_security_warning_title, 70 pkgTitle); 71 p.mMessage = getString( 72 R.string.notification_listener_security_warning_summary, 73 pkgTitle); 74 p.mPositiveButtonText = getString(R.string.allow); 75 p.mPositiveButtonListener = (a, b) -> onAllow(); 76 p.mNegativeButtonText = getString(R.string.deny); 77 p.mNegativeButtonListener = (a, b) -> cancel(); 78 AlertController 79 .create(this, this, getWindow()) 80 .installContent(p); 81 // Consistent with the permission dialog 82 // Used instead of p.mCancelable as that is only honored for AlertDialog 83 getWindow().setCloseOnTouchOutside(false); 84 } 85 86 @Override onResume()87 public void onResume() { 88 super.onResume(); 89 getWindow().addFlags( 90 WindowManager.LayoutParams.PRIVATE_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS); 91 } 92 93 @Override onPause()94 public void onPause() { 95 getWindow().clearFlags( 96 WindowManager.LayoutParams.PRIVATE_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS); 97 super.onPause(); 98 } 99 onAllow()100 private void onAllow() { 101 String requiredPermission = Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE; 102 try { 103 ServiceInfo serviceInfo = getPackageManager().getServiceInfo(mComponentName, 0); 104 if (!requiredPermission.equals(serviceInfo.permission)) { 105 Slog.e(LOG_TAG, 106 "Service " + mComponentName + " lacks permission " + requiredPermission); 107 return; 108 } 109 } catch (PackageManager.NameNotFoundException e) { 110 Slog.e(LOG_TAG, "Failed to get service info for " + mComponentName, e); 111 return; 112 } 113 114 mNm.setNotificationListenerAccessGranted(mComponentName, true); 115 116 finish(); 117 } 118 119 @Override dispatchPopulateAccessibilityEvent(AccessibilityEvent event)120 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) { 121 return AlertActivity.dispatchPopulateAccessibilityEvent(this, event); 122 } 123 124 @Override onBackPressed()125 public void onBackPressed() { 126 // Suppress finishing the activity on back button press, 127 // consistently with the permission dialog behavior 128 } 129 130 @Override cancel()131 public void cancel() { 132 finish(); 133 } 134 135 @Override dismiss()136 public void dismiss() { 137 // This is called after the click, since we finish when handling the 138 // click, don't do that again here. 139 if (!isFinishing()) { 140 finish(); 141 } 142 } 143 } 144