blob: 592ec09aa730c7149941f534330e18182d8a6cbe [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.Nullable;
20import android.content.res.ApkAssets;
21import android.content.res.AssetManager;
22import android.content.res.Resources;
23import android.content.res.ResourcesImpl;
24import android.util.Pair;
25
26import com.android.internal.annotations.GuardedBy;
27import com.android.internal.util.ArrayUtils;
28
29import java.util.ArrayList;
30import java.util.List;
31import java.util.Objects;
32
33/**
34 * @hide
35 */
36public class ResourceLoaderManager {
37
38 private final Object mLock = new Object();
39
40 @GuardedBy("mLock")
41 private final List<Pair<ResourceLoader, ResourcesProvider>> mResourceLoaders =
42 new ArrayList<>();
43
44 @GuardedBy("mLock")
45 private ResourcesImpl mResourcesImpl;
46
47 public ResourceLoaderManager(ResourcesImpl resourcesImpl) {
48 this.mResourcesImpl = resourcesImpl;
49 this.mResourcesImpl.getAssets().setResourceLoaderManager(this);
50 }
51
52 /**
53 * Copies the list to ensure that ongoing mutations don't affect the list if it's being used
54 * as a search set.
55 *
56 * @see Resources#getLoaders()
57 */
58 public List<Pair<ResourceLoader, ResourcesProvider>> getLoaders() {
59 synchronized (mLock) {
60 return new ArrayList<>(mResourceLoaders);
61 }
62 }
63
64 /**
65 * Returns a list for searching for a loader. Locks and copies the list to ensure that
66 * ongoing mutations don't affect the search set.
67 */
68 public List<Pair<ResourceLoader, ResourcesProvider>> getInternalList() {
69 synchronized (mLock) {
70 return new ArrayList<>(mResourceLoaders);
71 }
72 }
73
74 /**
75 * TODO(b/136251855): Consider optional boolean ignoreConfigurations to allow ResourceLoader
76 * to override every configuration in the target package
77 *
78 * @see Resources#addLoader(ResourceLoader, ResourcesProvider)
79 */
80 public void addLoader(ResourceLoader resourceLoader, ResourcesProvider resourcesProvider,
81 int index) {
82 synchronized (mLock) {
83 for (int listIndex = 0; listIndex < mResourceLoaders.size(); listIndex++) {
84 if (Objects.equals(mResourceLoaders.get(listIndex).first, resourceLoader)) {
85 throw new IllegalArgumentException("Cannot add the same ResourceLoader twice");
86 }
87 }
88
89 mResourceLoaders.add(index, Pair.create(resourceLoader, resourcesProvider));
90 updateLoaders();
91 }
92 }
93
94 /**
95 * @see Resources#removeLoader(ResourceLoader)
96 */
97 public int removeLoader(ResourceLoader resourceLoader) {
98 synchronized (mLock) {
99 int indexOfLoader = -1;
100
101 for (int index = 0; index < mResourceLoaders.size(); index++) {
102 if (mResourceLoaders.get(index).first == resourceLoader) {
103 indexOfLoader = index;
104 break;
105 }
106 }
107
108 if (indexOfLoader < 0) {
109 return indexOfLoader;
110 }
111
112 mResourceLoaders.remove(indexOfLoader);
113 updateLoaders();
114 return indexOfLoader;
115 }
116 }
117
118 /**
119 * @see Resources#setLoaders(List)
120 */
121 public void setLoaders(
122 @Nullable List<Pair<ResourceLoader, ResourcesProvider>> newLoadersAndProviders) {
123 synchronized (mLock) {
124 if (ArrayUtils.isEmpty(newLoadersAndProviders)) {
125 mResourceLoaders.clear();
126 updateLoaders();
127 return;
128 }
129
130 int size = newLoadersAndProviders.size();
131 for (int newIndex = 0; newIndex < size; newIndex++) {
132 ResourceLoader resourceLoader = newLoadersAndProviders.get(newIndex).first;
133 for (int oldIndex = 0; oldIndex < mResourceLoaders.size(); oldIndex++) {
134 if (Objects.equals(mResourceLoaders.get(oldIndex).first, resourceLoader)) {
135 throw new IllegalArgumentException(
136 "Cannot add the same ResourceLoader twice");
137 }
138 }
139 }
140
141 mResourceLoaders.clear();
142 mResourceLoaders.addAll(newLoadersAndProviders);
143
144 updateLoaders();
145 }
146 }
147
148 /**
149 * Swap the tracked {@link ResourcesImpl} and reattach any loaders to it.
150 */
151 public void onImplUpdate(ResourcesImpl resourcesImpl) {
152 synchronized (mLock) {
153 this.mResourcesImpl = resourcesImpl;
Winsonc932ff22019-12-05 16:42:54 -0800154 this.mResourcesImpl.getAssets().setResourceLoaderManager(this);
Winson9947f1e2019-08-16 10:20:39 -0700155 updateLoaders();
156 }
157 }
158
159 private void updateLoaders() {
160 synchronized (mLock) {
161 AssetManager assetManager = mResourcesImpl.getAssets();
162 ApkAssets[] existingApkAssets = assetManager.getApkAssets();
163 int baseApkAssetsSize = 0;
164 for (int index = existingApkAssets.length - 1; index >= 0; index--) {
165 // Loaders are always last, so the first non-loader is the end of the base assets
166 if (!existingApkAssets[index].isForLoader()) {
167 baseApkAssetsSize = index + 1;
168 break;
169 }
170 }
171
172 List<ApkAssets> newAssets = new ArrayList<>();
173 for (int index = 0; index < baseApkAssetsSize; index++) {
174 newAssets.add(existingApkAssets[index]);
175 }
176
177 int size = mResourceLoaders.size();
178 for (int index = 0; index < size; index++) {
179 ApkAssets apkAssets = mResourceLoaders.get(index).second.getApkAssets();
180 newAssets.add(apkAssets);
181 }
182
183 assetManager.setApkAssets(newAssets.toArray(new ApkAssets[0]), true);
184
185 // Short of resolving every resource, it's too difficult to determine what has changed
186 // when a resource loader is changed, so just clear everything.
187 mResourcesImpl.clearAllCaches();
188 }
189 }
190}