1 /* 2 * Copyright (C) 2023 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.adservices.service.adselection; 18 19 import static android.adservices.common.AdServicesStatusUtils.STATUS_SUCCESS; 20 21 import android.adservices.adid.AdId; 22 import android.adservices.adid.GetAdIdResult; 23 import android.adservices.adid.IGetAdIdCallback; 24 import android.annotation.NonNull; 25 import android.os.RemoteException; 26 27 import com.android.adservices.LoggerFactory; 28 import com.android.adservices.service.adid.AdIdCacheManager; 29 import com.android.adservices.service.adid.AdIdWorker; 30 31 import java.util.concurrent.TimeUnit; 32 33 public class MockAdIdWorker extends AdIdWorker { 34 35 public static final String MOCK_AD_ID = "35a4ac90-e4dc-4fe7-bbc6-95e804aa7dbc"; 36 private static final LoggerFactory.Logger sLogger = LoggerFactory.getFledgeLogger(); 37 private String mAdId; 38 private boolean mIsLadEnabled; 39 private int mErrorCode; 40 private long mDelayMs; 41 MockAdIdWorker(AdIdCacheManager adIdCacheManager)42 public MockAdIdWorker(AdIdCacheManager adIdCacheManager) { 43 super(adIdCacheManager); 44 } 45 setResult(String adId, boolean isLatEnabled)46 public void setResult(String adId, boolean isLatEnabled) { 47 mAdId = adId; 48 mIsLadEnabled = isLatEnabled; 49 mErrorCode = 0; 50 mDelayMs = 0L; 51 } 52 setDelay(long delayMs)53 public void setDelay(long delayMs) { 54 mDelayMs = delayMs; 55 } 56 getResult()57 public boolean getResult() { 58 return AdId.ZERO_OUT.equals(mAdId) || mIsLadEnabled; 59 } 60 setError(int errCode)61 public void setError(int errCode) { 62 mErrorCode = errCode; 63 mAdId = null; 64 mIsLadEnabled = false; 65 mDelayMs = 0L; 66 } 67 getError()68 public int getError() { 69 return mErrorCode; 70 } 71 72 @Override getAdId( @onNull String packageName, int appUid, @NonNull IGetAdIdCallback callback)73 public void getAdId( 74 @NonNull String packageName, int appUid, @NonNull IGetAdIdCallback callback) { 75 if (mAdId != null) { 76 if (mDelayMs > 0) { 77 try { 78 sLogger.v("Sleeping for %d", mDelayMs); 79 TimeUnit.MILLISECONDS.sleep(mDelayMs); 80 } catch (InterruptedException interruptedException) { 81 throw new RuntimeException("Delay configured but cannot be executed"); 82 } 83 } 84 sLogger.v( 85 "return GetAdIdResult with adId: %s and isLatEnabled as %b", 86 mAdId, mIsLadEnabled); 87 GetAdIdResult result = 88 new GetAdIdResult.Builder() 89 .setStatusCode(STATUS_SUCCESS) 90 .setErrorMessage("") 91 .setAdId(mAdId) 92 .setLatEnabled(mIsLadEnabled) 93 .build(); 94 try { 95 callback.onResult(result); 96 } catch (RemoteException exception) { 97 sLogger.e(exception, "Remote exception on calling callback.onResult"); 98 } 99 } else if (mErrorCode != 0) { 100 try { 101 callback.onError(mErrorCode); 102 } catch (RemoteException exception) { 103 sLogger.e(exception, "Remote exception on calling callback.onError"); 104 } 105 } else { 106 throw new NullPointerException("Neither result nor error are set."); 107 } 108 } 109 } 110