blob: 3e22eb21d3693f1946d3ce992789dd65e3571e7e [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 */
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;
Dianne Hackborn3accca02013-09-20 09:32:11 -070031 private final long mVersionCode;
Svet Ganov67882122016-12-11 16:36:34 -080032
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
Dianne Hackborn3accca02013-09-20 09:32:11 -070050 /**
51 * Creates a new instance. Use {@link PackageManager#VERSION_CODE_HIGHEST}
52 * to refer to the highest version code of this package.
53 * @param packageName The package name.
54 * @param versionCode The version code.
55 */
56 public VersionedPackage(@NonNull String packageName,
57 @VersionCode long versionCode) {
58 mPackageName = packageName;
59 mVersionCode = versionCode;
60 }
61
Svet Ganov67882122016-12-11 16:36:34 -080062 private VersionedPackage(Parcel parcel) {
63 mPackageName = parcel.readString();
Dianne Hackborn3accca02013-09-20 09:32:11 -070064 mVersionCode = parcel.readLong();
Svet Ganov67882122016-12-11 16:36:34 -080065 }
66
67 /**
68 * Gets the package name.
69 *
70 * @return The package name.
71 */
72 public @NonNull String getPackageName() {
73 return mPackageName;
74 }
75
76 /**
Dianne Hackborn3accca02013-09-20 09:32:11 -070077 * @deprecated use {@link #getLongVersionCode()} instead.
78 */
79 @Deprecated
80 public @VersionCode int getVersionCode() {
81 return (int) (mVersionCode & 0x7fffffff);
82 }
83
84 /**
Svet Ganov67882122016-12-11 16:36:34 -080085 * Gets the version code.
86 *
87 * @return The version code.
88 */
Dianne Hackborn3accca02013-09-20 09:32:11 -070089 public @VersionCode long getLongVersionCode() {
Svet Ganov67882122016-12-11 16:36:34 -080090 return mVersionCode;
91 }
92
93 @Override
94 public String toString() {
95 return "VersionedPackage[" + mPackageName + "/" + mVersionCode + "]";
96 }
97
98 @Override
99 public int describeContents() {
100 return 0;
101 }
102
103 @Override
104 public void writeToParcel(Parcel parcel, int flags) {
105 parcel.writeString(mPackageName);
Dianne Hackborn3accca02013-09-20 09:32:11 -0700106 parcel.writeLong(mVersionCode);
Svet Ganov67882122016-12-11 16:36:34 -0800107 }
108
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700109 public static final @android.annotation.NonNull Creator<VersionedPackage> CREATOR = new Creator<VersionedPackage>() {
Svet Ganov67882122016-12-11 16:36:34 -0800110 @Override
111 public VersionedPackage createFromParcel(Parcel source) {
112 return new VersionedPackage(source);
113 }
114
115 @Override
116 public VersionedPackage[] newArray(int size) {
117 return new VersionedPackage[size];
118 }
119 };
120}