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.annotation.Nullable; 21 import android.os.Parcel; 22 import android.os.Parcelable; 23 24 import java.lang.annotation.Retention; 25 import java.lang.annotation.RetentionPolicy; 26 27 /** 28 * Encapsulates a package and its version code. 29 */ 30 public final class VersionedPackage implements Parcelable { 31 private final String mPackageName; 32 private final long mVersionCode; 33 34 /** @hide */ 35 @Retention(RetentionPolicy.SOURCE) 36 @IntRange(from = PackageManager.VERSION_CODE_HIGHEST) 37 public @interface VersionCode{} 38 39 /** 40 * Creates a new instance. Use {@link PackageManager#VERSION_CODE_HIGHEST} 41 * to refer to the highest version code of this package. 42 * @param packageName The package name. 43 * @param versionCode The version code. 44 */ VersionedPackage(@onNull String packageName, @VersionCode int versionCode)45 public VersionedPackage(@NonNull String packageName, 46 @VersionCode int versionCode) { 47 mPackageName = packageName; 48 mVersionCode = versionCode; 49 } 50 51 /** 52 * Creates a new instance. Use {@link PackageManager#VERSION_CODE_HIGHEST} 53 * to refer to the highest version code of this package. 54 * @param packageName The package name. 55 * @param versionCode The version code. 56 */ VersionedPackage(@onNull String packageName, @VersionCode long versionCode)57 public VersionedPackage(@NonNull String packageName, 58 @VersionCode long versionCode) { 59 mPackageName = packageName; 60 mVersionCode = versionCode; 61 } 62 VersionedPackage(Parcel parcel)63 private VersionedPackage(Parcel parcel) { 64 mPackageName = parcel.readString8(); 65 mVersionCode = parcel.readLong(); 66 } 67 68 /** 69 * Gets the package name. 70 * 71 * @return The package name. 72 */ getPackageName()73 public @NonNull String getPackageName() { 74 return mPackageName; 75 } 76 77 /** 78 * @deprecated use {@link #getLongVersionCode()} instead. 79 */ 80 @Deprecated getVersionCode()81 public @VersionCode int getVersionCode() { 82 return (int) (mVersionCode & 0x7fffffff); 83 } 84 85 /** 86 * Gets the version code. 87 * 88 * @return The version code. 89 */ getLongVersionCode()90 public @VersionCode long getLongVersionCode() { 91 return mVersionCode; 92 } 93 94 @Override toString()95 public String toString() { 96 return "VersionedPackage[" + mPackageName + "/" + mVersionCode + "]"; 97 } 98 99 @Override equals(@ullable Object o)100 public boolean equals(@Nullable Object o) { 101 return o instanceof VersionedPackage 102 && ((VersionedPackage) o).mPackageName.equals(mPackageName) 103 && ((VersionedPackage) o).mVersionCode == mVersionCode; 104 } 105 106 @Override hashCode()107 public int hashCode() { 108 // Roll our own hash function without using Objects#hash which incurs the overhead 109 // of autoboxing. 110 return 31 * mPackageName.hashCode() + Long.hashCode(mVersionCode); 111 } 112 113 @Override describeContents()114 public int describeContents() { 115 return 0; 116 } 117 118 @Override writeToParcel(Parcel parcel, int flags)119 public void writeToParcel(Parcel parcel, int flags) { 120 parcel.writeString8(mPackageName); 121 parcel.writeLong(mVersionCode); 122 } 123 124 public static final @android.annotation.NonNull Creator<VersionedPackage> CREATOR = new Creator<VersionedPackage>() { 125 @Override 126 public VersionedPackage createFromParcel(Parcel source) { 127 return new VersionedPackage(source); 128 } 129 130 @Override 131 public VersionedPackage[] newArray(int size) { 132 return new VersionedPackage[size]; 133 } 134 }; 135 } 136