blob: 096301c92faab099146bf238592125722175f709 [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;
Svetoslav Ganov0cb43ef2017-04-18 14:28:35 -070077
Dianne Hackborn3accca02013-09-20 09:32:11 -070078 private final long mVersion;
Svetoslav Ganov0cb43ef2017-04-18 14:28:35 -070079 private final @Type int mType;
Svet Ganov67882122016-12-11 16:36:34 -080080 private final VersionedPackage mDeclaringPackage;
81 private final List<VersionedPackage> mDependentPackages;
Nicolas Geoffray5e498b02018-10-27 13:48:54 +010082 private List<SharedLibraryInfo> mDependencies;
Svet Ganov67882122016-12-11 16:36:34 -080083
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 Geoffray103454f2018-10-22 10:23:50 +010095 public SharedLibraryInfo(String path, String packageName, String name, long version, int type,
Nicolas Geoffray5e498b02018-10-27 13:48:54 +010096 VersionedPackage declaringPackage, List<VersionedPackage> dependentPackages,
97 List<SharedLibraryInfo> dependencies) {
Nicolas Geoffray103454f2018-10-22 10:23:50 +010098 mPath = path;
99 mPackageName = packageName;
Svet Ganov67882122016-12-11 16:36:34 -0800100 mName = name;
101 mVersion = version;
102 mType = type;
103 mDeclaringPackage = declaringPackage;
104 mDependentPackages = dependentPackages;
Nicolas Geoffray5e498b02018-10-27 13:48:54 +0100105 mDependencies = dependencies;
Svet Ganov67882122016-12-11 16:36:34 -0800106 }
107
108 private SharedLibraryInfo(Parcel parcel) {
Nicolas Geoffray103454f2018-10-22 10:23:50 +0100109 this(parcel.readString(), parcel.readString(), parcel.readString(), parcel.readLong(),
Nicolas Geoffray5e498b02018-10-27 13:48:54 +0100110 parcel.readInt(), parcel.readParcelable(null), parcel.readArrayList(null),
111 parcel.createTypedArrayList(SharedLibraryInfo.CREATOR));
Svet Ganov67882122016-12-11 16:36:34 -0800112 }
113
Svetoslav Ganov0cb43ef2017-04-18 14:28:35 -0700114 /**
115 * Gets the type of this library.
116 *
117 * @return The library type.
118 */
119 public @Type int getType() {
Svet Ganov67882122016-12-11 16:36:34 -0800120 return mType;
121 }
122
123 /**
Svetoslav Ganov0cb43ef2017-04-18 14:28:35 -0700124 * Gets the library name an app defines in its manifest
125 * to depend on the library.
Svet Ganov67882122016-12-11 16:36:34 -0800126 *
127 * @return The name.
128 */
129 public String getName() {
130 return mName;
131 }
132
133 /**
Nicolas Geoffray103454f2018-10-22 10:23:50 +0100134 * 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 Geoffray5e498b02018-10-27 13:48:54 +0100158 * 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 Hackborn3accca02013-09-20 09:32:11 -0700199 * @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 Ganov0cb43ef2017-04-18 14:28:35 -0700207 * 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 Ganov67882122016-12-11 16:36:34 -0800210 * are not versioned.
211 *
212 * @return The version.
213 */
Dianne Hackborn3accca02013-09-20 09:32:11 -0700214 public @IntRange(from = -1) long getLongVersion() {
Svet Ganov67882122016-12-11 16:36:34 -0800215 return mVersion;
216 }
217
218 /**
Svetoslav Ganov0cb43ef2017-04-18 14:28:35 -0700219 * @removed
Svet Ganov67882122016-12-11 16:36:34 -0800220 */
221 public boolean isBuiltin() {
222 return mType == TYPE_BUILTIN;
223 }
224
225 /**
Svetoslav Ganov0cb43ef2017-04-18 14:28:35 -0700226 * @removed
Svet Ganov67882122016-12-11 16:36:34 -0800227 */
228 public boolean isDynamic() {
229 return mType == TYPE_DYNAMIC;
230 }
231
232 /**
Svetoslav Ganov0cb43ef2017-04-18 14:28:35 -0700233 * @removed
Svet Ganov67882122016-12-11 16:36:34 -0800234 */
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 Geoffray103454f2018-10-22 10:23:50 +0100274 parcel.writeString(mPath);
275 parcel.writeString(mPackageName);
Svet Ganov67882122016-12-11 16:36:34 -0800276 parcel.writeString(mName);
Dianne Hackborn3accca02013-09-20 09:32:11 -0700277 parcel.writeLong(mVersion);
Svet Ganov67882122016-12-11 16:36:34 -0800278 parcel.writeInt(mType);
279 parcel.writeParcelable(mDeclaringPackage, flags);
280 parcel.writeList(mDependentPackages);
Nicolas Geoffray5e498b02018-10-27 13:48:54 +0100281 parcel.writeTypedList(mDependencies);
Svet Ganov67882122016-12-11 16:36:34 -0800282 }
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}