blob: 8354296b68d7797484e37eb0dbc9643d52c1edb6 [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 Geoffray103454f2018-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 Geoffray5e498b02018-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;
36 * static - updatable non backwards-compatible emulating static linking.
37 */
38public final class SharedLibraryInfo implements Parcelable {
Svetoslav Ganov0cb43ef2017-04-18 14:28:35 -070039
40 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -070041 @IntDef(flag = true, prefix = { "TYPE_" }, value = {
42 TYPE_BUILTIN,
43 TYPE_DYNAMIC,
44 TYPE_STATIC,
45 })
Svetoslav Ganov0cb43ef2017-04-18 14:28:35 -070046 @Retention(RetentionPolicy.SOURCE)
47 @interface Type{}
48
Svet Ganov67882122016-12-11 16:36:34 -080049 /**
50 * Shared library type: this library is a part of the OS
51 * and cannot be updated or uninstalled.
Svet Ganov67882122016-12-11 16:36:34 -080052 */
Svetoslav Ganov0cb43ef2017-04-18 14:28:35 -070053 public static final int TYPE_BUILTIN = 0;
Svet Ganov67882122016-12-11 16:36:34 -080054
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 Ganov67882122016-12-11 16:36:34 -080059 */
Svetoslav Ganov0cb43ef2017-04-18 14:28:35 -070060 public static final int TYPE_DYNAMIC = 1;
Svet Ganov67882122016-12-11 16:36:34 -080061
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 Ganov67882122016-12-11 16:36:34 -080066 */
Svetoslav Ganov0cb43ef2017-04-18 14:28:35 -070067 public static final int TYPE_STATIC = 2;
Svet Ganov67882122016-12-11 16:36:34 -080068
69 /**
70 * Constant for referring to an undefined version.
71 */
72 public static final int VERSION_UNDEFINED = -1;
73
Nicolas Geoffray103454f2018-10-22 10:23:50 +010074 private final String mPath;
75 private final String mPackageName;
Svet Ganov67882122016-12-11 16:36:34 -080076 private final String mName;
Nicolas Geoffray972b39e2018-11-15 12:59:52 +000077 private final List<String> mCodePaths;
Svetoslav Ganov0cb43ef2017-04-18 14:28:35 -070078
Dianne Hackborn3accca02013-09-20 09:32:11 -070079 private final long mVersion;
Svetoslav Ganov0cb43ef2017-04-18 14:28:35 -070080 private final @Type int mType;
Svet Ganov67882122016-12-11 16:36:34 -080081 private final VersionedPackage mDeclaringPackage;
82 private final List<VersionedPackage> mDependentPackages;
Nicolas Geoffray5e498b02018-10-27 13:48:54 +010083 private List<SharedLibraryInfo> mDependencies;
Svet Ganov67882122016-12-11 16:36:34 -080084
85 /**
86 * Creates a new instance.
87 *
Nicolas Geoffray972b39e2018-11-15 12:59:52 +000088 * @param codePaths For a non {@link #TYPE_BUILTIN builtin} library, the locations of jars of
89 * this shared library. Null for builtin library.
Svet Ganov67882122016-12-11 16:36:34 -080090 * @param name The lib name.
91 * @param version The lib version if not builtin.
92 * @param type The lib type.
93 * @param declaringPackage The package that declares the library.
94 * @param dependentPackages The packages that depend on the library.
95 *
96 * @hide
97 */
Nicolas Geoffray972b39e2018-11-15 12:59:52 +000098 public SharedLibraryInfo(String path, String packageName, List<String> codePaths,
99 String name, long version, int type,
Nicolas Geoffray5e498b02018-10-27 13:48:54 +0100100 VersionedPackage declaringPackage, List<VersionedPackage> dependentPackages,
101 List<SharedLibraryInfo> dependencies) {
Nicolas Geoffray103454f2018-10-22 10:23:50 +0100102 mPath = path;
103 mPackageName = packageName;
Nicolas Geoffray972b39e2018-11-15 12:59:52 +0000104 mCodePaths = codePaths;
Svet Ganov67882122016-12-11 16:36:34 -0800105 mName = name;
106 mVersion = version;
107 mType = type;
108 mDeclaringPackage = declaringPackage;
109 mDependentPackages = dependentPackages;
Nicolas Geoffray5e498b02018-10-27 13:48:54 +0100110 mDependencies = dependencies;
Svet Ganov67882122016-12-11 16:36:34 -0800111 }
112
113 private SharedLibraryInfo(Parcel parcel) {
Nicolas Geoffray972b39e2018-11-15 12:59:52 +0000114 this(parcel.readString(), parcel.readString(), parcel.readArrayList(null),
115 parcel.readString(), parcel.readLong(),
Nicolas Geoffray5e498b02018-10-27 13:48:54 +0100116 parcel.readInt(), parcel.readParcelable(null), parcel.readArrayList(null),
117 parcel.createTypedArrayList(SharedLibraryInfo.CREATOR));
Svet Ganov67882122016-12-11 16:36:34 -0800118 }
119
Svetoslav Ganov0cb43ef2017-04-18 14:28:35 -0700120 /**
121 * Gets the type of this library.
122 *
123 * @return The library type.
124 */
125 public @Type int getType() {
Svet Ganov67882122016-12-11 16:36:34 -0800126 return mType;
127 }
128
129 /**
Svetoslav Ganov0cb43ef2017-04-18 14:28:35 -0700130 * Gets the library name an app defines in its manifest
131 * to depend on the library.
Svet Ganov67882122016-12-11 16:36:34 -0800132 *
133 * @return The name.
134 */
135 public String getName() {
136 return mName;
137 }
138
139 /**
Nicolas Geoffray103454f2018-10-22 10:23:50 +0100140 * If the shared library is a jar file, returns the path of that jar. Null otherwise.
141 * Only libraries with TYPE_BUILTIN are in jar files.
142 *
143 * @return The path.
144 *
145 * @hide
146 */
147 public @Nullable String getPath() {
148 return mPath;
149 }
150
151 /**
152 * If the shared library is an apk, returns the package name. Null otherwise.
153 * Only libraries with TYPE_DYNAMIC or TYPE_STATIC are in apks.
154 *
155 * @return The package name.
156 *
157 * @hide
158 */
159 public @Nullable String getPackageName() {
160 return mPackageName;
161 }
162
163 /**
Nicolas Geoffray972b39e2018-11-15 12:59:52 +0000164 * Get all code paths for that library.
165 *
166 * @return All code paths.
167 *
168 * @hide
169 */
170 public List<String> getAllCodePaths() {
171 if (getPath() != null) {
172 // Builtin library.
173 ArrayList<String> list = new ArrayList<>();
174 list.add(getPath());
175 return list;
176 } else {
177 // Static or dynamic library.
178 return mCodePaths;
179 }
180 }
181
182 /**
Nicolas Geoffray5e498b02018-10-27 13:48:54 +0100183 * Add a library dependency to that library. Note that this
184 * should be called under the package manager lock.
185 *
186 * @hide
187 */
188 public void addDependency(@Nullable SharedLibraryInfo info) {
189 if (info == null) {
190 // For convenience of the caller, allow null to be passed.
191 // This can happen when we create the dependencies of builtin
192 // libraries.
193 return;
194 }
195 if (mDependencies == null) {
196 mDependencies = new ArrayList<>();
197 }
198 mDependencies.add(info);
199 }
200
201 /**
202 * Clear all dependencies.
203 *
204 * @hide
205 */
206 public void clearDependencies() {
207 mDependencies = null;
208 }
209
210 /**
211 * Gets the libraries this library directly depends on. Note that
212 * the package manager prevents recursive dependencies when installing
213 * a package.
214 *
215 * @return The dependencies.
216 *
217 * @hide
218 */
219 public @Nullable List<SharedLibraryInfo> getDependencies() {
220 return mDependencies;
221 }
222
223 /**
Dianne Hackborn3accca02013-09-20 09:32:11 -0700224 * @deprecated Use {@link #getLongVersion()} instead.
225 */
226 @Deprecated
227 public @IntRange(from = -1) int getVersion() {
228 return mVersion < 0 ? (int) mVersion : (int) (mVersion & 0x7fffffff);
229 }
230
231 /**
Svetoslav Ganov0cb43ef2017-04-18 14:28:35 -0700232 * Gets the version of the library. For {@link #TYPE_STATIC static} libraries
233 * this is the declared version and for {@link #TYPE_DYNAMIC dynamic} and
234 * {@link #TYPE_BUILTIN builtin} it is {@link #VERSION_UNDEFINED} as these
Svet Ganov67882122016-12-11 16:36:34 -0800235 * are not versioned.
236 *
237 * @return The version.
238 */
Dianne Hackborn3accca02013-09-20 09:32:11 -0700239 public @IntRange(from = -1) long getLongVersion() {
Svet Ganov67882122016-12-11 16:36:34 -0800240 return mVersion;
241 }
242
243 /**
Svetoslav Ganov0cb43ef2017-04-18 14:28:35 -0700244 * @removed
Svet Ganov67882122016-12-11 16:36:34 -0800245 */
246 public boolean isBuiltin() {
247 return mType == TYPE_BUILTIN;
248 }
249
250 /**
Svetoslav Ganov0cb43ef2017-04-18 14:28:35 -0700251 * @removed
Svet Ganov67882122016-12-11 16:36:34 -0800252 */
253 public boolean isDynamic() {
254 return mType == TYPE_DYNAMIC;
255 }
256
257 /**
Svetoslav Ganov0cb43ef2017-04-18 14:28:35 -0700258 * @removed
Svet Ganov67882122016-12-11 16:36:34 -0800259 */
260 public boolean isStatic() {
261 return mType == TYPE_STATIC;
262 }
263
264 /**
265 * Gets the package that declares the library.
266 *
267 * @return The package declaring the library.
268 */
269 public @NonNull VersionedPackage getDeclaringPackage() {
270 return mDeclaringPackage;
271 }
272
273 /**
274 * Gets the packages that depend on the library.
275 *
276 * @return The dependent packages.
277 */
278 public @NonNull List<VersionedPackage> getDependentPackages() {
279 if (mDependentPackages == null) {
280 return Collections.emptyList();
281 }
282 return mDependentPackages;
283 }
284
285 @Override
286 public int describeContents() {
287 return 0;
288 }
289
290 @Override
291 public String toString() {
Steven Moreland1e35a412019-03-18 18:30:22 -0700292 return "SharedLibraryInfo{name:" + mName + ", type:" + typeToString(mType)
Svet Ganov67882122016-12-11 16:36:34 -0800293 + ", version:" + mVersion + (!getDependentPackages().isEmpty()
Steven Moreland1e35a412019-03-18 18:30:22 -0700294 ? " has dependents" : "") + "}";
Svet Ganov67882122016-12-11 16:36:34 -0800295 }
296
297 @Override
298 public void writeToParcel(Parcel parcel, int flags) {
Nicolas Geoffray103454f2018-10-22 10:23:50 +0100299 parcel.writeString(mPath);
300 parcel.writeString(mPackageName);
Nicolas Geoffray972b39e2018-11-15 12:59:52 +0000301 parcel.writeList(mCodePaths);
Svet Ganov67882122016-12-11 16:36:34 -0800302 parcel.writeString(mName);
Dianne Hackborn3accca02013-09-20 09:32:11 -0700303 parcel.writeLong(mVersion);
Svet Ganov67882122016-12-11 16:36:34 -0800304 parcel.writeInt(mType);
305 parcel.writeParcelable(mDeclaringPackage, flags);
306 parcel.writeList(mDependentPackages);
Nicolas Geoffray5e498b02018-10-27 13:48:54 +0100307 parcel.writeTypedList(mDependencies);
Svet Ganov67882122016-12-11 16:36:34 -0800308 }
309
310 private static String typeToString(int type) {
311 switch (type) {
312 case TYPE_BUILTIN: {
313 return "builtin";
314 }
315 case TYPE_DYNAMIC: {
316 return "dynamic";
317 }
318 case TYPE_STATIC: {
319 return "static";
320 }
321 default: {
322 return "unknown";
323 }
324 }
325 }
326
327 public static final Parcelable.Creator<SharedLibraryInfo> CREATOR =
328 new Parcelable.Creator<SharedLibraryInfo>() {
329 public SharedLibraryInfo createFromParcel(Parcel source) {
330 return new SharedLibraryInfo(source);
331 }
332
333 public SharedLibraryInfo[] newArray(int size) {
334 return new SharedLibraryInfo[size];
335 }
336 };
337}