1package com.bumptech.glide.load.data;
2
3import android.content.res.AssetManager;
4import android.util.Log;
5
6import com.bumptech.glide.Priority;
7
8import java.io.IOException;
9
10/**
11 * An abstract class for obtaining data for an asset path using an {@link android.content.res.AssetManager}.
12 *
13 * @param <T> The type of data obtained from the asset path (InputStream, FileDescriptor etc).
14 */
15public abstract class AssetPathFetcher<T> implements DataFetcher<T> {
16    private static final String TAG = "AssetUriFetcher";
17    private final String assetPath;
18    private final AssetManager assetManager;
19    private T data;
20
21    public AssetPathFetcher(AssetManager assetManager, String assetPath) {
22        this.assetManager = assetManager;
23        this.assetPath = assetPath;
24    }
25
26    @Override
27    public T loadData(Priority priority) throws Exception {
28        data = loadResource(assetManager, assetPath);
29        return data;
30    }
31
32    @Override
33    public void cleanup() {
34        if (data == null) {
35            return;
36        }
37        try {
38            close(data);
39        } catch (IOException e) {
40            if (Log.isLoggable(TAG, Log.VERBOSE)) {
41                Log.v(TAG, "Failed to close data", e);
42            }
43        }
44
45    }
46
47    @Override
48    public String getId() {
49        return assetPath;
50    }
51
52    @Override
53    public void cancel() {
54        // Do nothing.
55    }
56
57    /**
58     * Opens the given asset path with the given {@link android.content.res.AssetManager} and returns the conrete data
59     * type returned by the AssetManager.
60     *
61     * @param assetManager An AssetManager to use to open the given path.
62     * @param path A string path pointing to a resource in assets to open.
63     */
64    protected abstract T loadResource(AssetManager assetManager, String path) throws IOException;
65
66    /**
67     * Closes the concrete data type if necessary.
68     *
69     * @param data The data to close.
70     * @throws IOException
71     */
72    protected abstract void close(T data) throws IOException;
73}
74