Search in sources :

Example 91 with AssetManager

use of android.content.res.AssetManager in project AndroidLife by CaMnter.

the class AssetsUtils method extractAssets.

/**
 * 把 Assets 里面得文件复制到 /data/data/files 目录下
 *
 * @param context context
 * @param sourceName sourceName
 */
public static void extractAssets(@NonNull final Context context, @NonNull final String sourceName) {
    AssetManager assetManager = context.getAssets();
    InputStream is = null;
    FileOutputStream fos = null;
    try {
        is = assetManager.open(sourceName);
        File extractFile = context.getFileStreamPath(sourceName);
        fos = new FileOutputStream(extractFile);
        byte[] buffer = new byte[1024];
        int count;
        while ((count = is.read(buffer)) > 0) {
            fos.write(buffer, 0, count);
        }
        fos.flush();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        closeSilently(is);
        closeSilently(fos);
    }
}
Also used : AssetManager(android.content.res.AssetManager) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File)

Example 92 with AssetManager

use of android.content.res.AssetManager in project AndroidLife by CaMnter.

the class RocooFix method copyAsset.

/**
 * 复制 Asset 的文件
 *
 * @param context context
 * @param assetName Asset 内的文件名
 * @param dir 指定目录
 * @return 返回输出路径
 * @throws IOException
 */
public static String copyAsset(Context context, String assetName, File dir) throws IOException {
    File outFile = new File(dir, assetName);
    if (!outFile.exists()) {
        AssetManager assetManager = context.getAssets();
        InputStream in = assetManager.open(assetName);
        OutputStream out = new FileOutputStream(outFile);
        copyFile(in, out);
        in.close();
        out.close();
    }
    return outFile.getAbsolutePath();
}
Also used : AssetManager(android.content.res.AssetManager) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) ZipFile(java.util.zip.ZipFile) DexFile(dalvik.system.DexFile) File(java.io.File)

Example 93 with AssetManager

use of android.content.res.AssetManager in project AndroidLife by CaMnter.

the class LoadedPlugin method createResources.

@WorkerThread
private static Resources createResources(Context context, File apk) {
    if (Constants.COMBINE_RESOURCES) {
        Resources resources = ResourcesManager.createResources(context, apk.getAbsolutePath());
        ResourcesManager.hookResources(context, resources);
        return resources;
    } else {
        Resources hostResources = context.getResources();
        AssetManager assetManager = createAssetManager(context, apk);
        return new Resources(assetManager, hostResources.getDisplayMetrics(), hostResources.getConfiguration());
    }
}
Also used : AssetManager(android.content.res.AssetManager) Resources(android.content.res.Resources) WorkerThread(android.support.annotation.WorkerThread)

Example 94 with AssetManager

use of android.content.res.AssetManager in project AndroidLife by CaMnter.

the class LoadedPlugin method createAssetManager.

private static AssetManager createAssetManager(Context context, File apk) {
    try {
        AssetManager am = AssetManager.class.newInstance();
        ReflectUtil.invoke(AssetManager.class, am, "addAssetPath", apk.getAbsolutePath());
        return am;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
Also used : AssetManager(android.content.res.AssetManager)

Example 95 with AssetManager

use of android.content.res.AssetManager in project AndroidLife by CaMnter.

the class ResourcesManager method createResources.

/**
 * 反射创建 一个 复合资源
 *
 * --------------------------------------------------------------------------------------------
 *
 * 通过反射调用
 * AssetManager # addAssetPath(String path)
 *
 * 然后得到的 AssetManager
 * 直接 new Resources
 *
 * --------------------------------------------------------------------------------------------
 *
 * 1. 拿到宿主上的 AssetManager,低与 5.0.0 直接反射创建出来
 * 2. 反射调用 AssetManager # addAssetPath(String path),添加 当前插件 Apk
 * -  这样 AssetManager 生成的 Resources 就有 当前插件 和 宿主 的资源
 * 3. 获取之前加载过的 Apk 内存形式 - LoadedPlugin,然后循环调用 AssetManager # addAssetPath(String path)
 * -  这样 AssetManager 生成的 Resources 就有 当前插件 、之前加载过的插件 和 宿主 的资源
 * 4. 由于 MiUi, Vivo, Nubia 等的资源实现类不一样,在进行 new Resources 的时候,需要适配
 *
 * --------------------------------------------------------------------------------------------
 *
 * @param hostContext hostContext
 * @param apk apk
 * @return Resources
 */
public static synchronized Resources createResources(Context hostContext, String apk) {
    Resources hostResources = hostContext.getResources();
    Resources newResources = null;
    AssetManager assetManager;
    try {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            assetManager = AssetManager.class.newInstance();
            ReflectUtil.invoke(AssetManager.class, assetManager, "addAssetPath", hostContext.getApplicationInfo().sourceDir);
        } else {
            assetManager = hostResources.getAssets();
        }
        ReflectUtil.invoke(AssetManager.class, assetManager, "addAssetPath", apk);
        List<LoadedPlugin> pluginList = PluginManager.getInstance(hostContext).getAllLoadedPlugins();
        for (LoadedPlugin plugin : pluginList) {
            ReflectUtil.invoke(AssetManager.class, assetManager, "addAssetPath", plugin.getLocation());
        }
        if (isMiUi(hostResources)) {
            newResources = MiUiResourcesCompat.createResources(hostResources, assetManager);
        } else if (isVivo(hostResources)) {
            newResources = VivoResourcesCompat.createResources(hostContext, hostResources, assetManager);
        } else if (isNubia(hostResources)) {
            newResources = NubiaResourcesCompat.createResources(hostResources, assetManager);
        } else if (isNotRawResources(hostResources)) {
            newResources = AdaptationResourcesCompat.createResources(hostResources, assetManager);
        } else {
            // is raw android resources
            newResources = new Resources(assetManager, hostResources.getDisplayMetrics(), hostResources.getConfiguration());
        }
        // lastly, sync all LoadedPlugin to newResources
        for (LoadedPlugin plugin : pluginList) {
            plugin.updateResources(newResources);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return newResources;
}
Also used : AssetManager(android.content.res.AssetManager) Resources(android.content.res.Resources)

Aggregations

AssetManager (android.content.res.AssetManager)346 IOException (java.io.IOException)141 InputStream (java.io.InputStream)121 Resources (android.content.res.Resources)75 File (java.io.File)54 FileOutputStream (java.io.FileOutputStream)34 XmlResourceParser (android.content.res.XmlResourceParser)32 DisplayMetrics (android.util.DisplayMetrics)31 Bitmap (android.graphics.Bitmap)23 Configuration (android.content.res.Configuration)21 BufferedReader (java.io.BufferedReader)21 InputStreamReader (java.io.InputStreamReader)20 ArrayList (java.util.ArrayList)20 FileInputStream (java.io.FileInputStream)18 OutputStream (java.io.OutputStream)17 Context (android.content.Context)16 Intent (android.content.Intent)16 JsonParser (com.google.gson.JsonParser)16 ByteArrayInputStream (java.io.ByteArrayInputStream)16 Method (java.lang.reflect.Method)16