AsyncTaskCompat.java revision b14fc7c928307b6758688ed38590bf674c62a01b
1/* 2 * Copyright (C) 2014 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 17package android.support.v4.os; 18 19import android.os.AsyncTask; 20import android.os.Build; 21 22/** 23 * Helper for accessing features in {@link android.os.AsyncTask} 24 * introduced after API level 4 in a backwards compatible fashion. 25 */ 26public class AsyncTaskCompat { 27 28 /** 29 * Executes the task with the specified parameters, allowing multiple tasks to run in parallel 30 * on a pool of threads managed by {@link android.os.AsyncTask}. 31 * 32 * @param task The {@link android.os.AsyncTask} to execute. 33 * @param params The parameters of the task. 34 * @return the instance of AsyncTask. 35 */ 36 public static <Params, Progress, Result> AsyncTask<Params, Progress, Result> executeParallel( 37 AsyncTask<Params, Progress, Result> task, 38 Params... params) { 39 if (task == null) { 40 throw new IllegalArgumentException("task can not be null"); 41 } 42 43 if (Build.VERSION.SDK_INT >= 11) { 44 // From API 11 onwards, we need to manually select the THREAD_POOL_EXECUTOR 45 AsyncTaskCompatHoneycomb.executeParallel(task, params); 46 } else { 47 // Before API 11, all tasks were run in parallel 48 task.execute(params); 49 } 50 51 return task; 52 } 53 54} 55