blob: 870489ff1439d59f1a8eeb7f3774d26163136390 [file] [log] [blame]
Jake McGinty2ddee9b2015-04-03 16:44:04 -07001package com.davemorrissey.labs.subscaleview.decoder;
2
Khabensky Denisc95f6e22017-10-10 12:57:13 +03003import android.graphics.Bitmap;
Jake McGinty2ddee9b2015-04-03 16:44:04 -07004import android.support.annotation.NonNull;
5
Khabensky Denisc95f6e22017-10-10 12:57:13 +03006import java.lang.reflect.Constructor;
7import java.lang.reflect.InvocationTargetException;
8
Jake McGinty2ddee9b2015-04-03 16:44:04 -07009/**
10 * Compatibility factory to instantiate decoders with empty public constructors.
11 * @param <T> The base type of the decoder this factory will produce.
12 */
David Morrissey43bf80b2017-12-12 07:51:54 +000013@SuppressWarnings("WeakerAccess")
Khabensky Denisc95f6e22017-10-10 12:57:13 +030014public class CompatDecoderFactory<T> implements DecoderFactory<T> {
Jake McGinty2ddee9b2015-04-03 16:44:04 -070015
David Morrissey51605f62017-12-12 08:01:19 +000016 private final Class<? extends T> clazz;
17 private final Bitmap.Config bitmapConfig;
David Morrissey43bf80b2017-12-12 07:51:54 +000018
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) {
Khabensky Denisc95f6e22017-10-10 12:57:13 +030024 this(clazz, null);
Khabensky Denisc95f6e22017-10-10 12:57:13 +030025 }
David Morrissey43bf80b2017-12-12 07:51:54 +000026
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;
Khabensky Denisc95f6e22017-10-10 12:57:13 +030035 }
David Morrissey43bf80b2017-12-12 07:51:54 +000036
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
Jake McGinty2ddee9b2015-04-03 16:44:04 -070047}