summaryrefslogtreecommitdiffstats
path: root/src/com/android/launcher3/badge/BadgeInfo.java
blob: 532396cddb1cb5c521e148769b7dc561c38903ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.launcher3.badge;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;

import com.android.launcher3.notification.NotificationInfo;
import com.android.launcher3.util.PackageUserKey;

import java.util.ArrayList;
import java.util.List;

/**
 * Contains data to be used in an icon badge.
 */
public class BadgeInfo {

    /** Used to link this BadgeInfo to icons on the workspace and all apps */
    private PackageUserKey mPackageUserKey;

    /**
     * The keys of the notifications that this badge represents. These keys can later be
     * used to retrieve {@link NotificationInfo}'s.
     */
    private List<String> mNotificationKeys;

    /** This will only be initialized if the badge should display the notification icon. */
    private NotificationInfo mNotificationInfo;

    /**
     * When retrieving the notification icon, we draw it into this shader, which can be clipped
     * as necessary when drawn in a badge.
     */
    private Shader mNotificationIcon;

    public BadgeInfo(PackageUserKey packageUserKey) {
        mPackageUserKey = packageUserKey;
        mNotificationKeys = new ArrayList<>();
    }

    /**
     * Returns whether the notification was added (false if it already existed).
     */
    public boolean addNotificationKeyIfNotExists(String notificationKey) {
        if (mNotificationKeys.contains(notificationKey)) {
            return false;
        }
        return mNotificationKeys.add(notificationKey);
    }

    /**
     * Returns whether the notification was removed (false if it didn't exist).
     */
    public boolean removeNotificationKey(String notificationKey) {
        return mNotificationKeys.remove(notificationKey);
    }

    public List<String> getNotificationKeys() {
        return mNotificationKeys;
    }

    public int getNotificationCount() {
        return mNotificationKeys.size();
    }

    public void setNotificationToShow(@Nullable NotificationInfo notificationInfo) {
        mNotificationInfo = notificationInfo;
        mNotificationIcon = null;
    }

    public boolean hasNotificationToShow() {
        return mNotificationInfo != null;
    }

    /**
     * Returns a shader to set on a Paint that will draw the notification icon in a badge.
     *
     * The shader is cached until {@link #setNotificationToShow(NotificationInfo)} is called.
     */
    public @Nullable Shader getNotificationIconForBadge(Context context, int badgeColor,
            int badgeSize, int badgePadding) {
        if (mNotificationInfo == null) {
            return null;
        }
        if (mNotificationIcon == null) {
            Drawable icon = mNotificationInfo.getIconForBackground(context, badgeColor)
                    .getConstantState().newDrawable();
            int iconSize = badgeSize - badgePadding * 2;
            icon.setBounds(0, 0, iconSize, iconSize);
            Bitmap iconBitmap = Bitmap.createBitmap(badgeSize, badgeSize, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(iconBitmap);
            canvas.translate(badgePadding, badgePadding);
            icon.draw(canvas);
            mNotificationIcon = new BitmapShader(iconBitmap, Shader.TileMode.CLAMP,
                    Shader.TileMode.CLAMP);
        }
        return mNotificationIcon;
    }

    public boolean isIconLarge() {
        return mNotificationInfo != null && mNotificationInfo.isIconLarge();
    }

    /**
     * Whether newBadge represents the same PackageUserKey as this badge, and icons with
     * this badge should be invalidated. So, for instance, if a badge has 3 notifications
     * and one of those notifications is updated, this method should return false because
     * the badge still says "3" and the contents of those notifications are only retrieved
     * upon long-click. This method always returns true when adding or removing notifications,
     * or if the badge has a notification icon to show.
     */
    public boolean shouldBeInvalidated(BadgeInfo newBadge) {
        return mPackageUserKey.equals(newBadge.mPackageUserKey)
                && (getNotificationCount() != newBadge.getNotificationCount()
                    || hasNotificationToShow());
    }
}