aboutsummaryrefslogtreecommitdiff
path: root/library/src/main/java/com/bumptech/glide/load/data/AssetPathFetcher.java
blob: 7c9e62293ea41e9d9ac737ab2baaf2189f6f5709 (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
package com.bumptech.glide.load.data;

import android.content.res.AssetManager;
import android.util.Log;

import com.bumptech.glide.Priority;

import java.io.IOException;

/**
 * An abstract class for obtaining data for an asset path using an {@link android.content.res.AssetManager}.
 *
 * @param <T> The type of data obtained from the asset path (InputStream, FileDescriptor etc).
 */
public abstract class AssetPathFetcher<T> implements DataFetcher<T> {
    private static final String TAG = "AssetUriFetcher";
    private final String assetPath;
    private final AssetManager assetManager;
    private T data;

    public AssetPathFetcher(AssetManager assetManager, String assetPath) {
        this.assetManager = assetManager;
        this.assetPath = assetPath;
    }

    @Override
    public T loadData(Priority priority) throws Exception {
        data = loadResource(assetManager, assetPath);
        return data;
    }

    @Override
    public void cleanup() {
        if (data == null) {
            return;
        }
        try {
            close(data);
        } catch (IOException e) {
            if (Log.isLoggable(TAG, Log.VERBOSE)) {
                Log.v(TAG, "Failed to close data", e);
            }
        }

    }

    @Override
    public String getId() {
        return assetPath;
    }

    @Override
    public void cancel() {
        // Do nothing.
    }

    /**
     * Opens the given asset path with the given {@link android.content.res.AssetManager} and returns the conrete data
     * type returned by the AssetManager.
     *
     * @param assetManager An AssetManager to use to open the given path.
     * @param path A string path pointing to a resource in assets to open.
     */
    protected abstract T loadResource(AssetManager assetManager, String path) throws IOException;

    /**
     * Closes the concrete data type if necessary.
     *
     * @param data The data to close.
     * @throws IOException
     */
    protected abstract void close(T data) throws IOException;
}