package com.zxhyis.toolss.logic.utils;

import android.app.usage.UsageEvents;
import android.app.usage.UsageStatsManager;
import android.content.Context;
import android.content.pm.PackageManager;

import com.zxhyis.toolss.logic.LogEx;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

/* compiled from: R8$$SyntheticClass */
/* loaded from: classes.dex */
public final /* synthetic */ class TestN {

    /* renamed from: b  reason: collision with root package name */

    /* renamed from: c  reason: collision with root package name */

    /* renamed from: d  reason: collision with root package name */
    public final /* synthetic */ long f36678d;

    /* renamed from: f  reason: collision with root package name */
    public final /* synthetic */ long f36679f;

    /* renamed from: g  reason: collision with root package name */

    public /* synthetic */ TestN(long j10, long j11) {
        this.f36678d = j10;
        this.f36679f = j11;
    }

    int a = 0;

    public HashMap<String, List<UsageEvents.Event>> run(Context context) {

        UsageStatsManager usageStatsManager = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);

        HashMap hashMap = new HashMap<String, List<String>>();
        LinkedList linkedList = new LinkedList();
        UsageEvents queryEvents = usageStatsManager.queryEvents(this.f36678d, this.f36679f);
        UsageEvents.Event event = null;
        while (queryEvents != null && queryEvents.hasNextEvent()) {
            UsageEvents.Event event2 = new UsageEvents.Event();
            if (queryEvents.getNextEvent(event2) && event2.getEventType() != 23 && event2.getEventType() != 11) {
                if (!linkedList.isEmpty() && Objects.equals(((UsageEvents.Event) ((LinkedList) linkedList.getLast()).getLast()).getPackageName(), event2.getPackageName())) {
                    ((LinkedList) linkedList.getLast()).addLast(event2);
                } else {
                    LinkedList linkedList2 = new LinkedList();
                    linkedList2.addLast(event2);
                    linkedList.addLast(linkedList2);
                }
                if ((event == null || !Objects.equals(event.getPackageName(), event2.getPackageName())) && ((event == null || event.getEventType() == 2) && event2.getEventType() == 1)) {
                    List list = (List) hashMap.get(event2.getPackageName());
                    if (list == null) {
                        list = new ArrayList();
                    }
                    list.add(event2);
                    hashMap.put(event2.getPackageName(), list);
                }
                event = event2;
            }
        }

        HashMap hashMap2 = new HashMap<String, Integer>();
        Iterator it = linkedList.iterator();
        while (it.hasNext()) {
            LinkedList<UsageEvents.Event> linkedList3 = (LinkedList) it.next();
            if (linkedList3.size() >= 2) {
                if (!isEmpty(linkedList3)) {
                    for (UsageEvents.Event event3 : linkedList3) {
                        if (event3.getEventType() == 1) {
                            break;
                        }
                    }
                }
                String packageName = ((UsageEvents.Event) linkedList3.getFirst()).getPackageName();
                Integer num = (Integer) hashMap2.get(packageName);
                if (num == null) {
                    hashMap2.put(packageName, 1);
                } else {
                    hashMap2.put(packageName, Integer.valueOf(num.intValue() + 1));
                }
            }
        }

//        LogEx.INSTANCE.logDebug("hashMap2", "hashMap2.size= " + hashMap2.size());

//        hashMap2.forEach((key, value) -> {
//            LogEx.INSTANCE.logDebug("hashMap2", "key= " + key + " value= " + value);
//        });

        //应该是rv的实体类
        ArrayList arrayList = new ArrayList();


        //前台前台map集合
        hashMap.forEach((key, value) -> {
            String str = (String) key;
            int i7 = 0;
            int size = ((List<Integer>) value).size();
            if (hashMap2.containsKey(str)) {
                i7 = ((Integer) hashMap2.get(str)).intValue();
            } else {
                i7 = 0;
            }
            arrayList.add(new a(c(context, str), str, size, i7));


//            LogEx.INSTANCE.logDebug("hashMap", "key=" + key.toString());


//            ((List<?>) value).forEach(eventll -> {
//
//                UsageEvents.Event ss = (UsageEvents.Event) eventll;
//                if (ss.getPackageName().equals("com.tencent.mm")) {
//                    a = a + 1;
//                    LogEx.INSTANCE.logDebug("hashMap", ss.getPackageName() + " " + ss.getEventType() + " " + ss.getTimeStamp() + " a=" + a);
//                }
//            });

        });

//        arrayList.forEach((a) -> {
//            LogEx.INSTANCE.logDebug("arrayList", "a=" + a.toString());
//        });

        return (HashMap<String, List<UsageEvents.Event>>) hashMap;
    }

    public static String c(Context context, String str) {
        PackageManager packageManager = context.getPackageManager();
        try {
            return packageManager.getApplicationLabel(packageManager.getApplicationInfo(str, 0)).toString();
        } catch (Exception e10) {
//            f35107a.l(null, e10);
            return null;
        }
    }


    public Boolean isEmpty(Collection<?> collection) {
        if (collection != null && !collection.isEmpty()) {
            return false;
        }
        return true;
    }
}