aboutsummaryrefslogtreecommitdiff
path: root/library/src/main/java/com/bumptech/glide/GenericTranscodeRequest.java
blob: 4fa45389b7c29f984046605098c8dd538a2f1e12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.bumptech.glide;

import android.content.Context;

import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.model.ModelLoader;
import com.bumptech.glide.load.resource.transcode.ResourceTranscoder;
import com.bumptech.glide.load.resource.transcode.UnitTranscoder;
import com.bumptech.glide.manager.RequestTracker;
import com.bumptech.glide.provider.DataLoadProvider;
import com.bumptech.glide.provider.FixedLoadProvider;
import com.bumptech.glide.provider.LoadProvider;
import com.bumptech.glide.request.FutureTarget;
import com.bumptech.glide.request.target.Target;

import java.io.File;

/**
 * A class for handling requests to load a generic resource type or transcode the generic resource type into another
 * generic resource type.
 *
 * @param <A> The type of the model used to retrieve data.
 * @param <T> The type of data retrieved.
 * @param <Z> The type of resource to be decoded from the the data.
 */
public class GenericTranscodeRequest<A, T, Z> extends GenericRequestBuilder<A, T, Z, Z> implements DownloadOptions {
    private final Context context;
    private final A model;
    private final Glide glide;
    private final ModelLoader<A, T> modelLoader;
    private final Class<T> dataClass;
    private final Class<Z> resourceClass;
    private final RequestTracker requestTracker;
    private final RequestManager.OptionsApplier optionsApplier;

    private static <A, T, Z, R> LoadProvider<A, T, Z, R> build(Glide glide, ModelLoader<A, T> modelLoader,
            Class<T> dataClass, Class<Z> resourceClass, ResourceTranscoder<Z, R> transcoder) {
        if (transcoder == null) {
            transcoder = UnitTranscoder.get();
        }
        DataLoadProvider<T, Z> dataLoadProvider = glide.buildDataProvider(dataClass, resourceClass);
        return new FixedLoadProvider<A, T, Z, R>(modelLoader, transcoder, dataLoadProvider);
    }

    GenericTranscodeRequest(Context context, Glide glide, A model, ModelLoader<A, T> modelLoader,
            Class<T> dataClass, Class<Z> resourceClass, RequestTracker requestTracker,
            RequestManager.OptionsApplier optionsApplier) {
        super(context, model,
                build(glide, modelLoader, dataClass, resourceClass, (ResourceTranscoder<Z, Z>) null),
                resourceClass, glide, requestTracker);
        this.context = context;
        this.model = model;
        this.glide = glide;
        this.modelLoader = modelLoader;
        this.dataClass = dataClass;
        this.resourceClass = resourceClass;
        this.requestTracker = requestTracker;
        this.optionsApplier = optionsApplier;
    }

    /**
     * Adds a transcoder to this request to transcode from the resource type to the given transcode type.
     *
     * @param transcoder The transcoder to use.
     * @param transcodeClass The class of the resource type that will be transcoded to.
     * @param <R> The type of the resource that will be transcoded to.
     * @return A new request builder to set options for the transcoded load.
     */
    public <R> GenericRequestBuilder<A, T, Z, R> transcode(ResourceTranscoder<Z, R> transcoder,
            Class<R> transcodeClass) {
        return optionsApplier.apply(model, new GenericRequestBuilder<A, T, Z, R>(context, model,
                build(glide, modelLoader, dataClass, resourceClass, transcoder), transcodeClass, glide,
                requestTracker));
    }

    /**
     * Loads the original unmodified data into the cache and calls the given Target with the cache File.
     *
     * @param target The Target that will receive the cache File when the load completes
     * @param <Y> The type of Target.
     * @return The given Target.
     */
    public <Y extends Target<File>> Y downloadOnly(Y target) {
        return getDownloadOnlyRequest().into(target);
    }

    /**
     * Loads the original unmodified data into the cache and returns a {@link java.util.concurrent.Future} that can be
     * used to retrieve the cache File containing the data.
     *
     * @param width The width to use to fetch the data.
     * @param height The height to use to fetch the data.
     * @return A {@link java.util.concurrent.Future} that can be used to retrieve the cache File containing the data.
     */
    public FutureTarget<File> downloadOnly(int width, int height) {
        return getDownloadOnlyRequest().into(width, height);
    }

    private GenericRequestBuilder<A, T, File, File> getDownloadOnlyRequest() {
        ResourceTranscoder<File, File> transcoder = UnitTranscoder.get();
        DataLoadProvider<T, File> dataLoadProvider = glide.buildDataProvider(dataClass, File.class);
        FixedLoadProvider<A, T, File, File> fixedLoadProvider = new FixedLoadProvider<A, T, File, File>(modelLoader,
                transcoder, dataLoadProvider);
        return optionsApplier.apply(model, new GenericRequestBuilder<A, T, File, File>(context, model,
                fixedLoadProvider,
                File.class, glide,
                requestTracker)
                .priority(Priority.LOW)
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .skipMemoryCache(true));

    }
}