package com.example.vastlib.utils

import android.content.ComponentName
import android.content.Context
import android.content.pm.PackageManager
import android.provider.Settings

fun Context.notificationListenerEnable():Boolean {
    val flat = Settings.Secure.getString(contentResolver, "enabled_notification_listeners")
    return flat?.contains(packageName) == true
}

fun Context.restartNotificationListenerServiceState() {
    var notificationListenerServiceClass:String? = null
    kotlin.runCatching {
        val packageInfo = packageManager.getPackageInfo(packageName,
            PackageManager.GET_SERVICES or PackageManager.GET_DISABLED_COMPONENTS)
        for (serviceInfo in packageInfo.services) {
            if("android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" == serviceInfo.permission) {
                notificationListenerServiceClass = serviceInfo.name
            }
        }
    }

    if(null == notificationListenerServiceClass) return
    packageManager.setComponentEnabledSetting(ComponentName(this,
        notificationListenerServiceClass!!
    ), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP)

    packageManager.setComponentEnabledSetting(ComponentName(this,
        notificationListenerServiceClass!!
    ), PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP)
}