use of android.content.res.AssetManager in project GT by Tencent.
the class LibManager method loadLibrary.
public boolean loadLibrary(String libName, boolean load) {
String libFullName = "lib" + libName + ".so";
// String fromPath = "armeabi/" + libFullName;
// 如果是arm64的CPU,文件在assets目录下的arm64-v8a目录中
String fromPath = libFullName;
if (DeviceUtils.getABI().contains("arm64-v8a")) {
fromPath = "/arm64-v8a/" + fromPath;
}
String toPath = libPath + libFullName;
if (!FileUtil.isFileExists(Env.CMD_ROOT_PATH)) {
FileUtil.createDir(Env.CMD_ROOT_PATH);
}
if (!FileUtil.isFileExists(libPath)) {
FileUtil.createDir(libPath);
}
if (!FileUtil.isFileExists(toPath)) {
InputStream fs = null;
try {
AssetManager am = mApplicationContext.getAssets();
fs = am.open(fromPath);
FileUtil.copyInputToFile(fs, toPath);
} catch (Exception e) {
e.printStackTrace();
} finally {
FileUtil.closeInputStream(fs);
}
}
if (load) {
try {
// System.loadLibrary(libName); // 因为默认不在原始的lib目录下,所以不用该方法
System.load(toPath);
} catch (UnsatisfiedLinkError e) {
return false;
}
}
return true;
}
use of android.content.res.AssetManager in project tinker by Tencent.
the class TinkerResourcePatcher method isResourceCanPatch.
// private static Field publicSourceDirField = null;
// private static boolean isMiuiSystem = false;
public static void isResourceCanPatch(Context context) throws Throwable {
// - Replace mResDir to point to the external resource file instead of the .apk. This is
// used as the asset path for new Resources objects.
// - Set Application#mLoadedApk to the found LoadedApk instance
// Find the ActivityThread instance for the current thread
Class<?> activityThread = Class.forName("android.app.ActivityThread");
currentActivityThread = ShareReflectUtil.getActivityThread(context, activityThread);
// API version 8 has PackageInfo, 10 has LoadedApk. 9, I don't know.
Class<?> loadedApkClass;
try {
loadedApkClass = Class.forName("android.app.LoadedApk");
} catch (ClassNotFoundException e) {
loadedApkClass = Class.forName("android.app.ActivityThread$PackageInfo");
}
resDir = loadedApkClass.getDeclaredField("mResDir");
resDir.setAccessible(true);
packagesFiled = activityThread.getDeclaredField("mPackages");
packagesFiled.setAccessible(true);
resourcePackagesFiled = activityThread.getDeclaredField("mResourcePackages");
resourcePackagesFiled.setAccessible(true);
// Create a new AssetManager instance and point it to the resources
AssetManager assets = context.getAssets();
// Baidu os
if (assets.getClass().getName().equals("android.content.res.BaiduAssetManager")) {
Class baiduAssetManager = Class.forName("android.content.res.BaiduAssetManager");
newAssetManager = (AssetManager) baiduAssetManager.getConstructor().newInstance();
} else {
newAssetManager = AssetManager.class.getConstructor().newInstance();
}
addAssetPathMethod = AssetManager.class.getDeclaredMethod("addAssetPath", String.class);
addAssetPathMethod.setAccessible(true);
// Kitkat needs this method call, Lollipop doesn't. However, it doesn't seem to cause any harm
// in L, so we do it unconditionally.
ensureStringBlocksMethod = AssetManager.class.getDeclaredMethod("ensureStringBlocks");
ensureStringBlocksMethod.setAccessible(true);
// Iterate over all known Resources objects
if (SDK_INT >= KITKAT) {
//pre-N
// Find the singleton instance of ResourcesManager
Class<?> resourcesManagerClass = Class.forName("android.app.ResourcesManager");
Method mGetInstance = resourcesManagerClass.getDeclaredMethod("getInstance");
mGetInstance.setAccessible(true);
Object resourcesManager = mGetInstance.invoke(null);
try {
Field fMActiveResources = resourcesManagerClass.getDeclaredField("mActiveResources");
fMActiveResources.setAccessible(true);
ArrayMap<?, WeakReference<Resources>> activeResources19 = (ArrayMap<?, WeakReference<Resources>>) fMActiveResources.get(resourcesManager);
references = activeResources19.values();
} catch (NoSuchFieldException ignore) {
// N moved the resources to mResourceReferences
Field mResourceReferences = resourcesManagerClass.getDeclaredField("mResourceReferences");
mResourceReferences.setAccessible(true);
// resourceImpls = (ArrayMap<?, WeakReference<?>>) mResourceReferences.get("mResourceImpls");
references = (Collection<WeakReference<Resources>>) mResourceReferences.get(resourcesManager);
}
} else {
Field fMActiveResources = activityThread.getDeclaredField("mActiveResources");
fMActiveResources.setAccessible(true);
HashMap<?, WeakReference<Resources>> activeResources7 = (HashMap<?, WeakReference<Resources>>) fMActiveResources.get(currentActivityThread);
references = activeResources7.values();
}
// check resource
if (references == null) {
throw new IllegalStateException("resource references is null");
}
try {
assetsFiled = Resources.class.getDeclaredField("mAssets");
assetsFiled.setAccessible(true);
} catch (Throwable ignore) {
// N moved the mAssets inside an mResourcesImpl field
resourcesImplFiled = Resources.class.getDeclaredField("mResourcesImpl");
resourcesImplFiled.setAccessible(true);
}
// final Resources resources = context.getResources();
// isMiuiSystem = resources != null && MIUI_RESOURCE_CLASSNAME.equals(resources.getClass().getName());
// try {
// publicSourceDirField = ShareReflectUtil.findField(ApplicationInfo.class, "publicSourceDir");
// } catch (NoSuchFieldException e) {
// throw new IllegalStateException("cannot find 'mInstrumentation' field");
// }
}
use of android.content.res.AssetManager in project LitePal by LitePalFramework.
the class LitePalParser method getConfigInputStream.
/**
* Iterates all files in the root of assets folder. If find litepal.xml,
* open this file and return the input stream. Or throw
* ParseConfigurationFileException.
*
* @return The input stream of litepal.xml.
* @throws java.io.IOException
*/
private InputStream getConfigInputStream() throws IOException {
AssetManager assetManager = LitePalApplication.getContext().getAssets();
String[] fileNames = assetManager.list("");
if (fileNames != null && fileNames.length > 0) {
for (String fileName : fileNames) {
if (Const.Config.CONFIGURATION_FILE_NAME.equalsIgnoreCase(fileName)) {
return assetManager.open(fileName, AssetManager.ACCESS_BUFFER);
}
}
}
throw new ParseConfigurationFileException(ParseConfigurationFileException.CAN_NOT_FIND_LITEPAL_FILE);
}
use of android.content.res.AssetManager in project LitePal by LitePalFramework.
the class ModelListActivity method getInputStream.
private InputStream getInputStream() throws IOException {
AssetManager assetManager = LitePalApplication.getContext().getAssets();
String[] fileNames = assetManager.list("");
if (fileNames != null && fileNames.length > 0) {
for (String fileName : fileNames) {
if (Const.Config.CONFIGURATION_FILE_NAME.equalsIgnoreCase(fileName)) {
return assetManager.open(fileName, AssetManager.ACCESS_BUFFER);
}
}
}
throw new ParseConfigurationFileException(ParseConfigurationFileException.CAN_NOT_FIND_LITEPAL_FILE);
}
use of android.content.res.AssetManager in project MarsDaemon by Marswin.
the class DaemonStrategyXiaomi method copyAssets.
private void copyAssets(Context context, String assetsFilename, File file, String mode) throws IOException, InterruptedException {
AssetManager manager = context.getAssets();
final InputStream is = manager.open(assetsFilename);
copyFile(file, is, mode);
}
Aggregations