1 /* 2 * Copyright (C) 2022 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 com.android.tests.providers.appsetidsdk; 18 19 import android.adservices.appsetid.AppSetId; 20 import android.adservices.appsetid.AppSetIdManager; 21 import android.app.sdksandbox.LoadSdkException; 22 import android.app.sdksandbox.SandboxedSdk; 23 import android.app.sdksandbox.SandboxedSdkProvider; 24 import android.content.Context; 25 import android.os.Binder; 26 import android.os.Bundle; 27 import android.os.OutcomeReceiver; 28 import android.util.Log; 29 import android.view.View; 30 31 import java.util.concurrent.CompletableFuture; 32 import java.util.concurrent.Executor; 33 import java.util.concurrent.Executors; 34 35 public class AppSetIdSdk extends SandboxedSdkProvider { 36 private static final String TAG = "AppSetIdSdk"; 37 private static final Executor CALLBACK_EXECUTOR = Executors.newCachedThreadPool(); 38 39 @Override onLoadSdk(Bundle params)40 public SandboxedSdk onLoadSdk(Bundle params) throws LoadSdkException { 41 try { 42 AppSetIdManager appSetIdManager = AppSetIdManager.get(getContext()); 43 44 CompletableFuture<AppSetId> future = new CompletableFuture<>(); 45 OutcomeReceiver<AppSetId, Exception> callback = 46 new OutcomeReceiver<AppSetId, Exception>() { 47 @Override 48 public void onResult(AppSetId result) { 49 future.complete(result); 50 } 51 52 @Override 53 public void onError(Exception error) { 54 Log.e(TAG, "SDK Runtime.testAppSetId onError " + error.getMessage()); 55 } 56 }; 57 58 appSetIdManager.getAppSetId(CALLBACK_EXECUTOR, callback); 59 60 AppSetId resultAppSetId = future.get(); 61 62 if (resultAppSetId.getId() != null) { 63 // Successfully called the getAppSetId 64 Log.d( 65 TAG, 66 "Successfully called the getAppSetId. resultAppSetId.getId() = " 67 + resultAppSetId.getId()); 68 return new SandboxedSdk(new Binder()); 69 } else { 70 // Failed to call the getAppSetId 71 Log.e(TAG, "Failed to call the getAppSetId"); 72 throw new LoadSdkException(new Exception("AppSetId failed."), new Bundle()); 73 } 74 } catch (Exception e) { 75 Log.e(TAG, e.getMessage()); 76 // Throw an exception to tell the Test App that some errors occurred so 77 // that it will fail the test. 78 throw new LoadSdkException(e, new Bundle()); 79 } 80 } 81 82 @Override getView(Context windowContext, Bundle params, int width, int height)83 public View getView(Context windowContext, Bundle params, int width, int height) { 84 return null; 85 } 86 } 87