Search in sources :

Example 1 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 2 with AssetManager

use of android.content.res.AssetManager in project android-betterpickers by code-troopers.

the class TimeZoneData method loadTzsInZoneTab.

private HashSet<String> loadTzsInZoneTab(Context context) {
    HashSet<String> processedTimeZones = new HashSet<String>();
    AssetManager am = context.getAssets();
    InputStream is = null;
    /*
         * The 'backward' file contain mappings between new and old time zone
         * ids. We will explicitly ignore the old ones.
         */
    try {
        is = am.open("backward");
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        String line;
        while ((line = reader.readLine()) != null) {
            // Skip comment lines
            if (!line.startsWith("#") && line.length() > 0) {
                // 0: "Link"
                // 1: New tz id
                // Last: Old tz id
                String[] fields = line.split("\t+");
                String newTzId = fields[1];
                String oldTzId = fields[fields.length - 1];
                final TimeZone tz = TimeZone.getTimeZone(newTzId);
                if (tz == null) {
                    Log.e(TAG, "Timezone not found: " + newTzId);
                    continue;
                }
                processedTimeZones.add(oldTzId);
                if (DEBUG) {
                    Log.e(TAG, "# Dropping identical time zone from backward: " + oldTzId);
                }
                // Remember the cooler/newer time zone id
                if (mDefaultTimeZoneId != null && mDefaultTimeZoneId.equals(oldTzId)) {
                    mAlternateDefaultTimeZoneId = newTzId;
                }
            }
        }
    } catch (IOException ex) {
        Log.e(TAG, "Failed to read 'backward' file.");
    } finally {
        try {
            if (is != null) {
                is.close();
            }
        } catch (IOException ignored) {
        }
    }
    /*
         * zone.tab contains a list of time zones and country code. They are
         * "sorted first by country, then an order within the country that (1)
         * makes some geographical sense, and (2) puts the most populous zones
         * first, where that does not contradict (1)."
         */
    try {
        String lang = Locale.getDefault().getLanguage();
        is = am.open("zone.tab");
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        String line;
        while ((line = reader.readLine()) != null) {
            if (!line.startsWith("#")) {
                // Skip comment lines
                // 0: country code
                // 1: coordinates
                // 2: time zone id
                // 3: comments
                final String[] fields = line.split("\t");
                final String timeZoneId = fields[2];
                final String countryCode = fields[0];
                final TimeZone tz = TimeZone.getTimeZone(timeZoneId);
                if (tz == null) {
                    Log.e(TAG, "Timezone not found: " + timeZoneId);
                    continue;
                }
                /*
                     * Dropping non-GMT tzs without a country code. They are not
                     * really needed and they are dups but missing proper
                     * country codes. e.g. WET CET MST7MDT PST8PDT Asia/Khandyga
                     * Asia/Ust-Nera EST
                     */
                if (countryCode == null && !timeZoneId.startsWith("Etc/GMT")) {
                    processedTimeZones.add(timeZoneId);
                    continue;
                }
                // Remember the mapping between the country code and display
                // name
                String country = mCountryCodeToNameMap.get(countryCode);
                if (country == null) {
                    country = getCountryNames(lang, countryCode);
                    mCountryCodeToNameMap.put(countryCode, country);
                }
                // Find the country of the default tz
                if (mDefaultTimeZoneId != null && mDefaultTimeZoneCountry == null && timeZoneId.equals(mAlternateDefaultTimeZoneId)) {
                    mDefaultTimeZoneCountry = country;
                    TimeZone defaultTz = TimeZone.getTimeZone(mDefaultTimeZoneId);
                    if (defaultTz != null) {
                        mDefaultTimeZoneInfo = new TimeZoneInfo(defaultTz, country);
                        int tzToOverride = getIdenticalTimeZoneInTheCountry(mDefaultTimeZoneInfo);
                        if (tzToOverride == -1) {
                            if (DEBUG) {
                                Log.e(TAG, "Adding default time zone: " + mDefaultTimeZoneInfo.toString());
                            }
                            mTimeZones.add(mDefaultTimeZoneInfo);
                        } else {
                            mTimeZones.add(tzToOverride, mDefaultTimeZoneInfo);
                            if (DEBUG) {
                                TimeZoneInfo tzInfoToOverride = mTimeZones.get(tzToOverride);
                                String tzIdToOverride = tzInfoToOverride.mTzId;
                                Log.e(TAG, "Replaced by default tz: " + tzInfoToOverride.toString());
                                Log.e(TAG, "Adding default time zone: " + mDefaultTimeZoneInfo.toString());
                            }
                        }
                    }
                }
                // Add to the list of time zones if the time zone is unique
                // in the given country.
                TimeZoneInfo timeZoneInfo = new TimeZoneInfo(tz, country);
                int identicalTzIdx = getIdenticalTimeZoneInTheCountry(timeZoneInfo);
                if (identicalTzIdx == -1) {
                    if (DEBUG) {
                        Log.e(TAG, "# Adding time zone: " + timeZoneId + " ## " + tz.getDisplayName());
                    }
                    mTimeZones.add(timeZoneInfo);
                } else {
                    if (DEBUG) {
                        Log.e(TAG, "# Dropping identical time zone: " + timeZoneId + " ## " + tz.getDisplayName());
                    }
                }
                processedTimeZones.add(timeZoneId);
            }
        }
    } catch (IOException ex) {
        Log.e(TAG, "Failed to read 'zone.tab'.");
    } finally {
        try {
            if (is != null) {
                is.close();
            }
        } catch (IOException ignored) {
        }
    }
    return processedTimeZones;
}
Also used : TimeZone(java.util.TimeZone) AssetManager(android.content.res.AssetManager) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) HashSet(java.util.HashSet)

Example 3 with AssetManager

use of android.content.res.AssetManager in project cucumber-jvm by cucumber.

the class AndroidResourceLoader method resources.

@Override
public Iterable<Resource> resources(final String path, final String suffix) {
    try {
        final List<Resource> resources = new ArrayList<Resource>();
        final AssetManager assetManager = context.getAssets();
        addResourceRecursive(resources, assetManager, path, suffix);
        return resources;
    } catch (final IOException e) {
        throw new CucumberException("Error loading resources from " + path + " with suffix " + suffix, e);
    }
}
Also used : AssetManager(android.content.res.AssetManager) ArrayList(java.util.ArrayList) Resource(cucumber.runtime.io.Resource) IOException(java.io.IOException) CucumberException(cucumber.runtime.CucumberException)

Example 4 with AssetManager

use of android.content.res.AssetManager in project LollipopShowcase by mikepenz.

the class AppInfo method getEnglishRessources.

public Resources getEnglishRessources(Resources standardResources) {
    AssetManager assets = standardResources.getAssets();
    DisplayMetrics metrics = standardResources.getDisplayMetrics();
    Configuration config = new Configuration(standardResources.getConfiguration());
    config.locale = Locale.US;
    return new Resources(assets, metrics, config);
}
Also used : AssetManager(android.content.res.AssetManager) Configuration(android.content.res.Configuration) Resources(android.content.res.Resources) DisplayMetrics(android.util.DisplayMetrics)

Example 5 with AssetManager

use of android.content.res.AssetManager in project AndroidTraining by mixi-inc.

the class ActionBarSherlockCompat method loadUiOptionsFromManifest.

private static int loadUiOptionsFromManifest(Activity activity) {
    int uiOptions = 0;
    try {
        final String thisPackage = activity.getClass().getName();
        if (BuildConfig.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 (BuildConfig.DEBUG)
                        Log.d(TAG, "Got <application>");
                    for (int i = xml.getAttributeCount() - 1; i >= 0; i--) {
                        if (BuildConfig.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 (BuildConfig.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 (BuildConfig.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 (BuildConfig.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)

Aggregations

AssetManager (android.content.res.AssetManager)346 IOException (java.io.IOException)141 InputStream (java.io.InputStream)121 Resources (android.content.res.Resources)75 File (java.io.File)54 FileOutputStream (java.io.FileOutputStream)34 XmlResourceParser (android.content.res.XmlResourceParser)32 DisplayMetrics (android.util.DisplayMetrics)31 Bitmap (android.graphics.Bitmap)23 Configuration (android.content.res.Configuration)21 BufferedReader (java.io.BufferedReader)21 InputStreamReader (java.io.InputStreamReader)20 ArrayList (java.util.ArrayList)20 FileInputStream (java.io.FileInputStream)18 OutputStream (java.io.OutputStream)17 Context (android.content.Context)16 Intent (android.content.Intent)16 JsonParser (com.google.gson.JsonParser)16 ByteArrayInputStream (java.io.ByteArrayInputStream)16 Method (java.lang.reflect.Method)16