blob: af32aa2c687509686d3b04f464074c1662f9a3d6 [file] [log] [blame]
Winson9947f1e2019-08-16 10:20:39 -07001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.content.res.loader;
18
19import android.annotation.AnyRes;
20import android.annotation.NonNull;
21import android.annotation.Nullable;
22import android.content.res.AssetManager;
23import android.content.res.Resources;
24import android.content.res.XmlResourceParser;
25import android.graphics.drawable.Drawable;
26import android.os.ParcelFileDescriptor;
27import android.util.TypedValue;
28
29import java.io.IOException;
30import java.io.InputStream;
31
32/**
33 * Exposes methods for overriding file-based resource loading from a {@link Resources}.
34 *
35 * To be used with {@link Resources#addLoader(ResourceLoader, ResourcesProvider, int)} and related
36 * methods to override resource loading.
37 *
38 * Note that this class doesn't actually contain any resource data. Non-file-based resources are
39 * loaded directly from the {@link ResourcesProvider}'s .arsc representation.
40 *
41 * An instance's methods will only be called if its corresponding {@link ResourcesProvider}'s
42 * resources table contains an entry for the resource ID being resolved,
43 * with the exception of the non-cookie variants of {@link AssetManager}'s openAsset and
44 * openNonAsset.
45 *
46 * Those methods search backwards through all {@link ResourceLoader}s and then any paths provided
47 * by the application or system.
48 *
49 * Otherwise, an ARSC that defines R.drawable.some_id must be provided if a {@link ResourceLoader}
50 * wants to point R.drawable.some_id to a different file on disk.
51 */
52public interface ResourceLoader {
53
54 /**
55 * Given the value resolved from the string pool of the {@link ResourcesProvider} passed to
56 * {@link Resources#addLoader(ResourceLoader, ResourcesProvider, int)}, return a
57 * {@link Drawable} which should be returned by the parent
58 * {@link Resources#getDrawable(int, Resources.Theme)}.
59 *
60 * @param value the resolved {@link TypedValue} before it has been converted to a Drawable
61 * object
62 * @param id the R.drawable ID this resolution is for
63 * @param density the requested density
64 * @param theme the {@link Resources.Theme} resolved under
65 * @return null if resolution should try to find an entry inside the {@link ResourcesProvider},
66 * including calling through to {@link #loadAsset(String, int)} or {@link #loadAssetFd(String)}
67 */
68 @Nullable
69 default Drawable loadDrawable(@NonNull TypedValue value, int id, int density,
70 @Nullable Resources.Theme theme) {
71 return null;
72 }
73
74 /**
75 * Given the value resolved from the string pool of the {@link ResourcesProvider} passed to
76 * {@link Resources#addLoader(ResourceLoader, ResourcesProvider, int)}, return an
77 * {@link XmlResourceParser} which should be returned by the parent
78 * {@link Resources#getDrawable(int, Resources.Theme)}.
79 *
80 * @param path the string that was found in the string pool
81 * @param id the XML ID this resolution is for, can be R.anim, R.layout, or R.xml
82 * @return null if resolution should try to find an entry inside the {@link ResourcesProvider},
83 * including calling through to {@link #loadAssetFd(String)} (String, int)}
84 */
85 @Nullable
86 default XmlResourceParser loadXmlResourceParser(@NonNull String path, @AnyRes int id) {
87 return null;
88 }
89
90 /**
91 * Given the value resolved from the string pool of the {@link ResourcesProvider} passed to
92 * {@link Resources#addLoader(ResourceLoader, ResourcesProvider, int)}, return an
93 * {@link InputStream} which should be returned when an asset is loaded by {@link AssetManager}.
94 * Assets will be loaded from a provider's root, with anything in its assets subpath prefixed
95 * with "assets/".
96 *
97 * @param path the asset path to load
98 * @param accessMode {@link AssetManager} access mode; does not have to be respected
99 * @return null if resolution should try to find an entry inside the {@link ResourcesProvider}
100 */
101 @Nullable
102 default InputStream loadAsset(@NonNull String path, int accessMode) throws IOException {
103 return null;
104 }
105
106 /**
107 * {@link ParcelFileDescriptor} variant of {@link #loadAsset(String, int)}.
108 *
109 * @param path the asset path to load
110 * @return null if resolution should try to find an entry inside the {@link ResourcesProvider}
111 */
112 @Nullable
113 default ParcelFileDescriptor loadAssetFd(@NonNull String path) throws IOException {
114 return null;
115 }
116}