1 /* 2 * Copyright (C) 2024 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.functions; 18 19 import android.annotation.FlaggedApi; 20 import android.annotation.NonNull; 21 import android.annotation.Nullable; 22 import android.app.appsearch.GenericDocument; 23 import android.app.appsearch.safeparcel.AbstractSafeParcelable; 24 import android.app.appsearch.safeparcel.GenericDocumentParcel; 25 import android.app.appsearch.safeparcel.SafeParcelable; 26 import android.os.Bundle; 27 import android.os.Parcel; 28 import android.os.Parcelable; 29 30 import com.android.appsearch.flags.Flags; 31 32 import java.util.Objects; 33 import java.util.concurrent.Executor; 34 import java.util.function.Consumer; 35 36 /** 37 * Represents a request to execute a specific app function. 38 * 39 * @see AppFunctionManager#executeAppFunction(ExecuteAppFunctionRequest, Executor, Consumer) 40 */ 41 @FlaggedApi(Flags.FLAG_ENABLE_APP_FUNCTIONS) 42 @SafeParcelable.Class(creator = "ExecuteAppFunctionRequestCreator") 43 public final class ExecuteAppFunctionRequest extends AbstractSafeParcelable implements Parcelable { 44 @NonNull 45 public static final Parcelable.Creator<ExecuteAppFunctionRequest> CREATOR = 46 new ExecuteAppFunctionRequestCreator(); 47 48 @Field(id = 1, getter = "getTargetPackageName") 49 @NonNull 50 private final String mTargetPackageName; 51 52 @Field(id = 2, getter = "getFunctionIdentifier") 53 @NonNull 54 private final String mFunctionIdentifier; 55 56 /** 57 * {@link GenericDocument} is not a Parcelable, so storing it as a GenericDocumentParcel here. 58 */ 59 @Field(id = 3) 60 @NonNull 61 final GenericDocumentParcel mParameters; 62 63 @Field(id = 4, getter = "getExtras") 64 @NonNull 65 private final Bundle mExtras; 66 67 @Field(id = 5, getter = "getSha256Certificate") 68 @Nullable 69 private final byte[] mSha256Certificate; 70 71 @NonNull private final GenericDocument mParametersCached; 72 73 /** Returns the package name of the app that hosts the function. */ 74 @NonNull getTargetPackageName()75 public String getTargetPackageName() { 76 return mTargetPackageName; 77 } 78 79 /** Returns the unique string identifier of the app function to be executed. */ 80 @NonNull getFunctionIdentifier()81 public String getFunctionIdentifier() { 82 return mFunctionIdentifier; 83 } 84 85 /** 86 * Returns the parameters required to invoke this function. Within this {@link GenericDocument}, 87 * the property names are the names of the function parameters and the property values are the 88 * values of those parameters 89 * 90 * <p>The document may have missing parameters. Developers are advised to implement defensive 91 * handling measures. 92 */ 93 @NonNull getParameters()94 public GenericDocument getParameters() { 95 return mParametersCached; 96 } 97 98 /** 99 * Returns the expected certificate SHA-256 digests of the target package. Returns {@code null} 100 * if no certificate digest checking is configured. 101 * 102 * @see Builder#getSha256Certificate() 103 */ 104 @Nullable getSha256Certificate()105 public byte[] getSha256Certificate() { 106 return mSha256Certificate; 107 } 108 109 /** Returns additional metadata relevant to this function execution request. */ 110 @NonNull getExtras()111 public Bundle getExtras() { 112 return mExtras; 113 } 114 ExecuteAppFunctionRequest( @onNull String targetPackageName, @NonNull String functionIdentifier, @NonNull GenericDocument document, @NonNull Bundle extras, @Nullable byte[] sha256Certificate)115 private ExecuteAppFunctionRequest( 116 @NonNull String targetPackageName, 117 @NonNull String functionIdentifier, 118 @NonNull GenericDocument document, 119 @NonNull Bundle extras, 120 @Nullable byte[] sha256Certificate) { 121 mTargetPackageName = Objects.requireNonNull(targetPackageName); 122 mFunctionIdentifier = Objects.requireNonNull(functionIdentifier); 123 mParametersCached = Objects.requireNonNull(document); 124 mParameters = mParametersCached.getDocumentParcel(); 125 mExtras = Objects.requireNonNull(extras); 126 mSha256Certificate = sha256Certificate; 127 } 128 129 @Constructor ExecuteAppFunctionRequest( @aramid = 1) @onNull String targetPackageName, @Param(id = 2) @NonNull String functionIdentifier, @Param(id = 3) @NonNull GenericDocumentParcel parameters, @Param(id = 4) @NonNull Bundle extras, @Param(id = 5) @Nullable byte[] sha256Certificate)130 ExecuteAppFunctionRequest( 131 @Param(id = 1) @NonNull String targetPackageName, 132 @Param(id = 2) @NonNull String functionIdentifier, 133 @Param(id = 3) @NonNull GenericDocumentParcel parameters, 134 @Param(id = 4) @NonNull Bundle extras, 135 @Param(id = 5) @Nullable byte[] sha256Certificate) { 136 mTargetPackageName = Objects.requireNonNull(targetPackageName); 137 mFunctionIdentifier = Objects.requireNonNull(functionIdentifier); 138 mParameters = Objects.requireNonNull(parameters); 139 mParametersCached = new GenericDocument(mParameters); 140 mExtras = Objects.requireNonNull(extras); 141 mSha256Certificate = sha256Certificate; 142 } 143 144 @Override writeToParcel(@onNull Parcel dest, int flags)145 public void writeToParcel(@NonNull Parcel dest, int flags) { 146 ExecuteAppFunctionRequestCreator.writeToParcel(this, dest, flags); 147 } 148 149 /** The builder for creating {@link ExecuteAppFunctionRequest} instances. */ 150 @FlaggedApi(Flags.FLAG_ENABLE_APP_FUNCTIONS) 151 public static final class Builder { 152 @NonNull private final String mPackageName; 153 @NonNull private final String mFunctionIdentifier; 154 @NonNull private GenericDocument mParameters = GenericDocument.EMPTY; 155 @NonNull private Bundle mExtras = Bundle.EMPTY; 156 @Nullable private byte[] mSha256Certificate; 157 158 /** 159 * Creates a new instance of this builder class. 160 * 161 * @param packageName The package name of the target app providing the app function to 162 * invoke. 163 * @param functionIdentifier The identifier used by the {@link AppFunctionService} from the 164 * target app to uniquely identify the function to be invoked. 165 */ Builder(@onNull String packageName, @NonNull String functionIdentifier)166 public Builder(@NonNull String packageName, @NonNull String functionIdentifier) { 167 mPackageName = Objects.requireNonNull(packageName); 168 mFunctionIdentifier = Objects.requireNonNull(functionIdentifier); 169 } 170 171 /** 172 * Sets parameters for invoking the app function. Within this {@link GenericDocument}, the 173 * property names are the names of the function parameters and the property values are the 174 * values of those parameters. Defaults to an empty {@link GenericDocument} if not set. 175 */ 176 @NonNull setParameters(@onNull GenericDocument parameters)177 public Builder setParameters(@NonNull GenericDocument parameters) { 178 mParameters = parameters; 179 return this; 180 } 181 182 /** 183 * Sets the expected certificate SHA-256 digests for the target package. Setting this to 184 * {@code null} indicates that no certificate digest check will be performed. 185 * 186 * <p>SHA-256 certificate digests for a signed application can be retrieved with the <a 187 * href="{@docRoot}studio/command-line/apksigner/">apksigner tool</a> that is part of the 188 * Android SDK build tools. Use {@code apksigner verify --print-certs path/to/apk.apk} to 189 * retrieve the SHA-256 certificate digest for the target application. Once retrieved, the 190 * SHA-256 certificate digest should be converted to a {@code byte[]} by decoding it in 191 * base16: 192 * 193 * <pre> 194 * new android.content.pm.Signature(outputDigest).toByteArray(); 195 * </pre> 196 */ 197 @NonNull setSha256Certificate(@ullable byte[] sha256Certificate)198 public Builder setSha256Certificate(@Nullable byte[] sha256Certificate) { 199 mSha256Certificate = sha256Certificate; 200 return this; 201 } 202 203 /** 204 * Sets the additional metadata relevant to this function execution request. Defaults to an 205 * empty {@link Bundle} if not set. 206 */ 207 @NonNull setExtras(@onNull Bundle extras)208 public Builder setExtras(@NonNull Bundle extras) { 209 mExtras = extras; 210 return this; 211 } 212 213 /** Constructs a new {@link ExecuteAppFunctionRequest} from the contents of this builder. */ 214 @NonNull build()215 public ExecuteAppFunctionRequest build() { 216 return new ExecuteAppFunctionRequest( 217 mPackageName, mFunctionIdentifier, mParameters, mExtras, mSha256Certificate); 218 } 219 } 220 } 221