1 /* 2 * Copyright (C) 2017 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.dialer.common.concurrent; 18 19 import android.os.AsyncTask; 20 import android.support.annotation.NonNull; 21 import android.support.annotation.Nullable; 22 import com.android.dialer.common.concurrent.FallibleAsyncTask.FallibleTaskResult; 23 import com.google.auto.value.AutoValue; 24 25 /** 26 * A task that runs work in the background, passing Throwables from {@link 27 * #doInBackground(Object[])} to {@link #onPostExecute(Object)} through a {@link 28 * FallibleTaskResult}. 29 * 30 * @param <ParamsT> the type of the parameters sent to the task upon execution 31 * @param <ProgressT> the type of the progress units published during the background computation 32 * @param <ResultT> the type of the result of the background computation 33 * @deprecated Please use {@link DialerExecutors}. 34 */ 35 @Deprecated 36 public abstract class FallibleAsyncTask<ParamsT, ProgressT, ResultT> 37 extends AsyncTask<ParamsT, ProgressT, FallibleTaskResult<ResultT>> { 38 39 @Override doInBackground(ParamsT... params)40 protected final FallibleTaskResult<ResultT> doInBackground(ParamsT... params) { 41 try { 42 return FallibleTaskResult.createSuccessResult(doInBackgroundFallible(params)); 43 } catch (Throwable t) { 44 return FallibleTaskResult.createFailureResult(t); 45 } 46 } 47 48 /** Performs background work that may result in a Throwable. */ 49 @Nullable doInBackgroundFallible(ParamsT... params)50 protected abstract ResultT doInBackgroundFallible(ParamsT... params) throws Throwable; 51 52 /** 53 * Holds the result of processing from {@link #doInBackground(Object[])}. 54 * 55 * @param <ResultT> the type of the result of the background computation 56 */ 57 @AutoValue 58 public abstract static class FallibleTaskResult<ResultT> { 59 60 /** Creates an instance of FallibleTaskResult for the given throwable. */ createFailureResult(@onNull Throwable t)61 private static <ResultT> FallibleTaskResult<ResultT> createFailureResult(@NonNull Throwable t) { 62 return new AutoValue_FallibleAsyncTask_FallibleTaskResult<>(t, null); 63 } 64 65 /** Creates an instance of FallibleTaskResult for the given result. */ createSuccessResult( @ullable ResultT result)66 private static <ResultT> FallibleTaskResult<ResultT> createSuccessResult( 67 @Nullable ResultT result) { 68 return new AutoValue_FallibleAsyncTask_FallibleTaskResult<>(null, result); 69 } 70 71 /** 72 * Returns the Throwable thrown in {@link #doInBackground(Object[])}, or {@code null} if 73 * background work completed without throwing. 74 */ 75 @Nullable getThrowable()76 public abstract Throwable getThrowable(); 77 78 /** 79 * Returns the result of {@link #doInBackground(Object[])}, which may be {@code null}, or {@code 80 * null} if the background work threw a Throwable. 81 * 82 * <p>Use {@link #isFailure()} to determine if a {@code null} return is the result of a 83 * Throwable from the background work. 84 */ 85 @Nullable getResult()86 public abstract ResultT getResult(); 87 88 /** 89 * Returns {@code true} if this object is the result of background work that threw a Throwable. 90 */ isFailure()91 public boolean isFailure() { 92 //noinspection ThrowableResultOfMethodCallIgnored 93 return getThrowable() != null; 94 } 95 } 96 } 97