Svet Ganov | 6788212 | 2016-12-11 16:36:34 -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 | |
| 17 | package android.content.pm; |
| 18 | |
Svetoslav Ganov | 0cb43ef | 2017-04-18 14:28:35 -0700 | [diff] [blame] | 19 | import android.annotation.IntDef; |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 20 | import android.annotation.IntRange; |
| 21 | import android.annotation.NonNull; |
Nicolas Geoffray | abcd58e | 2018-10-22 10:23:50 +0100 | [diff] [blame] | 22 | import android.annotation.Nullable; |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 23 | import android.os.Parcel; |
| 24 | import android.os.Parcelable; |
| 25 | |
Svetoslav Ganov | 0cb43ef | 2017-04-18 14:28:35 -0700 | [diff] [blame] | 26 | import java.lang.annotation.Retention; |
| 27 | import java.lang.annotation.RetentionPolicy; |
Nicolas Geoffray | 10b0779 | 2018-10-27 13:48:54 +0100 | [diff] [blame] | 28 | import java.util.ArrayList; |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 29 | import java.util.Collections; |
| 30 | import java.util.List; |
| 31 | |
| 32 | /** |
| 33 | * This class provides information for a shared library. There are |
| 34 | * three types of shared libraries: builtin - non-updatable part of |
| 35 | * the OS; dynamic - updatable backwards-compatible dynamically linked; |
Patrick Baumann | 34cf12c | 2018-11-28 07:15:06 -0800 | [diff] [blame] | 36 | * static - non backwards-compatible emulating static linking. |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 37 | */ |
| 38 | public final class SharedLibraryInfo implements Parcelable { |
Svetoslav Ganov | 0cb43ef | 2017-04-18 14:28:35 -0700 | [diff] [blame] | 39 | |
| 40 | /** @hide */ |
Patrick Baumann | 34cf12c | 2018-11-28 07:15:06 -0800 | [diff] [blame] | 41 | public static SharedLibraryInfo createForStatic(PackageParser.Package pkg) { |
| 42 | return new SharedLibraryInfo(null, pkg.packageName, pkg.getAllCodePaths(), |
| 43 | pkg.staticSharedLibName, |
| 44 | pkg.staticSharedLibVersion, |
| 45 | TYPE_STATIC, |
| 46 | new VersionedPackage(pkg.manifestPackageName, pkg.getLongVersionCode()), |
| 47 | null, null); |
| 48 | } |
| 49 | |
| 50 | /** @hide */ |
| 51 | public static SharedLibraryInfo createForDynamic(PackageParser.Package pkg, String name) { |
| 52 | return new SharedLibraryInfo(null, pkg.packageName, pkg.getAllCodePaths(), name, |
| 53 | (long) VERSION_UNDEFINED, |
| 54 | TYPE_DYNAMIC, new VersionedPackage(pkg.packageName, pkg.getLongVersionCode()), |
| 55 | null, null); |
| 56 | } |
| 57 | |
| 58 | /** @hide */ |
Jeff Sharkey | ce8db99 | 2017-12-13 20:05:05 -0700 | [diff] [blame] | 59 | @IntDef(flag = true, prefix = { "TYPE_" }, value = { |
| 60 | TYPE_BUILTIN, |
| 61 | TYPE_DYNAMIC, |
| 62 | TYPE_STATIC, |
| 63 | }) |
Svetoslav Ganov | 0cb43ef | 2017-04-18 14:28:35 -0700 | [diff] [blame] | 64 | @Retention(RetentionPolicy.SOURCE) |
| 65 | @interface Type{} |
| 66 | |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 67 | /** |
| 68 | * Shared library type: this library is a part of the OS |
| 69 | * and cannot be updated or uninstalled. |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 70 | */ |
Svetoslav Ganov | 0cb43ef | 2017-04-18 14:28:35 -0700 | [diff] [blame] | 71 | public static final int TYPE_BUILTIN = 0; |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 72 | |
| 73 | /** |
| 74 | * Shared library type: this library is backwards-compatible, can |
| 75 | * be updated, and updates can be uninstalled. Clients link against |
| 76 | * the latest version of the library. |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 77 | */ |
Svetoslav Ganov | 0cb43ef | 2017-04-18 14:28:35 -0700 | [diff] [blame] | 78 | public static final int TYPE_DYNAMIC = 1; |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 79 | |
| 80 | /** |
| 81 | * Shared library type: this library is <strong>not</strong> backwards |
| 82 | * -compatible, can be updated and updates can be uninstalled. Clients |
| 83 | * link against a specific version of the library. |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 84 | */ |
Svetoslav Ganov | 0cb43ef | 2017-04-18 14:28:35 -0700 | [diff] [blame] | 85 | public static final int TYPE_STATIC = 2; |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * Constant for referring to an undefined version. |
| 89 | */ |
| 90 | public static final int VERSION_UNDEFINED = -1; |
| 91 | |
Nicolas Geoffray | abcd58e | 2018-10-22 10:23:50 +0100 | [diff] [blame] | 92 | private final String mPath; |
| 93 | private final String mPackageName; |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 94 | private final String mName; |
Nicolas Geoffray | 8d144eb | 2018-11-15 12:59:52 +0000 | [diff] [blame] | 95 | private final List<String> mCodePaths; |
Svetoslav Ganov | 0cb43ef | 2017-04-18 14:28:35 -0700 | [diff] [blame] | 96 | |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 97 | private final long mVersion; |
Svetoslav Ganov | 0cb43ef | 2017-04-18 14:28:35 -0700 | [diff] [blame] | 98 | private final @Type int mType; |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 99 | private final VersionedPackage mDeclaringPackage; |
| 100 | private final List<VersionedPackage> mDependentPackages; |
Nicolas Geoffray | 10b0779 | 2018-10-27 13:48:54 +0100 | [diff] [blame] | 101 | private List<SharedLibraryInfo> mDependencies; |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 102 | |
| 103 | /** |
| 104 | * Creates a new instance. |
| 105 | * |
Nicolas Geoffray | 8d144eb | 2018-11-15 12:59:52 +0000 | [diff] [blame] | 106 | * @param codePaths For a non {@link #TYPE_BUILTIN builtin} library, the locations of jars of |
| 107 | * this shared library. Null for builtin library. |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 108 | * @param name The lib name. |
| 109 | * @param version The lib version if not builtin. |
| 110 | * @param type The lib type. |
| 111 | * @param declaringPackage The package that declares the library. |
| 112 | * @param dependentPackages The packages that depend on the library. |
| 113 | * |
| 114 | * @hide |
| 115 | */ |
Nicolas Geoffray | 8d144eb | 2018-11-15 12:59:52 +0000 | [diff] [blame] | 116 | public SharedLibraryInfo(String path, String packageName, List<String> codePaths, |
| 117 | String name, long version, int type, |
Nicolas Geoffray | 10b0779 | 2018-10-27 13:48:54 +0100 | [diff] [blame] | 118 | VersionedPackage declaringPackage, List<VersionedPackage> dependentPackages, |
| 119 | List<SharedLibraryInfo> dependencies) { |
Nicolas Geoffray | abcd58e | 2018-10-22 10:23:50 +0100 | [diff] [blame] | 120 | mPath = path; |
| 121 | mPackageName = packageName; |
Nicolas Geoffray | 8d144eb | 2018-11-15 12:59:52 +0000 | [diff] [blame] | 122 | mCodePaths = codePaths; |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 123 | mName = name; |
| 124 | mVersion = version; |
| 125 | mType = type; |
| 126 | mDeclaringPackage = declaringPackage; |
| 127 | mDependentPackages = dependentPackages; |
Nicolas Geoffray | 10b0779 | 2018-10-27 13:48:54 +0100 | [diff] [blame] | 128 | mDependencies = dependencies; |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | private SharedLibraryInfo(Parcel parcel) { |
Nicolas Geoffray | 8d144eb | 2018-11-15 12:59:52 +0000 | [diff] [blame] | 132 | this(parcel.readString(), parcel.readString(), parcel.readArrayList(null), |
| 133 | parcel.readString(), parcel.readLong(), |
Nicolas Geoffray | 10b0779 | 2018-10-27 13:48:54 +0100 | [diff] [blame] | 134 | parcel.readInt(), parcel.readParcelable(null), parcel.readArrayList(null), |
| 135 | parcel.createTypedArrayList(SharedLibraryInfo.CREATOR)); |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Svetoslav Ganov | 0cb43ef | 2017-04-18 14:28:35 -0700 | [diff] [blame] | 138 | /** |
| 139 | * Gets the type of this library. |
| 140 | * |
| 141 | * @return The library type. |
| 142 | */ |
| 143 | public @Type int getType() { |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 144 | return mType; |
| 145 | } |
| 146 | |
| 147 | /** |
Svetoslav Ganov | 0cb43ef | 2017-04-18 14:28:35 -0700 | [diff] [blame] | 148 | * Gets the library name an app defines in its manifest |
| 149 | * to depend on the library. |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 150 | * |
| 151 | * @return The name. |
| 152 | */ |
| 153 | public String getName() { |
| 154 | return mName; |
| 155 | } |
| 156 | |
| 157 | /** |
Nicolas Geoffray | abcd58e | 2018-10-22 10:23:50 +0100 | [diff] [blame] | 158 | * If the shared library is a jar file, returns the path of that jar. Null otherwise. |
| 159 | * Only libraries with TYPE_BUILTIN are in jar files. |
| 160 | * |
| 161 | * @return The path. |
| 162 | * |
| 163 | * @hide |
| 164 | */ |
| 165 | public @Nullable String getPath() { |
| 166 | return mPath; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * If the shared library is an apk, returns the package name. Null otherwise. |
| 171 | * Only libraries with TYPE_DYNAMIC or TYPE_STATIC are in apks. |
| 172 | * |
| 173 | * @return The package name. |
| 174 | * |
| 175 | * @hide |
| 176 | */ |
| 177 | public @Nullable String getPackageName() { |
| 178 | return mPackageName; |
| 179 | } |
| 180 | |
| 181 | /** |
Nicolas Geoffray | 8d144eb | 2018-11-15 12:59:52 +0000 | [diff] [blame] | 182 | * Get all code paths for that library. |
| 183 | * |
| 184 | * @return All code paths. |
| 185 | * |
| 186 | * @hide |
| 187 | */ |
| 188 | public List<String> getAllCodePaths() { |
| 189 | if (getPath() != null) { |
| 190 | // Builtin library. |
| 191 | ArrayList<String> list = new ArrayList<>(); |
| 192 | list.add(getPath()); |
| 193 | return list; |
| 194 | } else { |
| 195 | // Static or dynamic library. |
| 196 | return mCodePaths; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | /** |
Nicolas Geoffray | 10b0779 | 2018-10-27 13:48:54 +0100 | [diff] [blame] | 201 | * Add a library dependency to that library. Note that this |
| 202 | * should be called under the package manager lock. |
| 203 | * |
| 204 | * @hide |
| 205 | */ |
| 206 | public void addDependency(@Nullable SharedLibraryInfo info) { |
| 207 | if (info == null) { |
| 208 | // For convenience of the caller, allow null to be passed. |
| 209 | // This can happen when we create the dependencies of builtin |
| 210 | // libraries. |
| 211 | return; |
| 212 | } |
| 213 | if (mDependencies == null) { |
| 214 | mDependencies = new ArrayList<>(); |
| 215 | } |
| 216 | mDependencies.add(info); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Clear all dependencies. |
| 221 | * |
| 222 | * @hide |
| 223 | */ |
| 224 | public void clearDependencies() { |
| 225 | mDependencies = null; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Gets the libraries this library directly depends on. Note that |
| 230 | * the package manager prevents recursive dependencies when installing |
| 231 | * a package. |
| 232 | * |
| 233 | * @return The dependencies. |
| 234 | * |
| 235 | * @hide |
| 236 | */ |
| 237 | public @Nullable List<SharedLibraryInfo> getDependencies() { |
| 238 | return mDependencies; |
| 239 | } |
| 240 | |
| 241 | /** |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 242 | * @deprecated Use {@link #getLongVersion()} instead. |
| 243 | */ |
| 244 | @Deprecated |
| 245 | public @IntRange(from = -1) int getVersion() { |
| 246 | return mVersion < 0 ? (int) mVersion : (int) (mVersion & 0x7fffffff); |
| 247 | } |
| 248 | |
| 249 | /** |
Svetoslav Ganov | 0cb43ef | 2017-04-18 14:28:35 -0700 | [diff] [blame] | 250 | * Gets the version of the library. For {@link #TYPE_STATIC static} libraries |
| 251 | * this is the declared version and for {@link #TYPE_DYNAMIC dynamic} and |
| 252 | * {@link #TYPE_BUILTIN builtin} it is {@link #VERSION_UNDEFINED} as these |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 253 | * are not versioned. |
| 254 | * |
| 255 | * @return The version. |
| 256 | */ |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 257 | public @IntRange(from = -1) long getLongVersion() { |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 258 | return mVersion; |
| 259 | } |
| 260 | |
| 261 | /** |
Svetoslav Ganov | 0cb43ef | 2017-04-18 14:28:35 -0700 | [diff] [blame] | 262 | * @removed |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 263 | */ |
| 264 | public boolean isBuiltin() { |
| 265 | return mType == TYPE_BUILTIN; |
| 266 | } |
| 267 | |
| 268 | /** |
Svetoslav Ganov | 0cb43ef | 2017-04-18 14:28:35 -0700 | [diff] [blame] | 269 | * @removed |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 270 | */ |
| 271 | public boolean isDynamic() { |
| 272 | return mType == TYPE_DYNAMIC; |
| 273 | } |
| 274 | |
| 275 | /** |
Svetoslav Ganov | 0cb43ef | 2017-04-18 14:28:35 -0700 | [diff] [blame] | 276 | * @removed |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 277 | */ |
| 278 | public boolean isStatic() { |
| 279 | return mType == TYPE_STATIC; |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Gets the package that declares the library. |
| 284 | * |
| 285 | * @return The package declaring the library. |
| 286 | */ |
| 287 | public @NonNull VersionedPackage getDeclaringPackage() { |
| 288 | return mDeclaringPackage; |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Gets the packages that depend on the library. |
| 293 | * |
| 294 | * @return The dependent packages. |
| 295 | */ |
| 296 | public @NonNull List<VersionedPackage> getDependentPackages() { |
| 297 | if (mDependentPackages == null) { |
| 298 | return Collections.emptyList(); |
| 299 | } |
| 300 | return mDependentPackages; |
| 301 | } |
| 302 | |
| 303 | @Override |
| 304 | public int describeContents() { |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | @Override |
| 309 | public String toString() { |
Steven Moreland | 1e35a41 | 2019-03-18 18:30:22 -0700 | [diff] [blame] | 310 | return "SharedLibraryInfo{name:" + mName + ", type:" + typeToString(mType) |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 311 | + ", version:" + mVersion + (!getDependentPackages().isEmpty() |
Steven Moreland | 1e35a41 | 2019-03-18 18:30:22 -0700 | [diff] [blame] | 312 | ? " has dependents" : "") + "}"; |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | @Override |
| 316 | public void writeToParcel(Parcel parcel, int flags) { |
Nicolas Geoffray | abcd58e | 2018-10-22 10:23:50 +0100 | [diff] [blame] | 317 | parcel.writeString(mPath); |
| 318 | parcel.writeString(mPackageName); |
Nicolas Geoffray | 8d144eb | 2018-11-15 12:59:52 +0000 | [diff] [blame] | 319 | parcel.writeList(mCodePaths); |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 320 | parcel.writeString(mName); |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 321 | parcel.writeLong(mVersion); |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 322 | parcel.writeInt(mType); |
| 323 | parcel.writeParcelable(mDeclaringPackage, flags); |
| 324 | parcel.writeList(mDependentPackages); |
Nicolas Geoffray | 10b0779 | 2018-10-27 13:48:54 +0100 | [diff] [blame] | 325 | parcel.writeTypedList(mDependencies); |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | private static String typeToString(int type) { |
| 329 | switch (type) { |
| 330 | case TYPE_BUILTIN: { |
| 331 | return "builtin"; |
| 332 | } |
| 333 | case TYPE_DYNAMIC: { |
| 334 | return "dynamic"; |
| 335 | } |
| 336 | case TYPE_STATIC: { |
| 337 | return "static"; |
| 338 | } |
| 339 | default: { |
| 340 | return "unknown"; |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 345 | public static final @android.annotation.NonNull Parcelable.Creator<SharedLibraryInfo> CREATOR = |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 346 | new Parcelable.Creator<SharedLibraryInfo>() { |
| 347 | public SharedLibraryInfo createFromParcel(Parcel source) { |
| 348 | return new SharedLibraryInfo(source); |
| 349 | } |
| 350 | |
| 351 | public SharedLibraryInfo[] newArray(int size) { |
| 352 | return new SharedLibraryInfo[size]; |
| 353 | } |
| 354 | }; |
| 355 | } |