Skip to content

Commit 7884795

Browse files
Guojing Yuanaoleary
Guojing Yuan
authored andcommitted
[CDM][NLS] Check if the NLS service has an intent-filter
Bug: 363248394 Test: CTS Flag: EXEMPT bugfix (cherry picked from commit 7ae59a42eb13f643d842525208619037c074371a) (cherry picked from https://googleplex-android-review.googlesource.com/q/commit:19914088c3f437c504988f64261e62a99c9d113b) Merged-In: Ib79c219cde8d73a218ceb7911f4552d43e384d8e Change-Id: Ib79c219cde8d73a218ceb7911f4552d43e384d8e
1 parent 18f989e commit 7884795

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

src/com/android/settings/notification/NotificationAccessConfirmationActivity.java

+30-20
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@
3131
import android.content.ComponentName;
3232
import android.content.Context;
3333
import android.content.DialogInterface;
34+
import android.content.Intent;
3435
import android.content.pm.ApplicationInfo;
3536
import android.content.pm.PackageItemInfo;
3637
import android.content.pm.PackageManager;
37-
import android.content.pm.ServiceInfo;
38+
import android.content.pm.ResolveInfo;
3839
import android.os.Bundle;
3940
import android.os.UserHandle;
4041
import android.os.UserManager;
42+
import android.service.notification.NotificationListenerService;
4143
import android.text.TextUtils;
4244
import android.util.Slog;
4345
import android.view.WindowManager;
@@ -48,6 +50,8 @@
4850
import com.android.internal.app.AlertController;
4951
import com.android.settings.R;
5052

53+
import java.util.List;
54+
5155
/** @hide */
5256
public class NotificationAccessConfirmationActivity extends Activity
5357
implements DialogInterface {
@@ -112,6 +116,31 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
112116
return;
113117
}
114118

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+
115144
AlertController.AlertParams p = new AlertController.AlertParams(this);
116145
p.mTitle = getString(
117146
R.string.notification_listener_security_warning_title,
@@ -146,19 +175,6 @@ public void onPause() {
146175
}
147176

148177
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-
162178
mNm.setNotificationListenerAccessGranted(mComponentName, true);
163179

164180
finish();
@@ -169,12 +185,6 @@ public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
169185
return AlertActivity.dispatchPopulateAccessibilityEvent(this, event);
170186
}
171187

172-
@Override
173-
public void onBackPressed() {
174-
// Suppress finishing the activity on back button press,
175-
// consistently with the permission dialog behavior
176-
}
177-
178188
@Override
179189
public void cancel() {
180190
finish();

0 commit comments

Comments
 (0)