Search in sources :

Example 6 with AssetManager

use of android.content.res.AssetManager in project cw-omnibus by commonsguy.

the class PresoRoster method load.

void load(Context ctxt) {
    Gson gson = new Gson();
    AssetManager assets = ctxt.getAssets();
    for (String presoDir : PRESO_ASSET_DIRS) {
        PresoContents c = loadPreso(gson, assets, presoDir);
        if (c != null) {
            c.id = presos.size();
            presos.add(c);
        }
    }
}
Also used : AssetManager(android.content.res.AssetManager) Gson(com.google.gson.Gson)

Example 7 with AssetManager

use of android.content.res.AssetManager in project Libraries-for-Android-Developers by eoecn.

the class ActionBarSherlockCompat method loadUiOptionsFromManifest.

private static int loadUiOptionsFromManifest(Activity activity) {
    int uiOptions = 0;
    try {
        final String thisPackage = activity.getClass().getName();
        if (ActionBarSherlock.DEBUG)
            Log.i(TAG, "Parsing AndroidManifest.xml for " + thisPackage);
        final String packageName = activity.getApplicationInfo().packageName;
        final AssetManager am = activity.createPackageContext(packageName, 0).getAssets();
        final XmlResourceParser xml = am.openXmlResourceParser("AndroidManifest.xml");
        int eventType = xml.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_TAG) {
                String name = xml.getName();
                if ("application".equals(name)) {
                    //Check if the <application> has the attribute
                    if (ActionBarSherlock.DEBUG)
                        Log.d(TAG, "Got <application>");
                    for (int i = xml.getAttributeCount() - 1; i >= 0; i--) {
                        if (ActionBarSherlock.DEBUG)
                            Log.d(TAG, xml.getAttributeName(i) + ": " + xml.getAttributeValue(i));
                        if ("uiOptions".equals(xml.getAttributeName(i))) {
                            uiOptions = xml.getAttributeIntValue(i, 0);
                            //out of for loop
                            break;
                        }
                    }
                } else if ("activity".equals(name)) {
                    //Check if the <activity> is us and has the attribute
                    if (ActionBarSherlock.DEBUG)
                        Log.d(TAG, "Got <activity>");
                    Integer activityUiOptions = null;
                    String activityPackage = null;
                    boolean isOurActivity = false;
                    for (int i = xml.getAttributeCount() - 1; i >= 0; i--) {
                        if (ActionBarSherlock.DEBUG)
                            Log.d(TAG, xml.getAttributeName(i) + ": " + xml.getAttributeValue(i));
                        //We need both uiOptions and name attributes
                        String attrName = xml.getAttributeName(i);
                        if ("uiOptions".equals(attrName)) {
                            activityUiOptions = xml.getAttributeIntValue(i, 0);
                        } else if ("name".equals(attrName)) {
                            activityPackage = cleanActivityName(packageName, xml.getAttributeValue(i));
                            if (!thisPackage.equals(activityPackage)) {
                                //out of for loop
                                break;
                            }
                            isOurActivity = true;
                        }
                        //Make sure we have both attributes before processing
                        if ((activityUiOptions != null) && (activityPackage != null)) {
                            //Our activity, uiOptions specified, override with our value
                            uiOptions = activityUiOptions.intValue();
                        }
                    }
                    if (isOurActivity) {
                        //do any more processing of the manifest
                        break;
                    }
                }
            }
            eventType = xml.nextToken();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (ActionBarSherlock.DEBUG)
        Log.i(TAG, "Returning " + Integer.toHexString(uiOptions));
    return uiOptions;
}
Also used : AssetManager(android.content.res.AssetManager) XmlResourceParser(android.content.res.XmlResourceParser) AndroidRuntimeException(android.util.AndroidRuntimeException)

Example 8 with AssetManager

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

the class Pm method getResources.

private Resources getResources(PackageItemInfo pii) {
    Resources res = mResourceCache.get(pii.packageName);
    if (res != null)
        return res;
    try {
        ApplicationInfo ai = mPm.getApplicationInfo(pii.packageName, 0, 0);
        AssetManager am = new AssetManager();
        am.addAssetPath(ai.publicSourceDir);
        res = new Resources(am, null, null);
        mResourceCache.put(pii.packageName, res);
        return res;
    } catch (RemoteException e) {
        System.err.println(e.toString());
        System.err.println(PM_NOT_RUNNING_ERR);
        return null;
    }
}
Also used : AssetManager(android.content.res.AssetManager) ApplicationInfo(android.content.pm.ApplicationInfo) Resources(android.content.res.Resources) RemoteException(android.os.RemoteException)

Example 9 with AssetManager

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

the class ActivityThread method applyConfigurationToResourcesLocked.

final int applyConfigurationToResourcesLocked(Configuration config, CompatibilityInfo compat) {
    if (mResConfiguration == null) {
        mResConfiguration = new Configuration();
    }
    if (!mResConfiguration.isOtherSeqNewer(config) && compat == null) {
        if (DEBUG_CONFIGURATION)
            Slog.v(TAG, "Skipping new config: curSeq=" + mResConfiguration.seq + ", newSeq=" + config.seq);
        return 0;
    }
    int changes = mResConfiguration.updateFrom(config);
    flushDisplayMetricsLocked();
    DisplayMetrics defaultDisplayMetrics = getDisplayMetricsLocked(Display.DEFAULT_DISPLAY, null);
    if (compat != null && (mResCompatibilityInfo == null || !mResCompatibilityInfo.equals(compat))) {
        mResCompatibilityInfo = compat;
        changes |= ActivityInfo.CONFIG_SCREEN_LAYOUT | ActivityInfo.CONFIG_SCREEN_SIZE | ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
    }
    // set it for java, this also affects newly created Resources
    if (config.locale != null) {
        Locale.setDefault(config.locale);
    }
    Resources.updateSystemConfiguration(config, defaultDisplayMetrics, compat);
    ApplicationPackageManager.configurationChanged();
    //Slog.i(TAG, "Configuration changed in " + currentPackageName());
    Configuration tmpConfig = null;
    Iterator<Map.Entry<ResourcesKey, WeakReference<Resources>>> it = mActiveResources.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<ResourcesKey, WeakReference<Resources>> entry = it.next();
        Resources r = entry.getValue().get();
        if (r != null) {
            if (DEBUG_CONFIGURATION)
                Slog.v(TAG, "Changing resources " + r + " config to: " + config);
            int displayId = entry.getKey().mDisplayId;
            boolean isDefaultDisplay = (displayId == Display.DEFAULT_DISPLAY);
            DisplayMetrics dm = defaultDisplayMetrics;
            Configuration overrideConfig = entry.getKey().mOverrideConfiguration;
            boolean themeChanged = (changes & ActivityInfo.CONFIG_THEME_RESOURCE) != 0;
            if (themeChanged) {
                AssetManager am = r.getAssets();
                if (am.hasThemeSupport()) {
                    detachThemeAssets(am);
                    if (!TextUtils.isEmpty(config.customTheme.getThemePackageName())) {
                        attachThemeAssets(am, config.customTheme);
                    }
                }
            }
            if (!isDefaultDisplay || overrideConfig != null) {
                if (tmpConfig == null) {
                    tmpConfig = new Configuration();
                }
                tmpConfig.setTo(config);
                if (!isDefaultDisplay) {
                    dm = getDisplayMetricsLocked(displayId, null);
                    applyNonDefaultDisplayMetricsToConfigurationLocked(dm, tmpConfig);
                }
                if (overrideConfig != null) {
                    tmpConfig.updateFrom(overrideConfig);
                }
                r.updateConfiguration(tmpConfig, dm, compat);
            } else {
                r.updateConfiguration(config, dm, compat);
            }
            if (themeChanged) {
                r.updateStringCache();
            }
        //Slog.i(TAG, "Updated app resources " + v.getKey()
        //        + " " + r + ": " + r.getConfiguration());
        } else {
            //Slog.i(TAG, "Removing old resources " + v.getKey());
            it.remove();
        }
    }
    return changes;
}
Also used : AssetManager(android.content.res.AssetManager) Configuration(android.content.res.Configuration) WeakReference(java.lang.ref.WeakReference) Resources(android.content.res.Resources) DisplayMetrics(android.util.DisplayMetrics) PackageRedirectionMap(android.content.res.PackageRedirectionMap) Map(java.util.Map) HashMap(java.util.HashMap)

Example 10 with AssetManager

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

the class ActivityThread method getTopLevelResources.

/**
     * Creates the top level Resources for applications with the given compatibility info.
     *
     * @param resDir the resource directory.
     * @param compInfo the compability info. It will use the default compatibility info when it's
     * null.
     */
Resources getTopLevelResources(String resDir, int displayId, Configuration overrideConfiguration, CompatibilityInfo compInfo) {
    ResourcesKey key = new ResourcesKey(resDir, displayId, overrideConfiguration, compInfo.applicationScale, compInfo.isThemeable);
    Resources r;
    synchronized (mPackages) {
        // Resources is app scale dependent.
        if (false) {
            Slog.w(TAG, "getTopLevelResources: " + resDir + " / " + compInfo.applicationScale);
        }
        WeakReference<Resources> wr = mActiveResources.get(key);
        r = wr != null ? wr.get() : null;
        //if (r != null) Slog.i(TAG, "isUpToDate " + resDir + ": " + r.getAssets().isUpToDate());
        if (r != null && r.getAssets().isUpToDate()) {
            if (false) {
                Slog.w(TAG, "Returning cached resources " + r + " " + resDir + ": appScale=" + r.getCompatibilityInfo().applicationScale);
            }
            return r;
        }
    }
    //if (r != null) {
    //    Slog.w(TAG, "Throwing away out-of-date resources!!!! "
    //            + r + " " + resDir);
    //}
    AssetManager assets = new AssetManager();
    assets.overrideHook(resDir, ExtendedPropertiesUtils.OverrideMode.FullNameExclude);
    assets.setThemeSupport(compInfo.isThemeable);
    if (assets.addAssetPath(resDir) == 0) {
        return null;
    }
    //Slog.i(TAG, "Resource: key=" + key + ", display metrics=" + metrics);
    DisplayMetrics dm = getDisplayMetricsLocked(displayId, null);
    dm.overrideHook(assets, ExtendedPropertiesUtils.OverrideMode.ExtendedProperties);
    Configuration config;
    boolean isDefaultDisplay = (displayId == Display.DEFAULT_DISPLAY);
    if (!isDefaultDisplay || key.mOverrideConfiguration != null) {
        config = new Configuration(getConfiguration());
        if (!isDefaultDisplay) {
            applyNonDefaultDisplayMetricsToConfigurationLocked(dm, config);
        }
        if (key.mOverrideConfiguration != null) {
            config.updateFrom(key.mOverrideConfiguration);
        }
    } else {
        config = getConfiguration();
    }
    /* Attach theme information to the resulting AssetManager when appropriate. */
    if (compInfo.isThemeable && config != null) {
        if (config.customTheme == null) {
            config.customTheme = CustomTheme.getBootTheme();
        }
        if (!TextUtils.isEmpty(config.customTheme.getThemePackageName())) {
            attachThemeAssets(assets, config.customTheme);
        }
    }
    r = new Resources(assets, dm, config, compInfo);
    if (false) {
        Slog.i(TAG, "Created app resources " + resDir + " " + r + ": " + r.getConfiguration() + " appScale=" + r.getCompatibilityInfo().applicationScale);
    }
    synchronized (mPackages) {
        WeakReference<Resources> wr = mActiveResources.get(key);
        Resources existing = wr != null ? wr.get() : null;
        if (existing != null && existing.getAssets().isUpToDate()) {
            // Someone else already created the resources while we were
            // unlocked; go ahead and use theirs.
            r.getAssets().close();
            return existing;
        }
        // XXX need to remove entries when weak references go away
        mActiveResources.put(key, new WeakReference<Resources>(r));
        return r;
    }
}
Also used : AssetManager(android.content.res.AssetManager) Configuration(android.content.res.Configuration) Resources(android.content.res.Resources) DisplayMetrics(android.util.DisplayMetrics)

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