Jake McGinty | 2ddee9b | 2015-04-03 16:44:04 -0700 | [diff] [blame] | 1 | package com.davemorrissey.labs.subscaleview.decoder; |
| 2 | |
Khabensky Denis | c95f6e2 | 2017-10-10 12:57:13 +0300 | [diff] [blame] | 3 | import android.graphics.Bitmap; |
Jake McGinty | 2ddee9b | 2015-04-03 16:44:04 -0700 | [diff] [blame] | 4 | import android.support.annotation.NonNull; |
| 5 | |
Khabensky Denis | c95f6e2 | 2017-10-10 12:57:13 +0300 | [diff] [blame] | 6 | import java.lang.reflect.Constructor; |
| 7 | import java.lang.reflect.InvocationTargetException; |
| 8 | |
Jake McGinty | 2ddee9b | 2015-04-03 16:44:04 -0700 | [diff] [blame] | 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 | */ |
David Morrissey | 43bf80b | 2017-12-12 07:51:54 +0000 | [diff] [blame] | 13 | @SuppressWarnings("WeakerAccess") |
Khabensky Denis | c95f6e2 | 2017-10-10 12:57:13 +0300 | [diff] [blame] | 14 | public class CompatDecoderFactory<T> implements DecoderFactory<T> { |
Jake McGinty | 2ddee9b | 2015-04-03 16:44:04 -0700 | [diff] [blame] | 15 | |
David Morrissey | 51605f6 | 2017-12-12 08:01:19 +0000 | [diff] [blame] | 16 | private final Class<? extends T> clazz; |
| 17 | private final Bitmap.Config bitmapConfig; |
David Morrissey | 43bf80b | 2017-12-12 07:51:54 +0000 | [diff] [blame] | 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) { |
Khabensky Denis | c95f6e2 | 2017-10-10 12:57:13 +0300 | [diff] [blame] | 24 | this(clazz, null); |
Khabensky Denis | c95f6e2 | 2017-10-10 12:57:13 +0300 | [diff] [blame] | 25 | } |
David Morrissey | 43bf80b | 2017-12-12 07:51:54 +0000 | [diff] [blame] | 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; |
Khabensky Denis | c95f6e2 | 2017-10-10 12:57:13 +0300 | [diff] [blame] | 35 | } |
David Morrissey | 43bf80b | 2017-12-12 07:51:54 +0000 | [diff] [blame] | 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 | |
Jake McGinty | 2ddee9b | 2015-04-03 16:44:04 -0700 | [diff] [blame] | 47 | } |