/** * Use to match a view against a potential view id. Such as ActionBar title etc. * * @param view not null view you want to see has resource matching name. * @param matches not null resource name to match against. Its not case sensitive. * @return true if matches false otherwise. */ protected static boolean matchesResourceIdName(View view, String matches) { if (view.getId() == View.NO_ID) return false; final String resourceEntryName = view.getResources().getResourceEntryName(view.getId()); return resourceEntryName.equalsIgnoreCase(matches); }
@Nullable private Uri getResourceUri(Integer model) { try { return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + resources.getResourcePackageName(model) + '/' + resources.getResourceTypeName(model) + '/' + resources.getResourceEntryName(model)); } catch (Resources.NotFoundException e) { if (Log.isLoggable(TAG, Log.WARN)) { Log.w(TAG, "Received invalid resource id: " + model, e); } return null; } }
private static String getResourceEntryName(View view, @IdRes int id) { if (view.isInEditMode()) { return "<unavailable while editing>"; } return view.getContext().getResources().getResourceEntryName(id); }
public <T extends View> T getView(int viewId){ View view = mViews.get(viewId); if(view == null){ view = mConvertView.findViewById(viewId); if(view == null){ String resName = context.getResources().getResourceEntryName(viewId); throw new RuntimeException("Can not find "+ resName + " in parent view !"); } mViews.put(viewId, view); } return (T)view; }
/** @return the resource name or "<undefined>" */ public static String getResourceName(final int resourceId) { try { return RuntimeEnvironment .application .getResources() .getResourceEntryName(resourceId); } catch (final NotFoundException notFoundException) { return "<undefined>"; } } }
public static Uri getUriFromDrawableResId(Context context, int drawableResId) { StringBuilder builder = new StringBuilder().append(ContentResolver.SCHEME_ANDROID_RESOURCE) .append("://") .append(context.getResources().getResourcePackageName(drawableResId)) .append("/") .append(context.getResources().getResourceTypeName(drawableResId)) .append("/") .append(context.getResources().getResourceEntryName(drawableResId)); return Uri.parse(builder.toString()); }
public static Uri getUriFromDrawableResId(Context context, int drawableResId) { StringBuilder builder = new StringBuilder().append(ContentResolver.SCHEME_ANDROID_RESOURCE) .append("://") .append(context.getResources().getResourcePackageName(drawableResId)) .append("/") .append(context.getResources().getResourceTypeName(drawableResId)) .append("/") .append(context.getResources().getResourceEntryName(drawableResId)); return Uri.parse(builder.toString()); }
public void dynamicAddSkinEnableView(Context context, View view, String attrName, int attrValueResId){ int id = attrValueResId; String entryName = context.getResources().getResourceEntryName(id); String typeName = context.getResources().getResourceTypeName(id); SkinAttr mSkinAttr = AttrFactory.get(attrName, id, entryName, typeName); SkinItem skinItem = new SkinItem(); skinItem.view = view; List<SkinAttr> viewAttrs = new ArrayList<SkinAttr>(); viewAttrs.add(mSkinAttr); skinItem.attrs = viewAttrs; addSkinView(skinItem); }
@UiThread // Implicit synchronization for use of shared resource VALUE. public static Drawable getTintedDrawable(Context context, @DrawableRes int id, @AttrRes int tintAttrId) { boolean attributeFound = context.getTheme().resolveAttribute(tintAttrId, VALUE, true); if (!attributeFound) { throw new Resources.NotFoundException("Required tint color attribute with name " + context.getResources().getResourceEntryName(tintAttrId) + " and attribute ID " + tintAttrId + " was not found."); } Drawable drawable = ContextCompat.getDrawable(context, id); drawable = DrawableCompat.wrap(drawable.mutate()); int color = ContextCompat.getColor(context, VALUE.resourceId); DrawableCompat.setTint(drawable, color); return drawable; }
public void setDisplayedChildId(int id) { if (getDisplayedChildId() == id) { return; } for (int i = 0, count = getChildCount(); i < count; i++) { if (getChildAt(i).getId() == id) { setDisplayedChild(i); return; } } String name = getResources().getResourceEntryName(id); throw new IllegalArgumentException("No view with ID " + name); }
@Test public void getResourceEntryName_forStyle() throws Exception { assertThat(resources.getResourceEntryName(android.R.style.TextAppearance_Small)) .isEqualTo("TextAppearance.Small"); }
@Test public void load_withApplicationIconResourceNameUri_asDrawable_producesNonNullDrawable() throws ExecutionException, InterruptedException, NameNotFoundException { for (String packageName : getInstalledPackages()) { int iconResourceId = getResourceId(packageName); Context toUse = context.createPackageContext(packageName, /*flags=*/ 0); Resources resources = toUse.getResources(); Uri uri = new Uri.Builder() .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE) .authority(packageName) .appendPath(resources.getResourceTypeName(iconResourceId)) .appendPath(resources.getResourceEntryName(iconResourceId)) .build(); Drawable drawable = Glide.with(context) .load(uri) .submit() .get(); assertThat(drawable).isNotNull(); } }
@Test public void loadVideoResourceUriString_fromName_withFrame_decodesFrame() { Resources resources = context.getResources(); int resourceId = ResourceIds.raw.video; Uri uri = new Uri.Builder() .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE) .authority(resources.getResourcePackageName(resourceId)) .appendPath(resources.getResourceTypeName(resourceId)) .appendPath(resources.getResourceEntryName(resourceId)) .build(); Bitmap frame = concurrency.get( GlideApp.with(context) .asBitmap() .load(uri.toString()) .frame(TimeUnit.SECONDS.toMicros(1)) .submit()); assertThat(frame).isNotNull(); } }
@Test public void load_withApplicationIconResourceNameUri_asDrawable_withTransform_nonNullDrawable() throws ExecutionException, InterruptedException, NameNotFoundException { for (String packageName : getInstalledPackages()) { int iconResourceId = getResourceId(packageName); Context toUse = context.createPackageContext(packageName, /*flags=*/ 0); Resources resources = toUse.getResources(); Uri uri = new Uri.Builder() .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE) .authority(packageName) .appendPath(resources.getResourceTypeName(iconResourceId)) .appendPath(resources.getResourceEntryName(iconResourceId)) .build(); Drawable drawable = Glide.with(context) .load(uri) .apply(centerCropTransform()) .submit() .get(); assertThat(drawable).isNotNull(); } }
@Test public void loadVideoResourceUriString_fromName_decodesFrame() { Resources resources = context.getResources(); int resourceId = ResourceIds.raw.video; Uri uri = new Uri.Builder() .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE) .authority(resources.getResourcePackageName(resourceId)) .appendPath(resources.getResourceTypeName(resourceId)) .appendPath(resources.getResourceEntryName(resourceId)) .build(); Bitmap frame = concurrency.get( GlideApp.with(context) .asBitmap() .load(uri.toString()) .submit()); assertThat(frame).isNotNull(); }
@Test public void loadVideoResourceUri_fromName_decodesFrame() { Resources resources = context.getResources(); int resourceId = ResourceIds.raw.video; Uri uri = new Uri.Builder() .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE) .authority(resources.getResourcePackageName(resourceId)) .appendPath(resources.getResourceTypeName(resourceId)) .appendPath(resources.getResourceEntryName(resourceId)) .build(); Drawable frame = concurrency.get( GlideApp.with(context) .load(uri) .submit()); assertThat(frame).isNotNull(); }
@Test public void loadVideoResourceUri_fromName_withFrame_decodesFrame() { Resources resources = context.getResources(); int resourceId = ResourceIds.raw.video; Uri uri = new Uri.Builder() .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE) .authority(resources.getResourcePackageName(resourceId)) .appendPath(resources.getResourceTypeName(resourceId)) .appendPath(resources.getResourceEntryName(resourceId)) .build(); Bitmap frame = concurrency.get( GlideApp.with(context) .asBitmap() .load(uri) .frame(TimeUnit.SECONDS.toMicros(1)) .submit()); assertThat(frame).isNotNull(); }
@Test public void load_withApplicationIconResourceNameUri_asBitmap_producesNonNullBitmap() throws ExecutionException, InterruptedException, NameNotFoundException { for (String packageName : getInstalledPackages()) { int iconResourceId = getResourceId(packageName); Context toUse = context.createPackageContext(packageName, /*flags=*/ 0); Resources resources = toUse.getResources(); Uri uri = new Uri.Builder() .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE) .authority(packageName) .appendPath(resources.getResourceTypeName(iconResourceId)) .appendPath(resources.getResourceEntryName(iconResourceId)) .build(); Bitmap bitmap = Glide.with(context) .asBitmap() .load(uri) .submit() .get(); assertThat(bitmap).isNotNull(); } }
@Test public void loadVideoResourceUri_asBitmap_fromName_decodesFrame() { Resources resources = context.getResources(); int resourceId = ResourceIds.raw.video; Uri uri = new Uri.Builder() .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE) .authority(resources.getResourcePackageName(resourceId)) .appendPath(resources.getResourceTypeName(resourceId)) .appendPath(resources.getResourceEntryName(resourceId)) .build(); Bitmap frame = concurrency.get( GlideApp.with(context) .asBitmap() .load(uri) .submit()); assertThat(frame).isNotNull(); }
@Test public void load_withApplicationIconResourceNameUri_asBitmap_withTransform_nonNullBitmap() throws ExecutionException, InterruptedException, NameNotFoundException { for (String packageName : getInstalledPackages()) { int iconResourceId = getResourceId(packageName); Context toUse = context.createPackageContext(packageName, /*flags=*/ 0); Resources resources = toUse.getResources(); Uri uri = new Uri.Builder() .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE) .authority(packageName) .appendPath(resources.getResourceTypeName(iconResourceId)) .appendPath(resources.getResourceEntryName(iconResourceId)) .build(); Bitmap bitmap = Glide.with(context) .asBitmap() .apply(centerCropTransform()) .load(uri) .submit() .get(); assertThat(bitmap).isNotNull(); } }