From: Geoffrey Pitsch Date: Mon, 17 Apr 2017 19:28:40 +0000 (-0400) Subject: Warning if app targeting O posts a notification with no channel X-Git-Tag: android-x86-9.0-r1~1044^2~1782^2 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=86c11e605ed2047eb35dae980275fb765a8a5b54;p=android-x86%2Fframeworks-base.git Warning if app targeting O posts a notification with no channel Uses toast, but does not prevent the notification from posting. Test: runtest systemui-notification Bug: 35378789 Change-Id: I81426ee22a004b1d766f810565822a1ea237ccb4 --- diff --git a/services/core/java/com/android/server/notification/NotificationManagerService.java b/services/core/java/com/android/server/notification/NotificationManagerService.java index f334ba41bdf1..f681a30acc87 100644 --- a/services/core/java/com/android/server/notification/NotificationManagerService.java +++ b/services/core/java/com/android/server/notification/NotificationManagerService.java @@ -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)) {