package com.bumptech.glide.load.model; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.util.Pools.Pool; import com.bumptech.glide.Priority; import com.bumptech.glide.load.DataSource; import com.bumptech.glide.load.Key; import com.bumptech.glide.load.Options; import com.bumptech.glide.load.data.DataFetcher; import com.bumptech.glide.load.data.DataFetcher.DataCallback; import com.bumptech.glide.load.engine.GlideException; import com.bumptech.glide.util.Preconditions; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * Allows attempting multiple ModelLoaders registered for a given model and data class. * *

TODO: we should try to find a way to remove this class. It exists to allow individual * ModelLoaders to delegate to multiple ModelLoaders without having to duplicate this logic * everywhere. We have very similar logic in the {@link * com.bumptech.glide.load.engine.DataFetcherGenerator} implementations and should try to avoid this * duplication.

*/ class MultiModelLoader implements ModelLoader { private final List> modelLoaders; private final Pool> exceptionListPool; MultiModelLoader(List> modelLoaders, Pool> exceptionListPool) { this.modelLoaders = modelLoaders; this.exceptionListPool = exceptionListPool; } @Override public LoadData buildLoadData(Model model, int width, int height, Options options) { Key sourceKey = null; int size = modelLoaders.size(); List> fetchers = new ArrayList<>(size); for (int i = 0; i < size; i++) { ModelLoader modelLoader = modelLoaders.get(i); if (modelLoader.handles(model)) { LoadData loadData = modelLoader.buildLoadData(model, width, height, options); if (loadData != null) { sourceKey = loadData.sourceKey; fetchers.add(loadData.fetcher); } } } return !fetchers.isEmpty() ? new LoadData<>(sourceKey, new MultiFetcher<>(fetchers, exceptionListPool)) : null; } @Override public boolean handles(Model model) { for (ModelLoader modelLoader : modelLoaders) { if (modelLoader.handles(model)) { return true; } } return false; } @Override public String toString() { return "MultiModelLoader{" + "modelLoaders=" + Arrays .toString(modelLoaders.toArray(new ModelLoader[modelLoaders.size()])) + '}'; } static class MultiFetcher implements DataFetcher, DataCallback { private final List> fetchers; private final Pool> throwableListPool; private int currentIndex; private Priority priority; private DataCallback callback; @Nullable private List exceptions; MultiFetcher(List> fetchers, Pool> throwableListPool) { this.throwableListPool = throwableListPool; Preconditions.checkNotEmpty(fetchers); this.fetchers = fetchers; currentIndex = 0; } @Override public void loadData(Priority priority, DataCallback callback) { this.priority = priority; this.callback = callback; exceptions = throwableListPool.acquire(); fetchers.get(currentIndex).loadData(priority, this); } @Override public void cleanup() { if (exceptions != null) { throwableListPool.release(exceptions); } exceptions = null; for (DataFetcher fetcher : fetchers) { fetcher.cleanup(); } } @Override public void cancel() { for (DataFetcher fetcher : fetchers) { fetcher.cancel(); } } @NonNull @Override public Class getDataClass() { return fetchers.get(0).getDataClass(); } @NonNull @Override public DataSource getDataSource() { return fetchers.get(0).getDataSource(); } @Override public void onDataReady(Data data) { if (data != null) { callback.onDataReady(data); } else { startNextOrFail(); } } @Override public void onLoadFailed(Exception e) { exceptions.add(e); startNextOrFail(); } private void startNextOrFail() { if (currentIndex < fetchers.size() - 1) { currentIndex++; loadData(priority, callback); } else { callback.onLoadFailed(new GlideException("Fetch failed", new ArrayList<>(exceptions))); } } } }