package com.bumptech.glide.load.model;
import android.os.ParcelFileDescriptor;
import android.util.Log;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.data.DataFetcher;
import java.io.InputStream;
public class ImageVideoModelLoader implements ModelLoader {
private static final String TAG = "IVML";
private final ModelLoader streamLoader;
private final ModelLoader fileDescriptorLoader;
public ImageVideoModelLoader(ModelLoader streamLoader,
ModelLoader fileDescriptorLoader) {
if (streamLoader == null && fileDescriptorLoader == null) {
throw new NullPointerException("At least one of streamLoader and fileDescriptorLoader must be non null");
}
this.streamLoader = streamLoader;
this.fileDescriptorLoader = fileDescriptorLoader;
}
@Override
public DataFetcher getResourceFetcher(A model, int width, int height) {
DataFetcher streamFetcher = null;
if (streamLoader != null) {
streamFetcher = streamLoader.getResourceFetcher(model, width, height);
}
DataFetcher fileDescriptorFetcher = null;
if (fileDescriptorLoader != null) {
fileDescriptorFetcher = fileDescriptorLoader.getResourceFetcher(model, width, height);
}
return new ImageVideoFetcher(streamFetcher, fileDescriptorFetcher);
}
public static class ImageVideoFetcher implements DataFetcher {
private final DataFetcher streamFetcher;
private final DataFetcher fileDescriptorFetcher;
public ImageVideoFetcher(DataFetcher streamFetcher,
DataFetcher fileDescriptorFetcher) {
this.streamFetcher = streamFetcher;
this.fileDescriptorFetcher = fileDescriptorFetcher;
}
@Override
public ImageVideoWrapper loadData(Priority priority) throws Exception {
InputStream is = null;
if (streamFetcher != null) {
try {
is = streamFetcher.loadData(priority);
} catch (Exception e) {
if (Log.isLoggable(TAG, Log.VERBOSE)) {
Log.v(TAG, "Exception fetching input stream, trying ParcelFileDescriptor", e);
}
if (fileDescriptorFetcher == null) {
throw e;
}
}
}
ParcelFileDescriptor fileDescriptor = null;
if (fileDescriptorFetcher != null) {
try {
fileDescriptor = fileDescriptorFetcher.loadData(priority);
} catch (Exception e) {
if (Log.isLoggable(TAG, Log.VERBOSE)) {
Log.v(TAG, "Exception fetching ParcelFileDescriptor", e);
}
if (is == null) {
throw e;
}
}
}
return new ImageVideoWrapper(is, fileDescriptor);
}
@Override
public void cleanup() {
//TODO: what if this throws?
if (streamFetcher != null) {
streamFetcher.cleanup();
}
if (fileDescriptorFetcher != null) {
fileDescriptorFetcher.cleanup();
}
}
@Override
public String getId() {
// Both the stream fetcher and the file descriptor fetcher should return the same id.
if (streamFetcher != null) {
return streamFetcher.getId();
} else {
return fileDescriptorFetcher.getId();
}
}
@Override
public void cancel() {
if (streamFetcher != null) {
streamFetcher.cancel();
}
if (fileDescriptorFetcher != null) {
fileDescriptorFetcher.cancel();
}
}
}
}