11package com .hjq .logcat ;
22
33import android .app .Application ;
4+ import android .app .NotificationChannel ;
5+ import android .app .NotificationManager ;
46import android .content .ContentProvider ;
57import android .content .ContentValues ;
68import android .content .Context ;
79import android .database .Cursor ;
810import android .net .Uri ;
11+ import android .os .Build ;
12+ import android .os .Build .VERSION ;
13+ import android .os .Build .VERSION_CODES ;
14+ import android .provider .Settings ;
915import android .support .v4 .app .NotificationManagerCompat ;
1016import android .widget .Toast ;
1117
@@ -29,7 +35,7 @@ public boolean onCreate() {
2935 Boolean windowEntrance = LogcatUtils .getMetaBooleanData (
3036 context , LogcatContract .META_DATA_LOGCAT_WINDOW_ENTRANCE );
3137 if (notifyEntrance == null && windowEntrance == null ) {
32- if (NotificationManagerCompat . from (context ). areNotificationsEnabled ( )) {
38+ if (isNotificationChannelEnabled (context )) {
3339 notifyEntrance = true ;
3440 } else {
3541 windowEntrance = true ;
@@ -44,7 +50,11 @@ public boolean onCreate() {
4450
4551 if (windowEntrance != null && windowEntrance ) {
4652 if (context instanceof Application ) {
47- LogcatDispatcher .with ((Application ) context );
53+ if (VERSION .SDK_INT >= VERSION_CODES .M && Settings .canDrawOverlays (context )) {
54+ LogcatGlobalDispatcher .launch ((Application ) context );
55+ } else {
56+ LogcatLocalDispatcher .launch ((Application ) context );
57+ }
4858 } else {
4959 Toast .makeText (context , R .string .logcat_launch_error , Toast .LENGTH_LONG ).show ();
5060 }
@@ -77,4 +87,22 @@ public int delete(Uri uri, String selection, String[] selectionArgs) {
7787 public int update (Uri uri , ContentValues values , String selection , String [] selectionArgs ) {
7888 return 0 ;
7989 }
90+
91+ private boolean isNotificationChannelEnabled (Context context ) {
92+ if (!NotificationManagerCompat .from (context ).areNotificationsEnabled ()) {
93+ return false ;
94+ }
95+
96+ if (Build .VERSION .SDK_INT < Build .VERSION_CODES .O ) {
97+ return true ;
98+ }
99+
100+ NotificationManager manager = (NotificationManager ) context .getSystemService (Context .NOTIFICATION_SERVICE );
101+ NotificationChannel channel = manager .getNotificationChannel (LogcatService .NOTIFICATION_CHANNEL_ID );
102+ if (channel == null ) {
103+ return true ;
104+ }
105+
106+ return channel .getImportance () != NotificationManager .IMPORTANCE_NONE ;
107+ }
80108}
0 commit comments