package com.ysnows.base.utils;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.ysnows.base.base.BApp;

import java.util.List;

public class PKUtil {
    public static boolean isWeixinAvilible() {
        final PackageManager packageManager = BApp.instance().getPackageManager();// 获取packagemanager
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals("com.tencent.mm")) {
                    return true;
                }
            }
        }

        return false;
    }

    /**
     * 判断qq是否可用
     *
     * @return
     */
    public static boolean isQQClientAvailable() {
        final PackageManager packageManager = BApp.instance().getPackageManager();
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals("com.tencent.mobileqq")) {
                    return true;
                }
                if (pn.equals("com.tencent.tim")) {
                    return true;
                }
            }
        }
        return false;
    }


    public static int getAppVersionCode() {
        if (BApp.instance() != null) {
            PackageManager pm = BApp.instance().getPackageManager();
            if (pm != null) {
                PackageInfo pi;
                try {
                    pi = pm.getPackageInfo(BApp.instance().getPackageName(), 0);
                    if (pi != null) {
                        return pi.versionCode;
                    }
                } catch (PackageManager.NameNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }
        return -1;
    }

    public static String getAppVersionName() {
        if (BApp.instance() != null) {
            PackageManager pm = BApp.instance().getPackageManager();
            if (pm != null) {
                PackageInfo pi;
                try {
                    pi = pm.getPackageInfo(BApp.instance().getPackageName(), 0);
                    if (pi != null) {
                        return pi.versionName;
                    }
                } catch (PackageManager.NameNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }
        return "";
    }


}
