/** * load cache image with Glide */ private void loadCache(Context ctx, ImageLoader img) { Glide.with(ctx).using(new StreamModelLoader<String>() { @Override public DataFetcher<InputStream> getResourceFetcher(final String model, int i, int i1) { return new DataFetcher<InputStream>() { @Override public InputStream loadData(Priority priority) throws Exception { throw new IOException(); } @Override public void cleanup() { } @Override public String getId() { return model; } @Override public void cancel() { } }; } }).load(img.getUrl()).placeholder(img.getPlaceHolder()).diskCacheStrategy(DiskCacheStrategy.ALL).into(img.getImgView()); }
private void clearCacheSyncAndHackNetwork() { // TODO only for testing: clear all caches before anything is loaded to always have a clean slate Glide.get(TestActivity.this).clearMemory(); final CountDownLatch latch = new CountDownLatch(1); new Thread() { @Override public void run() { Glide.get(TestActivity.this).clearDiskCache(); latch.countDown(); } }.start(); try { latch.await(); // never do this in production } catch (InterruptedException e) { e.printStackTrace(); } // TODO only for debug: override default Url handler to fail sometimes (50%) Glide.get(this).register(GlideUrl.class, InputStream.class, new ModelLoaderFactory<GlideUrl, InputStream>() { Random random = new Random(0); @Override public ModelLoader<GlideUrl, InputStream> build(Context context, GenericLoaderFactory factories) { return new StreamModelLoader<GlideUrl>() { @Override public DataFetcher<InputStream> getResourceFetcher(GlideUrl url, int width, int height) { return random.nextBoolean()? new HttpUrlFetcher(url) : new NetworkDisablingFetcher(url); } }; } @Override public void teardown() { } }); }
@Override public StreamModelLoader<DecryptableUri> build(Context context, GenericLoaderFactory factories) { return new DecryptableStreamUriLoader(context); }
@Override public StreamModelLoader<AttachmentModel> build(Context context, GenericLoaderFactory factories) { return new AttachmentStreamUriLoader(context); }
@Override public StreamModelLoader<ContactPhotoUri> build(Context context, GenericLoaderFactory factories) { return new ContactPhotoUriLoader(context); }
@Override public StreamModelLoader<Attachment> build(Context context, GenericLoaderFactory factories) { return new AttachmentLoader(context); }
/** * Set the {@link ModelLoader} to use for for a new load where the model loader translates from a model to an * {@link InputStream} resource for loading images. * * @param modelLoader The model loader to use. * @param <T> The type of the model. * @return A new {@link ImageModelRequest}. */ public <T> ImageModelRequest<T> using(final StreamModelLoader<T> modelLoader) { return new ImageModelRequest<T>(context, modelLoader, glide); }