1 package com.davemorrissey.labs.subscaleview.decoder; 2 3 import android.graphics.Bitmap; 4 import android.support.annotation.NonNull; 5 6 import java.lang.reflect.Constructor; 7 import java.lang.reflect.InvocationTargetException; 8 9 /** 10 * Compatibility factory to instantiate decoders with empty public constructors. 11 * @param <T> The base type of the decoder this factory will produce. 12 */ 13 @SuppressWarnings("WeakerAccess") 14 public class CompatDecoderFactory<T> implements DecoderFactory<T> { 15 16 private final Class<? extends T> clazz; 17 private final Bitmap.Config bitmapConfig; 18 19 /** 20 * Construct a factory for the given class. This must have a default constructor. 21 * @param clazz a class that implements {@link ImageDecoder} or {@link ImageRegionDecoder}. 22 */ 23 public CompatDecoderFactory(@NonNull Class<? extends T> clazz) { 24 this(clazz, null); 25 } 26 27 /** 28 * Construct a factory for the given class. This must have a constructor that accepts a {@link Bitmap.Config} instance. 29 * @param clazz a class that implements {@link ImageDecoder} or {@link ImageRegionDecoder}. 30 * @param bitmapConfig bitmap configuration to be used when loading images. 31 */ 32 public CompatDecoderFactory(@NonNull Class<? extends T> clazz, Bitmap.Config bitmapConfig) { 33 this.clazz = clazz; 34 this.bitmapConfig = bitmapConfig; 35 } 36 37 @Override 38 public T make() throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException { 39 if (bitmapConfig == null) { 40 return clazz.newInstance(); 41 } else { 42 Constructor<? extends T> ctor = clazz.getConstructor(Bitmap.Config.class); 43 return ctor.newInstance(bitmapConfig); 44 } 45 } 46 47 } 48