package com.mjsheng.myappstore.utils;

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.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;


import com.mjsheng.myappstore.bean.AppUpdateInfo;
import com.mjsheng.myappstore.bean.LocalAppInfo;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class AppsManagerUtils {

    public static AppUpdateInfo getUpAppsByPackageName(Context context, String packageName) {
        PackageManager pm = context.getPackageManager();
        AppUpdateInfo appInfo = new AppUpdateInfo();
        try {
            PackageInfo packinfo = pm.getPackageInfo(packageName, 0);
            appInfo.setPackageName(packageName);
            appInfo.setVersion(packinfo.versionName);
            if (packinfo.versionName == null || packinfo.versionName.equals("")) {
                appInfo.setVersion(packinfo.versionCode + "");
            }
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
        return appInfo;
    }

    public static LocalAppInfo getAppsByPackageName(Context context, String packageName) {
        PackageManager pm = context.getPackageManager();
        List<LocalAppInfo> appInfos = new ArrayList<LocalAppInfo>();
        Intent intent = new Intent(Intent.ACTION_MAIN, null);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        // 通过查询，获得所有ResolveInfo对象.
        List<ResolveInfo> infos = pm.queryIntentActivities(intent, 0);
        for (ResolveInfo info : infos) {
            // 不列出系统应用
            String pkg = info.activityInfo.packageName;
            if (pkg.equals(packageName)) {
                extractedAppInfo(pm, appInfos, info);
            }

        }
        return appInfos.size() == 0 ? null : appInfos.get(0);
    }

    public static List<AppUpdateInfo> getUpadteApps(Context context) {
        PackageManager pm = context.getPackageManager();
        List<AppUpdateInfo> appInfos = new ArrayList<AppUpdateInfo>();
        Intent intent = new Intent(Intent.ACTION_MAIN, null);

        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        // 通过查询，获得所有ResolveInfo对象.
        List<ResolveInfo> infos = pm.queryIntentActivities(intent, 0);
        for (ResolveInfo info : infos) {
            // 不列出系统应用
            String pkg = info.activityInfo.packageName;
            if (!isSystemApp(context, pkg)) {
                PackageInfo packageInfo = null;
                AppUpdateInfo appInfo = new AppUpdateInfo();
                appInfo.setPackageName(info.activityInfo.packageName);
                try {
                    packageInfo = pm.getPackageInfo(info.activityInfo.packageName, 0);
                    appInfo.setVersion(packageInfo.versionName);
                    if (packageInfo.versionName == null || packageInfo.versionName.equals("")) {
                        appInfo.setVersion(packageInfo.versionCode + "");
                    }
                } catch (NameNotFoundException e) {
                    e.printStackTrace();
                }
                appInfos.add(appInfo);
            }

        }
        return appInfos;
    }

    public static List<LocalAppInfo> getUserApps(Context context) {
        PackageManager pm = context.getPackageManager();
        List<LocalAppInfo> appInfos = new ArrayList<LocalAppInfo>();
        Intent intent = new Intent(Intent.ACTION_MAIN, null);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        // 通过查询，获得所有ResolveInfo对象.
        List<ResolveInfo> infos = pm.queryIntentActivities(intent, 0);
        for (ResolveInfo info : infos) {
            // 不列出系统应用
            String pkg = info.activityInfo.packageName;
            if (!isSystemApp(context, pkg)) {
                extractedAppInfo(pm, appInfos, info);
            }

        }
        return appInfos;
    }


    private static void extractedAppInfo(PackageManager pm,
                                         List<LocalAppInfo> appInfos, ResolveInfo info) {
        PackageInfo packageInfo = null;
        LocalAppInfo appInfo = new LocalAppInfo();
        appInfo.setIcon(info.activityInfo.loadIcon(pm));
        appInfo.setAppName(info.activityInfo.loadLabel(pm).toString());
        appInfo.setPackageName(info.activityInfo.packageName);

        try {
            packageInfo = pm.getPackageInfo(info.activityInfo.packageName, 0);
            appInfo.setVersion(packageInfo.versionName);
            if (packageInfo.versionName == null || packageInfo.versionName.equals("")) {
                appInfo.setVersion(packageInfo.versionCode + "");
            }
            String appInstallDir = info.activityInfo.applicationInfo.publicSourceDir;
            int size = Integer.valueOf((int) new File(appInstallDir).length());
            appInfo.setSize(size);
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
        appInfos.add(appInfo);
    }


    public static boolean isSystemApp(Context context, String pkg) {
        try {
            PackageInfo packageInfo = context.getPackageManager().getPackageInfo(pkg, 0);
            if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) <= 0) {
                if (pkg.equals(context.getPackageName())) {
                    return true;
                } else {
                    //第三方应用
                    return false;
                }
            } else {
                //系统应用
                return true;
            }
        } catch (NameNotFoundException e) {
            e.printStackTrace();
            return true;
        }
    }

}
