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