1 /* 2 * Copyright (C) 2024 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.settings.notification.modes; 18 19 import android.util.Log; 20 21 import androidx.annotation.NonNull; 22 23 import com.google.common.util.concurrent.FutureCallback; 24 import com.google.common.util.concurrent.Futures; 25 import com.google.common.util.concurrent.ListenableFuture; 26 27 import java.util.concurrent.CancellationException; 28 import java.util.concurrent.Executor; 29 import java.util.function.Consumer; 30 31 class FutureUtil { 32 33 private static final String TAG = "ZenFutureUtil"; 34 whenDone(ListenableFuture<V> future, Consumer<V> consumer, Executor executor)35 static <V> void whenDone(ListenableFuture<V> future, Consumer<V> consumer, Executor executor) { 36 whenDone(future, consumer, executor, "Error in future"); 37 } 38 whenDone(ListenableFuture<V> future, Consumer<V> consumer, Executor executor, String errorLogMessage, Object... errorLogMessageArgs)39 static <V> void whenDone(ListenableFuture<V> future, Consumer<V> consumer, Executor executor, 40 String errorLogMessage, Object... errorLogMessageArgs) { 41 Futures.addCallback(future, new FutureCallback<V>() { 42 @Override 43 public void onSuccess(V v) { 44 consumer.accept(v); 45 } 46 47 @Override 48 public void onFailure(@NonNull Throwable throwable) { 49 if (!(throwable instanceof CancellationException)) { 50 Log.e(TAG, String.format(errorLogMessage, errorLogMessageArgs), throwable); 51 } 52 } 53 }, executor); 54 } 55 } 56