@SuppressWarnings("unchecked") private <T, Z> void registerFailFactory(Class<T> failModel, Class<Z> failResource) throws Exception { DataFetcher<Z> failFetcher = mock(DataFetcher.class); doAnswer(new Util.CallDataReady<>(null)) .when(failFetcher) .loadData(isA(Priority.class), isA(DataFetcher.DataCallback.class)); when(failFetcher.getDataClass()).thenReturn(failResource); ModelLoader<T, Z> failLoader = mock(ModelLoader.class); when(failLoader.buildLoadData(isA(failModel), anyInt(), anyInt(), isA(Options.class))) .thenReturn(new ModelLoader.LoadData<>(mock(Key.class), failFetcher)); when(failLoader.handles(isA(failModel))).thenReturn(true); ModelLoaderFactory<T, Z> failFactory = mock(ModelLoaderFactory.class); when(failFactory.build(isA(MultiModelLoaderFactory.class))).thenReturn(failLoader); Glide.get(getContext()).getRegistry().prepend(failModel, failResource, failFactory); }
private static <X, Y> void registerMockModelLoader(Class<X> modelClass, Class<Y> dataClass, Y loadedData, Registry registry) { DataFetcher<Y> mockStreamFetcher = mock(DataFetcher.class); when(mockStreamFetcher.getDataClass()).thenReturn(dataClass); try { doAnswer(new Util.CallDataReady<>(loadedData)) .when(mockStreamFetcher) .loadData(isA(Priority.class), isA(DataFetcher.DataCallback.class)); } catch (Exception e) { throw new RuntimeException(e); } ModelLoader<X, Y> mockUrlLoader = mock(ModelLoader.class); when(mockUrlLoader.buildLoadData(isA(modelClass), anyInt(), anyInt(), isA(Options.class))) .thenReturn(new ModelLoader.LoadData<>(mock(Key.class), mockStreamFetcher)); when(mockUrlLoader.handles(isA(modelClass))).thenReturn(true); ModelLoaderFactory<X, Y> mockUrlLoaderFactory = mock(ModelLoaderFactory.class); when(mockUrlLoaderFactory.build(isA(MultiModelLoaderFactory.class))) .thenReturn(mockUrlLoader); registry.replace(modelClass, dataClass, mockUrlLoaderFactory); }
@SuppressWarnings("unchecked") private <T, Z> void registerFailFactory(Class<T> failModel, Class<Z> failResource) { DataFetcher<Z> failFetcher = mock(DataFetcher.class); doAnswer(new Util.CallDataReady<>(null)) .when(failFetcher) .loadData(isA(Priority.class), isA(DataFetcher.DataCallback.class)); when(failFetcher.getDataClass()).thenReturn(failResource); ModelLoader<T, Z> failLoader = mock(ModelLoader.class); when(failLoader.buildLoadData(isA(failModel), anyInt(), anyInt(), isA(Options.class))) .thenReturn(new ModelLoader.LoadData<>(mock(Key.class), failFetcher)); when(failLoader.handles(isA(failModel))).thenReturn(true); ModelLoaderFactory<T, Z> failFactory = mock(ModelLoaderFactory.class); when(failFactory.build(isA(MultiModelLoaderFactory.class))).thenReturn(failLoader); Glide.get(context).getRegistry().prepend(failModel, failResource, failFactory); }
@SuppressWarnings("unchecked") private <T> void registerMockStreamModelLoader(final Class<T> modelClass) { ModelLoader<T, InputStream> modelLoader = mockStreamModelLoader(modelClass); ModelLoaderFactory<T, InputStream> modelLoaderFactory = mock(ModelLoaderFactory.class); when(modelLoaderFactory.build(isA(MultiModelLoaderFactory.class))).thenReturn(modelLoader); Glide.get(RuntimeEnvironment.application).getRegistry() .prepend(modelClass, InputStream.class, modelLoaderFactory); }
public static synchronized <T> WaitModel<T> waitOn(T model) { @SuppressWarnings("unchecked") ModelLoaderFactory<WaitModel<T>, InputStream> streamFactory = new Factory<>((Class<T>) model.getClass(), InputStream.class); Glide.get(InstrumentationRegistry.getTargetContext()) .getRegistry() .replace(WaitModel.class, InputStream.class, streamFactory); return new WaitModel<>(model); }
@SuppressWarnings("unchecked") private <T> void registerMockStreamModelLoader(final Class<T> modelClass) { ModelLoader<T, InputStream> modelLoader = mockStreamModelLoader(modelClass); ModelLoaderFactory<T, InputStream> modelLoaderFactory = mock(ModelLoaderFactory.class); when(modelLoaderFactory.build(isA(MultiModelLoaderFactory.class))).thenReturn(modelLoader); Glide.get(context).getRegistry() .prepend(modelClass, InputStream.class, modelLoaderFactory); }
@Override protected void onResume() { super.onResume(); FileDescriptorUriLoader.Factory originalFactory = new FileDescriptorUriLoader.Factory(); ModelLoaderFactory<Uri, ParcelFileDescriptor> wrapperFactory = new ModelLoaderFactoryWrapper<>(originalFactory, this); Glide.get(this).register(Uri.class, ParcelFileDescriptor.class, wrapperFactory); }
public <Model, Data> Registry prepend(Class<Model> modelClass, Class<Data> dataClass, ModelLoaderFactory<Model, Data> factory) { modelLoaderRegistry.prepend(modelClass, dataClass, factory); return this; }
public <Model, Data> Registry replace(Class<Model> modelClass, Class<Data> dataClass, ModelLoaderFactory<Model, Data> factory) { modelLoaderRegistry.replace(modelClass, dataClass, factory); return this; }
@Override public ModelLoaderFactory<GlideUrl, InputStream> createUrlLoaderFactory() { return new VolleyUrlLoader.Factory(getRequestQueue()); }
public static ModelLoaderFactory<GlideUrl, InputStream> createUrlLoaderFactory() { if(sExecutor instanceof GlideLoaderFactory) { return ((GlideLoaderFactory)sExecutor).createUrlLoaderFactory(); } return null; }
/** * {@link Glide#register(Class, Class, ModelLoaderFactory) Register}'s params should be * {@code (Class<T>, Class<Y>, ModelLoaderFactory<? super T, Y>)}. This method works around that. */ @SuppressWarnings({"unchecked", "unused"}) private static <T> ModelLoaderFactory<T, InputStream> superFactory( ModelLoaderFactory<? super T, InputStream> factory, Class<T> modelType) { return (ModelLoaderFactory<T, InputStream>)factory; }
public ModelLoaderFactoryWrapper(ModelLoaderFactory<T, Y> wrapped, ExceptionHandler handler) { this.wrapped = wrapped; this.handler = handler; }
/** * Removes all default and previously registered {@link ModelLoaderFactory}s for the given data * and model class and replaces all of them with the single {@link ModelLoader} provided. * * <p>If you're attempting to add additional functionality or add a backup that should run only * after the default {@link ModelLoader}s run, use * {@link #append(Class, Class, ModelLoaderFactory)}. This method should be used only when you * want to ensure that Glide's default {@link ModelLoader}s do not run. * * <p>One good use case for this method is when you want to replace Glide's default networking * library with your OkHttp, Volley, or your own implementation. Using * {@link #prepend(Class, Class, ModelLoaderFactory)} or * {@link #append(Class, Class, ModelLoaderFactory)} may still allow Glide's default networking * library to run in some cases. Using this method will ensure that only your networking library * will run and that the request will fail otherwise. * * @see #prepend(Class, Class, ModelLoaderFactory) * @see #append(Class, Class, ModelLoaderFactory) * * @param modelClass The model class (e.g. URL, file path). * @param dataClass the data class (e.g. {@link java.io.InputStream}, * {@link java.io.FileDescriptor}). */ public <Model, Data> Registry replace( Class<Model> modelClass, Class<Data> dataClass, ModelLoaderFactory<? extends Model, ? extends Data> factory) { modelLoaderRegistry.replace(modelClass, dataClass, factory); return this; }
/** * Use the given factory to build a {@link ModelLoader} for models of the given class. Generally the best use of * this method is to replace one of the default factories or add an implementation for other similar low level * models. Typically the {@link ModelRequest#using(StreamModelLoader)} or * {@link ModelRequest#using(FileDescriptorModelLoader)} syntax is preferred because it directly links the model * with the ModelLoader being used to load it. Any factory replaced by the given factory will have its * {@link ModelLoaderFactory#teardown()}} method called. * * <p> * Note - If a factory already exists for the given class, it will be replaced. If that factory is not being * used for any other model class, {@link ModelLoaderFactory#teardown()} * will be called. * </p> * * <p> * Note - The factory must not be an anonymous inner class of an Activity or another object that cannot be * retained statically. * </p> * * @see ModelRequest#using(FileDescriptorModelLoader) * @see ModelRequest#using(StreamModelLoader) * * @param modelClass The model class. * @param resourceClass The resource class the model loader will translate the model type into. * @param factory The factory to use. * @param <T> The type of the model. * @param <Y> the type of the resource. */ public <T, Y> void register(Class<T> modelClass, Class<Y> resourceClass, ModelLoaderFactory<T, Y> factory) { ModelLoaderFactory<T, Y> removed = loaderFactory.register(modelClass, resourceClass, factory); if (removed != null) { removed.teardown(); } }
/** * Removes any {@link ModelLoaderFactory} registered for the given model and resource classes if one exists. If a * {@link ModelLoaderFactory} is removed, its {@link ModelLoaderFactory#teardown()}} method will be called. * * @param modelClass The model class. * @param resourceClass The resource class. * @param <T> The type of the model. * @param <Y> The type of the resource. */ public <T, Y> void unregister(Class<T> modelClass, Class<Y> resourceClass) { ModelLoaderFactory<T, Y> removed = loaderFactory.unregister(modelClass, resourceClass); if (removed != null) { removed.teardown(); } }
/** * Use the given factory to build a {@link com.bumptech.glide.load.model.ModelLoader} for models * of the given class. Generally the best use of this method is to replace one of the default * factories or add an implementation for other similar low level models. Any factory replaced by * the given factory will have its {@link ModelLoaderFactory#teardown()}} method called. * * <p> Note - If a factory already exists for the given class, it will be replaced. If that * factory is not being used for any other model class, {@link ModelLoaderFactory#teardown()} will * be called. </p> * * <p> Note - The factory must not be an anonymous inner class of an Activity or another object * that cannot be retained statically. </p> * * @param modelClass The model class. * @param dataClass the data class. */ public <Model, Data> Registry append(Class<Model> modelClass, Class<Data> dataClass, ModelLoaderFactory<Model, Data> factory) { modelLoaderRegistry.append(modelClass, dataClass, factory); return this; }
/** * Appends a new {@link ModelLoaderFactory} onto the end of the existing set so that the * constructed {@link ModelLoader} will be tried after all default and previously registered * {@link ModelLoader}s for the given model and data classes. * * <p>If you're attempting to replace an existing {@link ModelLoader}, use * {@link #prepend(Class, Class, ModelLoaderFactory)}. This method is best for new types of models * and/or data or as a way to add an additional fallback loader for an existing type of * model/data. * * <p>If multiple {@link ModelLoaderFactory}s are registered for the same model and/or data * classes, the {@link ModelLoader}s they produce will be attempted in the order the * {@link ModelLoaderFactory}s were registered. Only if all {@link ModelLoader}s fail will the * entire request fail. * * @see #prepend(Class, Class, ModelLoaderFactory) * @see #replace(Class, Class, ModelLoaderFactory) * * @param modelClass The model class (e.g. URL, file path). * @param dataClass the data class (e.g. {@link java.io.InputStream}, * {@link java.io.FileDescriptor}). */ public <Model, Data> Registry append(Class<Model> modelClass, Class<Data> dataClass, ModelLoaderFactory<Model, Data> factory) { modelLoaderRegistry.append(modelClass, dataClass, factory); return this; }
/** * Prepends a new {@link ModelLoaderFactory} onto the beginning of the existing set so that the * constructed {@link ModelLoader} will be tried before all default and previously registered * {@link ModelLoader}s for the given model and data classes. * * <p>If you're attempting to add additional functionality or add a backup that should run only * after the default {@link ModelLoader}s run, use * {@link #append(Class, Class, ModelLoaderFactory)}. This method is best for adding an additional * case to Glide's existing functionality that should run first. This method will still run * Glide's default {@link ModelLoader}s if the prepended {@link ModelLoader}s fail. * * <p>If multiple {@link ModelLoaderFactory}s are registered for the same model and/or data * classes, the {@link ModelLoader}s they produce will be attempted in the order the * {@link ModelLoaderFactory}s were registered. Only if all {@link ModelLoader}s fail will the * entire request fail. * * @see #append(Class, Class, ModelLoaderFactory) * @see #replace(Class, Class, ModelLoaderFactory) * * @param modelClass The model class (e.g. URL, file path). * @param dataClass the data class (e.g. {@link java.io.InputStream}, * {@link java.io.FileDescriptor}). */ public <Model, Data> Registry prepend(Class<Model> modelClass, Class<Data> dataClass, ModelLoaderFactory<Model, Data> factory) { modelLoaderRegistry.prepend(modelClass, dataClass, factory); return this; }
ModelLoaderFactory<GlideUrl, InputStream> createUrlLoaderFactory();