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.util.Log; 28 import android.view.View; 29 30 import com.android.adservices.shared.testing.OutcomeReceiverForTests; 31 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 42 try { 43 AppSetIdManager appSetIdManager = AppSetIdManager.get(getContext()); 44 OutcomeReceiverForTests<AppSetId> callback = new OutcomeReceiverForTests<>(); 45 appSetIdManager.getAppSetId(CALLBACK_EXECUTOR, callback); 46 47 AppSetId resultAppSetId = callback.assertResultReceived(); 48 49 if (resultAppSetId != null && resultAppSetId.getId() != null) { 50 // Successfully called the getAppSetId 51 if (resultAppSetId.getId().isEmpty()) { 52 Log.e(TAG, "AppSetId get empty result"); 53 } else { 54 Log.d( 55 TAG, 56 "Successfully called the getAppSetId. resultAppSetIdString = " 57 + resultAppSetId.getId()); 58 } 59 return new SandboxedSdk(new Binder()); 60 } else { 61 // Failed to call the getAppSetId 62 Exception exception = callback.getError(); 63 Log.e(TAG, "Failed to call the getAppSetId with exception: " + exception); 64 throw new LoadSdkException(new Exception("AppSetId failed."), new Bundle()); 65 } 66 } catch (Exception e) { 67 Log.e(TAG, e.getMessage()); 68 // Throw an exception to tell the Test App that some errors occurred so 69 // that it will fail the test. 70 throw new LoadSdkException(e, new Bundle()); 71 } 72 } 73 74 @Override getView(Context windowContext, Bundle params, int width, int height)75 public View getView(Context windowContext, Bundle params, int width, int height) { 76 return null; 77 } 78 } 79