15
15
*/
16
16
package com .github .moduth .blockcanary ;
17
17
18
- import android .annotation .TargetApi ;
18
+ import static android .app .PendingIntent .FLAG_UPDATE_CURRENT ;
19
+ import static android .os .Build .VERSION .SDK_INT ;
20
+ import static android .os .Build .VERSION_CODES .JELLY_BEAN ;
21
+
19
22
import android .app .Notification ;
23
+ import android .app .NotificationChannel ;
20
24
import android .app .NotificationManager ;
21
25
import android .app .PendingIntent ;
22
26
import android .content .Context ;
23
27
import android .content .Intent ;
24
- import android .util .Log ;
28
+ import android .os .Build ;
29
+
30
+ import androidx .core .app .NotificationCompat ;
31
+ import androidx .core .app .NotificationManagerCompat ;
25
32
26
33
import com .github .moduth .blockcanary .internal .BlockInfo ;
27
34
import com .github .moduth .blockcanary .ui .DisplayActivity ;
28
35
29
- import java .lang .reflect .InvocationTargetException ;
30
- import java .lang .reflect .Method ;
31
-
32
- import static android .app .PendingIntent .FLAG_UPDATE_CURRENT ;
33
- import static android .os .Build .VERSION .SDK_INT ;
34
- import static android .os .Build .VERSION_CODES .HONEYCOMB ;
35
- import static android .os .Build .VERSION_CODES .JELLY_BEAN ;
36
-
37
36
final class DisplayService implements BlockInterceptor {
38
37
39
38
private static final String TAG = "DisplayService" ;
@@ -49,39 +48,29 @@ public void onBlock(Context context, BlockInfo blockInfo) {
49
48
show (context , contentTitle , contentText , pendingIntent );
50
49
}
51
50
52
- @ TargetApi (HONEYCOMB )
53
51
private void show (Context context , String contentTitle , String contentText , PendingIntent pendingIntent ) {
54
- NotificationManager notificationManager = (NotificationManager )
55
- context .getSystemService (Context .NOTIFICATION_SERVICE );
52
+ NotificationManagerCompat notificationManager = NotificationManagerCompat .from (context );
56
53
57
54
Notification notification ;
58
- if (SDK_INT < HONEYCOMB ) {
59
- notification = new Notification ();
60
- notification .icon = R .drawable .block_canary_notification ;
61
- notification .when = System .currentTimeMillis ();
62
- notification .flags |= Notification .FLAG_AUTO_CANCEL ;
63
- notification .defaults = Notification .DEFAULT_SOUND ;
64
- try {
65
- Method deprecatedMethod = notification .getClass ().getMethod ("setLatestEventInfo" , Context .class , CharSequence .class , CharSequence .class , PendingIntent .class );
66
- deprecatedMethod .invoke (notification , context , contentTitle , contentText , pendingIntent );
67
- } catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException
68
- | InvocationTargetException e ) {
69
- Log .w (TAG , "Method not found" , e );
70
- }
55
+ String packageName = context .getPackageName ();
56
+ NotificationCompat .Builder builder = new NotificationCompat .Builder (context , packageName )
57
+ .setSmallIcon (R .drawable .block_canary_notification )
58
+ .setWhen (System .currentTimeMillis ())
59
+ .setContentTitle (contentTitle )
60
+ .setContentText (contentText )
61
+ .setAutoCancel (true )
62
+ .setContentIntent (pendingIntent )
63
+ .setDefaults (Notification .DEFAULT_SOUND );
64
+ if (SDK_INT >= Build .VERSION_CODES .O ) {
65
+ NotificationChannel notificationChannel = new NotificationChannel (packageName , packageName , NotificationManager .IMPORTANCE_LOW );
66
+ notificationChannel .setDescription (packageName );
67
+ notificationChannel .setShowBadge (false );
68
+ notificationManager .createNotificationChannel (notificationChannel );
69
+ }
70
+ if (SDK_INT < JELLY_BEAN ) {
71
+ notification = builder .getNotification ();
71
72
} else {
72
- Notification .Builder builder = new Notification .Builder (context )
73
- .setSmallIcon (R .drawable .block_canary_notification )
74
- .setWhen (System .currentTimeMillis ())
75
- .setContentTitle (contentTitle )
76
- .setContentText (contentText )
77
- .setAutoCancel (true )
78
- .setContentIntent (pendingIntent )
79
- .setDefaults (Notification .DEFAULT_SOUND );
80
- if (SDK_INT < JELLY_BEAN ) {
81
- notification = builder .getNotification ();
82
- } else {
83
- notification = builder .build ();
84
- }
73
+ notification = builder .build ();
85
74
}
86
75
notificationManager .notify (0xDEAFBEEF , notification );
87
76
}
0 commit comments