try to grant notification listener access

Signed-off-by: cjybyjk <cjybyjk@gmail.com>
diff --git a/app/src/main/java/org/exthmui/game/services/DanmakuService.java b/app/src/main/java/org/exthmui/game/services/DanmakuService.java
index fb0cf55..32b2c76 100644
--- a/app/src/main/java/org/exthmui/game/services/DanmakuService.java
+++ b/app/src/main/java/org/exthmui/game/services/DanmakuService.java
@@ -45,7 +45,11 @@
 
     @Override
     public void onListenerDisconnected() {
-        unregisterReceiver(configReceiver);
+        try {
+            unregisterReceiver(configReceiver);
+        } catch (Exception e) {
+            // do nothing
+        }
         super.onListenerDisconnected();
     }
 
diff --git a/app/src/main/java/org/exthmui/game/services/GamingService.java b/app/src/main/java/org/exthmui/game/services/GamingService.java
index 2a5f246..31d1393 100644
--- a/app/src/main/java/org/exthmui/game/services/GamingService.java
+++ b/app/src/main/java/org/exthmui/game/services/GamingService.java
@@ -6,6 +6,7 @@
 import android.app.PendingIntent;
 import android.app.Service;
 import android.content.BroadcastReceiver;
+import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
@@ -21,6 +22,7 @@
 import android.telephony.PhoneStateListener;
 import android.telephony.TelephonyManager;
 import android.text.TextUtils;
+import android.util.Log;
 import android.widget.Toast;
 
 import androidx.annotation.Nullable;
@@ -31,6 +33,10 @@
 import org.exthmui.game.R;
 import org.exthmui.game.misc.Constants;
 
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
 import lineageos.hardware.LineageHardwareManager;
 
 public class GamingService extends Service {
@@ -91,6 +97,8 @@
         super.onCreate();
         createNotificationChannel(this, Constants.CHANNEL_GAMING_MODE_STATUS, getString(R.string.channel_gaming_mode_status), NotificationManager.IMPORTANCE_LOW);
 
+        checkNotificationListener();
+
         mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
         mStatusBarService = IStatusBarService.Stub.asInterface(ServiceManager.getService(Context.STATUS_BAR_SERVICE));
         try {
@@ -130,6 +138,24 @@
         return super.onStartCommand(intent, flags, startId);
     }
 
+    private void checkNotificationListener() {
+        String notificationListeners = Settings.Secure.getString(getContentResolver(), Settings.Secure.ENABLED_NOTIFICATION_LISTENERS);
+        List<String> listenersList;
+        listenersList = new ArrayList<>();
+        if (!TextUtils.isEmpty(notificationListeners)) {
+            listenersList.addAll(Arrays.asList(notificationListeners.split(":")));
+        }
+        ComponentName danmakuComponent = new ComponentName(this, DanmakuService.class);
+        if (!listenersList.contains(danmakuComponent.flattenToString())) {
+            listenersList.add(danmakuComponent.flattenToString());
+            Settings.Secure.putString(getContentResolver(), Settings.Secure.ENABLED_NOTIFICATION_LISTENERS, String.join(":", listenersList));
+        }
+        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
+        if (!notificationManager.isNotificationListenerAccessGranted(danmakuComponent)) {
+            notificationManager.setNotificationListenerAccessGranted(danmakuComponent, true);
+        }
+    }
+
     private void updateConfig() {
         // danmaku
         mCurrentConfig.putBoolean(Constants.ConfigKeys.SHOW_DANMAKU, getBooleanSetting(Constants.ConfigKeys.SHOW_DANMAKU, Constants.ConfigDefaultValues.SHOW_DANMAKU));