package com.xhly.corelib.utils;

import static com.xhly.corelib.utils.BaseUtilsKt.LogShow;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Build;


import com.xhly.corelib.bean.AppInfo;

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

public class AppUtils {
    public static List<AppInfo> GetAppList(Context context) {
        List<AppInfo> list = new ArrayList<>();
        List<String>  pkgList=new ArrayList<>();
        PackageManager pm = context.getPackageManager();
        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        List<ResolveInfo> activities = pm.queryIntentActivities(mainIntent, 0);
        for (ResolveInfo info : activities) {
            String packName = info.activityInfo.packageName;
            //去重
            if (packName.equals(context.getPackageName())||pkgList.contains(packName)) {
                continue;
            }
            AppInfo mInfo = new AppInfo();
            mInfo.setIco(info.activityInfo.applicationInfo.loadIcon(pm));
            mInfo.setName(info.activityInfo.applicationInfo.loadLabel(pm).toString());
            LogShow("数据","数据类名" + packName+"|||"+info.activityInfo.name+"|||"+mInfo.getName());
            mInfo.setPackageName(packName);
            pkgList.add(packName);
            try {
                PackageInfo packageInfo = context.getPackageManager().getPackageInfo(packName, PackageManager.GET_META_DATA);
                 mInfo.setVersionName(packageInfo.versionName);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P){
                    mInfo.setVersionNum(packageInfo.getLongVersionCode());
                }else {
                    mInfo.setVersionNum(packageInfo.versionCode);
                }
            } catch (PackageManager.NameNotFoundException e) {
                throw new RuntimeException(e);
            }
            // 为应用程序的启动Activity 准备Intent
            Intent launchIntent = new Intent();
            launchIntent.setComponent(new ComponentName(packName,
                    info.activityInfo.name));
            mInfo.setIntent(launchIntent);
            list.add(mInfo);
        }
        return list;
    }

    /**
     * 判断应用是否已经安装
     */
    public static boolean isAppInstalled(Context context, String packageName) {
        try {
            PackageManager pm = context.getPackageManager();
            pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }

    public static boolean isActivityExist(Context context, Class<?> cls){
       try{
           PackageManager packageManager = context.getPackageManager();
           Intent intent = new Intent(context, cls);
           ResolveInfo resolveInfo = packageManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
           return resolveInfo != null;
       }catch (Exception e){
           return false;
       }
    }
}