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.os.Parcel; 23 import android.os.Parcelable; 24 25 import java.lang.annotation.Retention; 26 import java.lang.annotation.RetentionPolicy; 27 import java.util.Collections; 28 import java.util.List; 29 30 /** 31 * This class provides information for a shared library. There are 32 * three types of shared libraries: builtin - non-updatable part of 33 * the OS; dynamic - updatable backwards-compatible dynamically linked; 34 * static - updatable non backwards-compatible emulating static linking. 35 */ 36 public final class SharedLibraryInfo implements Parcelable { 37 38 /** @hide */ 39 @IntDef(flag = true, prefix = { "TYPE_" }, value = { 40 TYPE_BUILTIN, 41 TYPE_DYNAMIC, 42 TYPE_STATIC, 43 }) 44 @Retention(RetentionPolicy.SOURCE) 45 @interface Type{} 46 47 /** 48 * Shared library type: this library is a part of the OS 49 * and cannot be updated or uninstalled. 50 */ 51 public static final int TYPE_BUILTIN = 0; 52 53 /** 54 * Shared library type: this library is backwards-compatible, can 55 * be updated, and updates can be uninstalled. Clients link against 56 * the latest version of the library. 57 */ 58 public static final int TYPE_DYNAMIC = 1; 59 60 /** 61 * Shared library type: this library is <strong>not</strong> backwards 62 * -compatible, can be updated and updates can be uninstalled. Clients 63 * link against a specific version of the library. 64 */ 65 public static final int TYPE_STATIC = 2; 66 67 /** 68 * Constant for referring to an undefined version. 69 */ 70 public static final int VERSION_UNDEFINED = -1; 71 72 private final String mName; 73 74 private final long mVersion; 75 private final @Type int mType; 76 private final VersionedPackage mDeclaringPackage; 77 private final List<VersionedPackage> mDependentPackages; 78 79 /** 80 * Creates a new instance. 81 * 82 * @param name The lib name. 83 * @param version The lib version if not builtin. 84 * @param type The lib type. 85 * @param declaringPackage The package that declares the library. 86 * @param dependentPackages The packages that depend on the library. 87 * 88 * @hide 89 */ 90 public SharedLibraryInfo(String name, long version, int type, 91 VersionedPackage declaringPackage, List<VersionedPackage> dependentPackages) { 92 mName = name; 93 mVersion = version; 94 mType = type; 95 mDeclaringPackage = declaringPackage; 96 mDependentPackages = dependentPackages; 97 } 98 99 private SharedLibraryInfo(Parcel parcel) { 100 this(parcel.readString(), parcel.readLong(), parcel.readInt(), 101 parcel.readParcelable(null), parcel.readArrayList(null)); 102 } 103 104 /** 105 * Gets the type of this library. 106 * 107 * @return The library type. 108 */ 109 public @Type int getType() { 110 return mType; 111 } 112 113 /** 114 * Gets the library name an app defines in its manifest 115 * to depend on the library. 116 * 117 * @return The name. 118 */ 119 public String getName() { 120 return mName; 121 } 122 123 /** 124 * @deprecated Use {@link #getLongVersion()} instead. 125 */ 126 @Deprecated 127 public @IntRange(from = -1) int getVersion() { 128 return mVersion < 0 ? (int) mVersion : (int) (mVersion & 0x7fffffff); 129 } 130 131 /** 132 * Gets the version of the library. For {@link #TYPE_STATIC static} libraries 133 * this is the declared version and for {@link #TYPE_DYNAMIC dynamic} and 134 * {@link #TYPE_BUILTIN builtin} it is {@link #VERSION_UNDEFINED} as these 135 * are not versioned. 136 * 137 * @return The version. 138 */ 139 public @IntRange(from = -1) long getLongVersion() { 140 return mVersion; 141 } 142 143 /** 144 * @removed 145 */ 146 public boolean isBuiltin() { 147 return mType == TYPE_BUILTIN; 148 } 149 150 /** 151 * @removed 152 */ 153 public boolean isDynamic() { 154 return mType == TYPE_DYNAMIC; 155 } 156 157 /** 158 * @removed 159 */ 160 public boolean isStatic() { 161 return mType == TYPE_STATIC; 162 } 163 164 /** 165 * Gets the package that declares the library. 166 * 167 * @return The package declaring the library. 168 */ 169 public @NonNull VersionedPackage getDeclaringPackage() { 170 return mDeclaringPackage; 171 } 172 173 /** 174 * Gets the packages that depend on the library. 175 * 176 * @return The dependent packages. 177 */ 178 public @NonNull List<VersionedPackage> getDependentPackages() { 179 if (mDependentPackages == null) { 180 return Collections.emptyList(); 181 } 182 return mDependentPackages; 183 } 184 185 @Override 186 public int describeContents() { 187 return 0; 188 } 189 190 @Override 191 public String toString() { 192 return "SharedLibraryInfo[name:" + mName + ", type:" + typeToString(mType) 193 + ", version:" + mVersion + (!getDependentPackages().isEmpty() 194 ? " has dependents" : ""); 195 } 196 197 @Override 198 public void writeToParcel(Parcel parcel, int flags) { 199 parcel.writeString(mName); 200 parcel.writeLong(mVersion); 201 parcel.writeInt(mType); 202 parcel.writeParcelable(mDeclaringPackage, flags); 203 parcel.writeList(mDependentPackages); 204 } 205 206 private static String typeToString(int type) { 207 switch (type) { 208 case TYPE_BUILTIN: { 209 return "builtin"; 210 } 211 case TYPE_DYNAMIC: { 212 return "dynamic"; 213 } 214 case TYPE_STATIC: { 215 return "static"; 216 } 217 default: { 218 return "unknown"; 219 } 220 } 221 } 222 223 public static final Parcelable.Creator<SharedLibraryInfo> CREATOR = 224 new Parcelable.Creator<SharedLibraryInfo>() { 225 public SharedLibraryInfo createFromParcel(Parcel source) { 226 return new SharedLibraryInfo(source); 227 } 228 229 public SharedLibraryInfo[] newArray(int size) { 230 return new SharedLibraryInfo[size]; 231 } 232 }; 233 } 234