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 com.android.dialer.common.Assert;
22 import java.util.concurrent.Executor;
23 
24 /**
25  * Factory methods for creating AsyncTaskExecutors.
26  *
27  * <p>All of the factory methods on this class check first to see if you have set a static {@link
28  * AsyncTaskExecutorFactory} set through the {@link #setFactoryForTest(AsyncTaskExecutorFactory)}
29  * method, and if so delegate to that instead, which is one way of injecting dependencies for
30  * testing classes whose construction cannot be controlled such as {@link android.app.Activity}.
31  */
32 public final class AsyncTaskExecutors {
33 
34   /**
35    * A single instance of the {@link AsyncTaskExecutorFactory}, to which we delegate if it is
36    * non-null, for injecting when testing.
37    */
38   private static AsyncTaskExecutorFactory mInjectedAsyncTaskExecutorFactory = null;
39 
40   /**
41    * Creates an AsyncTaskExecutor that submits tasks to run with {@link AsyncTask#SERIAL_EXECUTOR}.
42    */
createAsyncTaskExecutor()43   public static AsyncTaskExecutor createAsyncTaskExecutor() {
44     synchronized (AsyncTaskExecutors.class) {
45       if (mInjectedAsyncTaskExecutorFactory != null) {
46         return mInjectedAsyncTaskExecutorFactory.createAsyncTaskExeuctor();
47       }
48       return new SimpleAsyncTaskExecutor(AsyncTask.SERIAL_EXECUTOR);
49     }
50   }
51 
52   /**
53    * Creates an AsyncTaskExecutor that submits tasks to run with {@link
54    * AsyncTask#THREAD_POOL_EXECUTOR}.
55    */
createThreadPoolExecutor()56   public static AsyncTaskExecutor createThreadPoolExecutor() {
57     synchronized (AsyncTaskExecutors.class) {
58       if (mInjectedAsyncTaskExecutorFactory != null) {
59         return mInjectedAsyncTaskExecutorFactory.createAsyncTaskExeuctor();
60       }
61       return new SimpleAsyncTaskExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
62     }
63   }
64 
setFactoryForTest(AsyncTaskExecutorFactory factory)65   public static void setFactoryForTest(AsyncTaskExecutorFactory factory) {
66     synchronized (AsyncTaskExecutors.class) {
67       mInjectedAsyncTaskExecutorFactory = factory;
68     }
69   }
70 
71   /** Interface for creating AsyncTaskExecutor objects. */
72   public interface AsyncTaskExecutorFactory {
73 
createAsyncTaskExeuctor()74     AsyncTaskExecutor createAsyncTaskExeuctor();
75   }
76 
77   static class SimpleAsyncTaskExecutor implements AsyncTaskExecutor {
78 
79     private final Executor mExecutor;
80 
SimpleAsyncTaskExecutor(Executor executor)81     public SimpleAsyncTaskExecutor(Executor executor) {
82       mExecutor = executor;
83     }
84 
85     @Override
86     @MainThread
submit(Object identifer, AsyncTask<T, ?, ?> task, T... params)87     public <T> AsyncTask<T, ?, ?> submit(Object identifer, AsyncTask<T, ?, ?> task, T... params) {
88       Assert.isMainThread();
89       return task.executeOnExecutor(mExecutor, params);
90     }
91   }
92 }
93