package com.lc.myapplication.security.locks.tool;

import android.annotation.SuppressLint;
import android.app.Service;
import android.app.usage.UsageEvents;
import android.app.usage.UsageStatsManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.os.UserManager;
import android.os.Vibrator;
import android.text.TextUtils;
import android.util.ArraySet;
import android.util.Log;

import com.bytedance.frameworks.encryptor.BuildConfig;
import com.lc.myapplication.security.locks.lockroom.SecurityLockDataBase;
import com.lc.myapplication.security.locks.lockroom.SecurityLockEntity;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;

public class AppsManager {


    public static Set<SecurityLockEntity> queryapps(Context context){
        String packageName = BuildConfig.APPLICATION_ID;
        Set<SecurityLockEntity> entitieslist = new ArraySet<>();//set保证存储元素不重复
        Intent intent = new Intent(Intent.ACTION_MAIN,null);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        PackageManager packageManager = context.getPackageManager();
        List<ResolveInfo> resolveInfoslist = packageManager.queryIntentActivities(intent,PackageManager.MATCH_ALL);
        for (ResolveInfo info : resolveInfoslist){
            String packname = info.activityInfo.packageName;
            if (packname == context.getPackageName()){
                continue;
            }
            SecurityLockEntity securityLockEntity = new SecurityLockEntity();
            securityLockEntity.setApppakname(packname);

            try {
                ApplicationInfo applicationInfo = packageManager.getApplicationInfo(packname,PackageManager.GET_UNINSTALLED_PACKAGES);
                CharSequence applicationLabel = packageManager.getApplicationLabel(applicationInfo);//获取应用程序的名称
                securityLockEntity.setApplsign(applicationLabel.toString());
                entitieslist.add(securityLockEntity);
            } catch (PackageManager.NameNotFoundException e) {
                throw new RuntimeException(e);
            }
        }
        return entitieslist;
    }
    public static void insertEntity(Set<SecurityLockEntity> list) {
        Log.d("--------size","----size"+list.size());
        MyTools.RUNIO(new Runnable() {
            @Override
            public void run() {
                for (SecurityLockEntity entityApp : list) {
                    SecurityLockDataBase.getSecurityLockDataBase().getsecurityLockDAO().InsertSecurityEntity(entityApp);
                }
            }
        });
    }
    public static void vir(Context context){
        Vibrator vibrator = (Vibrator) context.getSystemService(Service.VIBRATOR_SERVICE);
        vibrator.vibrate(300);
    }
    public static Drawable getapplogo(Context context,String packname){
        PackageManager packageManager = context.getPackageManager();
        Drawable appslogo;
        try {
            appslogo = packageManager.getApplicationIcon(packageManager.getApplicationInfo(packname,PackageManager.GET_UNINSTALLED_PACKAGES));
        } catch (PackageManager.NameNotFoundException e) {
            return null;
        }
        return appslogo;
    }
    public static String appsusage(Context context){
        UsageStatsManager usageStatsManager = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);
        long lastTime = System.currentTimeMillis();
        long startTime = lastTime - 10000;
        String result = "";
        UsageEvents.Event event = new UsageEvents.Event();
        UsageEvents usageEvents = usageStatsManager.queryEvents(startTime, lastTime);
        while (usageEvents.hasNextEvent()) {
            usageEvents.getNextEvent(event);
            if (event.getEventType() == UsageEvents.Event.MOVE_TO_FOREGROUND) {
                result = event.getPackageName();
            }
        }
        if (!TextUtils.isEmpty(result)) {
            return result;
        } else {
            return "";
        }
    }
}
