/*
 * Copyright 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.appsearch.app;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.RestrictTo;
import androidx.appsearch.util.BundleUtil;
import androidx.core.util.Preconditions;

/** This class represents a uniquely identifiable package. */
public class PackageIdentifier {
    private static final String PACKAGE_NAME_FIELD = "packageName";
    private static final String SHA256_CERTIFICATE_FIELD = "sha256Certificate";

    private final Bundle mBundle;

    /**
     * Creates a unique identifier for a package.
     *
     * @param packageName Name of the package.
     * @param sha256Certificate SHA256 certificate digest of the package.
     */
    public PackageIdentifier(@NonNull String packageName, @NonNull byte[] sha256Certificate) {
        mBundle = new Bundle();
        mBundle.putString(PACKAGE_NAME_FIELD, packageName);
        mBundle.putByteArray(SHA256_CERTIFICATE_FIELD, sha256Certificate);
    }

    /** @hide */
    @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
    public PackageIdentifier(@NonNull Bundle bundle) {
        mBundle = Preconditions.checkNotNull(bundle);
    }

    /** @hide */
    @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
    @NonNull
    public Bundle getBundle() {
        return mBundle;
    }

    @NonNull
    public String getPackageName() {
        return Preconditions.checkNotNull(mBundle.getString(PACKAGE_NAME_FIELD));
    }

    @NonNull
    public byte[] getSha256Certificate() {
        return Preconditions.checkNotNull(mBundle.getByteArray(SHA256_CERTIFICATE_FIELD));
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || !(obj instanceof PackageIdentifier)) {
            return false;
        }
        final PackageIdentifier other = (PackageIdentifier) obj;
        return BundleUtil.deepEquals(mBundle, other.mBundle);
    }

    @Override
    public int hashCode() {
        return BundleUtil.deepHashCode(mBundle);
    }
}

@@ -1174,16 +1174,594 @@
 %0A     *%0A
+     * %3Cp%3ESHA-256 certificate digests for a signed application can be retrieved with the%0A     * %3Ca href=%22%7B@docRoot%7Dstudio/command-line/apksigner/%22%3Eapksigner tool%3C/a%3E that is part of the%0A     * Android SDK build tools. Use %7B@code apksigner verify --print-certs path/to/apk.apk%7D to%0A     * retrieve the SHA-256 certificate digest for the target application. Once retrieved, the%0A     * SHA-256 certificate digest should be converted to a %7B@code byte%5B%5D%7D by decoding it in base16:%0A     * %3Cpre%3E%0A     * new android.content.pm.Signature(outputDigest).toByteArray();%0A     * %3C/pre%3E%0A     *%0A
      * @
@@ -1834,16 +1834,17 @@
 cate SHA
+-
 256 cert

Clarify docs on SHA256 cert generation for PackagerIdentifier