Search in sources :

Example 31 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 32 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 33 with AssetManager

use of android.content.res.AssetManager in project atlas by alibaba.

the class DelegateResources method updateResources.

private static synchronized void updateResources(Resources res, String assetPath, int assertType) throws Exception {
    if (sAssetManagerProcessor == null) {
        sAssetManagerProcessor = new AssetManagerProcessor();
    }
    AssetManager updatedAssetManager = sAssetManagerProcessor.updateAssetManager(res.getAssets(), assetPath, assertType);
    if (sResourcesProcessor == null) {
        sResourcesProcessor = getResourceProcessor();
    }
    sResourcesProcessor.updateResources(updatedAssetManager);
    if (sResourcesFetcher == null) {
        sResourcesFetcher = new ResourceIdFetcher();
    }
    sResourcesFetcher.addAssetForGetIdentifier(assetPath);
}
Also used : AssetManager(android.content.res.AssetManager)

Example 34 with AssetManager

use of android.content.res.AssetManager in project atlas by alibaba.

the class ResourceIdFetcher method addAssetForGetIdentifier.

public void addAssetForGetIdentifier(String newPath) throws InstantiationException, IllegalAccessException, InvocationTargetException {
    try {
        // Make asset for getIdentifier on android 5.0
        if ((AtlasHacks.AssetManager_addAssetPath != null) && (AtlasHacks.AssetManager_ensureStringBlocks != null) && Build.VERSION.SDK_INT > 20) {
            AssetManager asset = AssetManager.class.newInstance();
            AtlasHacks.AssetManager_addAssetPath.invoke(asset, newPath);
            AtlasHacks.AssetManager_ensureStringBlocks.invoke(asset);
            assetList.add(asset);
        }
    } catch (Throwable e) {
    }
}
Also used : AssetManager(android.content.res.AssetManager)

Example 35 with AssetManager

use of android.content.res.AssetManager in project android_frameworks_base by ParanoidAndroid.

the class AssetRedirectionManagerService method generatePackageRedirectionMap.

private PackageRedirectionMap generatePackageRedirectionMap(RedirectionKey key) {
    AssetManager assets = new AssetManager();
    boolean frameworkAssets = key.targetPackageName.equals("android");
    if (!frameworkAssets) {
        PackageInfo pi = getPackageInfo(mContext, key.targetPackageName);
        if (pi == null || pi.applicationInfo == null || assets.addAssetPath(pi.applicationInfo.publicSourceDir) == 0) {
            Log.w(TAG, "Unable to attach target package assets for " + key.targetPackageName);
            return null;
        }
    }
    PackageInfo pi = getPackageInfo(mContext, key.themePackageName);
    if (pi == null || pi.applicationInfo == null || pi.themeInfos == null || assets.addAssetPath(pi.applicationInfo.publicSourceDir) == 0) {
        Log.w(TAG, "Unable to attach theme package assets from " + key.themePackageName);
        return null;
    }
    PackageRedirectionMap resMap = new PackageRedirectionMap();
    /*
         * Apply a special redirection hack for the highest level <style>
         * replacing @android:style/Theme.
         */
    if (frameworkAssets) {
        int themeResourceId = findThemeResourceId(pi.themeInfos, key.themeId);
        assets.generateStyleRedirections(resMap.getNativePointer(), android.R.style.Theme, themeResourceId);
    }
    Resources res = new Resources(assets, null, null);
    generateExplicitRedirections(resMap, res, key.themePackageName, key.targetPackageName);
    return resMap;
}
Also used : AssetManager(android.content.res.AssetManager) PackageInfo(android.content.pm.PackageInfo) PackageRedirectionMap(android.content.res.PackageRedirectionMap) Resources(android.content.res.Resources)

Aggregations

AssetManager (android.content.res.AssetManager)201 IOException (java.io.IOException)75 InputStream (java.io.InputStream)60 Resources (android.content.res.Resources)48 XmlResourceParser (android.content.res.XmlResourceParser)28 File (java.io.File)28 DisplayMetrics (android.util.DisplayMetrics)25 Configuration (android.content.res.Configuration)16 Bitmap (android.graphics.Bitmap)13 BufferedReader (java.io.BufferedReader)13 FileOutputStream (java.io.FileOutputStream)13 InputStreamReader (java.io.InputStreamReader)12 FileInputStream (java.io.FileInputStream)10 FileNotFoundException (java.io.FileNotFoundException)10 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)10 Intent (android.content.Intent)8 AndroidRuntimeException (android.util.AndroidRuntimeException)8 OutputStream (java.io.OutputStream)8 Method (java.lang.reflect.Method)8 AttributeSet (android.util.AttributeSet)7