package cn.yyxx.commsdk.core.utils;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;

import cn.yyxx.commsdk.base.utils.Logger;

/**
 * @author #Suyghur.
 * Created on 2021/10/18
 */
public class ManifestUtils {

    public static String getMetaDataValue(Context context, String key) {
        Object value = "";
        if (TextUtils.isEmpty(key)) {
            Logger.logHandler("metaName is empty");
            return "";
        }
        try {
            ApplicationInfo info = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
            value = info.metaData.get(key);
            // 是否有引用
            if (null != value && value.toString().contains("ref=")) {
                String newKeyName = value.toString().split("=")[1];
                value = getMetaDataValue(context, newKeyName);
            }
        } catch (PackageManager.NameNotFoundException e) {
            Logger.logHandler("must config application meta data : " + key);
        }
        if (null == value) {
            value = "";
        }
        return value.toString();
    }
}
