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