package com.secspace.lib.common.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;

/**
 * Created by SKR-CYW on 2017/7/11.
 */

public final class BadgeUtil {
    /**
     * 设置华为桌面角标
     */
    public static void setHWBadgeNum(Context context, int num, Class clazz) {
        try {
            if (!checkIsSupportedByVersion(context))
                return;
            Bundle bunlde = new Bundle();
            bunlde.putString("package", context.getPackageName());
            bunlde.putString("class", clazz.getCanonicalName());
            bunlde.putInt("badgenumber", num);
            context.getContentResolver().call(Uri.parse("content://com.huawei.android.launcher.settings/badge/"), "change_badge", null, bunlde);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 检测华为EMUI版本是否支持桌面角标
     *
     * @param context
     * @return
     * @throws PackageManager.NameNotFoundException
     */
    private static boolean checkIsSupportedByVersion(Context context) throws PackageManager.NameNotFoundException {
        PackageManager manager = context.getPackageManager();
        PackageInfo info = manager.getPackageInfo("com.huawei.android.launcher", 0);
        if (info.versionCode >= 63029) {
            return true;
        }
        return false;
    }
}