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 */
16package android.content.pm;
17
18import android.annotation.IntRange;
19import android.annotation.NonNull;
20import android.os.Parcel;
21import android.os.Parcelable;
22
23import java.lang.annotation.Retention;
24import java.lang.annotation.RetentionPolicy;
25
26/**
27 * Encapsulates a package and its version code.
28 */
29public final class VersionedPackage implements Parcelable {
30    private final String mPackageName;
31    private final int mVersionCode;
32
33    /** @hide */
34    @Retention(RetentionPolicy.SOURCE)
35    @IntRange(from = PackageManager.VERSION_CODE_HIGHEST)
36    public @interface VersionCode{}
37
38    /**
39     * Creates a new instance. Use {@link PackageManager#VERSION_CODE_HIGHEST}
40     * to refer to the highest version code of this package.
41     * @param packageName The package name.
42     * @param versionCode The version code.
43     */
44    public VersionedPackage(@NonNull String packageName,
45            @VersionCode int versionCode) {
46        mPackageName = packageName;
47        mVersionCode = versionCode;
48    }
49
50    private VersionedPackage(Parcel parcel) {
51        mPackageName = parcel.readString();
52        mVersionCode = parcel.readInt();
53    }
54
55    /**
56     * Gets the package name.
57     *
58     * @return The package name.
59     */
60    public @NonNull String getPackageName() {
61        return mPackageName;
62    }
63
64    /**
65     * Gets the version code.
66     *
67     * @return The version code.
68     */
69    public @VersionCode int getVersionCode() {
70        return mVersionCode;
71    }
72
73    @Override
74    public String toString() {
75        return "VersionedPackage[" + mPackageName + "/" + mVersionCode + "]";
76    }
77
78    @Override
79    public int describeContents() {
80        return 0;
81    }
82
83    @Override
84    public void writeToParcel(Parcel parcel, int flags) {
85        parcel.writeString(mPackageName);
86        parcel.writeInt(mVersionCode);
87    }
88
89    public static final Creator<VersionedPackage> CREATOR = new Creator<VersionedPackage>() {
90        @Override
91        public VersionedPackage createFromParcel(Parcel source) {
92            return new VersionedPackage(source);
93        }
94
95        @Override
96        public VersionedPackage[] newArray(int size) {
97            return new VersionedPackage[size];
98        }
99    };
100}
101