| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.content.res; |
| 18 |
|
| 19 |
import android.annotation.AnyRes; |
| 20 |
import android.annotation.ArrayRes; |
| 21 |
import android.annotation.AttrRes; |
| 22 |
import android.annotation.NonNull; |
| 23 |
import android.annotation.Nullable; |
| 24 |
import android.annotation.StringRes; |
| 25 |
import android.annotation.StyleRes; |
| 26 |
import android.annotation.TestApi; |
| 27 |
import android.compat.annotation.UnsupportedAppUsage; |
| 28 |
import android.content.pm.ActivityInfo; |
| 29 |
import android.content.res.Configuration.NativeConfig; |
| 30 |
import android.content.res.loader.ResourcesLoader; |
| 31 |
import android.os.Build; |
| 32 |
import android.os.ParcelFileDescriptor; |
| 33 |
import android.util.ArraySet; |
| 34 |
import android.util.Log; |
| 35 |
import android.util.SparseArray; |
| 36 |
import android.util.TypedValue; |
| 37 |
|
| 38 |
import com.android.internal.annotations.GuardedBy; |
| 39 |
import com.android.internal.annotations.VisibleForTesting; |
| 40 |
import com.android.internal.content.om.OverlayConfig; |
| 41 |
|
| 42 |
import java.io.FileDescriptor; |
| 43 |
import java.io.FileNotFoundException; |
| 44 |
import java.io.IOException; |
| 45 |
import java.io.InputStream; |
| 46 |
import java.io.PrintWriter; |
| 47 |
import java.lang.ref.Reference; |
| 48 |
import java.util.ArrayList; |
| 49 |
import java.util.Arrays; |
| 50 |
import java.util.Collections; |
| 51 |
import java.util.HashMap; |
| 52 |
import java.util.List; |
| 53 |
import java.util.Locale; |
| 54 |
import java.util.Map; |
| 55 |
import java.util.Objects; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public final class AssetManager implements AutoCloseable { |
| 65 |
private static final String TAG = "AssetManager"; |
| 66 |
private static final boolean DEBUG_REFS = false; |
| 67 |
|
| 68 |
private static final String FRAMEWORK_APK_PATH = "/system/framework/framework-res.apk"; |
| 69 |
|
| 70 |
private static final Object sSync = new Object(); |
| 71 |
|
| 72 |
private static final ApkAssets[] sEmptyApkAssets = new ApkAssets[0]; |
| 73 |
|
| 74 |
|
| 75 |
@UnsupportedAppUsage |
| 76 |
@GuardedBy("sSync") static AssetManager sSystem = null; |
| 77 |
|
| 78 |
@GuardedBy("sSync") private static ApkAssets[] sSystemApkAssets = new ApkAssets[0]; |
| 79 |
@GuardedBy("sSync") private static ArraySet<ApkAssets> sSystemApkAssetsSet; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public static final int COOKIE_UNKNOWN = -1; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public static final int ACCESS_UNKNOWN = 0; |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public static final int ACCESS_RANDOM = 1; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public static final int ACCESS_STREAMING = 2; |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public static final int ACCESS_BUFFER = 3; |
| 108 |
|
| 109 |
@GuardedBy("this") private final TypedValue mValue = new TypedValue(); |
| 110 |
@GuardedBy("this") private final long[] mOffsets = new long[2]; |
| 111 |
|
| 112 |
|
| 113 |
@UnsupportedAppUsage |
| 114 |
@GuardedBy("this") private long mObject; |
| 115 |
|
| 116 |
|
| 117 |
@GuardedBy("this") private ApkAssets[] mApkAssets; |
| 118 |
|
| 119 |
|
| 120 |
@GuardedBy("this") private boolean mOpen = true; |
| 121 |
@GuardedBy("this") private int mNumRefs = 1; |
| 122 |
@GuardedBy("this") private HashMap<Long, RuntimeException> mRefStacks; |
| 123 |
|
| 124 |
private ResourcesLoader[] mLoaders; |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public static class Builder { |
| 135 |
private ArrayList<ApkAssets> mUserApkAssets = new ArrayList<>(); |
| 136 |
private ArrayList<ResourcesLoader> mLoaders = new ArrayList<>(); |
| 137 |
|
| 138 |
public Builder addApkAssets(ApkAssets apkAssets) { |
| 139 |
mUserApkAssets.add(apkAssets); |
| 140 |
return this; |
| 141 |
} |
| 142 |
|
| 143 |
public Builder addLoader(ResourcesLoader loader) { |
| 144 |
mLoaders.add(loader); |
| 145 |
return this; |
| 146 |
} |
| 147 |
|
| 148 |
public AssetManager build() { |
| 149 |
|
| 150 |
final ApkAssets[] systemApkAssets = getSystem().getApkAssets(); |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
final ArrayList<ApkAssets> loaderApkAssets = new ArrayList<>(); |
| 156 |
final ArraySet<ApkAssets> uniqueLoaderApkAssets = new ArraySet<>(); |
| 157 |
for (int i = mLoaders.size() - 1; i >= 0; i--) { |
| 158 |
final List<ApkAssets> currentLoaderApkAssets = mLoaders.get(i).getApkAssets(); |
| 159 |
for (int j = currentLoaderApkAssets.size() - 1; j >= 0; j--) { |
| 160 |
final ApkAssets apkAssets = currentLoaderApkAssets.get(j); |
| 161 |
if (uniqueLoaderApkAssets.add(apkAssets)) { |
| 162 |
loaderApkAssets.add(0, apkAssets); |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
final int totalApkAssetCount = systemApkAssets.length + mUserApkAssets.size() |
| 168 |
+ loaderApkAssets.size(); |
| 169 |
final ApkAssets[] apkAssets = new ApkAssets[totalApkAssetCount]; |
| 170 |
|
| 171 |
System.arraycopy(systemApkAssets, 0, apkAssets, 0, systemApkAssets.length); |
| 172 |
|
| 173 |
|
| 174 |
for (int i = 0, n = mUserApkAssets.size(); i < n; i++) { |
| 175 |
apkAssets[i + systemApkAssets.length] = mUserApkAssets.get(i); |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
for (int i = 0, n = loaderApkAssets.size(); i < n; i++) { |
| 180 |
apkAssets[i + systemApkAssets.length + mUserApkAssets.size()] = |
| 181 |
loaderApkAssets.get(i); |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
final AssetManager assetManager = new AssetManager(false ); |
| 187 |
assetManager.mApkAssets = apkAssets; |
| 188 |
AssetManager.nativeSetApkAssets(assetManager.mObject, apkAssets, |
| 189 |
false ); |
| 190 |
assetManager.mLoaders = mLoaders.isEmpty() ? null |
| 191 |
: mLoaders.toArray(new ResourcesLoader[0]); |
| 192 |
|
| 193 |
return assetManager; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
@UnsupportedAppUsage |
| 205 |
public AssetManager() { |
| 206 |
final ApkAssets[] assets; |
| 207 |
synchronized (sSync) { |
| 208 |
createSystemAssetsInZygoteLocked(false, FRAMEWORK_APK_PATH); |
| 209 |
assets = sSystemApkAssets; |
| 210 |
} |
| 211 |
|
| 212 |
mObject = nativeCreate(); |
| 213 |
if (DEBUG_REFS) { |
| 214 |
mNumRefs = 0; |
| 215 |
incRefsLocked(hashCode()); |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
setApkAssets(assets, false ); |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
@SuppressWarnings("unused") |
| 227 |
private AssetManager(boolean sentinel) { |
| 228 |
mObject = nativeCreate(); |
| 229 |
if (DEBUG_REFS) { |
| 230 |
mNumRefs = 0; |
| 231 |
incRefsLocked(hashCode()); |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
@GuardedBy("sSync") |
| 240 |
@VisibleForTesting |
| 241 |
public static void createSystemAssetsInZygoteLocked(boolean reinitialize, |
| 242 |
String frameworkPath) { |
| 243 |
if (sSystem != null && !reinitialize) { |
| 244 |
return; |
| 245 |
} |
| 246 |
|
| 247 |
try { |
| 248 |
final ArrayList<ApkAssets> apkAssets = new ArrayList<>(); |
| 249 |
apkAssets.add(ApkAssets.loadFromPath(frameworkPath, ApkAssets.PROPERTY_SYSTEM)); |
| 250 |
|
| 251 |
final String[] systemIdmapPaths = |
| 252 |
OverlayConfig.getZygoteInstance().createImmutableFrameworkIdmapsInZygote(); |
| 253 |
for (String idmapPath : systemIdmapPaths) { |
| 254 |
apkAssets.add(ApkAssets.loadOverlayFromPath(idmapPath, ApkAssets.PROPERTY_SYSTEM)); |
| 255 |
} |
| 256 |
|
| 257 |
sSystemApkAssetsSet = new ArraySet<>(apkAssets); |
| 258 |
sSystemApkAssets = apkAssets.toArray(new ApkAssets[apkAssets.size()]); |
| 259 |
if (sSystem == null) { |
| 260 |
sSystem = new AssetManager(true ); |
| 261 |
} |
| 262 |
sSystem.setApkAssets(sSystemApkAssets, false ); |
| 263 |
} catch (IOException e) { |
| 264 |
throw new IllegalStateException("Failed to create system AssetManager", e); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
@UnsupportedAppUsage |
| 274 |
public static AssetManager getSystem() { |
| 275 |
synchronized (sSync) { |
| 276 |
createSystemAssetsInZygoteLocked(false, FRAMEWORK_APK_PATH); |
| 277 |
return sSystem; |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
@Override |
| 285 |
public void close() { |
| 286 |
synchronized (this) { |
| 287 |
if (!mOpen) { |
| 288 |
return; |
| 289 |
} |
| 290 |
|
| 291 |
mOpen = false; |
| 292 |
decRefsLocked(hashCode()); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
public void setApkAssets(@NonNull ApkAssets[] apkAssets, boolean invalidateCaches) { |
| 307 |
Objects.requireNonNull(apkAssets, "apkAssets"); |
| 308 |
|
| 309 |
ApkAssets[] newApkAssets = new ApkAssets[sSystemApkAssets.length + apkAssets.length]; |
| 310 |
|
| 311 |
|
| 312 |
System.arraycopy(sSystemApkAssets, 0, newApkAssets, 0, sSystemApkAssets.length); |
| 313 |
|
| 314 |
|
| 315 |
int newLength = sSystemApkAssets.length; |
| 316 |
for (ApkAssets apkAsset : apkAssets) { |
| 317 |
if (!sSystemApkAssetsSet.contains(apkAsset)) { |
| 318 |
newApkAssets[newLength++] = apkAsset; |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
if (newLength != newApkAssets.length) { |
| 324 |
newApkAssets = Arrays.copyOf(newApkAssets, newLength); |
| 325 |
} |
| 326 |
|
| 327 |
synchronized (this) { |
| 328 |
ensureOpenLocked(); |
| 329 |
mApkAssets = newApkAssets; |
| 330 |
nativeSetApkAssets(mObject, mApkAssets, invalidateCaches); |
| 331 |
if (invalidateCaches) { |
| 332 |
|
| 333 |
invalidateCachesLocked(-1); |
| 334 |
} |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
void setLoaders(@NonNull List<ResourcesLoader> newLoaders) { |
| 343 |
Objects.requireNonNull(newLoaders, "newLoaders"); |
| 344 |
|
| 345 |
final ArrayList<ApkAssets> apkAssets = new ArrayList<>(); |
| 346 |
for (int i = 0; i < mApkAssets.length; i++) { |
| 347 |
|
| 348 |
if (!mApkAssets[i].isForLoader()) { |
| 349 |
apkAssets.add(mApkAssets[i]); |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
if (!newLoaders.isEmpty()) { |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
final int loaderStartIndex = apkAssets.size(); |
| 358 |
final ArraySet<ApkAssets> uniqueLoaderApkAssets = new ArraySet<>(); |
| 359 |
for (int i = newLoaders.size() - 1; i >= 0; i--) { |
| 360 |
final List<ApkAssets> currentLoaderApkAssets = newLoaders.get(i).getApkAssets(); |
| 361 |
for (int j = currentLoaderApkAssets.size() - 1; j >= 0; j--) { |
| 362 |
final ApkAssets loaderApkAssets = currentLoaderApkAssets.get(j); |
| 363 |
if (uniqueLoaderApkAssets.add(loaderApkAssets)) { |
| 364 |
apkAssets.add(loaderStartIndex, loaderApkAssets); |
| 365 |
} |
| 366 |
} |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
mLoaders = newLoaders.toArray(new ResourcesLoader[0]); |
| 371 |
setApkAssets(apkAssets.toArray(new ApkAssets[0]), true ); |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
private void invalidateCachesLocked(int diff) { |
| 381 |
|
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
@UnsupportedAppUsage |
| 390 |
public @NonNull ApkAssets[] getApkAssets() { |
| 391 |
synchronized (this) { |
| 392 |
if (mOpen) { |
| 393 |
return mApkAssets; |
| 394 |
} |
| 395 |
} |
| 396 |
return sEmptyApkAssets; |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
@TestApi |
| 401 |
public @NonNull String[] getApkPaths() { |
| 402 |
synchronized (this) { |
| 403 |
if (mOpen) { |
| 404 |
String[] paths = new String[mApkAssets.length]; |
| 405 |
final int count = mApkAssets.length; |
| 406 |
for (int i = 0; i < count; i++) { |
| 407 |
paths[i] = mApkAssets[i].getAssetPath(); |
| 408 |
} |
| 409 |
return paths; |
| 410 |
} |
| 411 |
} |
| 412 |
return new String[0]; |
| 413 |
} |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
public int findCookieForPath(@NonNull String path) { |
| 422 |
Objects.requireNonNull(path, "path"); |
| 423 |
synchronized (this) { |
| 424 |
ensureValidLocked(); |
| 425 |
final int count = mApkAssets.length; |
| 426 |
for (int i = 0; i < count; i++) { |
| 427 |
if (path.equals(mApkAssets[i].getAssetPath())) { |
| 428 |
return i + 1; |
| 429 |
} |
| 430 |
} |
| 431 |
} |
| 432 |
return 0; |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
@Deprecated |
| 440 |
@UnsupportedAppUsage |
| 441 |
public int addAssetPath(String path) { |
| 442 |
return addAssetPathInternal(path, false , false ); |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
@Deprecated |
| 450 |
@UnsupportedAppUsage |
| 451 |
public int addAssetPathAsSharedLibrary(String path) { |
| 452 |
return addAssetPathInternal(path, false , true ); |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
@Deprecated |
| 460 |
@UnsupportedAppUsage |
| 461 |
public int addOverlayPath(String path) { |
| 462 |
return addAssetPathInternal(path, true , false ); |
| 463 |
} |
| 464 |
|
| 465 |
private int addAssetPathInternal(String path, boolean overlay, boolean appAsLib) { |
| 466 |
Objects.requireNonNull(path, "path"); |
| 467 |
synchronized (this) { |
| 468 |
ensureOpenLocked(); |
| 469 |
final int count = mApkAssets.length; |
| 470 |
|
| 471 |
|
| 472 |
for (int i = 0; i < count; i++) { |
| 473 |
if (mApkAssets[i].getAssetPath().equals(path)) { |
| 474 |
return i + 1; |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
final ApkAssets assets; |
| 479 |
try { |
| 480 |
if (overlay) { |
| 481 |
|
| 482 |
|
| 483 |
final String idmapPath = "/data/resource-cache/" |
| 484 |
+ path.substring(1).replace('/', '@') |
| 485 |
+ "@idmap"; |
| 486 |
assets = ApkAssets.loadOverlayFromPath(idmapPath, 0 ); |
| 487 |
} else { |
| 488 |
assets = ApkAssets.loadFromPath(path, |
| 489 |
appAsLib ? ApkAssets.PROPERTY_DYNAMIC : 0); |
| 490 |
} |
| 491 |
} catch (IOException e) { |
| 492 |
return 0; |
| 493 |
} |
| 494 |
|
| 495 |
mApkAssets = Arrays.copyOf(mApkAssets, count + 1); |
| 496 |
mApkAssets[count] = assets; |
| 497 |
nativeSetApkAssets(mObject, mApkAssets, true); |
| 498 |
invalidateCachesLocked(-1); |
| 499 |
return count + 1; |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
|
| 504 |
@NonNull |
| 505 |
public List<ResourcesLoader> getLoaders() { |
| 506 |
return mLoaders == null ? Collections.emptyList() : Arrays.asList(mLoaders); |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
@GuardedBy("this") |
| 515 |
private void ensureValidLocked() { |
| 516 |
if (mObject == 0) { |
| 517 |
throw new RuntimeException("AssetManager has been destroyed"); |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
@GuardedBy("this") |
| 526 |
private void ensureOpenLocked() { |
| 527 |
|
| 528 |
if (!mOpen) { |
| 529 |
throw new RuntimeException("AssetManager has been closed"); |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
@UnsupportedAppUsage |
| 546 |
boolean getResourceValue(@AnyRes int resId, int densityDpi, @NonNull TypedValue outValue, |
| 547 |
boolean resolveRefs) { |
| 548 |
Objects.requireNonNull(outValue, "outValue"); |
| 549 |
synchronized (this) { |
| 550 |
ensureValidLocked(); |
| 551 |
final int cookie = nativeGetResourceValue( |
| 552 |
mObject, resId, (short) densityDpi, outValue, resolveRefs); |
| 553 |
if (cookie <= 0) { |
| 554 |
return false; |
| 555 |
} |
| 556 |
|
| 557 |
|
| 558 |
outValue.changingConfigurations = ActivityInfo.activityInfoConfigNativeToJava( |
| 559 |
outValue.changingConfigurations); |
| 560 |
|
| 561 |
if (outValue.type == TypedValue.TYPE_STRING) { |
| 562 |
if ((outValue.string = getPooledStringForCookie(cookie, outValue.data)) == null) { |
| 563 |
return false; |
| 564 |
} |
| 565 |
} |
| 566 |
return true; |
| 567 |
} |
| 568 |
} |
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
@UnsupportedAppUsage |
| 578 |
@Nullable CharSequence getResourceText(@StringRes int resId) { |
| 579 |
synchronized (this) { |
| 580 |
final TypedValue outValue = mValue; |
| 581 |
if (getResourceValue(resId, 0, outValue, true)) { |
| 582 |
return outValue.coerceToString(); |
| 583 |
} |
| 584 |
return null; |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
@UnsupportedAppUsage |
| 597 |
@Nullable CharSequence getResourceBagText(@StringRes int resId, int bagEntryId) { |
| 598 |
synchronized (this) { |
| 599 |
ensureValidLocked(); |
| 600 |
final TypedValue outValue = mValue; |
| 601 |
final int cookie = nativeGetResourceBagValue(mObject, resId, bagEntryId, outValue); |
| 602 |
if (cookie <= 0) { |
| 603 |
return null; |
| 604 |
} |
| 605 |
|
| 606 |
|
| 607 |
outValue.changingConfigurations = ActivityInfo.activityInfoConfigNativeToJava( |
| 608 |
outValue.changingConfigurations); |
| 609 |
|
| 610 |
if (outValue.type == TypedValue.TYPE_STRING) { |
| 611 |
return getPooledStringForCookie(cookie, outValue.data); |
| 612 |
} |
| 613 |
return outValue.coerceToString(); |
| 614 |
} |
| 615 |
} |
| 616 |
|
| 617 |
int getResourceArraySize(@ArrayRes int resId) { |
| 618 |
synchronized (this) { |
| 619 |
ensureValidLocked(); |
| 620 |
return nativeGetResourceArraySize(mObject, resId); |
| 621 |
} |
| 622 |
} |
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
int getResourceArray(@ArrayRes int resId, @NonNull int[] outData) { |
| 647 |
Objects.requireNonNull(outData, "outData"); |
| 648 |
synchronized (this) { |
| 649 |
ensureValidLocked(); |
| 650 |
return nativeGetResourceArray(mObject, resId, outData); |
| 651 |
} |
| 652 |
} |
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
@Nullable String[] getResourceStringArray(@ArrayRes int resId) { |
| 662 |
synchronized (this) { |
| 663 |
ensureValidLocked(); |
| 664 |
return nativeGetResourceStringArray(mObject, resId); |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
@Nullable CharSequence[] getResourceTextArray(@ArrayRes int resId) { |
| 675 |
synchronized (this) { |
| 676 |
ensureValidLocked(); |
| 677 |
final int[] rawInfoArray = nativeGetResourceStringArrayInfo(mObject, resId); |
| 678 |
if (rawInfoArray == null) { |
| 679 |
return null; |
| 680 |
} |
| 681 |
|
| 682 |
final int rawInfoArrayLen = rawInfoArray.length; |
| 683 |
final int infoArrayLen = rawInfoArrayLen / 2; |
| 684 |
final CharSequence[] retArray = new CharSequence[infoArrayLen]; |
| 685 |
for (int i = 0, j = 0; i < rawInfoArrayLen; i = i + 2, j++) { |
| 686 |
int cookie = rawInfoArray[i]; |
| 687 |
int index = rawInfoArray[i + 1]; |
| 688 |
retArray[j] = (index >= 0 && cookie > 0) |
| 689 |
? getPooledStringForCookie(cookie, index) : null; |
| 690 |
} |
| 691 |
return retArray; |
| 692 |
} |
| 693 |
} |
| 694 |
|
| 695 |
@Nullable int[] getResourceIntArray(@ArrayRes int resId) { |
| 696 |
synchronized (this) { |
| 697 |
ensureValidLocked(); |
| 698 |
return nativeGetResourceIntArray(mObject, resId); |
| 699 |
} |
| 700 |
} |
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
@AttrRes int[] getStyleAttributes(@StyleRes int resId) { |
| 710 |
synchronized (this) { |
| 711 |
ensureValidLocked(); |
| 712 |
return nativeGetStyleAttributes(mObject, resId); |
| 713 |
} |
| 714 |
} |
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
boolean getThemeValue(long theme, @AnyRes int resId, @NonNull TypedValue outValue, |
| 730 |
boolean resolveRefs) { |
| 731 |
Objects.requireNonNull(outValue, "outValue"); |
| 732 |
synchronized (this) { |
| 733 |
ensureValidLocked(); |
| 734 |
final int cookie = nativeThemeGetAttributeValue(mObject, theme, resId, outValue, |
| 735 |
resolveRefs); |
| 736 |
if (cookie <= 0) { |
| 737 |
return false; |
| 738 |
} |
| 739 |
|
| 740 |
|
| 741 |
outValue.changingConfigurations = ActivityInfo.activityInfoConfigNativeToJava( |
| 742 |
outValue.changingConfigurations); |
| 743 |
|
| 744 |
if (outValue.type == TypedValue.TYPE_STRING) { |
| 745 |
if ((outValue.string = getPooledStringForCookie(cookie, outValue.data)) == null) { |
| 746 |
return false; |
| 747 |
} |
| 748 |
} |
| 749 |
return true; |
| 750 |
} |
| 751 |
} |
| 752 |
|
| 753 |
void dumpTheme(long theme, int priority, String tag, String prefix) { |
| 754 |
synchronized (this) { |
| 755 |
ensureValidLocked(); |
| 756 |
nativeThemeDump(mObject, theme, priority, tag, prefix); |
| 757 |
} |
| 758 |
} |
| 759 |
|
| 760 |
@UnsupportedAppUsage |
| 761 |
@Nullable String getResourceName(@AnyRes int resId) { |
| 762 |
synchronized (this) { |
| 763 |
ensureValidLocked(); |
| 764 |
return nativeGetResourceName(mObject, resId); |
| 765 |
} |
| 766 |
} |
| 767 |
|
| 768 |
@UnsupportedAppUsage |
| 769 |
@Nullable String getResourcePackageName(@AnyRes int resId) { |
| 770 |
synchronized (this) { |
| 771 |
ensureValidLocked(); |
| 772 |
return nativeGetResourcePackageName(mObject, resId); |
| 773 |
} |
| 774 |
} |
| 775 |
|
| 776 |
@UnsupportedAppUsage |
| 777 |
@Nullable String getResourceTypeName(@AnyRes int resId) { |
| 778 |
synchronized (this) { |
| 779 |
ensureValidLocked(); |
| 780 |
return nativeGetResourceTypeName(mObject, resId); |
| 781 |
} |
| 782 |
} |
| 783 |
|
| 784 |
@UnsupportedAppUsage |
| 785 |
@Nullable String getResourceEntryName(@AnyRes int resId) { |
| 786 |
synchronized (this) { |
| 787 |
ensureValidLocked(); |
| 788 |
return nativeGetResourceEntryName(mObject, resId); |
| 789 |
} |
| 790 |
} |
| 791 |
|
| 792 |
@UnsupportedAppUsage |
| 793 |
@AnyRes int getResourceIdentifier(@NonNull String name, @Nullable String defType, |
| 794 |
@Nullable String defPackage) { |
| 795 |
synchronized (this) { |
| 796 |
ensureValidLocked(); |
| 797 |
|
| 798 |
return nativeGetResourceIdentifier(mObject, name, defType, defPackage); |
| 799 |
} |
| 800 |
} |
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
@StyleRes |
| 809 |
int getParentThemeIdentifier(@StyleRes int resId) { |
| 810 |
synchronized (this) { |
| 811 |
ensureValidLocked(); |
| 812 |
|
| 813 |
return nativeGetParentThemeIdentifier(mObject, resId); |
| 814 |
} |
| 815 |
} |
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
@TestApi |
| 828 |
public void setResourceResolutionLoggingEnabled(boolean enabled) { |
| 829 |
synchronized (this) { |
| 830 |
ensureValidLocked(); |
| 831 |
nativeSetResourceResolutionLoggingEnabled(mObject, enabled); |
| 832 |
} |
| 833 |
} |
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
@TestApi |
| 844 |
public @Nullable String getLastResourceResolution() { |
| 845 |
synchronized (this) { |
| 846 |
ensureValidLocked(); |
| 847 |
return nativeGetLastResourceResolution(mObject); |
| 848 |
} |
| 849 |
} |
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
public boolean containsAllocatedTable() { |
| 858 |
synchronized (this) { |
| 859 |
ensureValidLocked(); |
| 860 |
return nativeContainsAllocatedTable(mObject); |
| 861 |
} |
| 862 |
} |
| 863 |
|
| 864 |
@Nullable |
| 865 |
CharSequence getPooledStringForCookie(int cookie, int id) { |
| 866 |
|
| 867 |
return getApkAssets()[cookie - 1].getStringFromPool(id); |
| 868 |
} |
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
public @NonNull InputStream open(@NonNull String fileName) throws IOException { |
| 881 |
return open(fileName, ACCESS_STREAMING); |
| 882 |
} |
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
public @NonNull InputStream open(@NonNull String fileName, int accessMode) throws IOException { |
| 901 |
Objects.requireNonNull(fileName, "fileName"); |
| 902 |
synchronized (this) { |
| 903 |
ensureOpenLocked(); |
| 904 |
final long asset = nativeOpenAsset(mObject, fileName, accessMode); |
| 905 |
if (asset == 0) { |
| 906 |
throw new FileNotFoundException("Asset file: " + fileName); |
| 907 |
} |
| 908 |
final AssetInputStream assetInputStream = new AssetInputStream(asset); |
| 909 |
incRefsLocked(assetInputStream.hashCode()); |
| 910 |
return assetInputStream; |
| 911 |
} |
| 912 |
} |
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
public @NonNull AssetFileDescriptor openFd(@NonNull String fileName) throws IOException { |
| 925 |
Objects.requireNonNull(fileName, "fileName"); |
| 926 |
synchronized (this) { |
| 927 |
ensureOpenLocked(); |
| 928 |
final ParcelFileDescriptor pfd = nativeOpenAssetFd(mObject, fileName, mOffsets); |
| 929 |
if (pfd == null) { |
| 930 |
throw new FileNotFoundException("Asset file: " + fileName); |
| 931 |
} |
| 932 |
return new AssetFileDescriptor(pfd, mOffsets[0], mOffsets[1]); |
| 933 |
} |
| 934 |
} |
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
public @Nullable String[] list(@NonNull String path) throws IOException { |
| 949 |
Objects.requireNonNull(path, "path"); |
| 950 |
synchronized (this) { |
| 951 |
ensureValidLocked(); |
| 952 |
return nativeList(mObject, path); |
| 953 |
} |
| 954 |
} |
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
@UnsupportedAppUsage |
| 968 |
public @NonNull InputStream openNonAsset(@NonNull String fileName) throws IOException { |
| 969 |
return openNonAsset(0, fileName, ACCESS_STREAMING); |
| 970 |
} |
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 989 |
public @NonNull InputStream openNonAsset(@NonNull String fileName, int accessMode) |
| 990 |
throws IOException { |
| 991 |
return openNonAsset(0, fileName, accessMode); |
| 992 |
} |
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 1002 |
public @NonNull InputStream openNonAsset(int cookie, @NonNull String fileName) |
| 1003 |
throws IOException { |
| 1004 |
return openNonAsset(cookie, fileName, ACCESS_STREAMING); |
| 1005 |
} |
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
@UnsupportedAppUsage |
| 1016 |
public @NonNull InputStream openNonAsset(int cookie, @NonNull String fileName, int accessMode) |
| 1017 |
throws IOException { |
| 1018 |
Objects.requireNonNull(fileName, "fileName"); |
| 1019 |
synchronized (this) { |
| 1020 |
ensureOpenLocked(); |
| 1021 |
final long asset = nativeOpenNonAsset(mObject, cookie, fileName, accessMode); |
| 1022 |
if (asset == 0) { |
| 1023 |
throw new FileNotFoundException("Asset absolute file: " + fileName); |
| 1024 |
} |
| 1025 |
final AssetInputStream assetInputStream = new AssetInputStream(asset); |
| 1026 |
incRefsLocked(assetInputStream.hashCode()); |
| 1027 |
return assetInputStream; |
| 1028 |
} |
| 1029 |
} |
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
public @NonNull AssetFileDescriptor openNonAssetFd(@NonNull String fileName) |
| 1041 |
throws IOException { |
| 1042 |
return openNonAssetFd(0, fileName); |
| 1043 |
} |
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
public @NonNull AssetFileDescriptor openNonAssetFd(int cookie, @NonNull String fileName) |
| 1056 |
throws IOException { |
| 1057 |
Objects.requireNonNull(fileName, "fileName"); |
| 1058 |
synchronized (this) { |
| 1059 |
ensureOpenLocked(); |
| 1060 |
final ParcelFileDescriptor pfd = |
| 1061 |
nativeOpenNonAssetFd(mObject, cookie, fileName, mOffsets); |
| 1062 |
if (pfd == null) { |
| 1063 |
throw new FileNotFoundException("Asset absolute file: " + fileName); |
| 1064 |
} |
| 1065 |
return new AssetFileDescriptor(pfd, mOffsets[0], mOffsets[1]); |
| 1066 |
} |
| 1067 |
} |
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
public @NonNull XmlResourceParser openXmlResourceParser(@NonNull String fileName) |
| 1075 |
throws IOException { |
| 1076 |
return openXmlResourceParser(0, fileName); |
| 1077 |
} |
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
public @NonNull XmlResourceParser openXmlResourceParser(int cookie, @NonNull String fileName) |
| 1086 |
throws IOException { |
| 1087 |
try (XmlBlock block = openXmlBlockAsset(cookie, fileName)) { |
| 1088 |
XmlResourceParser parser = block.newParser(); |
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
if (parser == null) { |
| 1093 |
throw new AssertionError("block.newParser() returned a null parser"); |
| 1094 |
} |
| 1095 |
return parser; |
| 1096 |
} |
| 1097 |
} |
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
@NonNull XmlBlock openXmlBlockAsset(@NonNull String fileName) throws IOException { |
| 1106 |
return openXmlBlockAsset(0, fileName); |
| 1107 |
} |
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
@NonNull XmlBlock openXmlBlockAsset(int cookie, @NonNull String fileName) throws IOException { |
| 1118 |
Objects.requireNonNull(fileName, "fileName"); |
| 1119 |
synchronized (this) { |
| 1120 |
ensureOpenLocked(); |
| 1121 |
|
| 1122 |
final long xmlBlock = nativeOpenXmlAsset(mObject, cookie, fileName); |
| 1123 |
if (xmlBlock == 0) { |
| 1124 |
throw new FileNotFoundException("Asset XML file: " + fileName); |
| 1125 |
} |
| 1126 |
final XmlBlock block = new XmlBlock(this, xmlBlock); |
| 1127 |
incRefsLocked(block.hashCode()); |
| 1128 |
return block; |
| 1129 |
} |
| 1130 |
} |
| 1131 |
|
| 1132 |
void xmlBlockGone(int id) { |
| 1133 |
synchronized (this) { |
| 1134 |
decRefsLocked(id); |
| 1135 |
} |
| 1136 |
} |
| 1137 |
|
| 1138 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 1139 |
void applyStyle(long themePtr, @AttrRes int defStyleAttr, @StyleRes int defStyleRes, |
| 1140 |
@Nullable XmlBlock.Parser parser, @NonNull int[] inAttrs, long outValuesAddress, |
| 1141 |
long outIndicesAddress) { |
| 1142 |
Objects.requireNonNull(inAttrs, "inAttrs"); |
| 1143 |
synchronized (this) { |
| 1144 |
|
| 1145 |
|
| 1146 |
ensureValidLocked(); |
| 1147 |
nativeApplyStyle(mObject, themePtr, defStyleAttr, defStyleRes, |
| 1148 |
parser != null ? parser.mParseState : 0, inAttrs, outValuesAddress, |
| 1149 |
outIndicesAddress); |
| 1150 |
} |
| 1151 |
} |
| 1152 |
|
| 1153 |
int[] getAttributeResolutionStack(long themePtr, @AttrRes int defStyleAttr, |
| 1154 |
@StyleRes int defStyleRes, @StyleRes int xmlStyle) { |
| 1155 |
synchronized (this) { |
| 1156 |
return nativeAttributeResolutionStack( |
| 1157 |
mObject, themePtr, xmlStyle, defStyleAttr, defStyleRes); |
| 1158 |
} |
| 1159 |
} |
| 1160 |
|
| 1161 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 1162 |
boolean resolveAttrs(long themePtr, @AttrRes int defStyleAttr, @StyleRes int defStyleRes, |
| 1163 |
@Nullable int[] inValues, @NonNull int[] inAttrs, @NonNull int[] outValues, |
| 1164 |
@NonNull int[] outIndices) { |
| 1165 |
Objects.requireNonNull(inAttrs, "inAttrs"); |
| 1166 |
Objects.requireNonNull(outValues, "outValues"); |
| 1167 |
Objects.requireNonNull(outIndices, "outIndices"); |
| 1168 |
synchronized (this) { |
| 1169 |
|
| 1170 |
|
| 1171 |
ensureValidLocked(); |
| 1172 |
return nativeResolveAttrs(mObject, |
| 1173 |
themePtr, defStyleAttr, defStyleRes, inValues, inAttrs, outValues, outIndices); |
| 1174 |
} |
| 1175 |
} |
| 1176 |
|
| 1177 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 1178 |
boolean retrieveAttributes(@NonNull XmlBlock.Parser parser, @NonNull int[] inAttrs, |
| 1179 |
@NonNull int[] outValues, @NonNull int[] outIndices) { |
| 1180 |
Objects.requireNonNull(parser, "parser"); |
| 1181 |
Objects.requireNonNull(inAttrs, "inAttrs"); |
| 1182 |
Objects.requireNonNull(outValues, "outValues"); |
| 1183 |
Objects.requireNonNull(outIndices, "outIndices"); |
| 1184 |
synchronized (this) { |
| 1185 |
|
| 1186 |
|
| 1187 |
ensureValidLocked(); |
| 1188 |
return nativeRetrieveAttributes( |
| 1189 |
mObject, parser.mParseState, inAttrs, outValues, outIndices); |
| 1190 |
} |
| 1191 |
} |
| 1192 |
|
| 1193 |
@UnsupportedAppUsage |
| 1194 |
long createTheme() { |
| 1195 |
synchronized (this) { |
| 1196 |
ensureValidLocked(); |
| 1197 |
long themePtr = nativeThemeCreate(mObject); |
| 1198 |
incRefsLocked(themePtr); |
| 1199 |
return themePtr; |
| 1200 |
} |
| 1201 |
} |
| 1202 |
|
| 1203 |
void releaseTheme(long themePtr) { |
| 1204 |
synchronized (this) { |
| 1205 |
decRefsLocked(themePtr); |
| 1206 |
} |
| 1207 |
} |
| 1208 |
|
| 1209 |
static long getThemeFreeFunction() { |
| 1210 |
return nativeGetThemeFreeFunction(); |
| 1211 |
} |
| 1212 |
|
| 1213 |
void applyStyleToTheme(long themePtr, @StyleRes int resId, boolean force) { |
| 1214 |
synchronized (this) { |
| 1215 |
|
| 1216 |
|
| 1217 |
ensureValidLocked(); |
| 1218 |
nativeThemeApplyStyle(mObject, themePtr, resId, force); |
| 1219 |
} |
| 1220 |
} |
| 1221 |
|
| 1222 |
AssetManager rebaseTheme(long themePtr, @NonNull AssetManager newAssetManager, |
| 1223 |
@StyleRes int[] styleIds, @StyleRes boolean[] force, int count) { |
| 1224 |
|
| 1225 |
if (this != newAssetManager) { |
| 1226 |
synchronized (this) { |
| 1227 |
ensureValidLocked(); |
| 1228 |
decRefsLocked(themePtr); |
| 1229 |
} |
| 1230 |
synchronized (newAssetManager) { |
| 1231 |
newAssetManager.ensureValidLocked(); |
| 1232 |
newAssetManager.incRefsLocked(themePtr); |
| 1233 |
} |
| 1234 |
} |
| 1235 |
|
| 1236 |
try { |
| 1237 |
synchronized (newAssetManager) { |
| 1238 |
newAssetManager.ensureValidLocked(); |
| 1239 |
nativeThemeRebase(newAssetManager.mObject, themePtr, styleIds, force, count); |
| 1240 |
} |
| 1241 |
} finally { |
| 1242 |
Reference.reachabilityFence(newAssetManager); |
| 1243 |
} |
| 1244 |
return newAssetManager; |
| 1245 |
} |
| 1246 |
|
| 1247 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 1248 |
void setThemeTo(long dstThemePtr, @NonNull AssetManager srcAssetManager, long srcThemePtr) { |
| 1249 |
synchronized (this) { |
| 1250 |
ensureValidLocked(); |
| 1251 |
synchronized (srcAssetManager) { |
| 1252 |
srcAssetManager.ensureValidLocked(); |
| 1253 |
nativeThemeCopy(mObject, dstThemePtr, srcAssetManager.mObject, srcThemePtr); |
| 1254 |
} |
| 1255 |
} |
| 1256 |
} |
| 1257 |
|
| 1258 |
@Override |
| 1259 |
protected void finalize() throws Throwable { |
| 1260 |
if (DEBUG_REFS && mNumRefs != 0) { |
| 1261 |
Log.w(TAG, "AssetManager " + this + " finalized with non-zero refs: " + mNumRefs); |
| 1262 |
if (mRefStacks != null) { |
| 1263 |
for (RuntimeException e : mRefStacks.values()) { |
| 1264 |
Log.w(TAG, "Reference from here", e); |
| 1265 |
} |
| 1266 |
} |
| 1267 |
} |
| 1268 |
|
| 1269 |
synchronized (this) { |
| 1270 |
if (mObject != 0) { |
| 1271 |
nativeDestroy(mObject); |
| 1272 |
mObject = 0; |
| 1273 |
} |
| 1274 |
} |
| 1275 |
} |
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
|
| 1280 |
public final class AssetInputStream extends InputStream { |
| 1281 |
private long mAssetNativePtr; |
| 1282 |
private long mLength; |
| 1283 |
private long mMarkPos; |
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
@UnsupportedAppUsage |
| 1289 |
public final int getAssetInt() { |
| 1290 |
throw new UnsupportedOperationException(); |
| 1291 |
} |
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
@UnsupportedAppUsage |
| 1297 |
public final long getNativeAsset() { |
| 1298 |
return mAssetNativePtr; |
| 1299 |
} |
| 1300 |
|
| 1301 |
private AssetInputStream(long assetNativePtr) { |
| 1302 |
mAssetNativePtr = assetNativePtr; |
| 1303 |
mLength = nativeAssetGetLength(assetNativePtr); |
| 1304 |
} |
| 1305 |
|
| 1306 |
@Override |
| 1307 |
public final int read() throws IOException { |
| 1308 |
ensureOpen(); |
| 1309 |
return nativeAssetReadChar(mAssetNativePtr); |
| 1310 |
} |
| 1311 |
|
| 1312 |
@Override |
| 1313 |
public final int read(@NonNull byte[] b) throws IOException { |
| 1314 |
ensureOpen(); |
| 1315 |
Objects.requireNonNull(b, "b"); |
| 1316 |
return nativeAssetRead(mAssetNativePtr, b, 0, b.length); |
| 1317 |
} |
| 1318 |
|
| 1319 |
@Override |
| 1320 |
public final int read(@NonNull byte[] b, int off, int len) throws IOException { |
| 1321 |
ensureOpen(); |
| 1322 |
Objects.requireNonNull(b, "b"); |
| 1323 |
return nativeAssetRead(mAssetNativePtr, b, off, len); |
| 1324 |
} |
| 1325 |
|
| 1326 |
@Override |
| 1327 |
public final long skip(long n) throws IOException { |
| 1328 |
ensureOpen(); |
| 1329 |
long pos = nativeAssetSeek(mAssetNativePtr, 0, 0); |
| 1330 |
if ((pos + n) > mLength) { |
| 1331 |
n = mLength - pos; |
| 1332 |
} |
| 1333 |
if (n > 0) { |
| 1334 |
nativeAssetSeek(mAssetNativePtr, n, 0); |
| 1335 |
} |
| 1336 |
return n; |
| 1337 |
} |
| 1338 |
|
| 1339 |
@Override |
| 1340 |
public final int available() throws IOException { |
| 1341 |
ensureOpen(); |
| 1342 |
final long len = nativeAssetGetRemainingLength(mAssetNativePtr); |
| 1343 |
return len > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) len; |
| 1344 |
} |
| 1345 |
|
| 1346 |
@Override |
| 1347 |
public final boolean markSupported() { |
| 1348 |
return true; |
| 1349 |
} |
| 1350 |
|
| 1351 |
@Override |
| 1352 |
public final void mark(int readlimit) { |
| 1353 |
ensureOpen(); |
| 1354 |
mMarkPos = nativeAssetSeek(mAssetNativePtr, 0, 0); |
| 1355 |
} |
| 1356 |
|
| 1357 |
@Override |
| 1358 |
public final void reset() throws IOException { |
| 1359 |
ensureOpen(); |
| 1360 |
nativeAssetSeek(mAssetNativePtr, mMarkPos, -1); |
| 1361 |
} |
| 1362 |
|
| 1363 |
@Override |
| 1364 |
public final void close() throws IOException { |
| 1365 |
if (mAssetNativePtr != 0) { |
| 1366 |
nativeAssetDestroy(mAssetNativePtr); |
| 1367 |
mAssetNativePtr = 0; |
| 1368 |
|
| 1369 |
synchronized (AssetManager.this) { |
| 1370 |
decRefsLocked(hashCode()); |
| 1371 |
} |
| 1372 |
} |
| 1373 |
} |
| 1374 |
|
| 1375 |
@Override |
| 1376 |
protected void finalize() throws Throwable { |
| 1377 |
close(); |
| 1378 |
} |
| 1379 |
|
| 1380 |
private void ensureOpen() { |
| 1381 |
if (mAssetNativePtr == 0) { |
| 1382 |
throw new IllegalStateException("AssetInputStream is closed"); |
| 1383 |
} |
| 1384 |
} |
| 1385 |
} |
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
@UnsupportedAppUsage |
| 1394 |
public boolean isUpToDate() { |
| 1395 |
synchronized (this) { |
| 1396 |
if (!mOpen) { |
| 1397 |
return false; |
| 1398 |
} |
| 1399 |
|
| 1400 |
for (ApkAssets apkAssets : mApkAssets) { |
| 1401 |
if (!apkAssets.isUpToDate()) { |
| 1402 |
return false; |
| 1403 |
} |
| 1404 |
} |
| 1405 |
|
| 1406 |
return true; |
| 1407 |
} |
| 1408 |
} |
| 1409 |
|
| 1410 |
|
| 1411 |
|
| 1412 |
|
| 1413 |
|
| 1414 |
|
| 1415 |
|
| 1416 |
|
| 1417 |
|
| 1418 |
|
| 1419 |
|
| 1420 |
|
| 1421 |
public String[] getLocales() { |
| 1422 |
synchronized (this) { |
| 1423 |
ensureValidLocked(); |
| 1424 |
return nativeGetLocales(mObject, false ); |
| 1425 |
} |
| 1426 |
} |
| 1427 |
|
| 1428 |
|
| 1429 |
|
| 1430 |
|
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
|
| 1435 |
|
| 1436 |
|
| 1437 |
|
| 1438 |
public String[] getNonSystemLocales() { |
| 1439 |
synchronized (this) { |
| 1440 |
ensureValidLocked(); |
| 1441 |
return nativeGetLocales(mObject, true ); |
| 1442 |
} |
| 1443 |
} |
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
Configuration[] getSizeConfigurations() { |
| 1449 |
synchronized (this) { |
| 1450 |
ensureValidLocked(); |
| 1451 |
return nativeGetSizeConfigurations(mObject); |
| 1452 |
} |
| 1453 |
} |
| 1454 |
|
| 1455 |
|
| 1456 |
|
| 1457 |
|
| 1458 |
|
| 1459 |
|
| 1460 |
@UnsupportedAppUsage |
| 1461 |
public void setConfiguration(int mcc, int mnc, @Nullable String locale, int orientation, |
| 1462 |
int touchscreen, int density, int keyboard, int keyboardHidden, int navigation, |
| 1463 |
int screenWidth, int screenHeight, int smallestScreenWidthDp, int screenWidthDp, |
| 1464 |
int screenHeightDp, int screenLayout, int uiMode, int colorMode, int majorVersion) { |
| 1465 |
synchronized (this) { |
| 1466 |
ensureValidLocked(); |
| 1467 |
nativeSetConfiguration(mObject, mcc, mnc, locale, orientation, touchscreen, density, |
| 1468 |
keyboard, keyboardHidden, navigation, screenWidth, screenHeight, |
| 1469 |
smallestScreenWidthDp, screenWidthDp, screenHeightDp, screenLayout, uiMode, |
| 1470 |
colorMode, majorVersion); |
| 1471 |
} |
| 1472 |
} |
| 1473 |
|
| 1474 |
|
| 1475 |
|
| 1476 |
|
| 1477 |
@UnsupportedAppUsage |
| 1478 |
public SparseArray<String> getAssignedPackageIdentifiers() { |
| 1479 |
return getAssignedPackageIdentifiers(true, true); |
| 1480 |
} |
| 1481 |
|
| 1482 |
|
| 1483 |
|
| 1484 |
|
| 1485 |
public SparseArray<String> getAssignedPackageIdentifiers(boolean includeOverlays, |
| 1486 |
boolean includeLoaders) { |
| 1487 |
synchronized (this) { |
| 1488 |
ensureValidLocked(); |
| 1489 |
return nativeGetAssignedPackageIdentifiers(mObject, includeOverlays, includeLoaders); |
| 1490 |
} |
| 1491 |
} |
| 1492 |
|
| 1493 |
|
| 1494 |
|
| 1495 |
|
| 1496 |
@GuardedBy("this") |
| 1497 |
public @Nullable Map<String, String> getOverlayableMap(String packageName) { |
| 1498 |
synchronized (this) { |
| 1499 |
ensureValidLocked(); |
| 1500 |
return nativeGetOverlayableMap(mObject, packageName); |
| 1501 |
} |
| 1502 |
} |
| 1503 |
|
| 1504 |
|
| 1505 |
|
| 1506 |
|
| 1507 |
@TestApi |
| 1508 |
@GuardedBy("this") |
| 1509 |
public @Nullable String getOverlayablesToString(String packageName) { |
| 1510 |
synchronized (this) { |
| 1511 |
ensureValidLocked(); |
| 1512 |
return nativeGetOverlayablesToString(mObject, packageName); |
| 1513 |
} |
| 1514 |
} |
| 1515 |
|
| 1516 |
@GuardedBy("this") |
| 1517 |
private void incRefsLocked(long id) { |
| 1518 |
if (DEBUG_REFS) { |
| 1519 |
if (mRefStacks == null) { |
| 1520 |
mRefStacks = new HashMap<>(); |
| 1521 |
} |
| 1522 |
RuntimeException ex = new RuntimeException(); |
| 1523 |
ex.fillInStackTrace(); |
| 1524 |
mRefStacks.put(id, ex); |
| 1525 |
} |
| 1526 |
mNumRefs++; |
| 1527 |
} |
| 1528 |
|
| 1529 |
@GuardedBy("this") |
| 1530 |
private void decRefsLocked(long id) { |
| 1531 |
if (DEBUG_REFS && mRefStacks != null) { |
| 1532 |
mRefStacks.remove(id); |
| 1533 |
} |
| 1534 |
mNumRefs--; |
| 1535 |
if (mNumRefs == 0 && mObject != 0) { |
| 1536 |
nativeDestroy(mObject); |
| 1537 |
mObject = 0; |
| 1538 |
mApkAssets = sEmptyApkAssets; |
| 1539 |
} |
| 1540 |
} |
| 1541 |
|
| 1542 |
synchronized void dump(PrintWriter pw, String prefix) { |
| 1543 |
pw.println(prefix + "class=" + getClass()); |
| 1544 |
pw.println(prefix + "apkAssets="); |
| 1545 |
for (int i = 0; i < mApkAssets.length; i++) { |
| 1546 |
pw.println(prefix + i); |
| 1547 |
mApkAssets[i].dump(pw, prefix + " "); |
| 1548 |
} |
| 1549 |
} |
| 1550 |
|
| 1551 |
|
| 1552 |
private static native long nativeCreate(); |
| 1553 |
private static native void nativeDestroy(long ptr); |
| 1554 |
private static native void nativeSetApkAssets(long ptr, @NonNull ApkAssets[] apkAssets, |
| 1555 |
boolean invalidateCaches); |
| 1556 |
private static native void nativeSetConfiguration(long ptr, int mcc, int mnc, |
| 1557 |
@Nullable String locale, int orientation, int touchscreen, int density, int keyboard, |
| 1558 |
int keyboardHidden, int navigation, int screenWidth, int screenHeight, |
| 1559 |
int smallestScreenWidthDp, int screenWidthDp, int screenHeightDp, int screenLayout, |
| 1560 |
int uiMode, int colorMode, int majorVersion); |
| 1561 |
private static native @NonNull SparseArray<String> nativeGetAssignedPackageIdentifiers( |
| 1562 |
long ptr, boolean includeOverlays, boolean includeLoaders); |
| 1563 |
|
| 1564 |
|
| 1565 |
private static native boolean nativeContainsAllocatedTable(long ptr); |
| 1566 |
private static native @Nullable String[] nativeList(long ptr, @NonNull String path) |
| 1567 |
throws IOException; |
| 1568 |
private static native long nativeOpenAsset(long ptr, @NonNull String fileName, int accessMode); |
| 1569 |
private static native @Nullable ParcelFileDescriptor nativeOpenAssetFd(long ptr, |
| 1570 |
@NonNull String fileName, long[] outOffsets) throws IOException; |
| 1571 |
private static native long nativeOpenNonAsset(long ptr, int cookie, @NonNull String fileName, |
| 1572 |
int accessMode); |
| 1573 |
private static native @Nullable ParcelFileDescriptor nativeOpenNonAssetFd(long ptr, int cookie, |
| 1574 |
@NonNull String fileName, @NonNull long[] outOffsets) throws IOException; |
| 1575 |
private static native long nativeOpenXmlAsset(long ptr, int cookie, @NonNull String fileName); |
| 1576 |
private static native long nativeOpenXmlAssetFd(long ptr, int cookie, |
| 1577 |
@NonNull FileDescriptor fileDescriptor); |
| 1578 |
|
| 1579 |
|
| 1580 |
private static native int nativeGetResourceValue(long ptr, @AnyRes int resId, short density, |
| 1581 |
@NonNull TypedValue outValue, boolean resolveReferences); |
| 1582 |
private static native int nativeGetResourceBagValue(long ptr, @AnyRes int resId, int bagEntryId, |
| 1583 |
@NonNull TypedValue outValue); |
| 1584 |
|
| 1585 |
private static native @Nullable @AttrRes int[] nativeGetStyleAttributes(long ptr, |
| 1586 |
@StyleRes int resId); |
| 1587 |
private static native @Nullable String[] nativeGetResourceStringArray(long ptr, |
| 1588 |
@ArrayRes int resId); |
| 1589 |
private static native @Nullable int[] nativeGetResourceStringArrayInfo(long ptr, |
| 1590 |
@ArrayRes int resId); |
| 1591 |
private static native @Nullable int[] nativeGetResourceIntArray(long ptr, @ArrayRes int resId); |
| 1592 |
private static native int nativeGetResourceArraySize(long ptr, @ArrayRes int resId); |
| 1593 |
private static native int nativeGetResourceArray(long ptr, @ArrayRes int resId, |
| 1594 |
@NonNull int[] outValues); |
| 1595 |
|
| 1596 |
|
| 1597 |
private static native @AnyRes int nativeGetResourceIdentifier(long ptr, @NonNull String name, |
| 1598 |
@Nullable String defType, @Nullable String defPackage); |
| 1599 |
private static native @Nullable String nativeGetResourceName(long ptr, @AnyRes int resid); |
| 1600 |
private static native @Nullable String nativeGetResourcePackageName(long ptr, |
| 1601 |
@AnyRes int resid); |
| 1602 |
private static native @Nullable String nativeGetResourceTypeName(long ptr, @AnyRes int resid); |
| 1603 |
private static native @Nullable String nativeGetResourceEntryName(long ptr, @AnyRes int resid); |
| 1604 |
private static native @Nullable String[] nativeGetLocales(long ptr, boolean excludeSystem); |
| 1605 |
private static native @Nullable Configuration[] nativeGetSizeConfigurations(long ptr); |
| 1606 |
private static native void nativeSetResourceResolutionLoggingEnabled(long ptr, boolean enabled); |
| 1607 |
private static native @Nullable String nativeGetLastResourceResolution(long ptr); |
| 1608 |
|
| 1609 |
|
| 1610 |
private static native int[] nativeAttributeResolutionStack(long ptr, long themePtr, |
| 1611 |
@StyleRes int xmlStyleRes, @AttrRes int defStyleAttr, @StyleRes int defStyleRes); |
| 1612 |
private static native void nativeApplyStyle(long ptr, long themePtr, @AttrRes int defStyleAttr, |
| 1613 |
@StyleRes int defStyleRes, long xmlParserPtr, @NonNull int[] inAttrs, |
| 1614 |
long outValuesAddress, long outIndicesAddress); |
| 1615 |
private static native boolean nativeResolveAttrs(long ptr, long themePtr, |
| 1616 |
@AttrRes int defStyleAttr, @StyleRes int defStyleRes, @Nullable int[] inValues, |
| 1617 |
@NonNull int[] inAttrs, @NonNull int[] outValues, @NonNull int[] outIndices); |
| 1618 |
private static native boolean nativeRetrieveAttributes(long ptr, long xmlParserPtr, |
| 1619 |
@NonNull int[] inAttrs, @NonNull int[] outValues, @NonNull int[] outIndices); |
| 1620 |
|
| 1621 |
|
| 1622 |
private static native long nativeThemeCreate(long ptr); |
| 1623 |
private static native long nativeGetThemeFreeFunction(); |
| 1624 |
private static native void nativeThemeApplyStyle(long ptr, long themePtr, @StyleRes int resId, |
| 1625 |
boolean force); |
| 1626 |
private static native void nativeThemeRebase(long ptr, long themePtr, @NonNull int[] styleIds, |
| 1627 |
@NonNull boolean[] force, int styleSize); |
| 1628 |
private static native void nativeThemeCopy(long dstAssetManagerPtr, long dstThemePtr, |
| 1629 |
long srcAssetManagerPtr, long srcThemePtr); |
| 1630 |
private static native int nativeThemeGetAttributeValue(long ptr, long themePtr, |
| 1631 |
@AttrRes int resId, @NonNull TypedValue outValue, boolean resolve); |
| 1632 |
private static native void nativeThemeDump(long ptr, long themePtr, int priority, String tag, |
| 1633 |
String prefix); |
| 1634 |
static native @NativeConfig int nativeThemeGetChangingConfigurations(long themePtr); |
| 1635 |
@StyleRes |
| 1636 |
private static native int nativeGetParentThemeIdentifier(long ptr, @StyleRes int styleId); |
| 1637 |
|
| 1638 |
|
| 1639 |
private static native void nativeAssetDestroy(long assetPtr); |
| 1640 |
private static native int nativeAssetReadChar(long assetPtr); |
| 1641 |
private static native int nativeAssetRead(long assetPtr, byte[] b, int off, int len); |
| 1642 |
private static native long nativeAssetSeek(long assetPtr, long offset, int whence); |
| 1643 |
private static native long nativeAssetGetLength(long assetPtr); |
| 1644 |
private static native long nativeAssetGetRemainingLength(long assetPtr); |
| 1645 |
|
| 1646 |
private static native @Nullable Map nativeGetOverlayableMap(long ptr, |
| 1647 |
@NonNull String packageName); |
| 1648 |
private static native @Nullable String nativeGetOverlayablesToString(long ptr, |
| 1649 |
@NonNull String packageName); |
| 1650 |
|
| 1651 |
|
| 1652 |
|
| 1653 |
|
| 1654 |
|
| 1655 |
@UnsupportedAppUsage |
| 1656 |
public static native int getGlobalAssetCount(); |
| 1657 |
|
| 1658 |
|
| 1659 |
|
| 1660 |
|
| 1661 |
public static native String getAssetAllocations(); |
| 1662 |
|
| 1663 |
|
| 1664 |
|
| 1665 |
|
| 1666 |
@UnsupportedAppUsage |
| 1667 |
public static native int getGlobalAssetManagerCount(); |
| 1668 |
} |