OSDN Git Service

Warning if app targeting O posts a notification with no channel
authorGeoffrey Pitsch <gpitsch@google.com>
Mon, 17 Apr 2017 19:28:40 +0000 (15:28 -0400)
committerGeoffrey Pitsch <gpitsch@google.com>
Mon, 17 Apr 2017 19:28:40 +0000 (15:28 -0400)
Uses toast, but does not prevent the notification from posting.

Test: runtest systemui-notification
Bug: 35378789
Change-Id: I81426ee22a004b1d766f810565822a1ea237ccb4

services/core/java/com/android/server/notification/NotificationManagerService.java

index f334ba4..f681a30 100644 (file)
@@ -97,6 +97,7 @@ import android.media.AudioManagerInternal;
 import android.media.IRingtonePlayer;
 import android.net.Uri;
 import android.os.Binder;
+import android.os.Build;
 import android.os.Bundle;
 import android.os.Environment;
 import android.os.Handler;
@@ -3162,16 +3163,16 @@ public class NotificationManagerService extends SystemService {
             // STOPSHIP TODO: should throw instead of logging or toasting.
             // throw new IllegalArgumentException(noChannelStr);
             Log.e(TAG, noChannelStr);
-
-            final String noChannelToastStr =
-                    "Developer warning for package \"" + pkg + "\"\n" +
+            doDebugOnlyToast("Developer warning for package \"" + pkg + "\"\n" +
                     "Failed to post notification on channel \"" + channelId + "\"\n" +
-                    "See log for more details";
-            Toast noChannelToast =
-                    Toast.makeText(getContext(), noChannelToastStr, Toast.LENGTH_LONG);
-            noChannelToast.show();
+                    "See log for more details");
             return;
+        } else if (channelId == null && shouldWarnUseChannels(pkg, notificationUid)) {
+            // STOPSHIP TODO: remove once default channel is removed for all apps that target O.
+            doDebugOnlyToast("Developer warning for package \"" + pkg + "\"\n" +
+                    "Posted notification should specify a channel");
         }
+
         final StatusBarNotification n = new StatusBarNotification(
                 pkg, opPkg, id, tag, notificationUid, callingPid, notification,
                 user, null, System.currentTimeMillis());
@@ -3203,6 +3204,26 @@ public class NotificationManagerService extends SystemService {
         idOut[0] = id;
     }
 
+    private void doDebugOnlyToast(CharSequence toastText) {
+        if (Build.IS_DEBUGGABLE) {
+            Toast toast = Toast.makeText(getContext(), toastText, Toast.LENGTH_LONG);
+            toast.show();
+        }
+    }
+
+    // STOPSHIP - Remove once RankingHelper deletes default channel for all apps targeting O.
+    private boolean shouldWarnUseChannels(String pkg, int uid) {
+        try {
+            final int userId = UserHandle.getUserId(uid);
+            final ApplicationInfo applicationInfo =
+                    mPackageManagerClient.getApplicationInfoAsUser(pkg, 0, userId);
+            return applicationInfo.targetSdkVersion > Build.VERSION_CODES.N_MR1;
+        } catch (NameNotFoundException e) {
+            Slog.e(TAG, e.toString());
+            return false;
+        }
+    }
+
     private int resolveNotificationUid(String opPackageName, int callingUid, int userId) {
         // The system can post notifications on behalf of any package it wants
         if (isCallerSystem() && opPackageName != null && !"android".equals(opPackageName)) {