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