package com.example.applock.allmanager;

import android.app.Service;
import android.app.usage.UsageEvents;
import android.app.usage.UsageStatsManager;
import android.content.Context;
import android.content.Entity;
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.media.tv.interactive.AppLinkInfo;
import android.os.Vibrator;
import android.text.TextUtils;
import android.util.ArraySet;

import com.example.applock.roombase.AppEntity;

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

public class AppManager {
    public static Set<AppEntity> QueryPhoneApp(Context context){
      Set<AppEntity> applist = new ArraySet<>();
        Intent intent = new Intent(Intent.ACTION_MAIN,null);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);

        PackageManager packageManager = context.getPackageManager();

       List<ResolveInfo> resolveInfolist = packageManager.queryIntentActivities(intent,PackageManager.MATCH_ALL);
        for (ResolveInfo info : resolveInfolist){
            String appPackageName = info.activityInfo.packageName;
            if(appPackageName == context.getPackageName()){
                continue;
            }
            AppEntity appEntity = new AppEntity();
            appEntity.setApppackageName(appPackageName);
            try {
                ApplicationInfo applicationInfo = packageManager.getApplicationInfo(appPackageName,PackageManager.GET_UNINSTALLED_PACKAGES);
                CharSequence applicationLabel = packageManager.getApplicationLabel(applicationInfo);//获取应用程序的名称
                if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0){
                    appEntity.setAppfrom(false);
                }else {
                    appEntity.setAppfrom(true);
                }
                appEntity.setApplabel(applicationLabel.toString());
                applist.add(appEntity);
            } catch (PackageManager.NameNotFoundException e) {
                throw new RuntimeException(e);
            }
        }
        return applist;
    }
    public static String getAppVersions(Context context){
        String appversions = "";
        try {
            PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(),0);
            appversions = ((PackageInfo)packageInfo).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            throw new RuntimeException(e);
        }
        return appversions;
    }


    public static String getUsage(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 "";
        }
    }
    public static void startVibrator(Context context) {
        Vibrator vib = (Vibrator) context.getSystemService(Service.VIBRATOR_SERVICE);
        vib.vibrate(400);
    }

    public static Drawable getIcon(Context context, String packageName) {
        PackageManager packageManager = context.getPackageManager();
        Drawable icon;
        try {
            icon = packageManager.getApplicationIcon(packageManager.getApplicationInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES));
        } catch (PackageManager.NameNotFoundException e) {
            return null;
        }
        return icon;
    }
}
