Search in sources :

Example 41 with Notification

use of android.app.Notification in project platform_frameworks_base by android.

the class ServiceRecord method postNotification.

public void postNotification() {
    final int appUid = appInfo.uid;
    final int appPid = app.pid;
    if (foregroundId != 0 && foregroundNoti != null) {
        // Do asynchronous communication with notification manager to
        // avoid deadlocks.
        final String localPackageName = packageName;
        final int localForegroundId = foregroundId;
        final Notification _foregroundNoti = foregroundNoti;
        ams.mHandler.post(new Runnable() {

            public void run() {
                NotificationManagerInternal nm = LocalServices.getService(NotificationManagerInternal.class);
                if (nm == null) {
                    return;
                }
                Notification localForegroundNoti = _foregroundNoti;
                try {
                    if (localForegroundNoti.getSmallIcon() == null) {
                        // It is not correct for the caller to not supply a notification
                        // icon, but this used to be able to slip through, so for
                        // those dirty apps we will create a notification clearly
                        // blaming the app.
                        Slog.v(TAG, "Attempted to start a foreground service (" + name + ") with a broken notification (no icon: " + localForegroundNoti + ")");
                        CharSequence appName = appInfo.loadLabel(ams.mContext.getPackageManager());
                        if (appName == null) {
                            appName = appInfo.packageName;
                        }
                        Context ctx = null;
                        try {
                            ctx = ams.mContext.createPackageContextAsUser(appInfo.packageName, 0, new UserHandle(userId));
                            Notification.Builder notiBuilder = new Notification.Builder(ctx);
                            // it's ugly, but it clearly identifies the app
                            notiBuilder.setSmallIcon(appInfo.icon);
                            // mark as foreground
                            notiBuilder.setFlag(Notification.FLAG_FOREGROUND_SERVICE, true);
                            // we are doing the app a kindness here
                            notiBuilder.setPriority(Notification.PRIORITY_MIN);
                            Intent runningIntent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                            runningIntent.setData(Uri.fromParts("package", appInfo.packageName, null));
                            PendingIntent pi = PendingIntent.getActivity(ams.mContext, 0, runningIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                            notiBuilder.setColor(ams.mContext.getColor(com.android.internal.R.color.system_notification_accent_color));
                            notiBuilder.setContentTitle(ams.mContext.getString(com.android.internal.R.string.app_running_notification_title, appName));
                            notiBuilder.setContentText(ams.mContext.getString(com.android.internal.R.string.app_running_notification_text, appName));
                            notiBuilder.setContentIntent(pi);
                            localForegroundNoti = notiBuilder.build();
                        } catch (PackageManager.NameNotFoundException e) {
                        }
                    }
                    if (localForegroundNoti.getSmallIcon() == null) {
                        // being foreground.
                        throw new RuntimeException("invalid service notification: " + foregroundNoti);
                    }
                    int[] outId = new int[1];
                    nm.enqueueNotification(localPackageName, localPackageName, appUid, appPid, null, localForegroundId, localForegroundNoti, outId, userId);
                    // save it for amending next time
                    foregroundNoti = localForegroundNoti;
                } catch (RuntimeException e) {
                    Slog.w(TAG, "Error showing notification for service", e);
                    // If it gave us a garbage notification, it doesn't
                    // get to be foreground.
                    ams.setServiceForeground(name, ServiceRecord.this, 0, null, 0);
                    ams.crashApplication(appUid, appPid, localPackageName, "Bad notification for startForeground: " + e);
                }
            }
        });
    }
}
Also used : NotificationManagerInternal(com.android.server.notification.NotificationManagerInternal) Context(android.content.Context) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) Notification(android.app.Notification) UserHandle(android.os.UserHandle) PendingIntent(android.app.PendingIntent)

Example 42 with Notification

use of android.app.Notification in project platform_frameworks_base by android.

the class NotificationManagerService method maybeAddAutobundleSummary.

// Posts a 'fake' summary for a package that has exceeded the solo-notification limit.
private void maybeAddAutobundleSummary(Adjustment adjustment) {
    if (adjustment.getSignals() != null) {
        Bundle.setDefusable(adjustment.getSignals(), true);
        if (adjustment.getSignals().getBoolean(Adjustment.NEEDS_AUTOGROUPING_KEY, false)) {
            final String newAutoBundleKey = adjustment.getSignals().getString(Adjustment.GROUP_KEY_OVERRIDE_KEY, null);
            int userId = -1;
            NotificationRecord summaryRecord = null;
            synchronized (mNotificationList) {
                NotificationRecord notificationRecord = mNotificationsByKey.get(adjustment.getKey());
                if (notificationRecord == null) {
                    // adjustment will post a summary if needed.
                    return;
                }
                final StatusBarNotification adjustedSbn = notificationRecord.sbn;
                userId = adjustedSbn.getUser().getIdentifier();
                ArrayMap<String, String> summaries = mAutobundledSummaries.get(userId);
                if (summaries == null) {
                    summaries = new ArrayMap<>();
                }
                mAutobundledSummaries.put(userId, summaries);
                if (!summaries.containsKey(adjustment.getPackage()) && newAutoBundleKey != null) {
                    // Add summary
                    final ApplicationInfo appInfo = adjustedSbn.getNotification().extras.getParcelable(Notification.EXTRA_BUILDER_APPLICATION_INFO);
                    final Bundle extras = new Bundle();
                    extras.putParcelable(Notification.EXTRA_BUILDER_APPLICATION_INFO, appInfo);
                    final Notification summaryNotification = new Notification.Builder(getContext()).setSmallIcon(adjustedSbn.getNotification().getSmallIcon()).setGroupSummary(true).setGroup(newAutoBundleKey).setFlag(Notification.FLAG_AUTOGROUP_SUMMARY, true).setFlag(Notification.FLAG_GROUP_SUMMARY, true).setColor(adjustedSbn.getNotification().color).setLocalOnly(true).build();
                    summaryNotification.extras.putAll(extras);
                    Intent appIntent = getContext().getPackageManager().getLaunchIntentForPackage(adjustment.getPackage());
                    if (appIntent != null) {
                        summaryNotification.contentIntent = PendingIntent.getActivityAsUser(getContext(), 0, appIntent, 0, null, UserHandle.of(userId));
                    }
                    final StatusBarNotification summarySbn = new StatusBarNotification(adjustedSbn.getPackageName(), adjustedSbn.getOpPkg(), Integer.MAX_VALUE, Adjustment.GROUP_KEY_OVERRIDE_KEY, adjustedSbn.getUid(), adjustedSbn.getInitialPid(), summaryNotification, adjustedSbn.getUser(), newAutoBundleKey, System.currentTimeMillis());
                    summaryRecord = new NotificationRecord(getContext(), summarySbn);
                    summaries.put(adjustment.getPackage(), summarySbn.getKey());
                }
            }
            if (summaryRecord != null) {
                mHandler.post(new EnqueueNotificationRunnable(userId, summaryRecord));
            }
        }
    }
}
Also used : StatusBarNotification(android.service.notification.StatusBarNotification) Bundle(android.os.Bundle) ApplicationInfo(android.content.pm.ApplicationInfo) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) ITransientNotification(android.app.ITransientNotification) Notification(android.app.Notification) StatusBarNotification(android.service.notification.StatusBarNotification)

Example 43 with Notification

use of android.app.Notification in project platform_frameworks_base by android.

the class NotificationManagerService method cancelGroupChildrenLocked.

// Warning: The caller is responsible for invoking updateLightsLocked().
private void cancelGroupChildrenLocked(NotificationRecord r, int callingUid, int callingPid, String listenerName, int reason, boolean sendDelete) {
    Notification n = r.getNotification();
    if (!n.isGroupSummary()) {
        return;
    }
    String pkg = r.sbn.getPackageName();
    int userId = r.getUserId();
    if (pkg == null) {
        if (DBG)
            Log.e(TAG, "No package for group summary: " + r.getKey());
        return;
    }
    final int N = mNotificationList.size();
    for (int i = N - 1; i >= 0; i--) {
        NotificationRecord childR = mNotificationList.get(i);
        StatusBarNotification childSbn = childR.sbn;
        if ((childSbn.isGroup() && !childSbn.getNotification().isGroupSummary()) && childR.getGroupKey().equals(r.getGroupKey())) {
            EventLogTags.writeNotificationCancel(callingUid, callingPid, pkg, childSbn.getId(), childSbn.getTag(), userId, 0, 0, reason, listenerName);
            mNotificationList.remove(i);
            cancelNotificationLocked(childR, sendDelete, reason);
        }
    }
}
Also used : StatusBarNotification(android.service.notification.StatusBarNotification) ITransientNotification(android.app.ITransientNotification) Notification(android.app.Notification) StatusBarNotification(android.service.notification.StatusBarNotification)

Example 44 with Notification

use of android.app.Notification in project platform_frameworks_base by android.

the class NotificationRecord method dump.

void dump(PrintWriter pw, String prefix, Context baseContext, boolean redact) {
    final Notification notification = sbn.getNotification();
    final Icon icon = notification.getSmallIcon();
    String iconStr = String.valueOf(icon);
    if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
        iconStr += " / " + idDebugString(baseContext, icon.getResPackage(), icon.getResId());
    }
    pw.println(prefix + this);
    pw.println(prefix + "  uid=" + sbn.getUid() + " userId=" + sbn.getUserId());
    pw.println(prefix + "  icon=" + iconStr);
    pw.println(prefix + "  pri=" + notification.priority);
    pw.println(prefix + "  key=" + sbn.getKey());
    pw.println(prefix + "  seen=" + mIsSeen);
    pw.println(prefix + "  groupKey=" + getGroupKey());
    pw.println(prefix + "  contentIntent=" + notification.contentIntent);
    pw.println(prefix + "  deleteIntent=" + notification.deleteIntent);
    pw.println(prefix + "  tickerText=" + notification.tickerText);
    pw.println(prefix + "  contentView=" + notification.contentView);
    pw.println(prefix + String.format("  defaults=0x%08x flags=0x%08x", notification.defaults, notification.flags));
    pw.println(prefix + "  sound=" + notification.sound);
    pw.println(prefix + "  audioStreamType=" + notification.audioStreamType);
    pw.println(prefix + "  audioAttributes=" + notification.audioAttributes);
    pw.println(prefix + String.format("  color=0x%08x", notification.color));
    pw.println(prefix + "  vibrate=" + Arrays.toString(notification.vibrate));
    pw.println(prefix + String.format("  led=0x%08x onMs=%d offMs=%d", notification.ledARGB, notification.ledOnMS, notification.ledOffMS));
    if (notification.actions != null && notification.actions.length > 0) {
        pw.println(prefix + "  actions={");
        final int N = notification.actions.length;
        for (int i = 0; i < N; i++) {
            final Notification.Action action = notification.actions[i];
            if (action != null) {
                pw.println(String.format("%s    [%d] \"%s\" -> %s", prefix, i, action.title, action.actionIntent == null ? "null" : action.actionIntent.toString()));
            }
        }
        pw.println(prefix + "  }");
    }
    if (notification.extras != null && notification.extras.size() > 0) {
        pw.println(prefix + "  extras={");
        for (String key : notification.extras.keySet()) {
            pw.print(prefix + "    " + key + "=");
            Object val = notification.extras.get(key);
            if (val == null) {
                pw.println("null");
            } else {
                pw.print(val.getClass().getSimpleName());
                if (redact && (val instanceof CharSequence || val instanceof String)) {
                // redact contents from bugreports
                } else if (val instanceof Bitmap) {
                    pw.print(String.format(" (%dx%d)", ((Bitmap) val).getWidth(), ((Bitmap) val).getHeight()));
                } else if (val.getClass().isArray()) {
                    final int N = Array.getLength(val);
                    pw.print(" (" + N + ")");
                    if (!redact) {
                        for (int j = 0; j < N; j++) {
                            pw.println();
                            pw.print(String.format("%s      [%d] %s", prefix, j, String.valueOf(Array.get(val, j))));
                        }
                    }
                } else {
                    pw.print(" (" + String.valueOf(val) + ")");
                }
                pw.println();
            }
        }
        pw.println(prefix + "  }");
    }
    pw.println(prefix + "  stats=" + stats.toString());
    pw.println(prefix + "  mContactAffinity=" + mContactAffinity);
    pw.println(prefix + "  mRecentlyIntrusive=" + mRecentlyIntrusive);
    pw.println(prefix + "  mPackagePriority=" + mPackagePriority);
    pw.println(prefix + "  mPackageVisibility=" + mPackageVisibility);
    pw.println(prefix + "  mUserImportance=" + NotificationListenerService.Ranking.importanceToString(mUserImportance));
    pw.println(prefix + "  mImportance=" + NotificationListenerService.Ranking.importanceToString(mImportance));
    pw.println(prefix + "  mImportanceExplanation=" + mImportanceExplanation);
    pw.println(prefix + "  mIntercept=" + mIntercept);
    pw.println(prefix + "  mGlobalSortKey=" + mGlobalSortKey);
    pw.println(prefix + "  mRankingTimeMs=" + mRankingTimeMs);
    pw.println(prefix + "  mCreationTimeMs=" + mCreationTimeMs);
    pw.println(prefix + "  mVisibleSinceMs=" + mVisibleSinceMs);
    pw.println(prefix + "  mUpdateTimeMs=" + mUpdateTimeMs);
    pw.println(prefix + "  mSuppressedVisualEffects= " + mSuppressedVisualEffects);
}
Also used : Bitmap(android.graphics.Bitmap) Icon(android.graphics.drawable.Icon) Notification(android.app.Notification) StatusBarNotification(android.service.notification.StatusBarNotification)

Example 45 with Notification

use of android.app.Notification in project platform_frameworks_base by android.

the class NotificationRecord method defaultImportance.

private int defaultImportance() {
    final Notification n = sbn.getNotification();
    int importance = IMPORTANCE_DEFAULT;
    // Migrate notification flags to scores
    if (0 != (n.flags & Notification.FLAG_HIGH_PRIORITY)) {
        n.priority = Notification.PRIORITY_MAX;
    }
    switch(n.priority) {
        case Notification.PRIORITY_MIN:
            importance = IMPORTANCE_MIN;
            break;
        case Notification.PRIORITY_LOW:
            importance = IMPORTANCE_LOW;
            break;
        case Notification.PRIORITY_DEFAULT:
            importance = IMPORTANCE_DEFAULT;
            break;
        case Notification.PRIORITY_HIGH:
            importance = IMPORTANCE_HIGH;
            break;
        case Notification.PRIORITY_MAX:
            importance = IMPORTANCE_MAX;
            break;
    }
    stats.requestedImportance = importance;
    boolean isNoisy = (n.defaults & Notification.DEFAULT_SOUND) != 0 || (n.defaults & Notification.DEFAULT_VIBRATE) != 0 || n.sound != null || n.vibrate != null;
    stats.isNoisy = isNoisy;
    if (!isNoisy && importance > IMPORTANCE_LOW) {
        importance = IMPORTANCE_LOW;
    }
    if (isNoisy) {
        if (importance < IMPORTANCE_DEFAULT) {
            importance = IMPORTANCE_DEFAULT;
        }
    }
    if (n.fullScreenIntent != null) {
        importance = IMPORTANCE_MAX;
    }
    stats.naturalImportance = importance;
    return importance;
}
Also used : Notification(android.app.Notification) StatusBarNotification(android.service.notification.StatusBarNotification)

Aggregations

Notification (android.app.Notification)568 PendingIntent (android.app.PendingIntent)276 Intent (android.content.Intent)253 NotificationManager (android.app.NotificationManager)128 StatusBarNotification (android.service.notification.StatusBarNotification)96 NotificationCompat (android.support.v4.app.NotificationCompat)64 Resources (android.content.res.Resources)56 Bitmap (android.graphics.Bitmap)48 Test (org.junit.Test)48 Context (android.content.Context)47 ITransientNotification (android.app.ITransientNotification)32 RemoteViews (android.widget.RemoteViews)30 RemoteException (android.os.RemoteException)24 ApplicationInfo (android.content.pm.ApplicationInfo)17 UserHandle (android.os.UserHandle)17 Date (java.util.Date)16 Uri (android.net.Uri)15 Bundle (android.os.Bundle)15 ComponentName (android.content.ComponentName)13 File (java.io.File)13