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 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 } 82 83 @Override 84 public void onResume() { 85 super.onResume(); 86 getWindow().addFlags( 87 WindowManager.LayoutParams.PRIVATE_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS); 88 } 89 90 @Override 91 public void onPause() { 92 getWindow().clearFlags( 93 WindowManager.LayoutParams.PRIVATE_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS); 94 super.onPause(); 95 } 96 97 private void onAllow() { 98 String requiredPermission = Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE; 99 try { 100 ServiceInfo serviceInfo = getPackageManager().getServiceInfo(mComponentName, 0); 101 if (!requiredPermission.equals(serviceInfo.permission)) { 102 Slog.e(LOG_TAG, 103 "Service " + mComponentName + " lacks permission " + requiredPermission); 104 return; 105 } 106 } catch (PackageManager.NameNotFoundException e) { 107 Slog.e(LOG_TAG, "Failed to get service info for " + mComponentName, e); 108 return; 109 } 110 111 mNm.setNotificationListenerAccessGranted(mComponentName, true); 112 113 finish(); 114 } 115 116 @Override 117 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) { 118 return AlertActivity.dispatchPopulateAccessibilityEvent(this, event); 119 } 120 121 @Override 122 public void cancel() { 123 finish(); 124 } 125 126 @Override 127 public void dismiss() { 128 // This is called after the click, since we finish when handling the 129 // click, don't do that again here. 130 if (!isFinishing()) { 131 finish(); 132 } 133 } 134 } 135