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.adidsdk; 18 19 import android.adservices.adid.AdId; 20 import android.adservices.adid.AdIdManager; 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 AdIdSdk extends SandboxedSdkProvider { 36 private static final String TAG = "AdIdSdk"; 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 AdIdManager adIdManager = AdIdManager.get(getContext()); 43 44 CompletableFuture<AdId> future = new CompletableFuture<>(); 45 OutcomeReceiver<AdId, Exception> callback = 46 new OutcomeReceiver<AdId, Exception>() { 47 @Override 48 public void onResult(AdId result) { 49 future.complete(result); 50 } 51 52 @Override 53 public void onError(Exception error) { 54 Log.e(TAG, "SDK Runtime.testAdId onError " + error.getMessage()); 55 } 56 }; 57 58 adIdManager.getAdId(CALLBACK_EXECUTOR, callback); 59 60 AdId resultAdId = future.get(); 61 62 if (resultAdId.getAdId() != null) { 63 // Successfully called the getAdId 64 Log.d( 65 TAG, 66 "Successfully called the getAdId. resultAdId.getAdId() = " 67 + resultAdId.getAdId()); 68 return new SandboxedSdk(new Binder()); 69 } else { 70 // Failed to call the getAdId 71 Log.e(TAG, "Failed to call the getAdId"); 72 throw new LoadSdkException(new Exception("AdId 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