| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package android.content.res; |
| 17 |
|
| 18 |
import android.annotation.IntDef; |
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.annotation.Nullable; |
| 21 |
import android.compat.annotation.UnsupportedAppUsage; |
| 22 |
import android.content.om.OverlayableInfo; |
| 23 |
import android.content.res.loader.AssetsProvider; |
| 24 |
import android.content.res.loader.ResourcesProvider; |
| 25 |
import android.text.TextUtils; |
| 26 |
|
| 27 |
import com.android.internal.annotations.GuardedBy; |
| 28 |
|
| 29 |
import java.io.FileDescriptor; |
| 30 |
import java.io.IOException; |
| 31 |
import java.io.PrintWriter; |
| 32 |
import java.lang.annotation.Retention; |
| 33 |
import java.lang.annotation.RetentionPolicy; |
| 34 |
import java.util.Objects; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public final class ApkAssets { |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public static final int PROPERTY_SYSTEM = 1 << 0; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public static final int PROPERTY_DYNAMIC = 1 << 1; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public static final int PROPERTY_LOADER = 1 << 2; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
private static final int PROPERTY_OVERLAY = 1 << 3; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public static final int PROPERTY_DISABLE_INCREMENTAL_HARDENING = 1 << 4; |
| 78 |
|
| 79 |
|
| 80 |
@IntDef(prefix = { "PROPERTY_" }, value = { |
| 81 |
PROPERTY_SYSTEM, |
| 82 |
PROPERTY_DYNAMIC, |
| 83 |
PROPERTY_LOADER, |
| 84 |
PROPERTY_OVERLAY, |
| 85 |
}) |
| 86 |
@Retention(RetentionPolicy.SOURCE) |
| 87 |
public @interface PropertyFlags {} |
| 88 |
|
| 89 |
|
| 90 |
private static final int FORMAT_APK = 0; |
| 91 |
|
| 92 |
|
| 93 |
private static final int FORMAT_IDMAP = 1; |
| 94 |
|
| 95 |
|
| 96 |
private static final int FORMAT_ARSC = 2; |
| 97 |
|
| 98 |
|
| 99 |
private static final int FORMAT_DIR = 3; |
| 100 |
|
| 101 |
|
| 102 |
@IntDef(prefix = { "FORMAT_" }, value = { |
| 103 |
FORMAT_APK, |
| 104 |
FORMAT_IDMAP, |
| 105 |
FORMAT_ARSC, |
| 106 |
FORMAT_DIR |
| 107 |
}) |
| 108 |
@Retention(RetentionPolicy.SOURCE) |
| 109 |
public @interface FormatType {} |
| 110 |
|
| 111 |
@GuardedBy("this") |
| 112 |
private long mNativePtr; |
| 113 |
|
| 114 |
@Nullable |
| 115 |
@GuardedBy("this") |
| 116 |
private final StringBlock mStringBlock; |
| 117 |
|
| 118 |
@PropertyFlags |
| 119 |
private final int mFlags; |
| 120 |
|
| 121 |
@Nullable |
| 122 |
private final AssetsProvider mAssets; |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public static @NonNull ApkAssets loadFromPath(@NonNull String path) throws IOException { |
| 132 |
return loadFromPath(path, 0 ); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public static @NonNull ApkAssets loadFromPath(@NonNull String path, @PropertyFlags int flags) |
| 144 |
throws IOException { |
| 145 |
return new ApkAssets(FORMAT_APK, path, flags, null ); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
public static @NonNull ApkAssets loadFromPath(@NonNull String path, @PropertyFlags int flags, |
| 158 |
@Nullable AssetsProvider assets) throws IOException { |
| 159 |
return new ApkAssets(FORMAT_APK, path, flags, assets); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
public static @NonNull ApkAssets loadFromFd(@NonNull FileDescriptor fd, |
| 176 |
@NonNull String friendlyName, @PropertyFlags int flags, |
| 177 |
@Nullable AssetsProvider assets) throws IOException { |
| 178 |
return new ApkAssets(FORMAT_APK, fd, friendlyName, flags, assets); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
public static @NonNull ApkAssets loadFromFd(@NonNull FileDescriptor fd, |
| 199 |
@NonNull String friendlyName, long offset, long length, @PropertyFlags int flags, |
| 200 |
@Nullable AssetsProvider assets) |
| 201 |
throws IOException { |
| 202 |
return new ApkAssets(FORMAT_APK, fd, friendlyName, offset, length, flags, assets); |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
public static @NonNull ApkAssets loadOverlayFromPath(@NonNull String idmapPath, |
| 215 |
@PropertyFlags int flags) throws IOException { |
| 216 |
return new ApkAssets(FORMAT_IDMAP, idmapPath, flags, null ); |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
public static @NonNull ApkAssets loadTableFromFd(@NonNull FileDescriptor fd, |
| 234 |
@NonNull String friendlyName, @PropertyFlags int flags, |
| 235 |
@Nullable AssetsProvider assets) throws IOException { |
| 236 |
return new ApkAssets(FORMAT_ARSC, fd, friendlyName, flags, assets); |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
public static @NonNull ApkAssets loadTableFromFd(@NonNull FileDescriptor fd, |
| 258 |
@NonNull String friendlyName, long offset, long length, @PropertyFlags int flags, |
| 259 |
@Nullable AssetsProvider assets) throws IOException { |
| 260 |
return new ApkAssets(FORMAT_ARSC, fd, friendlyName, offset, length, flags, assets); |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
public static @NonNull ApkAssets loadFromDir(@NonNull String path, |
| 274 |
@PropertyFlags int flags, @Nullable AssetsProvider assets) throws IOException { |
| 275 |
return new ApkAssets(FORMAT_DIR, path, flags, assets); |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
@NonNull |
| 287 |
public static ApkAssets loadEmptyForLoader(@PropertyFlags int flags, |
| 288 |
@Nullable AssetsProvider assets) { |
| 289 |
return new ApkAssets(flags, assets); |
| 290 |
} |
| 291 |
|
| 292 |
private ApkAssets(@FormatType int format, @NonNull String path, @PropertyFlags int flags, |
| 293 |
@Nullable AssetsProvider assets) throws IOException { |
| 294 |
Objects.requireNonNull(path, "path"); |
| 295 |
mFlags = flags; |
| 296 |
mNativePtr = nativeLoad(format, path, flags, assets); |
| 297 |
mStringBlock = new StringBlock(nativeGetStringBlock(mNativePtr), true ); |
| 298 |
mAssets = assets; |
| 299 |
} |
| 300 |
|
| 301 |
private ApkAssets(@FormatType int format, @NonNull FileDescriptor fd, |
| 302 |
@NonNull String friendlyName, @PropertyFlags int flags, @Nullable AssetsProvider assets) |
| 303 |
throws IOException { |
| 304 |
Objects.requireNonNull(fd, "fd"); |
| 305 |
Objects.requireNonNull(friendlyName, "friendlyName"); |
| 306 |
mFlags = flags; |
| 307 |
mNativePtr = nativeLoadFd(format, fd, friendlyName, flags, assets); |
| 308 |
mStringBlock = new StringBlock(nativeGetStringBlock(mNativePtr), true ); |
| 309 |
mAssets = assets; |
| 310 |
} |
| 311 |
|
| 312 |
private ApkAssets(@FormatType int format, @NonNull FileDescriptor fd, |
| 313 |
@NonNull String friendlyName, long offset, long length, @PropertyFlags int flags, |
| 314 |
@Nullable AssetsProvider assets) throws IOException { |
| 315 |
Objects.requireNonNull(fd, "fd"); |
| 316 |
Objects.requireNonNull(friendlyName, "friendlyName"); |
| 317 |
mFlags = flags; |
| 318 |
mNativePtr = nativeLoadFdOffsets(format, fd, friendlyName, offset, length, flags, assets); |
| 319 |
mStringBlock = new StringBlock(nativeGetStringBlock(mNativePtr), true ); |
| 320 |
mAssets = assets; |
| 321 |
} |
| 322 |
|
| 323 |
private ApkAssets(@PropertyFlags int flags, @Nullable AssetsProvider assets) { |
| 324 |
mFlags = flags; |
| 325 |
mNativePtr = nativeLoadEmpty(flags, assets); |
| 326 |
mStringBlock = null; |
| 327 |
mAssets = assets; |
| 328 |
} |
| 329 |
|
| 330 |
@UnsupportedAppUsage |
| 331 |
public @NonNull String getAssetPath() { |
| 332 |
synchronized (this) { |
| 333 |
return TextUtils.emptyIfNull(nativeGetAssetPath(mNativePtr)); |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
public @NonNull String getDebugName() { |
| 339 |
synchronized (this) { |
| 340 |
return nativeGetDebugName(mNativePtr); |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
@Nullable |
| 345 |
CharSequence getStringFromPool(int idx) { |
| 346 |
if (mStringBlock == null) { |
| 347 |
return null; |
| 348 |
} |
| 349 |
|
| 350 |
synchronized (this) { |
| 351 |
return mStringBlock.getSequence(idx); |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
public boolean isForLoader() { |
| 357 |
return (mFlags & PROPERTY_LOADER) != 0; |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
@Nullable |
| 364 |
public AssetsProvider getAssetsProvider() { |
| 365 |
return mAssets; |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
public @NonNull XmlResourceParser openXml(@NonNull String fileName) throws IOException { |
| 378 |
Objects.requireNonNull(fileName, "fileName"); |
| 379 |
synchronized (this) { |
| 380 |
long nativeXmlPtr = nativeOpenXml(mNativePtr, fileName); |
| 381 |
try (XmlBlock block = new XmlBlock(null, nativeXmlPtr)) { |
| 382 |
XmlResourceParser parser = block.newParser(); |
| 383 |
|
| 384 |
|
| 385 |
if (parser == null) { |
| 386 |
throw new AssertionError("block.newParser() returned a null parser"); |
| 387 |
} |
| 388 |
return parser; |
| 389 |
} |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
@Nullable |
| 395 |
public OverlayableInfo getOverlayableInfo(String overlayableName) throws IOException { |
| 396 |
synchronized (this) { |
| 397 |
return nativeGetOverlayableInfo(mNativePtr, overlayableName); |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
public boolean definesOverlayable() throws IOException { |
| 403 |
synchronized (this) { |
| 404 |
return nativeDefinesOverlayable(mNativePtr); |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
public boolean isUpToDate() { |
| 412 |
synchronized (this) { |
| 413 |
return nativeIsUpToDate(mNativePtr); |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
@Override |
| 418 |
public String toString() { |
| 419 |
return "ApkAssets{path=" + getDebugName() + "}"; |
| 420 |
} |
| 421 |
|
| 422 |
@Override |
| 423 |
protected void finalize() throws Throwable { |
| 424 |
close(); |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
public void close() { |
| 431 |
synchronized (this) { |
| 432 |
if (mNativePtr != 0) { |
| 433 |
if (mStringBlock != null) { |
| 434 |
mStringBlock.close(); |
| 435 |
} |
| 436 |
nativeDestroy(mNativePtr); |
| 437 |
mNativePtr = 0; |
| 438 |
} |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
void dump(PrintWriter pw, String prefix) { |
| 443 |
pw.println(prefix + "class=" + getClass()); |
| 444 |
pw.println(prefix + "debugName=" + getDebugName()); |
| 445 |
pw.println(prefix + "assetPath=" + getAssetPath()); |
| 446 |
} |
| 447 |
|
| 448 |
private static native long nativeLoad(@FormatType int format, @NonNull String path, |
| 449 |
@PropertyFlags int flags, @Nullable AssetsProvider asset) throws IOException; |
| 450 |
private static native long nativeLoadEmpty(@PropertyFlags int flags, |
| 451 |
@Nullable AssetsProvider asset); |
| 452 |
private static native long nativeLoadFd(@FormatType int format, @NonNull FileDescriptor fd, |
| 453 |
@NonNull String friendlyName, @PropertyFlags int flags, |
| 454 |
@Nullable AssetsProvider asset) throws IOException; |
| 455 |
private static native long nativeLoadFdOffsets(@FormatType int format, |
| 456 |
@NonNull FileDescriptor fd, @NonNull String friendlyName, long offset, long length, |
| 457 |
@PropertyFlags int flags, @Nullable AssetsProvider asset) throws IOException; |
| 458 |
private static native void nativeDestroy(long ptr); |
| 459 |
private static native @NonNull String nativeGetAssetPath(long ptr); |
| 460 |
private static native @NonNull String nativeGetDebugName(long ptr); |
| 461 |
private static native long nativeGetStringBlock(long ptr); |
| 462 |
private static native boolean nativeIsUpToDate(long ptr); |
| 463 |
private static native long nativeOpenXml(long ptr, @NonNull String fileName) throws IOException; |
| 464 |
private static native @Nullable OverlayableInfo nativeGetOverlayableInfo(long ptr, |
| 465 |
String overlayableName) throws IOException; |
| 466 |
private static native boolean nativeDefinesOverlayable(long ptr) throws IOException; |
| 467 |
} |