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 package android.content.pm; 17 18 import android.annotation.IntRange; 19 import android.annotation.NonNull; 20 import android.os.Parcel; 21 import android.os.Parcelable; 22 23 import java.lang.annotation.Retention; 24 import java.lang.annotation.RetentionPolicy; 25 26 /** 27 * Encapsulates a package and its version code. 28 */ 29 public final class VersionedPackage implements Parcelable { 30 private final String mPackageName; 31 private final long 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 /** 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 62 private VersionedPackage(Parcel parcel) { 63 mPackageName = parcel.readString(); 64 mVersionCode = parcel.readLong(); 65 } 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 /** 77 * @deprecated use {@link #getLongVersionCode()} instead. 78 */ 79 @Deprecated 80 public @VersionCode int getVersionCode() { 81 return (int) (mVersionCode & 0x7fffffff); 82 } 83 84 /** 85 * Gets the version code. 86 * 87 * @return The version code. 88 */ 89 public @VersionCode long getLongVersionCode() { 90 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); 106 parcel.writeLong(mVersionCode); 107 } 108 109 public static final Creator<VersionedPackage> CREATOR = new Creator<VersionedPackage>() { 110 @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 } 121