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