package com.zswc.creativity.utils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.Settings;

import com.zswc.creativity.App;

public class PackageUtils {

    /**
     * 检查微信是否安装
     *
     * @param context
     *
     * @return
     */
    public static boolean checkAppInstalled(Context context, String packageName) {
        PackageManager packageManager = context.getPackageManager();
        try {
            PackageInfo packageInfo = packageManager.getPackageInfo(packageName, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
            if (packageInfo != null) {
                return true;
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return false;
    }


    /**
     * 卸载指定包名的应用
     *
     * @param packageName
     */
    public static boolean uninstall(Context context, String packageName) {
        boolean b = checkAppInstalled(context, packageName);
        if (b) {
            Uri packageURI = Uri.parse("package:".concat(packageName));
            Intent intent = new Intent(Intent.ACTION_DELETE);
            intent.setData(packageURI);
            context.startActivity(intent);
            return true;
        }
        return false;
    }

    /**
     * 跳转到指定包名的应用信息页
     *
     * @param packageName
     */
    public static boolean appInfo(Context context, String packageName) {
        boolean b = checkAppInstalled(context, packageName);
        if (b) {
            Uri packageURI = Uri.parse("package:".concat(packageName));
            Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            intent.setData(packageURI);
            context.startActivity(intent);
            return true;
        }
        return false;
    }


//    public static void openApp(Context context, String packageName) {
//        Intent intent = App.getApp().getPackageManager().getLaunchIntentForPackage(packageName);
//        if (intent != null) {
//            context.startActivity(intent);
//        }
//    }
}
