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