1 package com.example.sampleleanbacklauncher.notifications;
2 
3 import android.database.Cursor;
4 import android.graphics.Bitmap;
5 import android.graphics.BitmapFactory;
6 import android.graphics.drawable.Icon;
7 import android.os.Parcel;
8 
9 public class TvNotification {
10     /**
11      * This projection MUST be used for the query when using {@link #fromCursor(Cursor)}.
12      */
13     public static final String[] PROJECTION =
14             {NotificationsContract.COLUMN_SBN_KEY,
15                     NotificationsContract.COLUMN_PACKAGE_NAME,
16                     NotificationsContract.COLUMN_NOTIF_TITLE,
17                     NotificationsContract.COLUMN_NOTIF_TEXT,
18                     NotificationsContract.COLUMN_DISMISSIBLE,
19                     NotificationsContract.COLUMN_ONGOING,
20                     NotificationsContract.COLUMN_SMALL_ICON,
21                     NotificationsContract.COLUMN_CHANNEL,
22                     NotificationsContract.COLUMN_PROGRESS,
23                     NotificationsContract.COLUMN_PROGRESS_MAX,
24                     NotificationsContract.COLUMN_HAS_CONTENT_INTENT,
25                     NotificationsContract.COLUMN_BIG_PICTURE,
26                     NotificationsContract.COLUMN_CONTENT_BUTTON_LABEL,
27                     NotificationsContract.COLUMN_DISMISS_BUTTON_LABEL,
28                     NotificationsContract.COLUMN_TAG};
29 
30     public static final int COLUMN_INDEX_KEY = 0;
31     public static final int COLUMN_INDEX_PACKAGE_NAME = 1;
32     public static final int COLUMN_INDEX_NOTIF_TITLE = 2;
33     public static final int COLUMN_INDEX_NOTIF_TEXT = 3;
34     public static final int COLUMN_INDEX_DISMISSIBLE = 4;
35     public static final int COLUMN_INDEX_ONGOING = 5;
36     public static final int COLUMN_INDEX_SMALL_ICON = 6;
37     public static final int COLUMN_INDEX_CHANNEL = 7;
38     public static final int COLUMN_INDEX_PROGRESS = 8;
39     public static final int COLUMN_INDEX_PROGRESS_MAX = 9;
40     public static final int COLUMN_INDEX_HAS_CONTENT_INTENT = 10;
41     public static final int COLUMN_INDEX_BIG_PICTURE = 11;
42     public static final int COLUMN_INDEX_CONTENT_BUTTON_LABEL = 12;
43     public static final int COLUMN_INDEX_DISMISS_BUTTON_LABEL = 13;
44     public static final int COLUMN_INDEX_TAG = 14;
45 
46     private String mNotificationKey;
47     private String mPackageName;
48     private String mTitle;
49     private String mText;
50     private boolean mDismissible;
51     private boolean mIsOngoing;
52     private Icon mSmallIcon;
53     private int mChannel;
54     private int mProgress;
55     private int mProgressMax;
56     private boolean mHasContentIntent;
57     private Bitmap mBigPicture;
58     private String mContentButtonLabel;
59     private String mDismissButtonLabel;
60     private String mTag;
61 
TvNotification(String key, String packageName, String title, String text, boolean dismissible, boolean ongoing, Icon smallIcon, int channel, int progress, int progressMax, boolean hasContentIntent, Bitmap bigPicture, String contentButtonLabel, String dismissButtonLabel, String tag)62     public TvNotification(String key, String packageName, String title, String text,
63                           boolean dismissible, boolean ongoing, Icon smallIcon, int channel,
64                           int progress, int progressMax, boolean hasContentIntent, Bitmap bigPicture,
65                           String contentButtonLabel, String dismissButtonLabel, String tag) {
66         mNotificationKey = key;
67         mPackageName = packageName;
68         mTitle = title;
69         mText = text;
70         mDismissible = dismissible;
71         mIsOngoing = ongoing;
72         mSmallIcon = smallIcon;
73         mChannel = channel;
74         mProgress = progress;
75         mProgressMax = progressMax;
76         mHasContentIntent = hasContentIntent;
77         mBigPicture = bigPicture;
78         mContentButtonLabel = contentButtonLabel;
79         mDismissButtonLabel = dismissButtonLabel;
80         mTag = tag;
81     }
82 
getNotificationKey()83     public String getNotificationKey() {
84         return mNotificationKey;
85     }
86 
getPackageName()87     public String getPackageName() {
88         return mPackageName;
89     }
90 
getTitle()91     public String getTitle() {
92         return mTitle;
93     }
94 
getText()95     public String getText() {
96         return mText;
97     }
98 
isDismissible()99     public boolean isDismissible() {
100         return mDismissible;
101     }
102 
isOngoing()103     public boolean isOngoing() {
104         return mIsOngoing;
105     }
106 
getSmallIcon()107     public Icon getSmallIcon() {
108         return mSmallIcon;
109     }
110 
getChannel()111     public int getChannel() {
112         return mChannel;
113     }
114 
getProgress()115     public int getProgress() {
116         return mProgress;
117     }
118 
getProgressMax()119     public int getProgressMax() {
120         return mProgressMax;
121     }
122 
hasContentIntent()123     public boolean hasContentIntent() {
124         return mHasContentIntent;
125     }
126 
getBigPicture()127     public Bitmap getBigPicture() {
128         return mBigPicture;
129     }
130 
getContentButtonLabel()131     public String getContentButtonLabel() {
132         return mContentButtonLabel;
133     }
134 
getDismissButtonLabel()135     public String getDismissButtonLabel() {
136         return mDismissButtonLabel;
137     }
138 
getTag()139     public String getTag() {
140         return mTag;
141     }
142 
143     // Converts cursor returned from query with PROJECTION
fromCursor(Cursor cursor)144     public static TvNotification fromCursor(Cursor cursor) {
145         int index = 0;
146         String key = cursor.getString(index++);
147         String packageName = cursor.getString(index++);
148         String title = cursor.getString(index++);
149         String text = cursor.getString(index++);
150         boolean dismissible = cursor.getInt(index++) != 0;
151         boolean ongoing = cursor.getInt(index++) != 0;
152         byte[] smallIconData = cursor.getBlob(index++);
153         Icon smallIcon = getIconFromBytes(smallIconData);
154 
155         int channel = cursor.getInt(index++);
156         int progress = cursor.getInt(index++);
157         int progressMax = cursor.getInt(index++);
158         boolean hasContentIntent = cursor.getInt(index++) != 0;
159         byte[] bigPictureData = cursor.getBlob(index++);
160         Bitmap bigPicture = getBitmapFromBytes(bigPictureData);
161         String contentButtonLabel = cursor.getString(index++);
162         String dismissButtonLabel = cursor.getString(index++);
163         String tag = cursor.getString(index);
164 
165         return new TvNotification(key, packageName, title, text, dismissible, ongoing,
166                 smallIcon, channel, progress, progressMax, hasContentIntent, bigPicture,
167                 contentButtonLabel, dismissButtonLabel, tag);
168     }
169 
getBitmapFromBytes(byte[] blob)170     private static Bitmap getBitmapFromBytes(byte[] blob) {
171         if (blob != null) {
172             Bitmap bitmap = BitmapFactory.decodeByteArray(blob, 0, blob.length);
173             return bitmap;
174         }
175 
176         return null;
177     }
178 
getIconFromBytes(byte[] blob)179     private static Icon getIconFromBytes(byte[] blob) {
180         Parcel in = Parcel.obtain();
181         Icon icon = null;
182         if (blob != null) {
183             in.unmarshall(blob, 0, blob.length);
184             in.setDataPosition(0);
185             icon = in.readParcelable(Icon.class.getClassLoader());
186         }
187 
188         in.recycle();
189         return icon;
190     }
191 }
192 
193