package com.plugin.util;

import android.content.Context;

import com.plugin.core.PluginPublicXmlConst;

/**
 * Created by cailiming
 */
public class ResourceUtil {

    public static String getString(String value, Context pluginContext) {
        String idHex = null;
        if (value != null && value.startsWith("@") && value.length() == 9) {
            idHex = value.replace("@", "");

        } else if (value != null && value.startsWith("@android:") && value.length() == 17) {
            idHex = value.replace("@android:", "");
        }

        if (idHex != null) {
            try {
                int id = Integer.parseInt(idHex, 16);
                //此时context可能还没有初始化
                if (pluginContext != null) {
                    String des = pluginContext.getString(id);
                    return des;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return value;
    }

    public static Boolean getBoolean(String value, Context pluginContext) {
        String idHex = null;
        if (value != null && value.startsWith("@") && value.length() == 9) {
            idHex = value.replace("@", "");

        } else if (value != null && value.startsWith("@android:") && value.length() == 17) {
            idHex = value.replace("@android:", "");
        }

        if (idHex != null) {
            try {
                int id = Integer.parseInt(idHex, 16);
                //此时context可能还没有初始化
                if (pluginContext != null) {
                    return pluginContext.getResources().getBoolean(id);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (value != null) {
            return Boolean.parseBoolean(value);
        }

        return null;
    }

    public static int getResourceId(String value) {
        String idHex = null;
        if (value != null && value.startsWith("@") && value.length() == 9) {
            idHex = value.replace("@", "");

        } else if (value != null && value.startsWith("@android:") && value.length() == 17) {
            idHex = value.replace("@android:", "");
        }
        if (idHex != null) {
            try {
                int id = Integer.parseInt(idHex, 16);
                return id;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return 0;
    }

    public static boolean isMainResId(int resid) {
        //如果使用的使openatlasextention
        //默认宿主的资源id以0x7f3X开头
        return PluginPublicXmlConst.resourceMap.get(resid>>16) != null;
    }
}

@@ -98,16 +98,102 @@
 Const;%0A%0A
+import java.lang.reflect.InvocationTargetException;%0Aimport java.lang.reflect.Method;%0A%0A
 /**%0A * C
@@ -2712,11 +2712,974 @@
 l;%0A    %7D
+%0A%0A    public static void rewriteRValues(ClassLoader cl, String packageName, int id) %7B%0A        final Class%3C?%3E rClazz;%0A        try %7B%0A            rClazz = cl.loadClass(packageName + %22.R%22);%0A        %7D catch (ClassNotFoundException e) %7B%0A            LogUtil.d(%22No resource references to update in package %22 + packageName);%0A            return;%0A        %7D%0A%0A        final Method callback;%0A        try %7B%0A            callback = rClazz.getMethod(%22onResourcesLoaded%22, int.class);%0A        %7D catch (NoSuchMethodException e) %7B%0A            // No rewriting to be done.%0A            return;%0A        %7D%0A%0A        Throwable cause;%0A        try %7B%0A            callback.invoke(null, id);%0A            return;%0A        %7D catch (IllegalAccessException e) %7B%0A            cause = e;%0A        %7D catch (InvocationTargetException e) %7B%0A            cause = e.getCause();%0A        %7D%0A%0A        throw new RuntimeException(%22Failed to rewrite resource references for %22 + packageName,%0A                cause);%0A    %7D
 %0A%7D%0A

Add R Rewritter