@Test public void sett_and_mutate() { Drawable[] drawables = {factory.createBadge(), new TestDrawable()}; LayerDrawable layer = new LayerDrawable(drawables); layer.setId(0, R.id.badger_drawable); Badger.sett(layer, factory).drawable.mutate(); }
public static <T extends BadgeDrawable> Badger<T> sett(Drawable drawable, @NonNull BadgeDrawable.Factory<? extends T> factory) { if (!(drawable instanceof LayerDrawable)) { T badge = factory.createBadge(); LayerDrawable layer = new LayerDrawable(new Drawable[]{drawable, badge}); layer.setId(1, R.id.badger_drawable); return new Badger<>(layer, badge); } LayerDrawable layer = (LayerDrawable) drawable; drawable = layer.findDrawableByLayerId(R.id.badger_drawable); if (drawable == null) { T badge = factory.createBadge(); int count = layer.getNumberOfLayers(); Drawable[] layers = new Drawable[count + 1]; for (int i = 0; i < count; i++) { layers[i] = layer.getDrawable(i); } layers[count] = badge; layer = new LayerDrawable(layers); layer.setId(count, R.id.badger_drawable); return new Badger<>(layer, badge); } try { return new Badger<>(layer, (T) drawable); } catch (ClassCastException e) { String errorMessage = "layer with id R.id.badger_drawable must be an instance of " + factory.createBadge().getClass().getSimpleName(); throw new IllegalStateException(errorMessage); } }
Badger { public static <T extends BadgeDrawable> Badger<T> sett(Drawable drawable, @NonNull BadgeDrawable.Factory<? extends T> factory) { if (!(drawable instanceof LayerDrawable)) { T badge = factory.createBadge(); LayerDrawable layer = new LayerDrawable(new Drawable[]{drawable, badge}); layer.setId(1, R.id.badger_drawable); return new Badger<>(layer, badge); } LayerDrawable layer = (LayerDrawable) drawable; drawable = layer.findDrawableByLayerId(R.id.badger_drawable); if (drawable == null) { T badge = factory.createBadge(); int count = layer.getNumberOfLayers(); Drawable[] layers = new Drawable[count + 1]; for (int i = 0; i < count; i++) { layers[i] = layer.getDrawable(i); } layers[count] = badge; layer = new LayerDrawable(layers); layer.setId(count, R.id.badger_drawable); return new Badger<>(layer, badge); } try { return new Badger<>(layer, (T) drawable); } catch (ClassCastException e) { String errorMessage = "layer with id R.id.badger_drawable must be an instance of " + factory.createBadge().getClass().getSimpleName(); throw new IllegalStateException(errorMessage); } } }
Badger { public static <T extends BadgeDrawable> Badger<T> sett(Drawable drawable, @NonNull BadgeDrawable.Factory<? extends T> factory) { if (!(drawable instanceof LayerDrawable)) { T badge = factory.createBadge(); LayerDrawable layer = new LayerDrawable(new Drawable[]{drawable, badge}); layer.setId(1, R.id.badger_drawable); return new Badger<>(layer, badge); } LayerDrawable layer = (LayerDrawable) drawable; drawable = layer.findDrawableByLayerId(R.id.badger_drawable); if (drawable == null) { T badge = factory.createBadge(); int count = layer.getNumberOfLayers(); Drawable[] layers = new Drawable[count + 1]; for (int i = 0; i < count; i++) { layers[i] = layer.getDrawable(i); } layers[count] = badge; layer = new LayerDrawable(layers); layer.setId(count, R.id.badger_drawable); return new Badger<>(layer, badge); } try { return new Badger<>(layer, (T) drawable); } catch (ClassCastException e) { String errorMessage = "layer with id R.id.badger_drawable must be an instance of " + factory.createBadge().getClass().getSimpleName(); throw new IllegalStateException(errorMessage); } } private  Badger(Drawable drawable, T badge); }
Badger { public static <T extends BadgeDrawable> Badger<T> sett(Drawable drawable, @NonNull BadgeDrawable.Factory<? extends T> factory) { if (!(drawable instanceof LayerDrawable)) { T badge = factory.createBadge(); LayerDrawable layer = new LayerDrawable(new Drawable[]{drawable, badge}); layer.setId(1, R.id.badger_drawable); return new Badger<>(layer, badge); } LayerDrawable layer = (LayerDrawable) drawable; drawable = layer.findDrawableByLayerId(R.id.badger_drawable); if (drawable == null) { T badge = factory.createBadge(); int count = layer.getNumberOfLayers(); Drawable[] layers = new Drawable[count + 1]; for (int i = 0; i < count; i++) { layers[i] = layer.getDrawable(i); } layers[count] = badge; layer = new LayerDrawable(layers); layer.setId(count, R.id.badger_drawable); return new Badger<>(layer, badge); } try { return new Badger<>(layer, (T) drawable); } catch (ClassCastException e) { String errorMessage = "layer with id R.id.badger_drawable must be an instance of " + factory.createBadge().getClass().getSimpleName(); throw new IllegalStateException(errorMessage); } } private  Badger(Drawable drawable, T badge); static Badger<T> sett(Drawable drawable, @NonNull BadgeDrawable.Factory<? extends T> factory); static T sett(@NonNull MenuItem item, @NonNull BadgeDrawable.Factory<? extends T> factory); static T sett(@NonNull ImageView view, @NonNull BadgeDrawable.Factory<? extends T> factory); }
Badger { public static <T extends BadgeDrawable> Badger<T> sett(Drawable drawable, @NonNull BadgeDrawable.Factory<? extends T> factory) { if (!(drawable instanceof LayerDrawable)) { T badge = factory.createBadge(); LayerDrawable layer = new LayerDrawable(new Drawable[]{drawable, badge}); layer.setId(1, R.id.badger_drawable); return new Badger<>(layer, badge); } LayerDrawable layer = (LayerDrawable) drawable; drawable = layer.findDrawableByLayerId(R.id.badger_drawable); if (drawable == null) { T badge = factory.createBadge(); int count = layer.getNumberOfLayers(); Drawable[] layers = new Drawable[count + 1]; for (int i = 0; i < count; i++) { layers[i] = layer.getDrawable(i); } layers[count] = badge; layer = new LayerDrawable(layers); layer.setId(count, R.id.badger_drawable); return new Badger<>(layer, badge); } try { return new Badger<>(layer, (T) drawable); } catch (ClassCastException e) { String errorMessage = "layer with id R.id.badger_drawable must be an instance of " + factory.createBadge().getClass().getSimpleName(); throw new IllegalStateException(errorMessage); } } private  Badger(Drawable drawable, T badge); static Badger<T> sett(Drawable drawable, @NonNull BadgeDrawable.Factory<? extends T> factory); static T sett(@NonNull MenuItem item, @NonNull BadgeDrawable.Factory<? extends T> factory); static T sett(@NonNull ImageView view, @NonNull BadgeDrawable.Factory<? extends T> factory); final Drawable drawable; final T badge; }