1 /* 2 * Copyright 2020 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 17 package android.app.appsearch; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.app.appsearch.safeparcel.PackageIdentifierParcel; 22 23 import java.util.Objects; 24 25 /** This class represents a uniquely identifiable package. */ 26 // TODO(b/384721898): Switch to JSpecify annotations 27 @SuppressWarnings("JSpecifyNullness") 28 public class PackageIdentifier { 29 private final @NonNull PackageIdentifierParcel mPackageIdentifierParcel; 30 31 /** 32 * Creates a unique identifier for a package. 33 * 34 * <p>SHA-256 certificate digests for a signed application can be retrieved with the <a 35 * href="{@docRoot}studio/command-line/apksigner/">apksigner tool</a> that is part of the 36 * Android SDK build tools. Use {@code apksigner verify --print-certs path/to/apk.apk} to 37 * retrieve the SHA-256 certificate digest for the target application. Once retrieved, the 38 * SHA-256 certificate digest should be converted to a {@code byte[]} by decoding it in base16: 39 * 40 * <pre> 41 * new android.content.pm.Signature(outputDigest).toByteArray(); 42 * </pre> 43 * 44 * @param packageName Name of the package. 45 * @param sha256Certificate SHA-256 certificate digest of the package. 46 */ PackageIdentifier(@onNull String packageName, @NonNull byte[] sha256Certificate)47 public PackageIdentifier(@NonNull String packageName, @NonNull byte[] sha256Certificate) { 48 Objects.requireNonNull(packageName); 49 Objects.requireNonNull(sha256Certificate); 50 mPackageIdentifierParcel = new PackageIdentifierParcel(packageName, sha256Certificate); 51 } 52 53 /** @hide */ PackageIdentifier(@onNull PackageIdentifierParcel packageIdentifierParcel)54 public PackageIdentifier(@NonNull PackageIdentifierParcel packageIdentifierParcel) { 55 mPackageIdentifierParcel = Objects.requireNonNull(packageIdentifierParcel); 56 } 57 58 /** 59 * Returns the {@link PackageIdentifierParcel} holding the values for this {@link 60 * PackageIdentifier}. 61 * 62 * @hide 63 */ getPackageIdentifierParcel()64 public @NonNull PackageIdentifierParcel getPackageIdentifierParcel() { 65 return mPackageIdentifierParcel; 66 } 67 68 /** Returns the name for a package. */ getPackageName()69 public @NonNull String getPackageName() { 70 return mPackageIdentifierParcel.getPackageName(); 71 } 72 73 /** Returns the SHA-256 certificate for a package. */ getSha256Certificate()74 public @NonNull byte[] getSha256Certificate() { 75 return mPackageIdentifierParcel.getSha256Certificate(); 76 } 77 78 @Override equals(@ullable Object obj)79 public boolean equals(@Nullable Object obj) { 80 if (this == obj) { 81 return true; 82 } 83 if (!(obj instanceof PackageIdentifier)) { 84 return false; 85 } 86 final PackageIdentifier other = (PackageIdentifier) obj; 87 return mPackageIdentifierParcel.equals(other.getPackageIdentifierParcel()); 88 } 89 90 @Override hashCode()91 public int hashCode() { 92 return mPackageIdentifierParcel.hashCode(); 93 } 94 } 95