use of android.annotation.Nullable in project android_frameworks_base by DirtyUnicorns.
the class ResourcesImpl method loadDrawable.
@Nullable
Drawable loadDrawable(Resources wrapper, TypedValue value, int id, Resources.Theme theme, boolean useCache) throws NotFoundException {
try {
if (TRACE_FOR_PRELOAD) {
// Log only framework resources
if ((id >>> 24) == 0x1) {
final String name = getResourceName(id);
if (name != null) {
Log.d("PreloadDrawable", name);
}
}
}
final boolean isColorDrawable;
final DrawableCache caches;
final long key;
if (value.type >= TypedValue.TYPE_FIRST_COLOR_INT && value.type <= TypedValue.TYPE_LAST_COLOR_INT) {
isColorDrawable = true;
caches = mColorDrawableCache;
key = value.data;
} else {
isColorDrawable = false;
caches = mDrawableCache;
key = (((long) value.assetCookie) << 32) | value.data;
}
// we're currently preloading or we're not using the cache.
if (!mPreloading && useCache) {
final Drawable cachedDrawable = caches.getInstance(key, wrapper, theme);
if (cachedDrawable != null) {
return cachedDrawable;
}
}
// Next, check preloaded drawables. Preloaded drawables may contain
// unresolved theme attributes.
final Drawable.ConstantState cs;
if (isColorDrawable) {
cs = sPreloadedColorDrawables.get(key);
} else {
cs = sPreloadedDrawables[mConfiguration.getLayoutDirection()].get(key);
}
Drawable dr;
if (cs != null) {
dr = cs.newDrawable(wrapper);
} else if (isColorDrawable) {
dr = new ColorDrawable(value.data);
} else {
dr = loadDrawableForCookie(wrapper, value, id, null);
}
// Determine if the drawable has unresolved theme attributes. If it
// does, we'll need to apply a theme and store it in a theme-specific
// cache.
final boolean canApplyTheme = dr != null && dr.canApplyTheme();
if (canApplyTheme && theme != null) {
dr = dr.mutate();
dr.applyTheme(theme);
dr.clearMutated();
}
// pollute the cache with drawables loaded from a foreign density.
if (dr != null && useCache) {
dr.setChangingConfigurations(value.changingConfigurations);
cacheDrawable(value, isColorDrawable, caches, theme, canApplyTheme, key, dr);
}
return dr;
} catch (Exception e) {
String name;
try {
name = getResourceName(id);
} catch (NotFoundException e2) {
name = "(missing name)";
}
// The target drawable might fail to load for any number of
// reasons, but we always want to include the resource name.
// Since the client already expects this method to throw a
// NotFoundException, just throw one of those.
final NotFoundException nfe = new NotFoundException("Drawable " + name + " with resource ID #0x" + Integer.toHexString(id), e);
nfe.setStackTrace(new StackTraceElement[0]);
throw nfe;
}
}
use of android.annotation.Nullable in project android_frameworks_base by DirtyUnicorns.
the class ThemedResourceCache method getThemedLocked.
/**
* Returns the cached data for the specified theme, optionally creating a
* new entry if one does not already exist.
*
* @param t the theme for which to return cached data
* @param create {@code true} to create an entry if one does not already
* exist, {@code false} otherwise
* @return the cached data for the theme, or {@code null} if the cache is
* empty and {@code create} was {@code false}
*/
@Nullable
private LongSparseArray<WeakReference<T>> getThemedLocked(@Nullable Theme t, boolean create) {
if (t == null) {
if (mNullThemedEntries == null && create) {
mNullThemedEntries = new LongSparseArray<>(1);
}
return mNullThemedEntries;
}
if (mThemedEntries == null) {
if (create) {
mThemedEntries = new ArrayMap<>(1);
} else {
return null;
}
}
final ThemeKey key = t.getKey();
LongSparseArray<WeakReference<T>> cache = mThemedEntries.get(key);
if (cache == null && create) {
cache = new LongSparseArray<>(1);
final ThemeKey keyClone = key.clone();
mThemedEntries.put(keyClone, cache);
}
return cache;
}
use of android.annotation.Nullable in project android_frameworks_base by DirtyUnicorns.
the class TypedArray method getString.
/**
* Retrieves the string value for the attribute at <var>index</var>.
* <p>
* If the attribute is not a string, this method will attempt to coerce
* it to a string.
*
* @param index Index of attribute to retrieve.
*
* @return String holding string data. Any styling information is removed.
* Returns {@code null} if the attribute is not defined or could
* not be coerced to a string.
* @throws RuntimeException if the TypedArray has already been recycled.
*/
@Nullable
public String getString(@StyleableRes int index) {
if (mRecycled) {
throw new RuntimeException("Cannot make calls to a recycled instance!");
}
index *= AssetManager.STYLE_NUM_ENTRIES;
final int[] data = mData;
final int type = data[index + AssetManager.STYLE_TYPE];
if (type == TypedValue.TYPE_NULL) {
return null;
} else if (type == TypedValue.TYPE_STRING) {
return loadStringValueAt(index).toString();
}
final TypedValue v = mValue;
if (getValueAt(index, v)) {
final CharSequence cs = v.coerceToString();
return cs != null ? cs.toString() : null;
}
// We already checked for TYPE_NULL. This should never happen.
throw new RuntimeException("getString of bad type: 0x" + Integer.toHexString(type));
}
use of android.annotation.Nullable in project android_frameworks_base by DirtyUnicorns.
the class ShortcutInfo method getIntents.
/**
* Return the intent set with {@link Builder#setIntents(Intent[])}.
*
* <p>Launcher apps <b>cannot</b> see the intents. If a {@link ShortcutInfo} is
* obtained via {@link LauncherApps}, then this method will always return null.
* Launchers can only start a shortcut intent with {@link LauncherApps#startShortcut}.
*
* @see Builder#setIntents(Intent[])
*/
@Nullable
public Intent[] getIntents() {
final Intent[] ret = new Intent[mIntents.length];
for (int i = 0; i < ret.length; i++) {
ret[i] = new Intent(mIntents[i]);
setIntentExtras(ret[i], mIntentPersistableExtrases[i]);
}
return ret;
}
use of android.annotation.Nullable in project android_frameworks_base by DirtyUnicorns.
the class TimePickerClockDelegate method applyLegacyColorFixes.
/**
* The legacy text color might have been poorly defined. Ensures that it
* has an appropriate activated state, using the selected state if one
* exists or modifying the default text color otherwise.
*
* @param color a legacy text color, or {@code null}
* @return a color state list with an appropriate activated state, or
* {@code null} if a valid activated state could not be generated
*/
@Nullable
private ColorStateList applyLegacyColorFixes(@Nullable ColorStateList color) {
if (color == null || color.hasState(R.attr.state_activated)) {
return color;
}
final int activatedColor;
final int defaultColor;
if (color.hasState(R.attr.state_selected)) {
activatedColor = color.getColorForState(StateSet.get(StateSet.VIEW_STATE_ENABLED | StateSet.VIEW_STATE_SELECTED), 0);
defaultColor = color.getColorForState(StateSet.get(StateSet.VIEW_STATE_ENABLED), 0);
} else {
activatedColor = color.getDefaultColor();
// Generate a non-activated color using the disabled alpha.
final TypedArray ta = mContext.obtainStyledAttributes(ATTRS_DISABLED_ALPHA);
final float disabledAlpha = ta.getFloat(0, 0.30f);
defaultColor = multiplyAlphaComponent(activatedColor, disabledAlpha);
}
if (activatedColor == 0 || defaultColor == 0) {
// We somehow failed to obtain the colors.
return null;
}
final int[][] stateSet = new int[][] { { R.attr.state_activated }, {} };
final int[] colors = new int[] { activatedColor, defaultColor };
return new ColorStateList(stateSet, colors);
}
Aggregations