1package com.bumptech.glide.load.resource.bitmap;
2
3import android.graphics.Bitmap;
4import android.os.ParcelFileDescriptor;
5import android.util.Log;
6import com.bumptech.glide.load.engine.Resource;
7import com.bumptech.glide.load.ResourceDecoder;
8import com.bumptech.glide.load.model.ImageVideoWrapper;
9
10import java.io.IOException;
11import java.io.InputStream;
12
13public class ImageVideoBitmapDecoder implements ResourceDecoder<ImageVideoWrapper, Bitmap> {
14    private static final String TAG = "ImageVideoDecoder";
15    private final ResourceDecoder<InputStream, Bitmap> streamDecoder;
16    private final ResourceDecoder<ParcelFileDescriptor, Bitmap> fileDescriptorDecoder;
17
18    public ImageVideoBitmapDecoder(ResourceDecoder<InputStream, Bitmap> streamDecoder,
19            ResourceDecoder<ParcelFileDescriptor, Bitmap> fileDescriptorDecoder) {
20        this.streamDecoder = streamDecoder;
21        this.fileDescriptorDecoder = fileDescriptorDecoder;
22    }
23
24    @Override
25    public Resource<Bitmap> decode(ImageVideoWrapper source, int width, int height) throws IOException {
26        Resource<Bitmap> result = null;
27        InputStream is = source.getStream();
28        if (is != null) {
29            try {
30                result = streamDecoder.decode(source.getStream(), width, height);
31            } catch (IOException e) {
32                if (Log.isLoggable(TAG, Log.VERBOSE)) {
33                    Log.v(TAG, "Failed to load image from stream, trying FileDescriptor", e);
34                }
35            }
36        }
37
38        if (result == null) {
39            ParcelFileDescriptor fileDescriptor = source.getFileDescriptor();
40            if (fileDescriptor != null) {
41                result = fileDescriptorDecoder.decode(source.getFileDescriptor(), width, height);
42            }
43        }
44        return result;
45    }
46
47    @Override
48    public String getId() {
49        return "ImageVideoBitmapDecoder.com.bumptech.glide.load.resource.bitmap";
50    }
51}
52