package com.test.basd.onecleanmaster.helps.recentapp

import android.app.usage.UsageEvents
import android.app.usage.UsageStatsManager
import android.content.Context
import java.util.LinkedList

class LaunchTimeStat(val beginTs: Long, val ends: Long) {
    var a = 0
    fun run(context: Context): HashMap<String, MutableList<UsageEvents.Event>> {
        val usageStatsManager = context.getSystemService(Context.USAGE_STATS_SERVICE) as UsageStatsManager
        val hashMap = HashMap<String, MutableList<UsageEvents.Event>>()
        val linkedList = LinkedList<LinkedList<UsageEvents.Event>>()
        val queryEvents = usageStatsManager.queryEvents(beginTs, ends)
        var event: UsageEvents.Event? = null
        while (queryEvents != null && queryEvents.hasNextEvent()) {
            val event2 = UsageEvents.Event()
            if (queryEvents.getNextEvent(event2) && event2.eventType != 23 && event2.eventType != 11) {
                if (!linkedList.isEmpty() && linkedList.getLast().getLast().packageName == event2.packageName) {
                    linkedList.getLast().addLast(event2)
                } else {
                    val linkedList2 = LinkedList<UsageEvents.Event>()
                    linkedList2.addLast(event2)
                    linkedList.addLast(linkedList2)
                }
                if ((event == null || event.packageName != event2.packageName) && (event == null || event.eventType == 2) && event2.eventType == 1) {
                    var list = hashMap[event2.packageName]
                    if (list == null) {
                        list = ArrayList()
                    }
                    list.add(event2)
                    hashMap[event2.packageName] = list
                }
                event = event2
            }
        }
        return hashMap
    }

    companion object {
        fun c(context: Context, str: String?): String? {
            val packageManager = context.packageManager
            return try {
                packageManager.getApplicationLabel(packageManager.getApplicationInfo(str!!, 0)).toString()
            } catch (e10: Exception) {
                null
            }
        }
    }
}