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