Search in sources :

Example 91 with Resources

use of android.content.res.Resources in project XobotOS by xamarin.

the class DateUtils method getRelativeTimeSpanString.

/**
     * Returns a string describing 'time' as a time relative to 'now'.
     * <p>
     * Time spans in the past are formatted like "42 minutes ago". Time spans in
     * the future are formatted like "in 42 minutes".
     * <p>
     * Can use {@link #FORMAT_ABBREV_RELATIVE} flag to use abbreviated relative
     * times, like "42 mins ago".
     * 
     * @param time the time to describe, in milliseconds
     * @param now the current time in milliseconds
     * @param minResolution the minimum timespan to report. For example, a time
     *            3 seconds in the past will be reported as "0 minutes ago" if
     *            this is set to MINUTE_IN_MILLIS. Pass one of 0,
     *            MINUTE_IN_MILLIS, HOUR_IN_MILLIS, DAY_IN_MILLIS,
     *            WEEK_IN_MILLIS
     * @param flags a bit mask of formatting options, such as
     *            {@link #FORMAT_NUMERIC_DATE} or
     *            {@link #FORMAT_ABBREV_RELATIVE}
     */
public static CharSequence getRelativeTimeSpanString(long time, long now, long minResolution, int flags) {
    Resources r = Resources.getSystem();
    boolean abbrevRelative = (flags & (FORMAT_ABBREV_RELATIVE | FORMAT_ABBREV_ALL)) != 0;
    boolean past = (now >= time);
    long duration = Math.abs(now - time);
    int resId;
    long count;
    if (duration < MINUTE_IN_MILLIS && minResolution < MINUTE_IN_MILLIS) {
        count = duration / SECOND_IN_MILLIS;
        if (past) {
            if (abbrevRelative) {
                resId = com.android.internal.R.plurals.abbrev_num_seconds_ago;
            } else {
                resId = com.android.internal.R.plurals.num_seconds_ago;
            }
        } else {
            if (abbrevRelative) {
                resId = com.android.internal.R.plurals.abbrev_in_num_seconds;
            } else {
                resId = com.android.internal.R.plurals.in_num_seconds;
            }
        }
    } else if (duration < HOUR_IN_MILLIS && minResolution < HOUR_IN_MILLIS) {
        count = duration / MINUTE_IN_MILLIS;
        if (past) {
            if (abbrevRelative) {
                resId = com.android.internal.R.plurals.abbrev_num_minutes_ago;
            } else {
                resId = com.android.internal.R.plurals.num_minutes_ago;
            }
        } else {
            if (abbrevRelative) {
                resId = com.android.internal.R.plurals.abbrev_in_num_minutes;
            } else {
                resId = com.android.internal.R.plurals.in_num_minutes;
            }
        }
    } else if (duration < DAY_IN_MILLIS && minResolution < DAY_IN_MILLIS) {
        count = duration / HOUR_IN_MILLIS;
        if (past) {
            if (abbrevRelative) {
                resId = com.android.internal.R.plurals.abbrev_num_hours_ago;
            } else {
                resId = com.android.internal.R.plurals.num_hours_ago;
            }
        } else {
            if (abbrevRelative) {
                resId = com.android.internal.R.plurals.abbrev_in_num_hours;
            } else {
                resId = com.android.internal.R.plurals.in_num_hours;
            }
        }
    } else if (duration < WEEK_IN_MILLIS && minResolution < WEEK_IN_MILLIS) {
        count = getNumberOfDaysPassed(time, now);
        if (past) {
            if (abbrevRelative) {
                resId = com.android.internal.R.plurals.abbrev_num_days_ago;
            } else {
                resId = com.android.internal.R.plurals.num_days_ago;
            }
        } else {
            if (abbrevRelative) {
                resId = com.android.internal.R.plurals.abbrev_in_num_days;
            } else {
                resId = com.android.internal.R.plurals.in_num_days;
            }
        }
    } else {
        // in a null context.
        return formatDateRange(null, time, time, flags);
    }
    String format = r.getQuantityString(resId, (int) count);
    return String.format(format, count);
}
Also used : Resources(android.content.res.Resources)

Example 92 with Resources

use of android.content.res.Resources in project XobotOS by xamarin.

the class DateUtils method getDayOfWeekString.

/**
     * Return a string for the day of the week.
     * @param dayOfWeek One of {@link Calendar#SUNDAY Calendar.SUNDAY},
     *               {@link Calendar#MONDAY Calendar.MONDAY}, etc.
     * @param abbrev One of {@link #LENGTH_LONG}, {@link #LENGTH_SHORT},
     *               {@link #LENGTH_MEDIUM}, or {@link #LENGTH_SHORTEST}.
     *               Note that in most languages, {@link #LENGTH_SHORT}
     *               will return the same as {@link #LENGTH_MEDIUM}.
     *               Undefined lengths will return {@link #LENGTH_MEDIUM}
     *               but may return something different in the future.
     * @throws IndexOutOfBoundsException if the dayOfWeek is out of bounds.
     */
public static String getDayOfWeekString(int dayOfWeek, int abbrev) {
    int[] list;
    switch(abbrev) {
        case LENGTH_LONG:
            list = sDaysLong;
            break;
        case LENGTH_MEDIUM:
            list = sDaysMedium;
            break;
        case LENGTH_SHORT:
            list = sDaysShort;
            break;
        case LENGTH_SHORTER:
            list = sDaysShort;
            break;
        case LENGTH_SHORTEST:
            list = sDaysShortest;
            break;
        default:
            list = sDaysMedium;
            break;
    }
    Resources r = Resources.getSystem();
    return r.getString(list[dayOfWeek - Calendar.SUNDAY]);
}
Also used : Resources(android.content.res.Resources)

Example 93 with Resources

use of android.content.res.Resources in project XobotOS by xamarin.

the class DateUtils method initFormatStringsLocked.

private static void initFormatStringsLocked() {
    Resources r = Resources.getSystem();
    Configuration cfg = r.getConfiguration();
    if (sLastConfig == null || !sLastConfig.equals(cfg)) {
        sLastConfig = cfg;
        sStatusTimeFormat = java.text.DateFormat.getTimeInstance(java.text.DateFormat.SHORT);
        sElapsedFormatMMSS = r.getString(com.android.internal.R.string.elapsed_time_short_format_mm_ss);
        sElapsedFormatHMMSS = r.getString(com.android.internal.R.string.elapsed_time_short_format_h_mm_ss);
    }
}
Also used : Configuration(android.content.res.Configuration) Resources(android.content.res.Resources)

Example 94 with Resources

use of android.content.res.Resources in project XobotOS by xamarin.

the class Time method format.

/**
     * Print the current value given the format string provided. See man
     * strftime for what means what. The final string must be less than 256
     * characters.
     * @param format a string containing the desired format.
     * @return a String containing the current time expressed in the current locale.
     */
public String format(String format) {
    synchronized (Time.class) {
        Locale locale = Locale.getDefault();
        if (sLocale == null || locale == null || !(locale.equals(sLocale))) {
            Resources r = Resources.getSystem();
            sShortMonths = new String[] { r.getString(com.android.internal.R.string.month_medium_january), r.getString(com.android.internal.R.string.month_medium_february), r.getString(com.android.internal.R.string.month_medium_march), r.getString(com.android.internal.R.string.month_medium_april), r.getString(com.android.internal.R.string.month_medium_may), r.getString(com.android.internal.R.string.month_medium_june), r.getString(com.android.internal.R.string.month_medium_july), r.getString(com.android.internal.R.string.month_medium_august), r.getString(com.android.internal.R.string.month_medium_september), r.getString(com.android.internal.R.string.month_medium_october), r.getString(com.android.internal.R.string.month_medium_november), r.getString(com.android.internal.R.string.month_medium_december) };
            sLongMonths = new String[] { r.getString(com.android.internal.R.string.month_long_january), r.getString(com.android.internal.R.string.month_long_february), r.getString(com.android.internal.R.string.month_long_march), r.getString(com.android.internal.R.string.month_long_april), r.getString(com.android.internal.R.string.month_long_may), r.getString(com.android.internal.R.string.month_long_june), r.getString(com.android.internal.R.string.month_long_july), r.getString(com.android.internal.R.string.month_long_august), r.getString(com.android.internal.R.string.month_long_september), r.getString(com.android.internal.R.string.month_long_october), r.getString(com.android.internal.R.string.month_long_november), r.getString(com.android.internal.R.string.month_long_december) };
            sLongStandaloneMonths = new String[] { r.getString(com.android.internal.R.string.month_long_standalone_january), r.getString(com.android.internal.R.string.month_long_standalone_february), r.getString(com.android.internal.R.string.month_long_standalone_march), r.getString(com.android.internal.R.string.month_long_standalone_april), r.getString(com.android.internal.R.string.month_long_standalone_may), r.getString(com.android.internal.R.string.month_long_standalone_june), r.getString(com.android.internal.R.string.month_long_standalone_july), r.getString(com.android.internal.R.string.month_long_standalone_august), r.getString(com.android.internal.R.string.month_long_standalone_september), r.getString(com.android.internal.R.string.month_long_standalone_october), r.getString(com.android.internal.R.string.month_long_standalone_november), r.getString(com.android.internal.R.string.month_long_standalone_december) };
            sShortWeekdays = new String[] { r.getString(com.android.internal.R.string.day_of_week_medium_sunday), r.getString(com.android.internal.R.string.day_of_week_medium_monday), r.getString(com.android.internal.R.string.day_of_week_medium_tuesday), r.getString(com.android.internal.R.string.day_of_week_medium_wednesday), r.getString(com.android.internal.R.string.day_of_week_medium_thursday), r.getString(com.android.internal.R.string.day_of_week_medium_friday), r.getString(com.android.internal.R.string.day_of_week_medium_saturday) };
            sLongWeekdays = new String[] { r.getString(com.android.internal.R.string.day_of_week_long_sunday), r.getString(com.android.internal.R.string.day_of_week_long_monday), r.getString(com.android.internal.R.string.day_of_week_long_tuesday), r.getString(com.android.internal.R.string.day_of_week_long_wednesday), r.getString(com.android.internal.R.string.day_of_week_long_thursday), r.getString(com.android.internal.R.string.day_of_week_long_friday), r.getString(com.android.internal.R.string.day_of_week_long_saturday) };
            sTimeOnlyFormat = r.getString(com.android.internal.R.string.time_of_day);
            sDateOnlyFormat = r.getString(com.android.internal.R.string.month_day_year);
            sDateTimeFormat = r.getString(com.android.internal.R.string.date_and_time);
            sAm = r.getString(com.android.internal.R.string.am);
            sPm = r.getString(com.android.internal.R.string.pm);
            sLocale = locale;
        }
        return format1(format);
    }
}
Also used : Locale(java.util.Locale) Resources(android.content.res.Resources)

Example 95 with Resources

use of android.content.res.Resources in project XobotOS by xamarin.

the class SuggestionsAdapter method getDrawableFromResourceValue.

/**
     * Gets a drawable given a value provided by a suggestion provider.
     *
     * This value could be just the string value of a resource id
     * (e.g., "2130837524"), in which case we will try to retrieve a drawable from
     * the provider's resources. If the value is not an integer, it is
     * treated as a Uri and opened with
     * {@link ContentResolver#openOutputStream(android.net.Uri, String)}.
     *
     * All resources and URIs are read using the suggestion provider's context.
     *
     * If the string is not formatted as expected, or no drawable can be found for
     * the provided value, this method returns null.
     *
     * @param drawableId a string like "2130837524",
     *        "android.resource://com.android.alarmclock/2130837524",
     *        or "content://contacts/photos/253".
     * @return a Drawable, or null if none found
     */
private Drawable getDrawableFromResourceValue(String drawableId) {
    if (drawableId == null || drawableId.length() == 0 || "0".equals(drawableId)) {
        return null;
    }
    try {
        // First, see if it's just an integer
        int resourceId = Integer.parseInt(drawableId);
        // It's an int, look for it in the cache
        String drawableUri = ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + mProviderContext.getPackageName() + "/" + resourceId;
        // Must use URI as cache key, since ints are app-specific
        Drawable drawable = checkIconCache(drawableUri);
        if (drawable != null) {
            return drawable;
        }
        // Not cached, find it by resource ID
        drawable = mProviderContext.getResources().getDrawable(resourceId);
        // Stick it in the cache, using the URI as key
        storeInIconCache(drawableUri, drawable);
        return drawable;
    } catch (NumberFormatException nfe) {
        // It's not an integer, use it as a URI
        Drawable drawable = checkIconCache(drawableId);
        if (drawable != null) {
            return drawable;
        }
        Uri uri = Uri.parse(drawableId);
        drawable = getDrawable(uri);
        storeInIconCache(drawableId, drawable);
        return drawable;
    } catch (Resources.NotFoundException nfe) {
        // It was an integer, but it couldn't be found, bail out
        Log.w(LOG_TAG, "Icon resource not found: " + drawableId);
        return null;
    }
}
Also used : ColorDrawable(android.graphics.drawable.ColorDrawable) Drawable(android.graphics.drawable.Drawable) StateListDrawable(android.graphics.drawable.StateListDrawable) SpannableString(android.text.SpannableString) Resources(android.content.res.Resources) Uri(android.net.Uri)

Aggregations

Resources (android.content.res.Resources)3268 Context (android.content.Context)304 Intent (android.content.Intent)286 View (android.view.View)239 TextView (android.widget.TextView)217 PackageManager (android.content.pm.PackageManager)216 IOException (java.io.IOException)212 Drawable (android.graphics.drawable.Drawable)199 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)179 Paint (android.graphics.Paint)179 DisplayMetrics (android.util.DisplayMetrics)175 Bitmap (android.graphics.Bitmap)174 Configuration (android.content.res.Configuration)154 Point (android.graphics.Point)153 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)139 ArrayList (java.util.ArrayList)137 XmlResourceParser (android.content.res.XmlResourceParser)133 TypedArray (android.content.res.TypedArray)132 Test (org.junit.Test)127 PendingIntent (android.app.PendingIntent)123