package com.zxhyis.toolss.ui.page.recentusage;

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

import com.zxhyis.toolss.logic.LogEx;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Objects;

public class ScreenTimeStat {

    /* renamed from: b  reason: collision with root package name */
    public final /* synthetic */ long endTs;
    public final /* synthetic */ long beginTs;

    private String TAG = "ScreenTimeStat";

    public final Map<String, b> run(Context context) {

        LogEx.INSTANCE.logDebug(TAG, "ScreenTimeStat run", false);

        Object obj;
        Object obj2;

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

        HashMap hashMap = new HashMap();
        long j12 = 0;
        if (this.endTs - this.beginTs > 259200000) {
            List<UsageStats> queryUsageStats = usageStatsManager.queryUsageStats(0, this.beginTs, this.endTs);
            if (queryUsageStats != null) {
                for (UsageStats usageStats : queryUsageStats) {
                    if (usageStats.getTotalTimeInForeground() > j12 && usageStats.getLastTimeUsed() > this.beginTs) {
                        b bVar = (b) hashMap.get(usageStats.getPackageName());
                        if (bVar == null) {
                            hashMap.put(usageStats.getPackageName(), new b(getAppName(context, usageStats.getPackageName()), usageStats.getPackageName(), usageStats.getTotalTimeInForeground()));
                        } else {
                            bVar.c = usageStats.getTotalTimeInForeground() + bVar.c;
                        }
                    }
                    j12 = 0;
                }
            }
        } else {
            LinkedList linkedList = new LinkedList();
            UsageEvents queryEvents = usageStatsManager.queryEvents(this.beginTs, this.endTs);
            while (queryEvents != null && queryEvents.hasNextEvent()) {
                UsageEvents.Event event = new UsageEvents.Event();
                if (queryEvents.getNextEvent(event) && event.getEventType() != 23 && event.getEventType() != 11) {
                    if (!linkedList.isEmpty() && Objects.equals(((UsageEvents.Event) ((LinkedList) linkedList.getLast()).getLast()).getPackageName(), event.getPackageName())) {
                        ((LinkedList) linkedList.getLast()).addLast(event);
                    } else {
                        LinkedList linkedList2 = new LinkedList();
                        linkedList2.addLast(event);
                        linkedList.addLast(linkedList2);
                    }
                }
            }
            Iterator it = linkedList.iterator();
            while (it.hasNext()) {
                LinkedList linkedList3 = (LinkedList) it.next();
                String packageName = ((UsageEvents.Event) linkedList3.getFirst()).getPackageName();
                Iterator it2 = linkedList3.iterator();
                while (true) {
                    obj = null;
                    if (it2.hasNext()) {
                        obj2 = it2.next();
                        if (((UsageEvents.Event) obj2).getEventType() == 1) {
                            Log.i("screenStat", "112233");
                            break;
                        }
                    } else {
                        obj2 = null;
                        break;
                    }
                }
                UsageEvents.Event event2 = (UsageEvents.Event) obj2;
                ListIterator listIterator = linkedList3.listIterator(linkedList3.size());
                while (true) {
                    if (listIterator.hasPrevious()) {
                        Object previous = listIterator.previous();
                        if (((UsageEvents.Event) previous).getEventType() == 2) {
                            obj = previous;
                        }
                    } else {
                        break;
                    }
                }
                UsageEvents.Event event3 = (UsageEvents.Event) obj;
                if (event2 != null && event3 != null && event2.getTimeStamp() < event3.getTimeStamp()) {
                    b bVar2 = (b) hashMap.get(packageName);
                    if (bVar2 == null) {
                        bVar2 = new b(getAppName(context, packageName), packageName, 0L);
                    }
                    bVar2.c = (event3.getTimeStamp() - event2.getTimeStamp()) + bVar2.c;
                    hashMap.put(packageName, bVar2);
                }
            }
        }


        hashMap.forEach((key, value) -> {
            StringBuffer log = new StringBuffer();
            log.append("key=").append(key).append(" ");
            b ss = (b) value;
            log.append("pkg=").append(ss.getPackageName()).append(" ");
            log.append("f30766a=").append(ss.f30766a).append(" ");
            log.append("f30767b=").append(ss.f30767b).append(" ");
            log.append("c=").append(ss.c).append(" ");
            LogEx.INSTANCE.logDebug(TAG, log.toString(), false);
        });
        return hashMap;
    }

    public /* synthetic */ ScreenTimeStat(long beginTs, long endTs) {
        this.beginTs = beginTs;
        this.endTs = endTs;
    }

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