blob: 35609c9a892693080d56ae7280aeda2e6a8e97bd [file] [log] [blame]
Svet Ganov67882122016-12-11 16:36:34 -08001/*
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
17package android.content.pm;
18
Svetoslav Ganov0cb43ef2017-04-18 14:28:35 -070019import android.annotation.IntDef;
Svet Ganov67882122016-12-11 16:36:34 -080020import android.annotation.IntRange;
21import android.annotation.NonNull;
Nicolas Geoffrayabcd58e2018-10-22 10:23:50 +010022import android.annotation.Nullable;
Svet Ganov67882122016-12-11 16:36:34 -080023import android.os.Parcel;
24import android.os.Parcelable;
25
Svetoslav Ganov0cb43ef2017-04-18 14:28:35 -070026import java.lang.annotation.Retention;
27import java.lang.annotation.RetentionPolicy;
Nicolas Geoffray10b07792018-10-27 13:48:54 +010028import java.util.ArrayList;
Svet Ganov67882122016-12-11 16:36:34 -080029import java.util.Collections;
30import 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 Baumann34cf12c2018-11-28 07:15:06 -080036 * static - non backwards-compatible emulating static linking.
Svet Ganov67882122016-12-11 16:36:34 -080037 */
38public final class SharedLibraryInfo implements Parcelable {
Svetoslav Ganov0cb43ef2017-04-18 14:28:35 -070039
40 /** @hide */
Patrick Baumann34cf12c2018-11-28 07:15:06 -080041 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 Sharkeyce8db992017-12-13 20:05:05 -070059 @IntDef(flag = true, prefix = { "TYPE_" }, value = {
60 TYPE_BUILTIN,
61 TYPE_DYNAMIC,
62 TYPE_STATIC,
63 })
Svetoslav Ganov0cb43ef2017-04-18 14:28:35 -070064 @Retention(RetentionPolicy.SOURCE)
65 @interface Type{}
66
Svet Ganov67882122016-12-11 16:36:34 -080067 /**
68 * Shared library type: this library is a part of the OS
69 * and cannot be updated or uninstalled.
Svet Ganov67882122016-12-11 16:36:34 -080070 */
Svetoslav Ganov0cb43ef2017-04-18 14:28:35 -070071 public static final int TYPE_BUILTIN = 0;
Svet Ganov67882122016-12-11 16:36:34 -080072
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 Ganov67882122016-12-11 16:36:34 -080077 */
Svetoslav Ganov0cb43ef2017-04-18 14:28:35 -070078 public static final int TYPE_DYNAMIC = 1;
Svet Ganov67882122016-12-11 16:36:34 -080079
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 Ganov67882122016-12-11 16:36:34 -080084 */
Svetoslav Ganov0cb43ef2017-04-18 14:28:35 -070085 public static final int TYPE_STATIC = 2;
Svet Ganov67882122016-12-11 16:36:34 -080086
87 /**
88 * Constant for referring to an undefined version.
89 */
90 public static final int VERSION_UNDEFINED = -1;
91
Nicolas Geoffrayabcd58e2018-10-22 10:23:50 +010092 private final String mPath;
93 private final String mPackageName;
Svet Ganov67882122016-12-11 16:36:34 -080094 private final String mName;
Nicolas Geoffray8d144eb2018-11-15 12:59:52 +000095 private final List<String> mCodePaths;
Svetoslav Ganov0cb43ef2017-04-18 14:28:35 -070096
Dianne Hackborn3accca02013-09-20 09:32:11 -070097 private final long mVersion;
Svetoslav Ganov0cb43ef2017-04-18 14:28:35 -070098 private final @Type int mType;
Svet Ganov67882122016-12-11 16:36:34 -080099 private final VersionedPackage mDeclaringPackage;
100 private final List<VersionedPackage> mDependentPackages;
Nicolas Geoffray10b07792018-10-27 13:48:54 +0100101 private List<SharedLibraryInfo> mDependencies;
Svet Ganov67882122016-12-11 16:36:34 -0800102
103 /**
104 * Creates a new instance.
105 *
Nicolas Geoffray8d144eb2018-11-15 12:59:52 +0000106 * @param codePaths For a non {@link #TYPE_BUILTIN builtin} library, the locations of jars of
107 * this shared library. Null for builtin library.
Svet Ganov67882122016-12-11 16:36:34 -0800108 * @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 Geoffray8d144eb2018-11-15 12:59:52 +0000116 public SharedLibraryInfo(String path, String packageName, List<String> codePaths,
117 String name, long version, int type,
Nicolas Geoffray10b07792018-10-27 13:48:54 +0100118 VersionedPackage declaringPackage, List<VersionedPackage> dependentPackages,
119 List<SharedLibraryInfo> dependencies) {
Nicolas Geoffrayabcd58e2018-10-22 10:23:50 +0100120 mPath = path;
121 mPackageName = packageName;
Nicolas Geoffray8d144eb2018-11-15 12:59:52 +0000122 mCodePaths = codePaths;
Svet Ganov67882122016-12-11 16:36:34 -0800123 mName = name;
124 mVersion = version;
125 mType = type;
126 mDeclaringPackage = declaringPackage;
127 mDependentPackages = dependentPackages;
Nicolas Geoffray10b07792018-10-27 13:48:54 +0100128 mDependencies = dependencies;
Svet Ganov67882122016-12-11 16:36:34 -0800129 }
130
131 private SharedLibraryInfo(Parcel parcel) {
Nicolas Geoffray8d144eb2018-11-15 12:59:52 +0000132 this(parcel.readString(), parcel.readString(), parcel.readArrayList(null),
133 parcel.readString(), parcel.readLong(),
Nicolas Geoffray10b07792018-10-27 13:48:54 +0100134 parcel.readInt(), parcel.readParcelable(null), parcel.readArrayList(null),
135 parcel.createTypedArrayList(SharedLibraryInfo.CREATOR));
Svet Ganov67882122016-12-11 16:36:34 -0800136 }
137
Svetoslav Ganov0cb43ef2017-04-18 14:28:35 -0700138 /**
139 * Gets the type of this library.
140 *
141 * @return The library type.
142 */
143 public @Type int getType() {
Svet Ganov67882122016-12-11 16:36:34 -0800144 return mType;
145 }
146
147 /**
Svetoslav Ganov0cb43ef2017-04-18 14:28:35 -0700148 * Gets the library name an app defines in its manifest
149 * to depend on the library.
Svet Ganov67882122016-12-11 16:36:34 -0800150 *
151 * @return The name.
152 */
153 public String getName() {
154 return mName;
155 }
156
157 /**
Nicolas Geoffrayabcd58e2018-10-22 10:23:50 +0100158 * 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 Geoffray8d144eb2018-11-15 12:59:52 +0000182 * 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 Geoffray10b07792018-10-27 13:48:54 +0100201 * 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 Hackborn3accca02013-09-20 09:32:11 -0700242 * @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 Ganov0cb43ef2017-04-18 14:28:35 -0700250 * 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 Ganov67882122016-12-11 16:36:34 -0800253 * are not versioned.
254 *
255 * @return The version.
256 */
Dianne Hackborn3accca02013-09-20 09:32:11 -0700257 public @IntRange(from = -1) long getLongVersion() {
Svet Ganov67882122016-12-11 16:36:34 -0800258 return mVersion;
259 }
260
261 /**
Svetoslav Ganov0cb43ef2017-04-18 14:28:35 -0700262 * @removed
Svet Ganov67882122016-12-11 16:36:34 -0800263 */
264 public boolean isBuiltin() {
265 return mType == TYPE_BUILTIN;
266 }
267
268 /**
Svetoslav Ganov0cb43ef2017-04-18 14:28:35 -0700269 * @removed
Svet Ganov67882122016-12-11 16:36:34 -0800270 */
271 public boolean isDynamic() {
272 return mType == TYPE_DYNAMIC;
273 }
274
275 /**
Svetoslav Ganov0cb43ef2017-04-18 14:28:35 -0700276 * @removed
Svet Ganov67882122016-12-11 16:36:34 -0800277 */
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() {
310 return "SharedLibraryInfo[name:" + mName + ", type:" + typeToString(mType)
311 + ", version:" + mVersion + (!getDependentPackages().isEmpty()
312 ? " has dependents" : "");
313 }
314
315 @Override
316 public void writeToParcel(Parcel parcel, int flags) {
Nicolas Geoffrayabcd58e2018-10-22 10:23:50 +0100317 parcel.writeString(mPath);
318 parcel.writeString(mPackageName);
Nicolas Geoffray8d144eb2018-11-15 12:59:52 +0000319 parcel.writeList(mCodePaths);
Svet Ganov67882122016-12-11 16:36:34 -0800320 parcel.writeString(mName);
Dianne Hackborn3accca02013-09-20 09:32:11 -0700321 parcel.writeLong(mVersion);
Svet Ganov67882122016-12-11 16:36:34 -0800322 parcel.writeInt(mType);
323 parcel.writeParcelable(mDeclaringPackage, flags);
324 parcel.writeList(mDependentPackages);
Nicolas Geoffray10b07792018-10-27 13:48:54 +0100325 parcel.writeTypedList(mDependencies);
Svet Ganov67882122016-12-11 16:36:34 -0800326 }
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
345 public static final Parcelable.Creator<SharedLibraryInfo> CREATOR =
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}