aboutsummaryrefslogtreecommitdiff
path: root/library/src/main/java/com/davemorrissey/labs/subscaleview/decoder/CompatDecoderFactory.java
diff options
context:
space:
mode:
authorJon Miranda <jonmiranda@google.com>2018-02-08 19:10:54 +0000
committerandroid-build-merger <android-build-merger@google.com>2018-02-08 19:10:54 +0000
commit45ca08280baebd7342764bdbc9b520d7183d6ad7 (patch)
treefea66d74c8b32d168fc354d281a68937c582a928 /library/src/main/java/com/davemorrissey/labs/subscaleview/decoder/CompatDecoderFactory.java
parenteb2ff754284dce6eece14bdbc02d0d02928cf4b9 (diff)
parent851c104050afcffacb009c98c79de0ba3d945db1 (diff)
downloadsubsampling-scale-image-view-45ca08280baebd7342764bdbc9b520d7183d6ad7.tar.gz
am: 851c104050 Change-Id: I372e079b8ebd5beb7648a352bf2d5127b6539615
Diffstat (limited to 'library/src/main/java/com/davemorrissey/labs/subscaleview/decoder/CompatDecoderFactory.java')
-rw-r--r--library/src/main/java/com/davemorrissey/labs/subscaleview/decoder/CompatDecoderFactory.java47
1 files changed, 47 insertions, 0 deletions
diff --git a/library/src/main/java/com/davemorrissey/labs/subscaleview/decoder/CompatDecoderFactory.java b/library/src/main/java/com/davemorrissey/labs/subscaleview/decoder/CompatDecoderFactory.java
new file mode 100644
index 0000000..870489f
--- /dev/null
+++ b/library/src/main/java/com/davemorrissey/labs/subscaleview/decoder/CompatDecoderFactory.java
@@ -0,0 +1,47 @@
+package com.davemorrissey.labs.subscaleview.decoder;
+
+import android.graphics.Bitmap;
+import android.support.annotation.NonNull;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+
+/**
+ * Compatibility factory to instantiate decoders with empty public constructors.
+ * @param <T> The base type of the decoder this factory will produce.
+ */
+@SuppressWarnings("WeakerAccess")
+public class CompatDecoderFactory<T> implements DecoderFactory<T> {
+
+ private final Class<? extends T> clazz;
+ private final Bitmap.Config bitmapConfig;
+
+ /**
+ * Construct a factory for the given class. This must have a default constructor.
+ * @param clazz a class that implements {@link ImageDecoder} or {@link ImageRegionDecoder}.
+ */
+ public CompatDecoderFactory(@NonNull Class<? extends T> clazz) {
+ this(clazz, null);
+ }
+
+ /**
+ * Construct a factory for the given class. This must have a constructor that accepts a {@link Bitmap.Config} instance.
+ * @param clazz a class that implements {@link ImageDecoder} or {@link ImageRegionDecoder}.
+ * @param bitmapConfig bitmap configuration to be used when loading images.
+ */
+ public CompatDecoderFactory(@NonNull Class<? extends T> clazz, Bitmap.Config bitmapConfig) {
+ this.clazz = clazz;
+ this.bitmapConfig = bitmapConfig;
+ }
+
+ @Override
+ public T make() throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
+ if (bitmapConfig == null) {
+ return clazz.newInstance();
+ } else {
+ Constructor<? extends T> ctor = clazz.getConstructor(Bitmap.Config.class);
+ return ctor.newInstance(bitmapConfig);
+ }
+ }
+
+}