1
1
package com .lesimoes .androidnotificationlistener ;
2
2
3
+ import android .graphics .BitmapFactory ;
3
4
import android .service .notification .StatusBarNotification ;
4
5
import android .text .TextUtils ;
5
6
import android .app .Notification ;
6
7
import android .util .Log ;
7
8
import java .util .ArrayList ;
9
+ import android .content .pm .PackageManager ;
10
+ import android .content .res .Resources ;
11
+ import android .graphics .drawable .Drawable ;
12
+ import android .graphics .Bitmap ;
13
+ import java .io .ByteArrayOutputStream ;
14
+ import android .util .Base64 ;
15
+ import android .content .Context ;
16
+ import java .lang .Exception ;
17
+ import android .graphics .drawable .Icon ;
18
+ import android .graphics .drawable .BitmapDrawable ;
8
19
9
20
public class RNNotification {
10
21
private static final String TAG = "RNAndroidNotificationListener" ;
@@ -20,35 +31,52 @@ public class RNNotification {
20
31
protected String audioContentsURI ;
21
32
protected String imageBackgroundURI ;
22
33
protected String extraInfoText ;
34
+ protected String icon ;
35
+ protected String image ;
36
+ protected String time ;
23
37
24
- public RNNotification (StatusBarNotification sbn ) {
38
+ public RNNotification (Context context , StatusBarNotification sbn ) {
25
39
Notification notification = sbn .getNotification ();
26
40
27
41
if (notification != null && notification .extras != null ) {
28
42
String packageName = sbn .getPackageName ();
29
43
30
- CharSequence titleChars = notification .extras .getCharSequence (Notification .EXTRA_TITLE );
31
- CharSequence titleBigChars = notification .extras .getCharSequence (Notification .EXTRA_TITLE_BIG );
32
- CharSequence textChars = notification .extras .getCharSequence (Notification .EXTRA_TEXT );
33
- CharSequence subTextChars = notification .extras .getCharSequence (Notification .EXTRA_SUB_TEXT );
34
- CharSequence summaryTextChars = notification .extras .getCharSequence (Notification .EXTRA_SUMMARY_TEXT );
35
- CharSequence bigTextChars = notification .extras .getCharSequence (Notification .EXTRA_BIG_TEXT );
36
- CharSequence audioContentsURIChars = notification .extras .getCharSequence (Notification .EXTRA_AUDIO_CONTENTS_URI );
37
- CharSequence imageBackgroundURIChars = notification .extras .getCharSequence (Notification .EXTRA_BACKGROUND_IMAGE_URI );
38
- CharSequence extraInfoTextChars = notification .extras .getCharSequence (Notification .EXTRA_INFO_TEXT );
39
- CharSequence [] lines = notification .extras .getCharSequenceArray (Notification .EXTRA_TEXT_LINES );
40
-
44
+ this .time = Long .toString (sbn .getPostTime ());
41
45
this .app = TextUtils .isEmpty (packageName ) ? "Unknown App" : packageName ;
42
- this .title = titleChars != null ? titleChars .toString ().trim () : "" ;
43
- this .titleBig = titleBigChars != null ? titleBigChars .toString ().trim () : "" ;
44
- this .text = textChars != null ? textChars .toString ().trim () : "" ;
45
- this .subText = subTextChars != null ? subTextChars .toString ().trim () : "" ;
46
- this .summaryText = summaryTextChars != null ? summaryTextChars .toString ().trim () : "" ;
47
- this .bigText = bigTextChars != null ? bigTextChars .toString ().trim () : "" ;
48
- this .audioContentsURI = audioContentsURIChars != null ? audioContentsURIChars .toString ().trim () : "" ;
49
- this .imageBackgroundURI = imageBackgroundURIChars != null ? imageBackgroundURIChars .toString ().trim () : "" ;
50
- this .extraInfoText = extraInfoTextChars != null ? extraInfoTextChars .toString ().trim () : "" ;
51
- this .groupedMessages = new ArrayList <RNGroupedNotification >();
46
+ this .title = this .getPropertySafely (notification , Notification .EXTRA_TITLE );
47
+ this .titleBig = this .getPropertySafely (notification , Notification .EXTRA_TITLE_BIG );
48
+ this .text = this .getPropertySafely (notification , Notification .EXTRA_TEXT );
49
+ this .subText = this .getPropertySafely (notification , Notification .EXTRA_SUB_TEXT );
50
+ this .summaryText = this .getPropertySafely (notification , Notification .EXTRA_SUMMARY_TEXT );
51
+ this .bigText = this .getPropertySafely (notification , Notification .EXTRA_BIG_TEXT );
52
+ this .audioContentsURI = this .getPropertySafely (notification , Notification .EXTRA_AUDIO_CONTENTS_URI );
53
+ this .imageBackgroundURI = this .getPropertySafely (notification , Notification .EXTRA_BACKGROUND_IMAGE_URI );
54
+ this .extraInfoText = this .getPropertySafely (notification , Notification .EXTRA_INFO_TEXT );
55
+
56
+ this .icon = this .getNotificationIcon (context , notification , packageName );
57
+ this .image = this .getNotificationImage (notification );
58
+ this .groupedMessages = this .getGroupedNotifications (notification );
59
+ } else {
60
+ Log .d (TAG , "The notification received has no data" );
61
+ }
62
+ }
63
+
64
+ private String getPropertySafely (Notification notification , String propKey ) {
65
+ try {
66
+ CharSequence propCharSequence = notification .extras .getCharSequence (propKey );
67
+
68
+ return propCharSequence == null ? "" : propCharSequence .toString ().trim ();
69
+ } catch (Exception e ) {
70
+ Log .d (TAG , e .getMessage ());
71
+ return "" ;
72
+ }
73
+ }
74
+
75
+ private ArrayList <RNGroupedNotification > getGroupedNotifications (Notification notification ) {
76
+ ArrayList <RNGroupedNotification > result = new ArrayList <RNGroupedNotification >();
77
+
78
+ try {
79
+ CharSequence [] lines = notification .extras .getCharSequenceArray (Notification .EXTRA_TEXT_LINES );
52
80
53
81
if (lines != null && lines .length > 0 ) {
54
82
for (CharSequence line : lines ) {
@@ -58,8 +86,90 @@ public RNNotification(StatusBarNotification sbn) {
58
86
}
59
87
}
60
88
}
61
- } else {
62
- Log .d (TAG , "The notification received has no data" );
89
+
90
+ return result ;
91
+ } catch (Exception e ) {
92
+ Log .d (TAG , e .getMessage ());
93
+ return result ;
94
+ }
95
+ }
96
+
97
+ private String getNotificationIcon (Context context , Notification notification , String packageName ) {
98
+ try {
99
+ int iconId = notification .extras .getInt (Notification .EXTRA_SMALL_ICON );
100
+
101
+ String result = "" ;
102
+
103
+ if (iconId <= 0 ) {
104
+ Icon iconInstance = notification .getSmallIcon ();
105
+ Drawable iconDrawable = iconInstance .loadDrawable (context );
106
+ Bitmap iconBitmap = ((BitmapDrawable ) iconDrawable ).getBitmap ();
107
+
108
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
109
+ iconBitmap .compress (Bitmap .CompressFormat .PNG , 100 , outputStream );
110
+
111
+ result = Base64 .encodeToString (outputStream .toByteArray (), Base64 .DEFAULT );
112
+ } else {
113
+ PackageManager manager = context .getPackageManager ();
114
+ Resources resources = manager .getResourcesForApplication (packageName );
115
+
116
+ Drawable iconDrawable = resources .getDrawable (iconId );
117
+ Bitmap iconBitmap = ((BitmapDrawable ) iconDrawable ).getBitmap ();
118
+
119
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
120
+ iconBitmap .compress (Bitmap .CompressFormat .PNG , 100 , outputStream );
121
+
122
+ result = Base64 .encodeToString (outputStream .toByteArray (), Base64 .DEFAULT );
123
+ }
124
+
125
+ return TextUtils .isEmpty (result ) ? result : "data:image/png;base64," + result ;
126
+ } catch (Exception e ) {
127
+ Log .d (TAG , e .getMessage ());
128
+ return "" ;
129
+ }
130
+ }
131
+
132
+ private String getNotificationImage (Notification notification ) {
133
+ try {
134
+ if (!notification .extras .containsKey (Notification .EXTRA_PICTURE )) return "" ;
135
+
136
+ Bitmap imageBitmap = (Bitmap ) notification .extras .get (Notification .EXTRA_PICTURE );
137
+
138
+ BitmapFactory .Options options = new BitmapFactory .Options ();
139
+ options .inSampleSize = this .calculateInSampleSize (options , 100 ,100 );
140
+ options .inJustDecodeBounds = false ;
141
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
142
+ imageBitmap .compress (Bitmap .CompressFormat .JPEG , 30 , outputStream );
143
+
144
+ String result = Base64 .encodeToString (outputStream .toByteArray (), Base64 .DEFAULT );
145
+
146
+ return TextUtils .isEmpty (result ) ? result : "data:image/png;base64," + result ;
147
+ } catch (Exception e ) {
148
+ Log .d (TAG , e .getMessage ());
149
+ return "" ;
63
150
}
64
151
}
152
+
153
+ public int calculateInSampleSize (
154
+ BitmapFactory .Options options , int reqWidth , int reqHeight ) {
155
+ // Raw height and width of image
156
+ final int height = options .outHeight ;
157
+ final int width = options .outWidth ;
158
+ int inSampleSize = 1 ;
159
+
160
+ if (height > reqHeight || width > reqWidth ) {
161
+
162
+ final int halfHeight = height / 2 ;
163
+ final int halfWidth = width / 2 ;
164
+
165
+ // Calculate the largest inSampleSize value that is a power of 2 and keeps both
166
+ // height and width larger than the requested height and width.
167
+ while ((halfHeight / inSampleSize ) >= reqHeight
168
+ && (halfWidth / inSampleSize ) >= reqWidth ) {
169
+ inSampleSize *= 2 ;
170
+ }
171
+ }
172
+
173
+ return inSampleSize ;
174
+ }
65
175
}
0 commit comments