Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | package android.content.res; |
| 17 | |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 18 | import android.annotation.IntDef; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 19 | import android.annotation.NonNull; |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 20 | import android.annotation.Nullable; |
Artur Satayev | e23a0eb | 2019-12-10 17:47:52 +0000 | [diff] [blame] | 21 | import android.compat.annotation.UnsupportedAppUsage; |
Winson | d9d1736 | 2019-10-02 12:41:29 -0700 | [diff] [blame] | 22 | import android.content.om.OverlayableInfo; |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 23 | import android.content.res.loader.ResourcesProvider; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 24 | |
| 25 | import com.android.internal.annotations.GuardedBy; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 26 | |
| 27 | import java.io.FileDescriptor; |
| 28 | import java.io.IOException; |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 29 | import java.lang.annotation.Retention; |
| 30 | import java.lang.annotation.RetentionPolicy; |
Daulet Zhanguzin | a2044e1 | 2019-12-30 16:34:59 +0000 | [diff] [blame] | 31 | import java.util.Objects; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * The loaded, immutable, in-memory representation of an APK. |
| 35 | * |
| 36 | * The main implementation is native C++ and there is very little API surface exposed here. The APK |
| 37 | * is mainly accessed via {@link AssetManager}. |
| 38 | * |
| 39 | * Since the ApkAssets instance is immutable, it can be reused and shared across AssetManagers, |
| 40 | * making the creation of AssetManagers very cheap. |
| 41 | * @hide |
| 42 | */ |
| 43 | public final class ApkAssets { |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * The apk assets contains framework resource values specified by the system. |
| 47 | * This allows some functions to filter out this package when computing what |
| 48 | * configurations/resources are available. |
| 49 | */ |
| 50 | public static final int PROPERTY_SYSTEM = 1 << 0; |
| 51 | |
| 52 | /** |
| 53 | * The apk assets is a shared library or was loaded as a shared library by force. |
| 54 | * The package ids of dynamic apk assets are assigned at runtime instead of compile time. |
| 55 | */ |
| 56 | public static final int PROPERTY_DYNAMIC = 1 << 1; |
| 57 | |
| 58 | /** |
| 59 | * The apk assets has been loaded dynamically using a {@link ResourcesProvider}. |
| 60 | * Loader apk assets overlay resources like RROs except they are not backed by an idmap. |
| 61 | */ |
| 62 | public static final int PROPERTY_LOADER = 1 << 2; |
| 63 | |
| 64 | /** |
| 65 | * The apk assets is a RRO. |
| 66 | * An RRO overlays resource values of its target package. |
| 67 | */ |
| 68 | private static final int PROPERTY_OVERLAY = 1 << 3; |
| 69 | |
| 70 | /** Flags that change the behavior of loaded apk assets. */ |
| 71 | @IntDef(prefix = { "PROPERTY_" }, value = { |
| 72 | PROPERTY_SYSTEM, |
| 73 | PROPERTY_DYNAMIC, |
| 74 | PROPERTY_LOADER, |
| 75 | PROPERTY_OVERLAY, |
| 76 | }) |
| 77 | @Retention(RetentionPolicy.SOURCE) |
| 78 | public @interface PropertyFlags {} |
| 79 | |
| 80 | /** The path used to load the apk assets represents an APK file. */ |
| 81 | private static final int FORMAT_APK = 0; |
| 82 | |
| 83 | /** The path used to load the apk assets represents an idmap file. */ |
| 84 | private static final int FORMAT_IDMAP = 1; |
| 85 | |
| 86 | /** The path used to load the apk assets represents an resources.arsc file. */ |
| 87 | private static final int FORMAT_ARSC = 2; |
| 88 | |
Ryan Mitchell | c07aa70 | 2020-03-10 13:49:12 -0700 | [diff] [blame^] | 89 | /** the path used to load the apk assets represents a directory. */ |
| 90 | private static final int FORMAT_DIR = 3; |
| 91 | |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 92 | // Format types that change how the apk assets are loaded. |
| 93 | @IntDef(prefix = { "FORMAT_" }, value = { |
| 94 | FORMAT_APK, |
| 95 | FORMAT_IDMAP, |
| 96 | FORMAT_ARSC, |
Ryan Mitchell | c07aa70 | 2020-03-10 13:49:12 -0700 | [diff] [blame^] | 97 | FORMAT_DIR |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 98 | }) |
| 99 | @Retention(RetentionPolicy.SOURCE) |
| 100 | public @interface FormatType {} |
| 101 | |
| 102 | @GuardedBy("this") |
| 103 | private final long mNativePtr; |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 104 | |
| 105 | @Nullable |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 106 | @GuardedBy("this") |
| 107 | private final StringBlock mStringBlock; |
Winson | 7a3d82a | 2019-03-07 12:54:24 -0800 | [diff] [blame] | 108 | |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 109 | @GuardedBy("this") |
| 110 | private boolean mOpen = true; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 111 | |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 112 | @PropertyFlags |
| 113 | private final int mFlags; |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 114 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 115 | /** |
| 116 | * Creates a new ApkAssets instance from the given path on disk. |
| 117 | * |
| 118 | * @param path The path to an APK on disk. |
| 119 | * @return a new instance of ApkAssets. |
| 120 | * @throws IOException if a disk I/O error or parsing error occurred. |
| 121 | */ |
| 122 | public static @NonNull ApkAssets loadFromPath(@NonNull String path) throws IOException { |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 123 | return loadFromPath(path, 0 /* flags */); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Creates a new ApkAssets instance from the given path on disk. |
| 128 | * |
| 129 | * @param path The path to an APK on disk. |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 130 | * @param flags flags that change the behavior of loaded apk assets |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 131 | * @return a new instance of ApkAssets. |
| 132 | * @throws IOException if a disk I/O error or parsing error occurred. |
| 133 | */ |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 134 | public static @NonNull ApkAssets loadFromPath(@NonNull String path, @PropertyFlags int flags) |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 135 | throws IOException { |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 136 | return new ApkAssets(FORMAT_APK, path, flags); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /** |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 140 | * Creates a new ApkAssets instance from the given file descriptor. |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 141 | * |
| 142 | * Performs a dup of the underlying fd, so you must take care of still closing |
| 143 | * the FileDescriptor yourself (and can do that whenever you want). |
| 144 | * |
| 145 | * @param fd The FileDescriptor of an open, readable APK. |
| 146 | * @param friendlyName The friendly name used to identify this ApkAssets when logging. |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 147 | * @param flags flags that change the behavior of loaded apk assets |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 148 | * @return a new instance of ApkAssets. |
| 149 | * @throws IOException if a disk I/O error or parsing error occurred. |
| 150 | */ |
| 151 | public static @NonNull ApkAssets loadFromFd(@NonNull FileDescriptor fd, |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 152 | @NonNull String friendlyName, @PropertyFlags int flags) throws IOException { |
| 153 | return new ApkAssets(FORMAT_APK, fd, friendlyName, flags); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Creates a new ApkAssets instance from the given file descriptor. |
| 158 | * |
| 159 | * Performs a dup of the underlying fd, so you must take care of still closing |
| 160 | * the FileDescriptor yourself (and can do that whenever you want). |
| 161 | * |
| 162 | * @param fd The FileDescriptor of an open, readable APK. |
| 163 | * @param friendlyName The friendly name used to identify this ApkAssets when logging. |
| 164 | * @param offset The location within the file that the apk starts. This must be 0 if length is |
| 165 | * {@link AssetFileDescriptor#UNKNOWN_LENGTH}. |
| 166 | * @param length The number of bytes of the apk, or {@link AssetFileDescriptor#UNKNOWN_LENGTH} |
| 167 | * if it extends to the end of the file. |
| 168 | * @param flags flags that change the behavior of loaded apk assets |
| 169 | * @return a new instance of ApkAssets. |
| 170 | * @throws IOException if a disk I/O error or parsing error occurred. |
| 171 | */ |
| 172 | public static @NonNull ApkAssets loadFromFd(@NonNull FileDescriptor fd, |
| 173 | @NonNull String friendlyName, long offset, long length, @PropertyFlags int flags) |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 174 | throws IOException { |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 175 | return new ApkAssets(FORMAT_APK, fd, friendlyName, offset, length, flags); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Creates a new ApkAssets instance from the IDMAP at idmapPath. The overlay APK path |
| 180 | * is encoded within the IDMAP. |
| 181 | * |
| 182 | * @param idmapPath Path to the IDMAP of an overlay APK. |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 183 | * @param flags flags that change the behavior of loaded apk assets |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 184 | * @return a new instance of ApkAssets. |
| 185 | * @throws IOException if a disk I/O error or parsing error occurred. |
| 186 | */ |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 187 | public static @NonNull ApkAssets loadOverlayFromPath(@NonNull String idmapPath, |
| 188 | @PropertyFlags int flags) throws IOException { |
| 189 | return new ApkAssets(FORMAT_IDMAP, idmapPath, flags); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 190 | } |
| 191 | |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 192 | /** |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 193 | * Creates a new ApkAssets instance from the given file descriptor representing a resources.arsc |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 194 | * for use with a {@link ResourcesProvider}. |
| 195 | * |
| 196 | * Performs a dup of the underlying fd, so you must take care of still closing |
| 197 | * the FileDescriptor yourself (and can do that whenever you want). |
| 198 | * |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 199 | * @param fd The FileDescriptor of an open, readable resources.arsc. |
| 200 | * @param friendlyName The friendly name used to identify this ApkAssets when logging. |
| 201 | * @param flags flags that change the behavior of loaded apk assets |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 202 | * @return a new instance of ApkAssets. |
| 203 | * @throws IOException if a disk I/O error or parsing error occurred. |
| 204 | */ |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 205 | public static @NonNull ApkAssets loadTableFromFd(@NonNull FileDescriptor fd, |
| 206 | @NonNull String friendlyName, @PropertyFlags int flags) throws IOException { |
| 207 | return new ApkAssets(FORMAT_ARSC, fd, friendlyName, flags); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Creates a new ApkAssets instance from the given file descriptor representing a resources.arsc |
| 212 | * for use with a {@link ResourcesProvider}. |
| 213 | * |
| 214 | * Performs a dup of the underlying fd, so you must take care of still closing |
| 215 | * the FileDescriptor yourself (and can do that whenever you want). |
| 216 | * |
| 217 | * @param fd The FileDescriptor of an open, readable resources.arsc. |
| 218 | * @param friendlyName The friendly name used to identify this ApkAssets when logging. |
| 219 | * @param offset The location within the file that the table starts. This must be 0 if length is |
| 220 | * {@link AssetFileDescriptor#UNKNOWN_LENGTH}. |
| 221 | * @param length The number of bytes of the table, or {@link AssetFileDescriptor#UNKNOWN_LENGTH} |
| 222 | * if it extends to the end of the file. |
| 223 | * @param flags flags that change the behavior of loaded apk assets |
| 224 | * @return a new instance of ApkAssets. |
| 225 | * @throws IOException if a disk I/O error or parsing error occurred. |
| 226 | */ |
| 227 | public static @NonNull ApkAssets loadTableFromFd(@NonNull FileDescriptor fd, |
| 228 | @NonNull String friendlyName, long offset, long length, @PropertyFlags int flags) |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 229 | throws IOException { |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 230 | return new ApkAssets(FORMAT_ARSC, fd, friendlyName, offset, length, flags); |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /** |
Ryan Mitchell | c07aa70 | 2020-03-10 13:49:12 -0700 | [diff] [blame^] | 234 | * Creates a new ApkAssets instance from the given directory path. The directory should have the |
| 235 | * file structure of an APK. |
| 236 | * |
| 237 | * @param path The path to a directory on disk. |
| 238 | * @param flags flags that change the behavior of loaded apk assets |
| 239 | * @return a new instance of ApkAssets. |
| 240 | * @throws IOException if a disk I/O error or parsing error occurred. |
| 241 | */ |
| 242 | public static @NonNull ApkAssets loadFromDir(@NonNull String path, |
| 243 | @PropertyFlags int flags) throws IOException { |
| 244 | return new ApkAssets(FORMAT_DIR, path, flags); |
| 245 | } |
| 246 | |
| 247 | /** |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 248 | * Generates an entirely empty ApkAssets. Needed because the ApkAssets instance and presence |
| 249 | * is required for a lot of APIs, and it's easier to have a non-null reference rather than |
| 250 | * tracking a separate identifier. |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 251 | * |
| 252 | * @param flags flags that change the behavior of loaded apk assets |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 253 | */ |
| 254 | @NonNull |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 255 | public static ApkAssets loadEmptyForLoader(@PropertyFlags int flags) { |
| 256 | return new ApkAssets(flags); |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 259 | private ApkAssets(@FormatType int format, @NonNull String path, @PropertyFlags int flags) |
| 260 | throws IOException { |
Daulet Zhanguzin | a2044e1 | 2019-12-30 16:34:59 +0000 | [diff] [blame] | 261 | Objects.requireNonNull(path, "path"); |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 262 | mFlags = flags; |
| 263 | mNativePtr = nativeLoad(format, path, flags); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 264 | mStringBlock = new StringBlock(nativeGetStringBlock(mNativePtr), true /*useSparse*/); |
| 265 | } |
| 266 | |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 267 | private ApkAssets(@FormatType int format, @NonNull FileDescriptor fd, |
| 268 | @NonNull String friendlyName, @PropertyFlags int flags) throws IOException { |
Daulet Zhanguzin | a2044e1 | 2019-12-30 16:34:59 +0000 | [diff] [blame] | 269 | Objects.requireNonNull(fd, "fd"); |
| 270 | Objects.requireNonNull(friendlyName, "friendlyName"); |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 271 | mFlags = flags; |
| 272 | mNativePtr = nativeLoadFd(format, fd, friendlyName, flags); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 273 | mStringBlock = new StringBlock(nativeGetStringBlock(mNativePtr), true /*useSparse*/); |
| 274 | } |
| 275 | |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 276 | private ApkAssets(@FormatType int format, @NonNull FileDescriptor fd, |
| 277 | @NonNull String friendlyName, long offset, long length, @PropertyFlags int flags) |
| 278 | throws IOException { |
| 279 | Objects.requireNonNull(fd, "fd"); |
| 280 | Objects.requireNonNull(friendlyName, "friendlyName"); |
| 281 | mFlags = flags; |
| 282 | mNativePtr = nativeLoadFdOffsets(format, fd, friendlyName, offset, length, flags); |
| 283 | mStringBlock = new StringBlock(nativeGetStringBlock(mNativePtr), true /*useSparse*/); |
| 284 | } |
| 285 | |
| 286 | private ApkAssets(@PropertyFlags int flags) { |
| 287 | mFlags = flags; |
| 288 | mNativePtr = nativeLoadEmpty(flags); |
| 289 | mStringBlock = null; |
| 290 | } |
| 291 | |
Mathew Inwood | 5c0d354 | 2018-08-14 13:54:31 +0100 | [diff] [blame] | 292 | @UnsupportedAppUsage |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 293 | public @NonNull String getAssetPath() { |
| 294 | synchronized (this) { |
| 295 | return nativeGetAssetPath(mNativePtr); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | CharSequence getStringFromPool(int idx) { |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 300 | if (mStringBlock == null) { |
| 301 | return null; |
| 302 | } |
| 303 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 304 | synchronized (this) { |
| 305 | return mStringBlock.get(idx); |
| 306 | } |
| 307 | } |
| 308 | |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 309 | /** Returns whether this apk assets was loaded using a {@link ResourcesProvider}. */ |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 310 | public boolean isForLoader() { |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 311 | return (mFlags & PROPERTY_LOADER) != 0; |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 314 | /** |
| 315 | * Retrieve a parser for a compiled XML file. This is associated with a single APK and |
| 316 | * <em>NOT</em> a full AssetManager. This means that shared-library references will not be |
| 317 | * dynamically assigned runtime package IDs. |
| 318 | * |
| 319 | * @param fileName The path to the file within the APK. |
| 320 | * @return An XmlResourceParser. |
| 321 | * @throws IOException if the file was not found or an error occurred retrieving it. |
| 322 | */ |
| 323 | public @NonNull XmlResourceParser openXml(@NonNull String fileName) throws IOException { |
Daulet Zhanguzin | a2044e1 | 2019-12-30 16:34:59 +0000 | [diff] [blame] | 324 | Objects.requireNonNull(fileName, "fileName"); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 325 | synchronized (this) { |
| 326 | long nativeXmlPtr = nativeOpenXml(mNativePtr, fileName); |
| 327 | try (XmlBlock block = new XmlBlock(null, nativeXmlPtr)) { |
| 328 | XmlResourceParser parser = block.newParser(); |
| 329 | // If nativeOpenXml doesn't throw, it will always return a valid native pointer, |
| 330 | // which makes newParser always return non-null. But let's be paranoid. |
| 331 | if (parser == null) { |
| 332 | throw new AssertionError("block.newParser() returned a null parser"); |
| 333 | } |
| 334 | return parser; |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | |
Winson | d9d1736 | 2019-10-02 12:41:29 -0700 | [diff] [blame] | 339 | /** @hide */ |
| 340 | @Nullable |
| 341 | public OverlayableInfo getOverlayableInfo(String overlayableName) throws IOException { |
| 342 | return nativeGetOverlayableInfo(mNativePtr, overlayableName); |
| 343 | } |
| 344 | |
| 345 | /** @hide */ |
| 346 | public boolean definesOverlayable() throws IOException { |
| 347 | return nativeDefinesOverlayable(mNativePtr); |
| 348 | } |
| 349 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 350 | /** |
| 351 | * Returns false if the underlying APK was changed since this ApkAssets was loaded. |
| 352 | */ |
| 353 | public boolean isUpToDate() { |
| 354 | synchronized (this) { |
| 355 | return nativeIsUpToDate(mNativePtr); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | @Override |
| 360 | public String toString() { |
| 361 | return "ApkAssets{path=" + getAssetPath() + "}"; |
| 362 | } |
| 363 | |
| 364 | @Override |
| 365 | protected void finalize() throws Throwable { |
Winson | 7a3d82a | 2019-03-07 12:54:24 -0800 | [diff] [blame] | 366 | close(); |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * Closes this class and the contained {@link #mStringBlock}. |
| 371 | */ |
Ryan Mitchell | 4043ca7 | 2019-06-03 16:11:24 -0700 | [diff] [blame] | 372 | public void close() { |
Winson | 7a3d82a | 2019-03-07 12:54:24 -0800 | [diff] [blame] | 373 | synchronized (this) { |
| 374 | if (mOpen) { |
| 375 | mOpen = false; |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 376 | if (mStringBlock != null) { |
| 377 | mStringBlock.close(); |
| 378 | } |
Winson | 7a3d82a | 2019-03-07 12:54:24 -0800 | [diff] [blame] | 379 | nativeDestroy(mNativePtr); |
| 380 | } |
| 381 | } |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 382 | } |
| 383 | |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 384 | private static native long nativeLoad(@FormatType int format, @NonNull String path, |
| 385 | @PropertyFlags int flags) throws IOException; |
| 386 | private static native long nativeLoadEmpty(@PropertyFlags int flags); |
| 387 | private static native long nativeLoadFd(@FormatType int format, @NonNull FileDescriptor fd, |
| 388 | @NonNull String friendlyName, @PropertyFlags int flags) throws IOException; |
| 389 | private static native long nativeLoadFdOffsets(@FormatType int format, |
| 390 | @NonNull FileDescriptor fd, @NonNull String friendlyName, long offset, long length, |
| 391 | @PropertyFlags int flags) throws IOException; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 392 | private static native void nativeDestroy(long ptr); |
| 393 | private static native @NonNull String nativeGetAssetPath(long ptr); |
| 394 | private static native long nativeGetStringBlock(long ptr); |
| 395 | private static native boolean nativeIsUpToDate(long ptr); |
| 396 | private static native long nativeOpenXml(long ptr, @NonNull String fileName) throws IOException; |
Winson | d9d1736 | 2019-10-02 12:41:29 -0700 | [diff] [blame] | 397 | private static native @Nullable OverlayableInfo nativeGetOverlayableInfo(long ptr, |
| 398 | String overlayableName) throws IOException; |
| 399 | private static native boolean nativeDefinesOverlayable(long ptr) throws IOException; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 400 | } |