Java Code Examples for android.app.NotificationChannel#isDeleted()

The following examples show how to use android.app.NotificationChannel#isDeleted() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RankingHelper.java    From android_9.0.0_r45 with Apache License 2.0 8 votes vote down vote up
@Override
public NotificationChannel getNotificationChannel(String pkg, int uid, String channelId,
        boolean includeDeleted) {
    Preconditions.checkNotNull(pkg);
    Record r = getOrCreateRecord(pkg, uid);
    if (r == null) {
        return null;
    }
    if (channelId == null) {
        channelId = NotificationChannel.DEFAULT_CHANNEL_ID;
    }
    final NotificationChannel nc = r.channels.get(channelId);
    if (nc != null && (includeDeleted || !nc.isDeleted())) {
        return nc;
    }
    return null;
}
 
Example 2
Source File: RankingHelper.java    From android_9.0.0_r45 with Apache License 2.0 8 votes vote down vote up
public NotificationChannelGroup getNotificationChannelGroupWithChannels(String pkg,
        int uid, String groupId, boolean includeDeleted) {
    Preconditions.checkNotNull(pkg);
    Record r = getRecord(pkg, uid);
    if (r == null || groupId == null || !r.groups.containsKey(groupId)) {
        return null;
    }
    NotificationChannelGroup group = r.groups.get(groupId).clone();
    group.setChannels(new ArrayList<>());
    int N = r.channels.size();
    for (int i = 0; i < N; i++) {
        final NotificationChannel nc = r.channels.valueAt(i);
        if (includeDeleted || !nc.isDeleted()) {
            if (groupId.equals(nc.getGroup())) {
                group.addChannel(nc);
            }
        }
    }
    return group;
}
 
Example 3
Source File: RankingHelper.java    From android_9.0.0_r45 with Apache License 2.0 8 votes vote down vote up
@Override
public ParceledListSlice<NotificationChannel> getNotificationChannels(String pkg, int uid,
        boolean includeDeleted) {
    Preconditions.checkNotNull(pkg);
    List<NotificationChannel> channels = new ArrayList<>();
    Record r = getRecord(pkg, uid);
    if (r == null) {
        return ParceledListSlice.emptyList();
    }
    int N = r.channels.size();
    for (int i = 0; i < N; i++) {
        final NotificationChannel nc = r.channels.valueAt(i);
        if (includeDeleted || !nc.isDeleted()) {
            channels.add(nc);
        }
    }
    return new ParceledListSlice<>(channels);
}
 
Example 4
Source File: RankingHelper.java    From android_9.0.0_r45 with Apache License 2.0 8 votes vote down vote up
public int getDeletedChannelCount(String pkg, int uid) {
    Preconditions.checkNotNull(pkg);
    int deletedCount = 0;
    Record r = getRecord(pkg, uid);
    if (r == null) {
        return deletedCount;
    }
    int N = r.channels.size();
    for (int i = 0; i < N; i++) {
        final NotificationChannel nc = r.channels.valueAt(i);
        if (nc.isDeleted()) {
            deletedCount++;
        }
    }
    return deletedCount;
}
 
Example 5
Source File: RankingHelper.java    From android_9.0.0_r45 with Apache License 2.0 8 votes vote down vote up
public int getBlockedChannelCount(String pkg, int uid) {
    Preconditions.checkNotNull(pkg);
    int blockedCount = 0;
    Record r = getRecord(pkg, uid);
    if (r == null) {
        return blockedCount;
    }
    int N = r.channels.size();
    for (int i = 0; i < N; i++) {
        final NotificationChannel nc = r.channels.valueAt(i);
        if (!nc.isDeleted() && IMPORTANCE_NONE == nc.getImportance()) {
            blockedCount++;
        }
    }
    return blockedCount;
}
 
Example 6
Source File: RankingHelper.java    From android_9.0.0_r45 with Apache License 2.0 8 votes vote down vote up
public void updateChannelsBypassingDnd() {
    synchronized (mRecords) {
        final int numRecords = mRecords.size();
        for (int recordIndex = 0; recordIndex < numRecords; recordIndex++) {
            final Record r = mRecords.valueAt(recordIndex);
            final int numChannels = r.channels.size();

            for (int channelIndex = 0; channelIndex < numChannels; channelIndex++) {
                NotificationChannel channel = r.channels.valueAt(channelIndex);
                if (!channel.isDeleted() && channel.canBypassDnd()) {
                    if (!mAreChannelsBypassingDnd) {
                        mAreChannelsBypassingDnd = true;
                        updateZenPolicy(true);
                    }
                    return;
                }
            }
        }
    }

    if (mAreChannelsBypassingDnd) {
        mAreChannelsBypassingDnd = false;
        updateZenPolicy(false);
    }
}
 
Example 7
Source File: RankingHelper.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
public void writeXml(XmlSerializer out, boolean forBackup) throws IOException {
    out.startTag(null, TAG_RANKING);
    out.attribute(null, ATT_VERSION, Integer.toString(XML_VERSION));

    synchronized (mRecords) {
        final int N = mRecords.size();
        for (int i = 0; i < N; i++) {
            final Record r = mRecords.valueAt(i);
            //TODO: http://b/22388012
            if (forBackup && UserHandle.getUserId(r.uid) != UserHandle.USER_SYSTEM) {
                continue;
            }
            final boolean hasNonDefaultSettings =
                    r.importance != DEFAULT_IMPORTANCE
                        || r.priority != DEFAULT_PRIORITY
                        || r.visibility != DEFAULT_VISIBILITY
                        || r.showBadge != DEFAULT_SHOW_BADGE
                        || r.lockedAppFields != DEFAULT_LOCKED_APP_FIELDS
                        || r.channels.size() > 0
                        || r.groups.size() > 0;
            if (hasNonDefaultSettings) {
                out.startTag(null, TAG_PACKAGE);
                out.attribute(null, ATT_NAME, r.pkg);
                if (r.importance != DEFAULT_IMPORTANCE) {
                    out.attribute(null, ATT_IMPORTANCE, Integer.toString(r.importance));
                }
                if (r.priority != DEFAULT_PRIORITY) {
                    out.attribute(null, ATT_PRIORITY, Integer.toString(r.priority));
                }
                if (r.visibility != DEFAULT_VISIBILITY) {
                    out.attribute(null, ATT_VISIBILITY, Integer.toString(r.visibility));
                }
                out.attribute(null, ATT_SHOW_BADGE, Boolean.toString(r.showBadge));
                out.attribute(null, ATT_APP_USER_LOCKED_FIELDS,
                        Integer.toString(r.lockedAppFields));

                if (!forBackup) {
                    out.attribute(null, ATT_UID, Integer.toString(r.uid));
                }

                for (NotificationChannelGroup group : r.groups.values()) {
                    group.writeXml(out);
                }

                for (NotificationChannel channel : r.channels.values()) {
                    if (forBackup) {
                        if (!channel.isDeleted()) {
                            channel.writeXmlForBackup(out, mContext);
                        }
                    } else {
                        channel.writeXml(out);
                    }
                }

                out.endTag(null, TAG_PACKAGE);
            }
        }
    }
    out.endTag(null, TAG_RANKING);
}
 
Example 8
Source File: RankingHelper.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
@Override
public void createNotificationChannel(String pkg, int uid, NotificationChannel channel,
        boolean fromTargetApp, boolean hasDndAccess) {
    Preconditions.checkNotNull(pkg);
    Preconditions.checkNotNull(channel);
    Preconditions.checkNotNull(channel.getId());
    Preconditions.checkArgument(!TextUtils.isEmpty(channel.getName()));
    Record r = getOrCreateRecord(pkg, uid);
    if (r == null) {
        throw new IllegalArgumentException("Invalid package");
    }
    if (channel.getGroup() != null && !r.groups.containsKey(channel.getGroup())) {
        throw new IllegalArgumentException("NotificationChannelGroup doesn't exist");
    }
    if (NotificationChannel.DEFAULT_CHANNEL_ID.equals(channel.getId())) {
        throw new IllegalArgumentException("Reserved id");
    }
    NotificationChannel existing = r.channels.get(channel.getId());
    // Keep most of the existing settings
    if (existing != null && fromTargetApp) {
        if (existing.isDeleted()) {
            existing.setDeleted(false);

            // log a resurrected channel as if it's new again
            MetricsLogger.action(getChannelLog(channel, pkg).setType(
                    MetricsProto.MetricsEvent.TYPE_OPEN));
        }

        existing.setName(channel.getName().toString());
        existing.setDescription(channel.getDescription());
        existing.setBlockableSystem(channel.isBlockableSystem());
        if (existing.getGroup() == null) {
            existing.setGroup(channel.getGroup());
        }

        // Apps are allowed to downgrade channel importance if the user has not changed any
        // fields on this channel yet.
        if (existing.getUserLockedFields() == 0 &&
                channel.getImportance() < existing.getImportance()) {
            existing.setImportance(channel.getImportance());
        }

        // system apps and dnd access apps can bypass dnd if the user hasn't changed any
        // fields on the channel yet
        if (existing.getUserLockedFields() == 0 && hasDndAccess) {
            boolean bypassDnd = channel.canBypassDnd();
            existing.setBypassDnd(bypassDnd);

            if (bypassDnd != mAreChannelsBypassingDnd) {
                updateChannelsBypassingDnd();
            }
        }

        updateConfig();
        return;
    }
    if (channel.getImportance() < IMPORTANCE_NONE
            || channel.getImportance() > NotificationManager.IMPORTANCE_MAX) {
        throw new IllegalArgumentException("Invalid importance level");
    }

    // Reset fields that apps aren't allowed to set.
    if (fromTargetApp && !hasDndAccess) {
        channel.setBypassDnd(r.priority == Notification.PRIORITY_MAX);
    }
    if (fromTargetApp) {
        channel.setLockscreenVisibility(r.visibility);
    }
    clearLockedFields(channel);
    if (channel.getLockscreenVisibility() == Notification.VISIBILITY_PUBLIC) {
        channel.setLockscreenVisibility(Ranking.VISIBILITY_NO_OVERRIDE);
    }
    if (!r.showBadge) {
        channel.setShowBadge(false);
    }

    r.channels.put(channel.getId(), channel);
    if (channel.canBypassDnd() != mAreChannelsBypassingDnd) {
        updateChannelsBypassingDnd();
    }
    MetricsLogger.action(getChannelLog(channel, pkg).setType(
            MetricsProto.MetricsEvent.TYPE_OPEN));
}
 
Example 9
Source File: RankingHelper.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
@Override
public void updateNotificationChannel(String pkg, int uid, NotificationChannel updatedChannel,
        boolean fromUser) {
    Preconditions.checkNotNull(updatedChannel);
    Preconditions.checkNotNull(updatedChannel.getId());
    Record r = getOrCreateRecord(pkg, uid);
    if (r == null) {
        throw new IllegalArgumentException("Invalid package");
    }
    NotificationChannel channel = r.channels.get(updatedChannel.getId());
    if (channel == null || channel.isDeleted()) {
        throw new IllegalArgumentException("Channel does not exist");
    }
    if (updatedChannel.getLockscreenVisibility() == Notification.VISIBILITY_PUBLIC) {
        updatedChannel.setLockscreenVisibility(Ranking.VISIBILITY_NO_OVERRIDE);
    }
    if (!fromUser) {
        updatedChannel.unlockFields(updatedChannel.getUserLockedFields());
    }
    if (fromUser) {
        updatedChannel.lockFields(channel.getUserLockedFields());
        lockFieldsForUpdate(channel, updatedChannel);
    }
    r.channels.put(updatedChannel.getId(), updatedChannel);

    if (NotificationChannel.DEFAULT_CHANNEL_ID.equals(updatedChannel.getId())) {
        // copy settings to app level so they are inherited by new channels
        // when the app migrates
        r.importance = updatedChannel.getImportance();
        r.priority = updatedChannel.canBypassDnd()
                ? Notification.PRIORITY_MAX : Notification.PRIORITY_DEFAULT;
        r.visibility = updatedChannel.getLockscreenVisibility();
        r.showBadge = updatedChannel.canShowBadge();
    }

    if (!channel.equals(updatedChannel)) {
        // only log if there are real changes
        MetricsLogger.action(getChannelLog(updatedChannel, pkg));
    }

    if (updatedChannel.canBypassDnd() != mAreChannelsBypassingDnd) {
        updateChannelsBypassingDnd();
    }
    updateConfig();
}
 
Example 10
Source File: RankingHelper.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
@Override
public ParceledListSlice<NotificationChannelGroup> getNotificationChannelGroups(String pkg,
        int uid, boolean includeDeleted, boolean includeNonGrouped, boolean includeEmpty) {
    Preconditions.checkNotNull(pkg);
    Map<String, NotificationChannelGroup> groups = new ArrayMap<>();
    Record r = getRecord(pkg, uid);
    if (r == null) {
        return ParceledListSlice.emptyList();
    }
    NotificationChannelGroup nonGrouped = new NotificationChannelGroup(null, null);
    int N = r.channels.size();
    for (int i = 0; i < N; i++) {
        final NotificationChannel nc = r.channels.valueAt(i);
        if (includeDeleted || !nc.isDeleted()) {
            if (nc.getGroup() != null) {
                if (r.groups.get(nc.getGroup()) != null) {
                    NotificationChannelGroup ncg = groups.get(nc.getGroup());
                    if (ncg == null) {
                        ncg = r.groups.get(nc.getGroup()).clone();
                        ncg.setChannels(new ArrayList<>());
                        groups.put(nc.getGroup(), ncg);

                    }
                    ncg.addChannel(nc);
                }
            } else {
                nonGrouped.addChannel(nc);
            }
        }
    }
    if (includeNonGrouped && nonGrouped.getChannels().size() > 0) {
        groups.put(null, nonGrouped);
    }
    if (includeEmpty) {
        for (NotificationChannelGroup group : r.groups.values()) {
            if (!groups.containsKey(group.getId())) {
                groups.put(group.getId(), group);
            }
        }
    }
    return new ParceledListSlice<>(new ArrayList<>(groups.values()));
}