Search in sources :

Example 16 with AssetManager

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;
}
Also used : AssetManager(android.content.res.AssetManager) InputStream(java.io.InputStream)

Example 17 with AssetManager

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");
//        }
}
Also used : AssetManager(android.content.res.AssetManager) HashMap(java.util.HashMap) ArrayMap(android.util.ArrayMap) Method(java.lang.reflect.Method) Field(java.lang.reflect.Field) WeakReference(java.lang.ref.WeakReference) Collection(java.util.Collection) Resources(android.content.res.Resources)

Example 18 with AssetManager

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);
}
Also used : AssetManager(android.content.res.AssetManager) ParseConfigurationFileException(org.litepal.exceptions.ParseConfigurationFileException)

Example 19 with AssetManager

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);
}
Also used : AssetManager(android.content.res.AssetManager) ParseConfigurationFileException(org.litepal.exceptions.ParseConfigurationFileException)

Example 20 with AssetManager

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);
}
Also used : AssetManager(android.content.res.AssetManager) InputStream(java.io.InputStream)

Aggregations

AssetManager (android.content.res.AssetManager)196 IOException (java.io.IOException)70 InputStream (java.io.InputStream)58 Resources (android.content.res.Resources)47 XmlResourceParser (android.content.res.XmlResourceParser)28 File (java.io.File)28 DisplayMetrics (android.util.DisplayMetrics)24 Configuration (android.content.res.Configuration)15 BufferedReader (java.io.BufferedReader)13 FileOutputStream (java.io.FileOutputStream)13 Bitmap (android.graphics.Bitmap)12 InputStreamReader (java.io.InputStreamReader)12 FileInputStream (java.io.FileInputStream)10 FileNotFoundException (java.io.FileNotFoundException)10 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)10 AndroidRuntimeException (android.util.AndroidRuntimeException)8 OutputStream (java.io.OutputStream)8 Method (java.lang.reflect.Method)8 Intent (android.content.Intent)7 AttributeSet (android.util.AttributeSet)7