31
31
import android .content .ComponentName ;
32
32
import android .content .Context ;
33
33
import android .content .DialogInterface ;
34
+ import android .content .Intent ;
34
35
import android .content .pm .ApplicationInfo ;
35
36
import android .content .pm .PackageItemInfo ;
36
37
import android .content .pm .PackageManager ;
37
- import android .content .pm .ServiceInfo ;
38
+ import android .content .pm .ResolveInfo ;
38
39
import android .os .Bundle ;
39
40
import android .os .UserHandle ;
40
41
import android .os .UserManager ;
42
+ import android .service .notification .NotificationListenerService ;
41
43
import android .text .TextUtils ;
42
44
import android .util .Slog ;
43
45
import android .view .WindowManager ;
48
50
import com .android .internal .app .AlertController ;
49
51
import com .android .settings .R ;
50
52
53
+ import java .util .List ;
54
+
51
55
/** @hide */
52
56
public class NotificationAccessConfirmationActivity extends Activity
53
57
implements DialogInterface {
@@ -112,6 +116,31 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
112
116
return ;
113
117
}
114
118
119
+ // Check NLS service info.
120
+ String requiredPermission = Manifest .permission .BIND_NOTIFICATION_LISTENER_SERVICE ;
121
+ Intent NLSIntent = new Intent (NotificationListenerService .SERVICE_INTERFACE );
122
+ List <ResolveInfo > matchedServiceList = getPackageManager ().queryIntentServicesAsUser (
123
+ NLSIntent , /* flags */ 0 , mUserId );
124
+ boolean hasNLSIntentFilter = false ;
125
+ for (ResolveInfo service : matchedServiceList ) {
126
+ if (service .serviceInfo .packageName .equals (mComponentName .getPackageName ())) {
127
+ if (!requiredPermission .equals (service .serviceInfo .permission )) {
128
+ Slog .e (LOG_TAG , "Service " + mComponentName + " lacks permission "
129
+ + requiredPermission );
130
+ finish ();
131
+ return ;
132
+ }
133
+ hasNLSIntentFilter = true ;
134
+ break ;
135
+ }
136
+ }
137
+ if (!hasNLSIntentFilter ) {
138
+ Slog .e (LOG_TAG , "Service " + mComponentName + " lacks an intent-filter action "
139
+ + "for android.service.notification.NotificationListenerService." );
140
+ finish ();
141
+ return ;
142
+ }
143
+
115
144
AlertController .AlertParams p = new AlertController .AlertParams (this );
116
145
p .mTitle = getString (
117
146
R .string .notification_listener_security_warning_title ,
@@ -146,19 +175,6 @@ public void onPause() {
146
175
}
147
176
148
177
private void onAllow () {
149
- String requiredPermission = Manifest .permission .BIND_NOTIFICATION_LISTENER_SERVICE ;
150
- try {
151
- ServiceInfo serviceInfo = getPackageManager ().getServiceInfo (mComponentName , 0 );
152
- if (!requiredPermission .equals (serviceInfo .permission )) {
153
- Slog .e (LOG_TAG ,
154
- "Service " + mComponentName + " lacks permission " + requiredPermission );
155
- return ;
156
- }
157
- } catch (PackageManager .NameNotFoundException e ) {
158
- Slog .e (LOG_TAG , "Failed to get service info for " + mComponentName , e );
159
- return ;
160
- }
161
-
162
178
mNm .setNotificationListenerAccessGranted (mComponentName , true );
163
179
164
180
finish ();
@@ -169,12 +185,6 @@ public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
169
185
return AlertActivity .dispatchPopulateAccessibilityEvent (this , event );
170
186
}
171
187
172
- @ Override
173
- public void onBackPressed () {
174
- // Suppress finishing the activity on back button press,
175
- // consistently with the permission dialog behavior
176
- }
177
-
178
188
@ Override
179
189
public void cancel () {
180
190
finish ();
0 commit comments