OSDN Git Service

Check target api version before crashing apps.
authorJulia Reynolds <juliacr@google.com>
Wed, 11 Oct 2017 12:44:46 +0000 (08:44 -0400)
committerandroid-build-team Robot <android-build-team-robot@google.com>
Thu, 12 Oct 2017 04:35:56 +0000 (04:35 +0000)
All foreground service notifications need to be posted to a valid
notification channel, but only crash apps that don't provide a
valid notification if they target O_MR1+.

Fixes: 66905243
Test: manual, open apps with invalid notifications that do and don't
target O_MR1.

Change-Id: I60897302bf9806cba0e0be365e65c5e8c4ef4806
(cherry picked from commit 5a4399a34cd73d80041348463fd6e9d5954f77ef)

services/core/java/com/android/server/am/ServiceRecord.java

index ac85e6b..16995e5 100644 (file)
@@ -33,6 +33,7 @@ import android.content.pm.PackageManager;
 import android.content.pm.ServiceInfo;
 import android.net.Uri;
 import android.os.Binder;
+import android.os.Build;
 import android.os.IBinder;
 import android.os.RemoteException;
 import android.os.SystemClock;
@@ -517,14 +518,27 @@ final class ServiceRecord extends Binder {
                             } catch (PackageManager.NameNotFoundException e) {
                             }
                         }
-                        if (localForegroundNoti.getSmallIcon() == null
-                                || nm.getNotificationChannel(localPackageName, appUid,
+                        if (nm.getNotificationChannel(localPackageName, appUid,
                                 localForegroundNoti.getChannelId()) == null) {
+                            int targetSdkVersion = Build.VERSION_CODES.O_MR1;
+                            try {
+                                final ApplicationInfo applicationInfo =
+                                        ams.mContext.getPackageManager().getApplicationInfoAsUser(
+                                                appInfo.packageName, 0, userId);
+                                targetSdkVersion = applicationInfo.targetSdkVersion;
+                            } catch (PackageManager.NameNotFoundException e) {
+                            }
+                            if (targetSdkVersion >= Build.VERSION_CODES.O_MR1) {
+                                throw new RuntimeException(
+                                        "invalid channel for service notification: "
+                                                + foregroundNoti);
+                            }
+                        }
+                        if (localForegroundNoti.getSmallIcon() == null) {
                             // Notifications whose icon is 0 are defined to not show
                             // a notification, silently ignoring it.  We don't want to
                             // just ignore it, we want to prevent the service from
                             // being foreground.
-                            // Also every notification needs a channel.
                             throw new RuntimeException("invalid service notification: "
                                     + foregroundNoti);
                         }