package com.zxhyis.toolss.common.notification

import android.app.ActivityManager
import android.app.ActivityManager.RunningAppProcessInfo
import android.content.Context




object JinChengCheck {

    fun getAliveProcessName(context: Context): String? {
        val processNames = arrayOf(
            "com.zxhyis.tools",
            "com.zxhyis.tools:mainService1",
            "com.zxhyis.tools:mainService2",
            "com.zxhyis.tools:mainService3"
        )
        for (processName in processNames) {
            if (isProcessAlive(context, processName)) {
                return if (isCurrentProcess(context, processName)) {
                    null
                } else processName
            }
        }
        return null
    }

     fun isProcessAlive(context: Context, processName: String): Boolean {
        val activityManager: ActivityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        val runningAppProcesses: List<RunningAppProcessInfo> = activityManager.getRunningAppProcesses() ?: return false
        for (processInfo in runningAppProcesses) {
            if (processInfo.processName.equals(processName)) {
                return true
            }
        }
        return false
    }

     fun isCurrentProcess(context: Context, processName: String): Boolean {
        return processName == context.packageName
    }
}