The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.content.res; |
| 18 | |
Alan Viverette | e273b93 | 2016-01-05 10:25:54 -0500 | [diff] [blame] | 19 | import android.annotation.AnyRes; |
| 20 | import android.annotation.ArrayRes; |
| 21 | import android.annotation.NonNull; |
| 22 | import android.annotation.Nullable; |
| 23 | import android.annotation.StringRes; |
Alan Viverette | 9ad386b | 2017-01-26 14:00:20 -0500 | [diff] [blame] | 24 | import android.content.pm.ActivityInfo; |
Alan Viverette | ac85f90 | 2016-03-11 15:15:51 -0500 | [diff] [blame] | 25 | import android.content.res.Configuration.NativeConfig; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | import android.os.ParcelFileDescriptor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.util.Log; |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 28 | import android.util.SparseArray; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | import android.util.TypedValue; |
| 30 | |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 31 | import java.io.FileDescriptor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | import java.io.FileNotFoundException; |
| 33 | import java.io.IOException; |
| 34 | import java.io.InputStream; |
Dianne Hackborn | c3b91fd | 2010-02-23 17:25:30 -0800 | [diff] [blame] | 35 | import java.util.HashMap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | |
| 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 Sharkey | da96e13 | 2014-07-15 14:54:09 -0700 | [diff] [blame] | 44 | public final class AssetManager implements AutoCloseable { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 45 | /* modes used when opening an asset */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | |
| 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 Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 68 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 76 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 82 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | /** |
| 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 Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 93 | * {@hide} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | */ |
| 95 | public AssetManager() { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 96 | synchronized (this) { |
| 97 | if (DEBUG_REFS) { |
| 98 | mNumRefs = 0; |
| 99 | incRefsLocked(this.hashCode()); |
Dianne Hackborn | c3b91fd | 2010-02-23 17:25:30 -0800 | [diff] [blame] | 100 | } |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 101 | init(false); |
| 102 | if (localLOGV) Log.v(TAG, "New asset manager: " + this); |
| 103 | ensureSystemAssets(); |
Dianne Hackborn | c3b91fd | 2010-02-23 17:25:30 -0800 | [diff] [blame] | 104 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 107 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | /** |
| 129 | * Return a global shared asset manager that provides access to only |
| 130 | * system assets (no application assets). |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 131 | * {@hide} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 132 | */ |
| 133 | public static AssetManager getSystem() { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 134 | ensureSystemAssets(); |
| 135 | return sSystem; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Close this asset manager. |
| 140 | */ |
| 141 | public void close() { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 142 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /** |
Adam Lesinski | b20a0ce | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 153 | * 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 Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 159 | @Nullable |
| 160 | final CharSequence getResourceText(@StringRes int resId) { |
Adam Lesinski | b20a0ce | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 161 | 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 Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 175 | * @param bagEntryId |
Adam Lesinski | b20a0ce | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 176 | * @return the string value, or {@code null} |
| 177 | */ |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 178 | @Nullable |
| 179 | final CharSequence getResourceBagText(@StringRes int resId, int bagEntryId) { |
Adam Lesinski | b20a0ce | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 180 | synchronized (this) { |
Adam Lesinski | b20a0ce | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 181 | final TypedValue outValue = mValue; |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 182 | final int block = loadResourceBagValue(resId, bagEntryId, outValue, true); |
| 183 | if (block < 0) { |
Adam Lesinski | b20a0ce | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 184 | 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 Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 192 | return mStringBlocks[block].get(outValue.data); |
Adam Lesinski | b20a0ce | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 193 | } |
| 194 | return outValue.coerceToString(); |
| 195 | } |
| 196 | } |
| 197 | |
Adam Lesinski | b20a0ce | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 198 | /** |
| 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 Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 205 | @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 Lesinski | b20a0ce | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 224 | synchronized (this) { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 225 | 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 Lesinski | b20a0ce | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
| 241 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 242 | * Retrieve the text array associated with a particular resource |
| 243 | * identifier. |
Alan Viverette | e273b93 | 2016-01-05 10:25:54 -0500 | [diff] [blame] | 244 | * |
| 245 | * @param resId the resource id of the string array |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 246 | */ |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 247 | final @Nullable CharSequence[] getResourceTextArray(@ArrayRes int resId) { |
Johan Redestig | 22723ff | 2016-08-10 14:57:27 +0200 | [diff] [blame] | 248 | synchronized (this) { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 249 | final int[] rawInfoArray = getArrayStringInfo(resId); |
Adam Lesinski | ba9576d | 2017-03-16 17:42:04 -0700 | [diff] [blame] | 250 | if (rawInfoArray == null) { |
| 251 | return null; |
| 252 | } |
Johan Redestig | 22723ff | 2016-08-10 14:57:27 +0200 | [diff] [blame] | 253 | final int rawInfoArrayLen = rawInfoArray.length; |
| 254 | final int infoArrayLen = rawInfoArrayLen / 2; |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 255 | int block; |
| 256 | int index; |
Johan Redestig | 22723ff | 2016-08-10 14:57:27 +0200 | [diff] [blame] | 257 | final CharSequence[] retArray = new CharSequence[infoArrayLen]; |
| 258 | for (int i = 0, j = 0; i < rawInfoArrayLen; i = i + 2, j++) { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 259 | block = rawInfoArray[i]; |
| 260 | index = rawInfoArray[i + 1]; |
| 261 | retArray[j] = index >= 0 ? mStringBlocks[block].get(index) : null; |
Johan Redestig | 22723ff | 2016-08-10 14:57:27 +0200 | [diff] [blame] | 262 | } |
| 263 | return retArray; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 264 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 265 | } |
Alan Viverette | e273b93 | 2016-01-05 10:25:54 -0500 | [diff] [blame] | 266 | |
| 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 Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 280 | final boolean getThemeValue(long theme, @AnyRes int resId, @NonNull TypedValue outValue, |
Alan Viverette | e273b93 | 2016-01-05 10:25:54 -0500 | [diff] [blame] | 281 | boolean resolveRefs) { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 282 | 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 Viverette | e273b93 | 2016-01-05 10:25:54 -0500 | [diff] [blame] | 305 | synchronized (this) { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 306 | if (mStringBlocks == null) { |
| 307 | makeStringBlocks(sSystem.mStringBlocks); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 308 | } |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 309 | return mStringBlocks; |
| 310 | } |
| 311 | } |
Adam Lesinski | b20a0ce | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 312 | |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 313 | /*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 Lesinski | b20a0ce | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 324 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 328 | /*package*/ final CharSequence getPooledStringForCookie(int cookie, int id) { |
Johan Redestig | 22723ff | 2016-08-10 14:57:27 +0200 | [diff] [blame] | 329 | synchronized (this) { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 330 | // Cookies map to string blocks starting at 1. |
| 331 | return mStringBlocks[cookie - 1].get(id); |
Johan Redestig | 22723ff | 2016-08-10 14:57:27 +0200 | [diff] [blame] | 332 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 333 | } |
| 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 Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 340 | * @param fileName The name of the asset to open. This name can be |
| 341 | * hierarchical. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 342 | * |
| 343 | * @see #open(String, int) |
| 344 | * @see #list |
| 345 | */ |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 346 | public final InputStream open(String fileName) throws IOException { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 347 | 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 Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 356 | * @param fileName The name of the asset to open. This name can be |
| 357 | * hierarchical. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 358 | * @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 Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 367 | public final InputStream open(String fileName, int accessMode) |
| 368 | throws IOException { |
Dianne Hackborn | 60d7db4 | 2009-11-16 17:16:26 -0800 | [diff] [blame] | 369 | synchronized (this) { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 370 | if (!mOpen) { |
| 371 | throw new RuntimeException("Assetmanager has been closed"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 372 | } |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 373 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 379 | } |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 380 | throw new FileNotFoundException("Asset file: " + fileName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 381 | } |
| 382 | |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 383 | public final AssetFileDescriptor openFd(String fileName) |
| 384 | throws IOException { |
Dianne Hackborn | 60d7db4 | 2009-11-16 17:16:26 -0800 | [diff] [blame] | 385 | synchronized (this) { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 386 | if (!mOpen) { |
| 387 | throw new RuntimeException("Assetmanager has been closed"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 388 | } |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 389 | ParcelFileDescriptor pfd = openAssetFd(fileName, mOffsets); |
| 390 | if (pfd != null) { |
| 391 | return new AssetFileDescriptor(pfd, mOffsets[0], mOffsets[1]); |
| 392 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 393 | } |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 394 | throw new FileNotFoundException("Asset file: " + fileName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 395 | } |
| 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 Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 409 | public native final String[] list(String path) |
| 410 | throws IOException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 411 | |
| 412 | /** |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 413 | * {@hide} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 414 | * 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 Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 418 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 419 | * @see #open(String) |
| 420 | */ |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 421 | public final InputStream openNonAsset(String fileName) throws IOException { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 422 | return openNonAsset(0, fileName, ACCESS_STREAMING); |
| 423 | } |
| 424 | |
| 425 | /** |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 426 | * {@hide} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 427 | * 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 Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 431 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 432 | * @see #open(String, int) |
| 433 | */ |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 434 | public final InputStream openNonAsset(String fileName, int accessMode) |
| 435 | throws IOException { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 436 | return openNonAsset(0, fileName, accessMode); |
| 437 | } |
| 438 | |
| 439 | /** |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 440 | * {@hide} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 441 | * Open a non-asset in a specified package. Not for use by applications. |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 442 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 443 | * @param cookie Identifier of the package to be opened. |
| 444 | * @param fileName Name of the asset to retrieve. |
| 445 | */ |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 446 | public final InputStream openNonAsset(int cookie, String fileName) |
| 447 | throws IOException { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 448 | return openNonAsset(cookie, fileName, ACCESS_STREAMING); |
| 449 | } |
| 450 | |
| 451 | /** |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 452 | * {@hide} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 453 | * Open a non-asset in a specified package. Not for use by applications. |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 454 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 455 | * @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 Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 459 | public final InputStream openNonAsset(int cookie, String fileName, int accessMode) |
| 460 | throws IOException { |
Dianne Hackborn | 60d7db4 | 2009-11-16 17:16:26 -0800 | [diff] [blame] | 461 | synchronized (this) { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 462 | if (!mOpen) { |
| 463 | throw new RuntimeException("Assetmanager has been closed"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 464 | } |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 465 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 471 | } |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 472 | throw new FileNotFoundException("Asset absolute file: " + fileName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 473 | } |
| 474 | |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 475 | public final AssetFileDescriptor openNonAssetFd(String fileName) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 476 | throws IOException { |
| 477 | return openNonAssetFd(0, fileName); |
| 478 | } |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 479 | |
| 480 | public final AssetFileDescriptor openNonAssetFd(int cookie, |
| 481 | String fileName) throws IOException { |
Dianne Hackborn | 60d7db4 | 2009-11-16 17:16:26 -0800 | [diff] [blame] | 482 | synchronized (this) { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 483 | if (!mOpen) { |
| 484 | throw new RuntimeException("Assetmanager has been closed"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 485 | } |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 486 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 491 | } |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 492 | throw new FileNotFoundException("Asset absolute file: " + fileName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | /** |
| 496 | * Retrieve a parser for a compiled XML file. |
| 497 | * |
| 498 | * @param fileName The name of the file to retrieve. |
| 499 | */ |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 500 | public final XmlResourceParser openXmlResourceParser(String fileName) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 501 | 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 Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 511 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | /** |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 520 | * {@hide} |
| 521 | * Retrieve a non-asset as a compiled XML file. Not for use by |
| 522 | * applications. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 523 | * |
| 524 | * @param fileName The name of the file to retrieve. |
| 525 | */ |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 526 | /*package*/ final XmlBlock openXmlBlockAsset(String fileName) |
| 527 | throws IOException { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 528 | return openXmlBlockAsset(0, fileName); |
| 529 | } |
| 530 | |
| 531 | /** |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 532 | * {@hide} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 533 | * 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 Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 539 | /*package*/ final XmlBlock openXmlBlockAsset(int cookie, String fileName) |
| 540 | throws IOException { |
Dianne Hackborn | 60d7db4 | 2009-11-16 17:16:26 -0800 | [diff] [blame] | 541 | synchronized (this) { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 542 | if (!mOpen) { |
| 543 | throw new RuntimeException("Assetmanager has been closed"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 544 | } |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 545 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 551 | } |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 552 | throw new FileNotFoundException("Asset XML file: " + fileName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 553 | } |
| 554 | |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 555 | /*package*/ void xmlBlockGone(int id) { |
Dianne Hackborn | 60d7db4 | 2009-11-16 17:16:26 -0800 | [diff] [blame] | 556 | synchronized (this) { |
Dianne Hackborn | c3b91fd | 2010-02-23 17:25:30 -0800 | [diff] [blame] | 557 | decRefsLocked(id); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 558 | } |
| 559 | } |
| 560 | |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 561 | /*package*/ final long createTheme() { |
Dianne Hackborn | 60d7db4 | 2009-11-16 17:16:26 -0800 | [diff] [blame] | 562 | synchronized (this) { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 563 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 569 | } |
| 570 | } |
| 571 | |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 572 | /*package*/ final void releaseTheme(long theme) { |
Dianne Hackborn | 60d7db4 | 2009-11-16 17:16:26 -0800 | [diff] [blame] | 573 | synchronized (this) { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 574 | deleteTheme(theme); |
| 575 | decRefsLocked(theme); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 576 | } |
| 577 | } |
| 578 | |
| 579 | protected void finalize() throws Throwable { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 580 | 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 Hackborn | c3b91fd | 2010-02-23 17:25:30 -0800 | [diff] [blame] | 588 | } |
| 589 | } |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 590 | destroy(); |
| 591 | } finally { |
| 592 | super.finalize(); |
Dianne Hackborn | c3b91fd | 2010-02-23 17:25:30 -0800 | [diff] [blame] | 593 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 594 | } |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 595 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 596 | public final class AssetInputStream extends InputStream { |
Narayan Kamath | 2ba4eff | 2014-01-16 19:00:32 +0000 | [diff] [blame] | 597 | /** |
| 598 | * @hide |
| 599 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 600 | public final int getAssetInt() { |
Narayan Kamath | 2d19d20 | 2014-02-25 15:48:07 +0000 | [diff] [blame] | 601 | throw new UnsupportedOperationException(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 602 | } |
Ashok Bhat | a0545dd | 2014-01-14 10:52:35 +0000 | [diff] [blame] | 603 | /** |
| 604 | * @hide |
| 605 | */ |
| 606 | public final long getNativeAsset() { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 607 | return mAsset; |
Ashok Bhat | a0545dd | 2014-01-14 10:52:35 +0000 | [diff] [blame] | 608 | } |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 609 | private AssetInputStream(long asset) |
| 610 | { |
| 611 | mAsset = asset; |
| 612 | mLength = getAssetLength(asset); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 613 | } |
| 614 | public final int read() throws IOException { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 615 | return readAssetChar(mAsset); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 616 | } |
| 617 | public final boolean markSupported() { |
| 618 | return true; |
| 619 | } |
| 620 | public final int available() throws IOException { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 621 | long len = getAssetRemainingLength(mAsset); |
| 622 | return len > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int)len; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 623 | } |
| 624 | public final void close() throws IOException { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 625 | synchronized (AssetManager.this) { |
| 626 | if (mAsset != 0) { |
| 627 | destroyAsset(mAsset); |
| 628 | mAsset = 0; |
Dianne Hackborn | c3b91fd | 2010-02-23 17:25:30 -0800 | [diff] [blame] | 629 | decRefsLocked(hashCode()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 630 | } |
| 631 | } |
| 632 | } |
| 633 | public final void mark(int readlimit) { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 634 | mMarkPos = seekAsset(mAsset, 0, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 635 | } |
| 636 | public final void reset() throws IOException { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 637 | seekAsset(mAsset, mMarkPos, -1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 638 | } |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 639 | public final int read(byte[] b) throws IOException { |
| 640 | return readAsset(mAsset, b, 0, b.length); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 641 | } |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 642 | public final int read(byte[] b, int off, int len) throws IOException { |
| 643 | return readAsset(mAsset, b, off, len); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 644 | } |
| 645 | public final long skip(long n) throws IOException { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 646 | long pos = seekAsset(mAsset, 0, 0); |
| 647 | if ((pos+n) > mLength) { |
| 648 | n = mLength-pos; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 649 | } |
| 650 | if (n > 0) { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 651 | seekAsset(mAsset, n, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 652 | } |
| 653 | return n; |
| 654 | } |
| 655 | |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 656 | protected void finalize() throws Throwable |
| 657 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 658 | close(); |
| 659 | } |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 660 | |
| 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 Root | 3a19833 | 2010-01-21 15:51:48 -0800 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 769 | * 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 Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 772 | * {@hide} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 773 | */ |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 774 | public native final boolean isUpToDate(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 775 | |
| 776 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 777 | * Get the locales that this asset manager contains data for. |
Narayan Kamath | 11bce12 | 2014-11-12 13:28:20 +0000 | [diff] [blame] | 778 | * |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 786 | */ |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 787 | public native final String[] getLocales(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 788 | |
Roozbeh Pournader | 1c686f2 | 2015-12-18 14:22:14 -0800 | [diff] [blame] | 789 | /** |
| 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 Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 797 | * {@hide} |
Roozbeh Pournader | 1c686f2 | 2015-12-18 14:22:14 -0800 | [diff] [blame] | 798 | */ |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 799 | public native final String[] getNonSystemLocales(); |
| 800 | |
| 801 | /** {@hide} */ |
| 802 | public native final Configuration[] getSizeConfigurations(); |
Filip Gruszczynski | 2349332 | 2015-07-29 17:02:59 -0700 | [diff] [blame] | 803 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 804 | /** |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 805 | * Change the configuation used when retrieving resources. Not for use by |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 806 | * applications. |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 807 | * {@hide} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 808 | */ |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 809 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 814 | |
| 815 | /** |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 816 | * Retrieve the resource identifier for the given resource name. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 817 | */ |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 818 | /*package*/ native final int getResourceIdentifier(String name, |
| 819 | String defType, |
| 820 | String defPackage); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 821 | |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 822 | /*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 Hackborn | c3b91fd | 2010-02-23 17:25:30 -0800 | [diff] [blame] | 919 | if (DEBUG_REFS) { |
| 920 | if (mRefStacks == null) { |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 921 | mRefStacks = new HashMap<Long, RuntimeException>(); |
Dianne Hackborn | c3b91fd | 2010-02-23 17:25:30 -0800 | [diff] [blame] | 922 | } |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 923 | RuntimeException ex = new RuntimeException(); |
| 924 | ex.fillInStackTrace(); |
| 925 | mRefStacks.put(id, ex); |
Dianne Hackborn | c3b91fd | 2010-02-23 17:25:30 -0800 | [diff] [blame] | 926 | } |
| 927 | mNumRefs++; |
| 928 | } |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 929 | |
| 930 | private final void decRefsLocked(long id) { |
Dianne Hackborn | c3b91fd | 2010-02-23 17:25:30 -0800 | [diff] [blame] | 931 | if (DEBUG_REFS && mRefStacks != null) { |
| 932 | mRefStacks.remove(id); |
| 933 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 934 | mNumRefs--; |
Adam Lesinski | 7fb3831 | 2018-01-23 03:17:26 -0800 | [diff] [blame] | 935 | //System.out.println("Dec streams: mNumRefs=" + mNumRefs |
| 936 | // + " mReleased=" + mReleased); |
| 937 | if (mNumRefs == 0) { |
| 938 | destroy(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 939 | } |
| 940 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 941 | } |