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);
}
}
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();
}
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());
}
}
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;
}
}
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;
}
Aggregations