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