1 /* 2 * Copyright (C) 2011 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.MainThread; 21 import java.util.concurrent.Executor; 22 23 /** 24 * Interface used to submit {@link AsyncTask} objects to run in the background. 25 * 26 * <p>This interface has a direct parallel with the {@link Executor} interface. It exists to 27 * decouple the mechanics of AsyncTask submission from the description of how that AsyncTask will 28 * execute. 29 * 30 * <p>One immediate benefit of this approach is that testing becomes much easier, since it is easy 31 * to introduce a mock or fake AsyncTaskExecutor in unit/integration tests, and thus inspect which 32 * tasks have been submitted and control their execution in an orderly manner. 33 * 34 * <p>Another benefit in due course will be the management of the submitted tasks. An extension to 35 * this interface is planned to allow Activities to easily cancel all the submitted tasks that are 36 * still pending in the onDestroy() method of the Activity. 37 */ 38 public interface AsyncTaskExecutor { 39 40 /** 41 * Executes the given AsyncTask with the default Executor. 42 * 43 * <p>This method <b>must only be called from the ui thread</b>. 44 * 45 * <p>The identifier supplied is any Object that can be used to identify the task later. Most 46 * commonly this will be an enum which the tests can also refer to. {@code null} is also accepted, 47 * though of course this won't help in identifying the task later. 48 */ 49 @MainThread submit(Object identifier, AsyncTask<T, ?, ?> task, T... params)50 <T> AsyncTask<T, ?, ?> submit(Object identifier, AsyncTask<T, ?, ?> task, T... params); 51 } 52