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 android.adservices.common; 18 19 import android.annotation.NonNull; 20 import android.os.Build; 21 import android.os.OutcomeReceiver; 22 23 import androidx.annotation.RequiresApi; 24 25 /** 26 * Utility class to convert between {@link OutcomeReceiver} and {@link AdServicesOutcomeReceiver}. 27 * 28 * @hide 29 */ 30 @RequiresApi(Build.VERSION_CODES.S) 31 public final class OutcomeReceiverConverter { OutcomeReceiverConverter()32 private OutcomeReceiverConverter() { 33 // Prevent instantiation 34 } 35 36 /** 37 * Converts an instance of {@link OutcomeReceiver} to a custom {@link 38 * AdServicesOutcomeReceiver}. 39 * 40 * @param callback the instance of {@link OutcomeReceiver} to wrap 41 * @return an {@link AdServicesOutcomeReceiver} that wraps the original input 42 * @param <R> the type of Result that the receiver can process 43 * @param <E> the type of Exception that can be handled by the receiver 44 */ 45 public static <R, E extends Throwable> toAdServicesOutcomeReceiver( OutcomeReceiver<R, E> callback)46 AdServicesOutcomeReceiver<R, E> toAdServicesOutcomeReceiver( 47 OutcomeReceiver<R, E> callback) { 48 if (callback == null) { 49 return null; 50 } 51 52 return new AdServicesOutcomeReceiver<R, E>() { 53 @Override 54 public void onResult(R result) { 55 callback.onResult(result); 56 } 57 58 @Override 59 public void onError(@NonNull E error) { 60 callback.onError(error); 61 } 62 }; 63 } 64 } 65