blob: 78665609bdd406ec92ea5baff97b266b968879e9 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 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;
18
Alan Viverettee273b932016-01-05 10:25:54 -050019import android.annotation.AnyRes;
20import android.annotation.ArrayRes;
21import android.annotation.NonNull;
22import android.annotation.Nullable;
23import android.annotation.StringRes;
Alan Viverette9ad386b2017-01-26 14:00:20 -050024import android.content.pm.ActivityInfo;
Alan Viveretteac85f902016-03-11 15:15:51 -050025import android.content.res.Configuration.NativeConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.os.ParcelFileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.util.Log;
Adam Lesinskide898ff2014-01-29 18:20:45 -080028import android.util.SparseArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.util.TypedValue;
30
Adam Lesinski7fb38312018-01-23 03:17:26 -080031import java.io.FileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import java.io.FileNotFoundException;
33import java.io.IOException;
34import java.io.InputStream;
Dianne Hackbornc3b91fd2010-02-23 17:25:30 -080035import java.util.HashMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036
37/**
38 * Provides access to an application's raw asset files; see {@link Resources}
39 * for the way most applications will want to retrieve their resource data.
40 * This class presents a lower-level API that allows you to open and read raw
41 * files that have been bundled with the application as a simple stream of
42 * bytes.
43 */
Jeff Sharkeyda96e132014-07-15 14:54:09 -070044public final class AssetManager implements AutoCloseable {
Adam Lesinski7fb38312018-01-23 03:17:26 -080045 /* modes used when opening an asset */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046
47 /**
48 * Mode for {@link #open(String, int)}: no specific information about how
49 * data will be accessed.
50 */
51 public static final int ACCESS_UNKNOWN = 0;
52 /**
53 * Mode for {@link #open(String, int)}: Read chunks, and seek forward and
54 * backward.
55 */
56 public static final int ACCESS_RANDOM = 1;
57 /**
58 * Mode for {@link #open(String, int)}: Read sequentially, with an
59 * occasional forward seek.
60 */
61 public static final int ACCESS_STREAMING = 2;
62 /**
63 * Mode for {@link #open(String, int)}: Attempt to load contents into
64 * memory, for fast small reads.
65 */
66 public static final int ACCESS_BUFFER = 3;
67
Adam Lesinski7fb38312018-01-23 03:17:26 -080068 private static final String TAG = "AssetManager";
69 private static final boolean localLOGV = false || false;
70
71 private static final boolean DEBUG_REFS = false;
72
73 private static final Object sSync = new Object();
74 /*package*/ static AssetManager sSystem = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075
Adam Lesinski7fb38312018-01-23 03:17:26 -080076 private final TypedValue mValue = new TypedValue();
77 private final long[] mOffsets = new long[2];
78
79 // For communication with native code.
80 private long mObject;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081
Adam Lesinski7fb38312018-01-23 03:17:26 -080082 private StringBlock mStringBlocks[] = null;
83
84 private int mNumRefs = 1;
85 private boolean mOpen = true;
86 private HashMap<Long, RuntimeException> mRefStacks;
87
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 /**
89 * Create a new AssetManager containing only the basic system assets.
90 * Applications will not generally use this method, instead retrieving the
91 * appropriate asset manager with {@link Resources#getAssets}. Not for
92 * use by applications.
Adam Lesinski7fb38312018-01-23 03:17:26 -080093 * {@hide}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 */
95 public AssetManager() {
Adam Lesinski7fb38312018-01-23 03:17:26 -080096 synchronized (this) {
97 if (DEBUG_REFS) {
98 mNumRefs = 0;
99 incRefsLocked(this.hashCode());
Dianne Hackbornc3b91fd2010-02-23 17:25:30 -0800100 }
Adam Lesinski7fb38312018-01-23 03:17:26 -0800101 init(false);
102 if (localLOGV) Log.v(TAG, "New asset manager: " + this);
103 ensureSystemAssets();
Dianne Hackbornc3b91fd2010-02-23 17:25:30 -0800104 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 }
106
Adam Lesinski7fb38312018-01-23 03:17:26 -0800107 private static void ensureSystemAssets() {
108 synchronized (sSync) {
109 if (sSystem == null) {
110 AssetManager system = new AssetManager(true);
111 system.makeStringBlocks(null);
112 sSystem = system;
113 }
114 }
115 }
116
117 private AssetManager(boolean isSystem) {
118 if (DEBUG_REFS) {
119 synchronized (this) {
120 mNumRefs = 0;
121 incRefsLocked(this.hashCode());
122 }
123 }
124 init(true);
125 if (localLOGV) Log.v(TAG, "New asset manager: " + this);
126 }
127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 /**
129 * Return a global shared asset manager that provides access to only
130 * system assets (no application assets).
Adam Lesinski7fb38312018-01-23 03:17:26 -0800131 * {@hide}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 */
133 public static AssetManager getSystem() {
Adam Lesinski7fb38312018-01-23 03:17:26 -0800134 ensureSystemAssets();
135 return sSystem;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 }
137
138 /**
139 * Close this asset manager.
140 */
141 public void close() {
Adam Lesinski7fb38312018-01-23 03:17:26 -0800142 synchronized(this) {
143 //System.out.println("Release: num=" + mNumRefs
144 // + ", released=" + mReleased);
145 if (mOpen) {
146 mOpen = false;
147 decRefsLocked(this.hashCode());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 }
151
152 /**
Adam Lesinskib20a0ce2017-01-23 12:58:11 -0800153 * Retrieves the string value associated with a particular resource
154 * identifier for the current configuration.
155 *
156 * @param resId the resource identifier to load
157 * @return the string value, or {@code null}
158 */
Adam Lesinski7fb38312018-01-23 03:17:26 -0800159 @Nullable
160 final CharSequence getResourceText(@StringRes int resId) {
Adam Lesinskib20a0ce2017-01-23 12:58:11 -0800161 synchronized (this) {
162 final TypedValue outValue = mValue;
163 if (getResourceValue(resId, 0, outValue, true)) {
164 return outValue.coerceToString();
165 }
166 return null;
167 }
168 }
169
170 /**
171 * Retrieves the string value associated with a particular resource
172 * identifier for the current configuration.
173 *
174 * @param resId the resource identifier to load
Adam Lesinski7fb38312018-01-23 03:17:26 -0800175 * @param bagEntryId
Adam Lesinskib20a0ce2017-01-23 12:58:11 -0800176 * @return the string value, or {@code null}
177 */
Adam Lesinski7fb38312018-01-23 03:17:26 -0800178 @Nullable
179 final CharSequence getResourceBagText(@StringRes int resId, int bagEntryId) {
Adam Lesinskib20a0ce2017-01-23 12:58:11 -0800180 synchronized (this) {
Adam Lesinskib20a0ce2017-01-23 12:58:11 -0800181 final TypedValue outValue = mValue;
Adam Lesinski7fb38312018-01-23 03:17:26 -0800182 final int block = loadResourceBagValue(resId, bagEntryId, outValue, true);
183 if (block < 0) {
Adam Lesinskib20a0ce2017-01-23 12:58:11 -0800184 return null;
185 }
186
187 // Convert the changing configurations flags populated by native code.
188 outValue.changingConfigurations = ActivityInfo.activityInfoConfigNativeToJava(
189 outValue.changingConfigurations);
190
191 if (outValue.type == TypedValue.TYPE_STRING) {
Adam Lesinski7fb38312018-01-23 03:17:26 -0800192 return mStringBlocks[block].get(outValue.data);
Adam Lesinskib20a0ce2017-01-23 12:58:11 -0800193 }
194 return outValue.coerceToString();
195 }
196 }
197
Adam Lesinskib20a0ce2017-01-23 12:58:11 -0800198 /**
199 * Retrieves the string array associated with a particular resource
200 * identifier for the current configuration.
201 *
202 * @param resId the resource identifier of the string array
203 * @return the string array, or {@code null}
204 */
Adam Lesinski7fb38312018-01-23 03:17:26 -0800205 @Nullable
206 final String[] getResourceStringArray(@ArrayRes int resId) {
207 return getArrayStringResource(resId);
208 }
209
210 /**
211 * Populates {@code outValue} with the data associated a particular
212 * resource identifier for the current configuration.
213 *
214 * @param resId the resource identifier to load
215 * @param densityDpi the density bucket for which to load the resource
216 * @param outValue the typed value in which to put the data
217 * @param resolveRefs {@code true} to resolve references, {@code false}
218 * to leave them unresolved
219 * @return {@code true} if the data was loaded into {@code outValue},
220 * {@code false} otherwise
221 */
222 final boolean getResourceValue(@AnyRes int resId, int densityDpi, @NonNull TypedValue outValue,
223 boolean resolveRefs) {
Adam Lesinskib20a0ce2017-01-23 12:58:11 -0800224 synchronized (this) {
Adam Lesinski7fb38312018-01-23 03:17:26 -0800225 final int block = loadResourceValue(resId, (short) densityDpi, outValue, resolveRefs);
226 if (block < 0) {
227 return false;
228 }
229
230 // Convert the changing configurations flags populated by native code.
231 outValue.changingConfigurations = ActivityInfo.activityInfoConfigNativeToJava(
232 outValue.changingConfigurations);
233
234 if (outValue.type == TypedValue.TYPE_STRING) {
235 outValue.string = mStringBlocks[block].get(outValue.data);
236 }
237 return true;
Adam Lesinskib20a0ce2017-01-23 12:58:11 -0800238 }
239 }
240
241 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 * Retrieve the text array associated with a particular resource
243 * identifier.
Alan Viverettee273b932016-01-05 10:25:54 -0500244 *
245 * @param resId the resource id of the string array
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 */
Adam Lesinski7fb38312018-01-23 03:17:26 -0800247 final @Nullable CharSequence[] getResourceTextArray(@ArrayRes int resId) {
Johan Redestig22723ff2016-08-10 14:57:27 +0200248 synchronized (this) {
Adam Lesinski7fb38312018-01-23 03:17:26 -0800249 final int[] rawInfoArray = getArrayStringInfo(resId);
Adam Lesinskiba9576d2017-03-16 17:42:04 -0700250 if (rawInfoArray == null) {
251 return null;
252 }
Johan Redestig22723ff2016-08-10 14:57:27 +0200253 final int rawInfoArrayLen = rawInfoArray.length;
254 final int infoArrayLen = rawInfoArrayLen / 2;
Adam Lesinski7fb38312018-01-23 03:17:26 -0800255 int block;
256 int index;
Johan Redestig22723ff2016-08-10 14:57:27 +0200257 final CharSequence[] retArray = new CharSequence[infoArrayLen];
258 for (int i = 0, j = 0; i < rawInfoArrayLen; i = i + 2, j++) {
Adam Lesinski7fb38312018-01-23 03:17:26 -0800259 block = rawInfoArray[i];
260 index = rawInfoArray[i + 1];
261 retArray[j] = index >= 0 ? mStringBlocks[block].get(index) : null;
Johan Redestig22723ff2016-08-10 14:57:27 +0200262 }
263 return retArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 }
Alan Viverettee273b932016-01-05 10:25:54 -0500266
267 /**
268 * Populates {@code outValue} with the data associated with a particular
269 * resource identifier for the current configuration. Resolves theme
270 * attributes against the specified theme.
271 *
272 * @param theme the native pointer of the theme
273 * @param resId the resource identifier to load
274 * @param outValue the typed value in which to put the data
275 * @param resolveRefs {@code true} to resolve references, {@code false}
276 * to leave them unresolved
277 * @return {@code true} if the data was loaded into {@code outValue},
278 * {@code false} otherwise
279 */
Adam Lesinski7fb38312018-01-23 03:17:26 -0800280 final boolean getThemeValue(long theme, @AnyRes int resId, @NonNull TypedValue outValue,
Alan Viverettee273b932016-01-05 10:25:54 -0500281 boolean resolveRefs) {
Adam Lesinski7fb38312018-01-23 03:17:26 -0800282 final int block = loadThemeAttributeValue(theme, resId, outValue, resolveRefs);
283 if (block < 0) {
284 return false;
285 }
286
287 // Convert the changing configurations flags populated by native code.
288 outValue.changingConfigurations = ActivityInfo.activityInfoConfigNativeToJava(
289 outValue.changingConfigurations);
290
291 if (outValue.type == TypedValue.TYPE_STRING) {
292 final StringBlock[] blocks = ensureStringBlocks();
293 outValue.string = blocks[block].get(outValue.data);
294 }
295 return true;
296 }
297
298 /**
299 * Ensures the string blocks are loaded.
300 *
301 * @return the string blocks
302 */
303 @NonNull
304 final StringBlock[] ensureStringBlocks() {
Alan Viverettee273b932016-01-05 10:25:54 -0500305 synchronized (this) {
Adam Lesinski7fb38312018-01-23 03:17:26 -0800306 if (mStringBlocks == null) {
307 makeStringBlocks(sSystem.mStringBlocks);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 }
Adam Lesinski7fb38312018-01-23 03:17:26 -0800309 return mStringBlocks;
310 }
311 }
Adam Lesinskib20a0ce2017-01-23 12:58:11 -0800312
Adam Lesinski7fb38312018-01-23 03:17:26 -0800313 /*package*/ final void makeStringBlocks(StringBlock[] seed) {
314 final int seedNum = (seed != null) ? seed.length : 0;
315 final int num = getStringBlockCount();
316 mStringBlocks = new StringBlock[num];
317 if (localLOGV) Log.v(TAG, "Making string blocks for " + this
318 + ": " + num);
319 for (int i=0; i<num; i++) {
320 if (i < seedNum) {
321 mStringBlocks[i] = seed[i];
322 } else {
323 mStringBlocks[i] = new StringBlock(getNativeStringBlock(i), true);
Adam Lesinskib20a0ce2017-01-23 12:58:11 -0800324 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 }
326 }
327
Adam Lesinski7fb38312018-01-23 03:17:26 -0800328 /*package*/ final CharSequence getPooledStringForCookie(int cookie, int id) {
Johan Redestig22723ff2016-08-10 14:57:27 +0200329 synchronized (this) {
Adam Lesinski7fb38312018-01-23 03:17:26 -0800330 // Cookies map to string blocks starting at 1.
331 return mStringBlocks[cookie - 1].get(id);
Johan Redestig22723ff2016-08-10 14:57:27 +0200332 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 }
334
335 /**
336 * Open an asset using ACCESS_STREAMING mode. This provides access to
337 * files that have been bundled with an application as assets -- that is,
338 * files placed in to the "assets" directory.
339 *
Adam Lesinski7fb38312018-01-23 03:17:26 -0800340 * @param fileName The name of the asset to open. This name can be
341 * hierarchical.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 *
343 * @see #open(String, int)
344 * @see #list
345 */
Adam Lesinski7fb38312018-01-23 03:17:26 -0800346 public final InputStream open(String fileName) throws IOException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 return open(fileName, ACCESS_STREAMING);
348 }
349
350 /**
351 * Open an asset using an explicit access mode, returning an InputStream to
352 * read its contents. This provides access to files that have been bundled
353 * with an application as assets -- that is, files placed in to the
354 * "assets" directory.
355 *
Adam Lesinski7fb38312018-01-23 03:17:26 -0800356 * @param fileName The name of the asset to open. This name can be
357 * hierarchical.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 * @param accessMode Desired access mode for retrieving the data.
359 *
360 * @see #ACCESS_UNKNOWN
361 * @see #ACCESS_STREAMING
362 * @see #ACCESS_RANDOM
363 * @see #ACCESS_BUFFER
364 * @see #open(String)
365 * @see #list
366 */
Adam Lesinski7fb38312018-01-23 03:17:26 -0800367 public final InputStream open(String fileName, int accessMode)
368 throws IOException {
Dianne Hackborn60d7db42009-11-16 17:16:26 -0800369 synchronized (this) {
Adam Lesinski7fb38312018-01-23 03:17:26 -0800370 if (!mOpen) {
371 throw new RuntimeException("Assetmanager has been closed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 }
Adam Lesinski7fb38312018-01-23 03:17:26 -0800373 long asset = openAsset(fileName, accessMode);
374 if (asset != 0) {
375 AssetInputStream res = new AssetInputStream(asset);
376 incRefsLocked(res.hashCode());
377 return res;
378 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 }
Adam Lesinski7fb38312018-01-23 03:17:26 -0800380 throw new FileNotFoundException("Asset file: " + fileName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 }
382
Adam Lesinski7fb38312018-01-23 03:17:26 -0800383 public final AssetFileDescriptor openFd(String fileName)
384 throws IOException {
Dianne Hackborn60d7db42009-11-16 17:16:26 -0800385 synchronized (this) {
Adam Lesinski7fb38312018-01-23 03:17:26 -0800386 if (!mOpen) {
387 throw new RuntimeException("Assetmanager has been closed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 }
Adam Lesinski7fb38312018-01-23 03:17:26 -0800389 ParcelFileDescriptor pfd = openAssetFd(fileName, mOffsets);
390 if (pfd != null) {
391 return new AssetFileDescriptor(pfd, mOffsets[0], mOffsets[1]);
392 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 }
Adam Lesinski7fb38312018-01-23 03:17:26 -0800394 throw new FileNotFoundException("Asset file: " + fileName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 }
396
397 /**
398 * Return a String array of all the assets at the given path.
399 *
400 * @param path A relative path within the assets, i.e., "docs/home.html".
401 *
402 * @return String[] Array of strings, one for each asset. These file
403 * names are relative to 'path'. You can open the file by
404 * concatenating 'path' and a name in the returned string (via
405 * File) and passing that to open().
406 *
407 * @see #open
408 */
Adam Lesinski7fb38312018-01-23 03:17:26 -0800409 public native final String[] list(String path)
410 throws IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411
412 /**
Adam Lesinski7fb38312018-01-23 03:17:26 -0800413 * {@hide}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 * Open a non-asset file as an asset using ACCESS_STREAMING mode. This
415 * provides direct access to all of the files included in an application
416 * package (not only its assets). Applications should not normally use
417 * this.
Adam Lesinski7fb38312018-01-23 03:17:26 -0800418 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 * @see #open(String)
420 */
Adam Lesinski7fb38312018-01-23 03:17:26 -0800421 public final InputStream openNonAsset(String fileName) throws IOException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 return openNonAsset(0, fileName, ACCESS_STREAMING);
423 }
424
425 /**
Adam Lesinski7fb38312018-01-23 03:17:26 -0800426 * {@hide}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 * Open a non-asset file as an asset using a specific access mode. This
428 * provides direct access to all of the files included in an application
429 * package (not only its assets). Applications should not normally use
430 * this.
Adam Lesinski7fb38312018-01-23 03:17:26 -0800431 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 * @see #open(String, int)
433 */
Adam Lesinski7fb38312018-01-23 03:17:26 -0800434 public final InputStream openNonAsset(String fileName, int accessMode)
435 throws IOException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 return openNonAsset(0, fileName, accessMode);
437 }
438
439 /**
Adam Lesinski7fb38312018-01-23 03:17:26 -0800440 * {@hide}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 * Open a non-asset in a specified package. Not for use by applications.
Adam Lesinski7fb38312018-01-23 03:17:26 -0800442 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 * @param cookie Identifier of the package to be opened.
444 * @param fileName Name of the asset to retrieve.
445 */
Adam Lesinski7fb38312018-01-23 03:17:26 -0800446 public final InputStream openNonAsset(int cookie, String fileName)
447 throws IOException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 return openNonAsset(cookie, fileName, ACCESS_STREAMING);
449 }
450
451 /**
Adam Lesinski7fb38312018-01-23 03:17:26 -0800452 * {@hide}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 * Open a non-asset in a specified package. Not for use by applications.
Adam Lesinski7fb38312018-01-23 03:17:26 -0800454 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 * @param cookie Identifier of the package to be opened.
456 * @param fileName Name of the asset to retrieve.
457 * @param accessMode Desired access mode for retrieving the data.
458 */
Adam Lesinski7fb38312018-01-23 03:17:26 -0800459 public final InputStream openNonAsset(int cookie, String fileName, int accessMode)
460 throws IOException {
Dianne Hackborn60d7db42009-11-16 17:16:26 -0800461 synchronized (this) {
Adam Lesinski7fb38312018-01-23 03:17:26 -0800462 if (!mOpen) {
463 throw new RuntimeException("Assetmanager has been closed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 }
Adam Lesinski7fb38312018-01-23 03:17:26 -0800465 long asset = openNonAssetNative(cookie, fileName, accessMode);
466 if (asset != 0) {
467 AssetInputStream res = new AssetInputStream(asset);
468 incRefsLocked(res.hashCode());
469 return res;
470 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 }
Adam Lesinski7fb38312018-01-23 03:17:26 -0800472 throw new FileNotFoundException("Asset absolute file: " + fileName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 }
474
Adam Lesinski7fb38312018-01-23 03:17:26 -0800475 public final AssetFileDescriptor openNonAssetFd(String fileName)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 throws IOException {
477 return openNonAssetFd(0, fileName);
478 }
Adam Lesinski7fb38312018-01-23 03:17:26 -0800479
480 public final AssetFileDescriptor openNonAssetFd(int cookie,
481 String fileName) throws IOException {
Dianne Hackborn60d7db42009-11-16 17:16:26 -0800482 synchronized (this) {
Adam Lesinski7fb38312018-01-23 03:17:26 -0800483 if (!mOpen) {
484 throw new RuntimeException("Assetmanager has been closed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 }
Adam Lesinski7fb38312018-01-23 03:17:26 -0800486 ParcelFileDescriptor pfd = openNonAssetFdNative(cookie,
487 fileName, mOffsets);
488 if (pfd != null) {
489 return new AssetFileDescriptor(pfd, mOffsets[0], mOffsets[1]);
490 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 }
Adam Lesinski7fb38312018-01-23 03:17:26 -0800492 throw new FileNotFoundException("Asset absolute file: " + fileName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 }
494
495 /**
496 * Retrieve a parser for a compiled XML file.
497 *
498 * @param fileName The name of the file to retrieve.
499 */
Adam Lesinski7fb38312018-01-23 03:17:26 -0800500 public final XmlResourceParser openXmlResourceParser(String fileName)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 throws IOException {
502 return openXmlResourceParser(0, fileName);
503 }
504
505 /**
506 * Retrieve a parser for a compiled XML file.
507 *
508 * @param cookie Identifier of the package to be opened.
509 * @param fileName The name of the file to retrieve.
510 */
Adam Lesinski7fb38312018-01-23 03:17:26 -0800511 public final XmlResourceParser openXmlResourceParser(int cookie,
512 String fileName) throws IOException {
513 XmlBlock block = openXmlBlockAsset(cookie, fileName);
514 XmlResourceParser rp = block.newParser();
515 block.close();
516 return rp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 }
518
519 /**
Adam Lesinski7fb38312018-01-23 03:17:26 -0800520 * {@hide}
521 * Retrieve a non-asset as a compiled XML file. Not for use by
522 * applications.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 *
524 * @param fileName The name of the file to retrieve.
525 */
Adam Lesinski7fb38312018-01-23 03:17:26 -0800526 /*package*/ final XmlBlock openXmlBlockAsset(String fileName)
527 throws IOException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 return openXmlBlockAsset(0, fileName);
529 }
530
531 /**
Adam Lesinski7fb38312018-01-23 03:17:26 -0800532 * {@hide}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 * Retrieve a non-asset as a compiled XML file. Not for use by
534 * applications.
535 *
536 * @param cookie Identifier of the package to be opened.
537 * @param fileName Name of the asset to retrieve.
538 */
Adam Lesinski7fb38312018-01-23 03:17:26 -0800539 /*package*/ final XmlBlock openXmlBlockAsset(int cookie, String fileName)
540 throws IOException {
Dianne Hackborn60d7db42009-11-16 17:16:26 -0800541 synchronized (this) {
Adam Lesinski7fb38312018-01-23 03:17:26 -0800542 if (!mOpen) {
543 throw new RuntimeException("Assetmanager has been closed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 }
Adam Lesinski7fb38312018-01-23 03:17:26 -0800545 long xmlBlock = openXmlAssetNative(cookie, fileName);
546 if (xmlBlock != 0) {
547 XmlBlock res = new XmlBlock(this, xmlBlock);
548 incRefsLocked(res.hashCode());
549 return res;
550 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 }
Adam Lesinski7fb38312018-01-23 03:17:26 -0800552 throw new FileNotFoundException("Asset XML file: " + fileName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 }
554
Adam Lesinski7fb38312018-01-23 03:17:26 -0800555 /*package*/ void xmlBlockGone(int id) {
Dianne Hackborn60d7db42009-11-16 17:16:26 -0800556 synchronized (this) {
Dianne Hackbornc3b91fd2010-02-23 17:25:30 -0800557 decRefsLocked(id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 }
559 }
560
Adam Lesinski7fb38312018-01-23 03:17:26 -0800561 /*package*/ final long createTheme() {
Dianne Hackborn60d7db42009-11-16 17:16:26 -0800562 synchronized (this) {
Adam Lesinski7fb38312018-01-23 03:17:26 -0800563 if (!mOpen) {
564 throw new RuntimeException("Assetmanager has been closed");
565 }
566 long res = newTheme();
567 incRefsLocked(res);
568 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 }
570 }
571
Adam Lesinski7fb38312018-01-23 03:17:26 -0800572 /*package*/ final void releaseTheme(long theme) {
Dianne Hackborn60d7db42009-11-16 17:16:26 -0800573 synchronized (this) {
Adam Lesinski7fb38312018-01-23 03:17:26 -0800574 deleteTheme(theme);
575 decRefsLocked(theme);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 }
577 }
578
579 protected void finalize() throws Throwable {
Adam Lesinski7fb38312018-01-23 03:17:26 -0800580 try {
581 if (DEBUG_REFS && mNumRefs != 0) {
582 Log.w(TAG, "AssetManager " + this
583 + " finalized with non-zero refs: " + mNumRefs);
584 if (mRefStacks != null) {
585 for (RuntimeException e : mRefStacks.values()) {
586 Log.w(TAG, "Reference from here", e);
587 }
Dianne Hackbornc3b91fd2010-02-23 17:25:30 -0800588 }
589 }
Adam Lesinski7fb38312018-01-23 03:17:26 -0800590 destroy();
591 } finally {
592 super.finalize();
Dianne Hackbornc3b91fd2010-02-23 17:25:30 -0800593 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594 }
Adam Lesinski7fb38312018-01-23 03:17:26 -0800595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 public final class AssetInputStream extends InputStream {
Narayan Kamath2ba4eff2014-01-16 19:00:32 +0000597 /**
598 * @hide
599 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 public final int getAssetInt() {
Narayan Kamath2d19d202014-02-25 15:48:07 +0000601 throw new UnsupportedOperationException();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 }
Ashok Bhata0545dd2014-01-14 10:52:35 +0000603 /**
604 * @hide
605 */
606 public final long getNativeAsset() {
Adam Lesinski7fb38312018-01-23 03:17:26 -0800607 return mAsset;
Ashok Bhata0545dd2014-01-14 10:52:35 +0000608 }
Adam Lesinski7fb38312018-01-23 03:17:26 -0800609 private AssetInputStream(long asset)
610 {
611 mAsset = asset;
612 mLength = getAssetLength(asset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 }
614 public final int read() throws IOException {
Adam Lesinski7fb38312018-01-23 03:17:26 -0800615 return readAssetChar(mAsset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 }
617 public final boolean markSupported() {
618 return true;
619 }
620 public final int available() throws IOException {
Adam Lesinski7fb38312018-01-23 03:17:26 -0800621 long len = getAssetRemainingLength(mAsset);
622 return len > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int)len;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 }
624 public final void close() throws IOException {
Adam Lesinski7fb38312018-01-23 03:17:26 -0800625 synchronized (AssetManager.this) {
626 if (mAsset != 0) {
627 destroyAsset(mAsset);
628 mAsset = 0;
Dianne Hackbornc3b91fd2010-02-23 17:25:30 -0800629 decRefsLocked(hashCode());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 }
631 }
632 }
633 public final void mark(int readlimit) {
Adam Lesinski7fb38312018-01-23 03:17:26 -0800634 mMarkPos = seekAsset(mAsset, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 }
636 public final void reset() throws IOException {
Adam Lesinski7fb38312018-01-23 03:17:26 -0800637 seekAsset(mAsset, mMarkPos, -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 }
Adam Lesinski7fb38312018-01-23 03:17:26 -0800639 public final int read(byte[] b) throws IOException {
640 return readAsset(mAsset, b, 0, b.length);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 }
Adam Lesinski7fb38312018-01-23 03:17:26 -0800642 public final int read(byte[] b, int off, int len) throws IOException {
643 return readAsset(mAsset, b, off, len);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 }
645 public final long skip(long n) throws IOException {
Adam Lesinski7fb38312018-01-23 03:17:26 -0800646 long pos = seekAsset(mAsset, 0, 0);
647 if ((pos+n) > mLength) {
648 n = mLength-pos;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 }
650 if (n > 0) {
Adam Lesinski7fb38312018-01-23 03:17:26 -0800651 seekAsset(mAsset, n, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652 }
653 return n;
654 }
655
Adam Lesinski7fb38312018-01-23 03:17:26 -0800656 protected void finalize() throws Throwable
657 {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 close();
659 }
Adam Lesinski7fb38312018-01-23 03:17:26 -0800660
661 private long mAsset;
662 private long mLength;
663 private long mMarkPos;
664 }
665
666 /**
667 * Add an additional set of assets to the asset manager. This can be
668 * either a directory or ZIP file. Not for use by applications. Returns
669 * the cookie of the added asset, or 0 on failure.
670 * {@hide}
671 */
672 public final int addAssetPath(String path) {
673 return addAssetPathInternal(path, false);
674 }
675
676 /**
677 * Add an application assets to the asset manager and loading it as shared library.
678 * This can be either a directory or ZIP file. Not for use by applications. Returns
679 * the cookie of the added asset, or 0 on failure.
680 * {@hide}
681 */
682 public final int addAssetPathAsSharedLibrary(String path) {
683 return addAssetPathInternal(path, true);
684 }
685
686 private final int addAssetPathInternal(String path, boolean appAsLib) {
687 synchronized (this) {
688 int res = addAssetPathNative(path, appAsLib);
689 makeStringBlocks(mStringBlocks);
690 return res;
691 }
692 }
693
694 private native final int addAssetPathNative(String path, boolean appAsLib);
695
696 /**
697 * Add an additional set of assets to the asset manager from an already open
698 * FileDescriptor. Not for use by applications.
699 * This does not give full AssetManager functionality for these assets,
700 * since the origin of the file is not known for purposes of sharing,
701 * overlay resolution, and other features. However it does allow you
702 * to do simple access to the contents of the given fd as an apk file.
703 * Performs a dup of the underlying fd, so you must take care of still closing
704 * the FileDescriptor yourself (and can do that whenever you want).
705 * Returns the cookie of the added asset, or 0 on failure.
706 * {@hide}
707 */
708 public int addAssetFd(FileDescriptor fd, String debugPathName) {
709 return addAssetFdInternal(fd, debugPathName, false);
710 }
711
712 private int addAssetFdInternal(FileDescriptor fd, String debugPathName,
713 boolean appAsLib) {
714 synchronized (this) {
715 int res = addAssetFdNative(fd, debugPathName, appAsLib);
716 makeStringBlocks(mStringBlocks);
717 return res;
718 }
719 }
720
721 private native int addAssetFdNative(FileDescriptor fd, String debugPathName,
722 boolean appAsLib);
723
724 /**
725 * Add a set of assets to overlay an already added set of assets.
726 *
727 * This is only intended for application resources. System wide resources
728 * are handled before any Java code is executed.
729 *
730 * {@hide}
731 */
732
733 public final int addOverlayPath(String idmapPath) {
734 synchronized (this) {
735 int res = addOverlayPathNative(idmapPath);
736 makeStringBlocks(mStringBlocks);
737 return res;
738 }
739 }
740
741 /**
742 * See addOverlayPath.
743 *
744 * {@hide}
745 */
746 public native final int addOverlayPathNative(String idmapPath);
747
748 /**
749 * Add multiple sets of assets to the asset manager at once. See
750 * {@link #addAssetPath(String)} for more information. Returns array of
751 * cookies for each added asset with 0 indicating failure, or null if
752 * the input array of paths is null.
753 * {@hide}
754 */
755 public final int[] addAssetPaths(String[] paths) {
756 if (paths == null) {
757 return null;
758 }
759
760 int[] cookies = new int[paths.length];
761 for (int i = 0; i < paths.length; i++) {
762 cookies[i] = addAssetPath(paths[i]);
763 }
764
765 return cookies;
Kenny Root3a198332010-01-21 15:51:48 -0800766 }
767
768 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769 * Determine whether the state in this asset manager is up-to-date with
770 * the files on the filesystem. If false is returned, you need to
771 * instantiate a new AssetManager class to see the new data.
Adam Lesinski7fb38312018-01-23 03:17:26 -0800772 * {@hide}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773 */
Adam Lesinski7fb38312018-01-23 03:17:26 -0800774 public native final boolean isUpToDate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775
776 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777 * Get the locales that this asset manager contains data for.
Narayan Kamath11bce122014-11-12 13:28:20 +0000778 *
779 * <p>On SDK 21 (Android 5.0: Lollipop) and above, Locale strings are valid
780 * <a href="https://tools.ietf.org/html/bcp47">BCP-47</a> language tags and can be
781 * parsed using {@link java.util.Locale#forLanguageTag(String)}.
782 *
783 * <p>On SDK 20 (Android 4.4W: Kitkat for watches) and below, locale strings
784 * are of the form {@code ll_CC} where {@code ll} is a two letter language code,
785 * and {@code CC} is a two letter country code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 */
Adam Lesinski7fb38312018-01-23 03:17:26 -0800787 public native final String[] getLocales();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800788
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800789 /**
790 * Same as getLocales(), except that locales that are only provided by the system (i.e. those
791 * present in framework-res.apk or its overlays) will not be listed.
792 *
793 * For example, if the "system" assets support English, French, and German, and the additional
794 * assets support Cherokee and French, getLocales() would return
795 * [Cherokee, English, French, German], while getNonSystemLocales() would return
796 * [Cherokee, French].
Adam Lesinski7fb38312018-01-23 03:17:26 -0800797 * {@hide}
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800798 */
Adam Lesinski7fb38312018-01-23 03:17:26 -0800799 public native final String[] getNonSystemLocales();
800
801 /** {@hide} */
802 public native final Configuration[] getSizeConfigurations();
Filip Gruszczynski23493322015-07-29 17:02:59 -0700803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 /**
Adam Lesinski7fb38312018-01-23 03:17:26 -0800805 * Change the configuation used when retrieving resources. Not for use by
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 * applications.
Adam Lesinski7fb38312018-01-23 03:17:26 -0800807 * {@hide}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 */
Adam Lesinski7fb38312018-01-23 03:17:26 -0800809 public native final void setConfiguration(int mcc, int mnc, String locale,
810 int orientation, int touchscreen, int density, int keyboard,
811 int keyboardHidden, int navigation, int screenWidth, int screenHeight,
812 int smallestScreenWidthDp, int screenWidthDp, int screenHeightDp,
813 int screenLayout, int uiMode, int colorMode, int majorVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814
815 /**
Adam Lesinski7fb38312018-01-23 03:17:26 -0800816 * Retrieve the resource identifier for the given resource name.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817 */
Adam Lesinski7fb38312018-01-23 03:17:26 -0800818 /*package*/ native final int getResourceIdentifier(String name,
819 String defType,
820 String defPackage);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821
Adam Lesinski7fb38312018-01-23 03:17:26 -0800822 /*package*/ native final String getResourceName(int resid);
823 /*package*/ native final String getResourcePackageName(int resid);
824 /*package*/ native final String getResourceTypeName(int resid);
825 /*package*/ native final String getResourceEntryName(int resid);
826
827 private native final long openAsset(String fileName, int accessMode);
828 private final native ParcelFileDescriptor openAssetFd(String fileName,
829 long[] outOffsets) throws IOException;
830 private native final long openNonAssetNative(int cookie, String fileName,
831 int accessMode);
832 private native ParcelFileDescriptor openNonAssetFdNative(int cookie,
833 String fileName, long[] outOffsets) throws IOException;
834 private native final void destroyAsset(long asset);
835 private native final int readAssetChar(long asset);
836 private native final int readAsset(long asset, byte[] b, int off, int len);
837 private native final long seekAsset(long asset, long offset, int whence);
838 private native final long getAssetLength(long asset);
839 private native final long getAssetRemainingLength(long asset);
840
841 /** Returns true if the resource was found, filling in mRetStringBlock and
842 * mRetData. */
843 private native final int loadResourceValue(int ident, short density, TypedValue outValue,
844 boolean resolve);
845 /** Returns true if the resource was found, filling in mRetStringBlock and
846 * mRetData. */
847 private native final int loadResourceBagValue(int ident, int bagEntryId, TypedValue outValue,
848 boolean resolve);
849 /*package*/ static final int STYLE_NUM_ENTRIES = 6;
850 /*package*/ static final int STYLE_TYPE = 0;
851 /*package*/ static final int STYLE_DATA = 1;
852 /*package*/ static final int STYLE_ASSET_COOKIE = 2;
853 /*package*/ static final int STYLE_RESOURCE_ID = 3;
854
855 /* Offset within typed data array for native changingConfigurations. */
856 static final int STYLE_CHANGING_CONFIGURATIONS = 4;
857
858 /*package*/ static final int STYLE_DENSITY = 5;
859 /*package*/ native static final void applyStyle(long theme,
860 int defStyleAttr, int defStyleRes, long xmlParser,
861 int[] inAttrs, int length, long outValuesAddress, long outIndicesAddress);
862 /*package*/ native static final boolean resolveAttrs(long theme,
863 int defStyleAttr, int defStyleRes, int[] inValues,
864 int[] inAttrs, int[] outValues, int[] outIndices);
865 /*package*/ native final boolean retrieveAttributes(
866 long xmlParser, int[] inAttrs, int[] outValues, int[] outIndices);
867 /*package*/ native final int getArraySize(int resource);
868 /*package*/ native final int retrieveArray(int resource, int[] outValues);
869 private native final int getStringBlockCount();
870 private native final long getNativeStringBlock(int block);
871
872 /**
873 * {@hide}
874 */
875 public native final String getCookieName(int cookie);
876
877 /**
878 * {@hide}
879 */
880 public native final SparseArray<String> getAssignedPackageIdentifiers();
881
882 /**
883 * {@hide}
884 */
885 public native static final int getGlobalAssetCount();
886
887 /**
888 * {@hide}
889 */
890 public native static final String getAssetAllocations();
891
892 /**
893 * {@hide}
894 */
895 public native static final int getGlobalAssetManagerCount();
896
897 private native final long newTheme();
898 private native final void deleteTheme(long theme);
899 /*package*/ native static final void applyThemeStyle(long theme, int styleRes, boolean force);
900 /*package*/ native static final void copyTheme(long dest, long source);
901 /*package*/ native static final void clearTheme(long theme);
902 /*package*/ native static final int loadThemeAttributeValue(long theme, int ident,
903 TypedValue outValue,
904 boolean resolve);
905 /*package*/ native static final void dumpTheme(long theme, int priority, String tag, String prefix);
906 /*package*/ native static final @NativeConfig int getThemeChangingConfigurations(long theme);
907
908 private native final long openXmlAssetNative(int cookie, String fileName);
909
910 private native final String[] getArrayStringResource(int arrayRes);
911 private native final int[] getArrayStringInfo(int arrayRes);
912 /*package*/ native final int[] getArrayIntResource(int arrayRes);
913 /*package*/ native final int[] getStyleAttributes(int themeRes);
914
915 private native final void init(boolean isSystem);
916 private native final void destroy();
917
918 private final void incRefsLocked(long id) {
Dianne Hackbornc3b91fd2010-02-23 17:25:30 -0800919 if (DEBUG_REFS) {
920 if (mRefStacks == null) {
Adam Lesinski7fb38312018-01-23 03:17:26 -0800921 mRefStacks = new HashMap<Long, RuntimeException>();
Dianne Hackbornc3b91fd2010-02-23 17:25:30 -0800922 }
Ashok Bhat896043d2014-01-17 16:02:38 +0000923 RuntimeException ex = new RuntimeException();
924 ex.fillInStackTrace();
925 mRefStacks.put(id, ex);
Dianne Hackbornc3b91fd2010-02-23 17:25:30 -0800926 }
927 mNumRefs++;
928 }
Adam Lesinski7fb38312018-01-23 03:17:26 -0800929
930 private final void decRefsLocked(long id) {
Dianne Hackbornc3b91fd2010-02-23 17:25:30 -0800931 if (DEBUG_REFS && mRefStacks != null) {
932 mRefStacks.remove(id);
933 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800934 mNumRefs--;
Adam Lesinski7fb38312018-01-23 03:17:26 -0800935 //System.out.println("Dec streams: mNumRefs=" + mNumRefs
936 // + " mReleased=" + mReleased);
937 if (mNumRefs == 0) {
938 destroy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 }
940 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941}