1package com.bumptech.glide.load.model;
2
3import android.os.ParcelFileDescriptor;
4import android.util.Log;
5import com.bumptech.glide.Priority;
6import com.bumptech.glide.load.data.DataFetcher;
7
8import java.io.InputStream;
9
10public class ImageVideoModelLoader<A> implements ModelLoader<A, ImageVideoWrapper> {
11    private static final String TAG = "IVML";
12
13    private final ModelLoader<A, InputStream> streamLoader;
14    private final ModelLoader<A, ParcelFileDescriptor> fileDescriptorLoader;
15
16    public ImageVideoModelLoader(ModelLoader<A, InputStream> streamLoader,
17            ModelLoader<A, ParcelFileDescriptor> fileDescriptorLoader) {
18        if (streamLoader == null && fileDescriptorLoader == null) {
19            throw new NullPointerException("At least one of streamLoader and fileDescriptorLoader must be non null");
20        }
21        this.streamLoader = streamLoader;
22        this.fileDescriptorLoader = fileDescriptorLoader;
23    }
24
25    @Override
26    public DataFetcher<ImageVideoWrapper> getResourceFetcher(A model, int width, int height) {
27        DataFetcher<InputStream> streamFetcher = null;
28        if (streamLoader != null) {
29            streamFetcher = streamLoader.getResourceFetcher(model, width, height);
30        }
31        DataFetcher<ParcelFileDescriptor> fileDescriptorFetcher = null;
32        if (fileDescriptorLoader != null) {
33            fileDescriptorFetcher = fileDescriptorLoader.getResourceFetcher(model, width, height);
34        }
35        return new ImageVideoFetcher(streamFetcher, fileDescriptorFetcher);
36    }
37
38    public static class ImageVideoFetcher implements DataFetcher<ImageVideoWrapper> {
39        private final DataFetcher<InputStream> streamFetcher;
40        private final DataFetcher<ParcelFileDescriptor> fileDescriptorFetcher;
41
42        public ImageVideoFetcher(DataFetcher<InputStream> streamFetcher,
43                DataFetcher<ParcelFileDescriptor> fileDescriptorFetcher) {
44            this.streamFetcher = streamFetcher;
45            this.fileDescriptorFetcher = fileDescriptorFetcher;
46        }
47        @Override
48        public ImageVideoWrapper loadData(Priority priority) throws Exception {
49            InputStream is = null;
50            if (streamFetcher != null) {
51                try {
52                    is = streamFetcher.loadData(priority);
53                } catch (Exception e) {
54                    if (Log.isLoggable(TAG, Log.VERBOSE)) {
55                        Log.v(TAG, "Exception fetching input stream, trying ParcelFileDescriptor", e);
56                    }
57                    if (fileDescriptorFetcher == null) {
58                        throw e;
59                    }
60                }
61            }
62            ParcelFileDescriptor fileDescriptor = null;
63            if (fileDescriptorFetcher != null) {
64                try {
65                    fileDescriptor = fileDescriptorFetcher.loadData(priority);
66                } catch (Exception e) {
67                    if (Log.isLoggable(TAG, Log.VERBOSE)) {
68                        Log.v(TAG, "Exception fetching ParcelFileDescriptor", e);
69                    }
70                    if (is == null) {
71                        throw e;
72                    }
73                }
74            }
75            return new ImageVideoWrapper(is, fileDescriptor);
76        }
77
78        @Override
79        public void cleanup() {
80            //TODO: what if this throws?
81            if (streamFetcher != null) {
82                streamFetcher.cleanup();
83            }
84            if (fileDescriptorFetcher != null) {
85                fileDescriptorFetcher.cleanup();
86            }
87        }
88
89        @Override
90        public String getId() {
91            // Both the stream fetcher and the file descriptor fetcher should return the same id.
92            if (streamFetcher != null) {
93                return streamFetcher.getId();
94            } else {
95                return fileDescriptorFetcher.getId();
96            }
97        }
98
99        @Override
100        public void cancel() {
101            if (streamFetcher != null) {
102                streamFetcher.cancel();
103            }
104            if (fileDescriptorFetcher != null) {
105                fileDescriptorFetcher.cancel();
106            }
107        }
108    }
109}
110