Commit 05ddc100 authored by Sam Maier's avatar Sam Maier Committed by Commit Bot

Android: Remove all Params usages in AsyncTask

This is step 1 in an API shrinking effort for AsyncTask. There is no reason for Params to exist instead of just using constructor arguments, so we are changing the few usages over. Step 2 will be the removal of the Params argument.

Bug: 843745
Change-Id: I36a03dd91b7af10763587fb5048e9f8d775f203e
Reviewed-on: https://chromium-review.googlesource.com/1136730Reviewed-by: default avatarYaron Friedman <yfriedman@chromium.org>
Reviewed-by: default avatarPeter Beverloo <peter@chromium.org>
Reviewed-by: default avataragrieve <agrieve@chromium.org>
Commit-Queue: Sam Maier <smaier@chromium.org>
Cr-Commit-Position: refs/heads/master@{#577550}
parent 6c913f9d
...@@ -217,9 +217,9 @@ public final class WarmupManager { ...@@ -217,9 +217,9 @@ public final class WarmupManager {
*/ */
private void prefetchDnsForUrlInBackground(final String url) { private void prefetchDnsForUrlInBackground(final String url) {
mDnsRequestsInFlight.add(url); mDnsRequestsInFlight.add(url);
new AsyncTask<String, Void, Void>() { new AsyncTask<Void, Void, Void>() {
@Override @Override
protected Void doInBackground(String... params) { protected Void doInBackground(Void... params) {
try (TraceEvent e = try (TraceEvent e =
TraceEvent.scoped("WarmupManager.prefetchDnsForUrlInBackground")) { TraceEvent.scoped("WarmupManager.prefetchDnsForUrlInBackground")) {
InetAddress.getByName(new URL(url).getHost()); InetAddress.getByName(new URL(url).getHost());
...@@ -242,7 +242,8 @@ public final class WarmupManager { ...@@ -242,7 +242,8 @@ public final class WarmupManager {
maybePreconnectUrlAndSubResources(profile, url); maybePreconnectUrlAndSubResources(profile, url);
} }
} }
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, url); }
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} }
/** Launches a background DNS query for a given URL if the data reduction proxy is not in use. /** Launches a background DNS query for a given URL if the data reduction proxy is not in use.
......
...@@ -23,7 +23,7 @@ import java.util.concurrent.ExecutionException; ...@@ -23,7 +23,7 @@ import java.util.concurrent.ExecutionException;
public abstract class AsyncFeedbackSourceAdapter<Result> implements AsyncFeedbackSource { public abstract class AsyncFeedbackSourceAdapter<Result> implements AsyncFeedbackSource {
private Worker mWorker; private Worker mWorker;
private class Worker extends AsyncTask<Context, Void, Result> { private class Worker extends AsyncTask<Void, Void, Result> {
private final Runnable mCallback; private final Runnable mCallback;
public Worker(Runnable callback) { public Worker(Runnable callback) {
...@@ -32,14 +32,13 @@ public abstract class AsyncFeedbackSourceAdapter<Result> implements AsyncFeedbac ...@@ -32,14 +32,13 @@ public abstract class AsyncFeedbackSourceAdapter<Result> implements AsyncFeedbac
// AsyncTask implementation. // AsyncTask implementation.
@Override @Override
protected Result doInBackground(Context... params) { protected Result doInBackground(Void... params) {
return AsyncFeedbackSourceAdapter.this.doInBackground(params[0]); return AsyncFeedbackSourceAdapter.this.doInBackground(
ContextUtils.getApplicationContext());
} }
@Override @Override
protected void onPostExecute(Result result) { protected void onPostExecute(Result result) {
super.onPostExecute(result);
mCallback.run(); mCallback.run();
} }
} }
...@@ -75,7 +74,6 @@ public abstract class AsyncFeedbackSourceAdapter<Result> implements AsyncFeedbac ...@@ -75,7 +74,6 @@ public abstract class AsyncFeedbackSourceAdapter<Result> implements AsyncFeedbac
public final void start(Runnable callback) { public final void start(Runnable callback) {
if (mWorker != null) return; if (mWorker != null) return;
mWorker = new Worker(callback); mWorker = new Worker(callback);
mWorker.executeOnExecutor( mWorker.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
AsyncTask.THREAD_POOL_EXECUTOR, ContextUtils.getApplicationContext());
} }
} }
\ No newline at end of file
...@@ -66,12 +66,18 @@ public class OfflinePageEvaluationBridge { ...@@ -66,12 +66,18 @@ public class OfflinePageEvaluationBridge {
* Class used for writing logs to external log file asynchronously to prevent violating strict * Class used for writing logs to external log file asynchronously to prevent violating strict
* mode during test. * mode during test.
*/ */
private class LogTask extends AsyncTask<String, Void, Void> { private class LogTask extends AsyncTask<Void, Void, Void> {
final String mLogString;
LogTask(String logString) {
mLogString = logString;
}
@Override @Override
protected Void doInBackground(String... strings) { protected Void doInBackground(Void... params) {
try { try {
synchronized (mLogOutput) { synchronized (mLogOutput) {
mLogOutput.write(strings[0]); mLogOutput.write(mLogString);
mLogOutput.flush(); mLogOutput.flush();
} }
} catch (IOException e) { } catch (IOException e) {
...@@ -198,9 +204,9 @@ public class OfflinePageEvaluationBridge { ...@@ -198,9 +204,9 @@ public class OfflinePageEvaluationBridge {
new SimpleDateFormat("MM-dd HH:mm:ss.SSS", Locale.getDefault()); new SimpleDateFormat("MM-dd HH:mm:ss.SSS", Locale.getDefault());
String logString = formatter.format(date) + ": " + sourceTag + " | " + message String logString = formatter.format(date) + ": " + sourceTag + " | " + message
+ System.getProperty("line.separator"); + System.getProperty("line.separator");
LogTask logTask = new LogTask(); LogTask logTask = new LogTask(logString);
Log.d(TAG, logString); Log.d(TAG, logString);
logTask.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, logString); logTask.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
} }
public void closeLog() { public void closeLog() {
......
...@@ -17,18 +17,21 @@ import java.util.concurrent.TimeUnit; ...@@ -17,18 +17,21 @@ import java.util.concurrent.TimeUnit;
/** /**
* A worker task to scale bitmaps in the background. * A worker task to scale bitmaps in the background.
*/ */
class BitmapScalerTask extends AsyncTask<Bitmap, Void, Bitmap> { class BitmapScalerTask extends AsyncTask<Void, Void, Bitmap> {
private final LruCache<String, Bitmap> mCache; private final LruCache<String, Bitmap> mCache;
private final String mFilePath; private final String mFilePath;
private final int mSize; private final int mSize;
private final Bitmap mBitmap;
/** /**
* A BitmapScalerTask constructor. * A BitmapScalerTask constructor.
*/ */
public BitmapScalerTask(LruCache<String, Bitmap> cache, String filePath, int size) { public BitmapScalerTask(
LruCache<String, Bitmap> cache, String filePath, int size, Bitmap bitmap) {
mCache = cache; mCache = cache;
mFilePath = filePath; mFilePath = filePath;
mSize = size; mSize = size;
mBitmap = bitmap;
} }
/** /**
...@@ -37,13 +40,13 @@ class BitmapScalerTask extends AsyncTask<Bitmap, Void, Bitmap> { ...@@ -37,13 +40,13 @@ class BitmapScalerTask extends AsyncTask<Bitmap, Void, Bitmap> {
* @return A sorted list of images (by last-modified first). * @return A sorted list of images (by last-modified first).
*/ */
@Override @Override
protected Bitmap doInBackground(Bitmap... bitmaps) { protected Bitmap doInBackground(Void... params) {
assert !ThreadUtils.runningOnUiThread(); assert !ThreadUtils.runningOnUiThread();
if (isCancelled()) return null; if (isCancelled()) return null;
long begin = SystemClock.elapsedRealtime(); long begin = SystemClock.elapsedRealtime();
Bitmap bitmap = BitmapUtils.scale(bitmaps[0], mSize, false); Bitmap bitmap = BitmapUtils.scale(mBitmap, mSize, false);
long scaleTime = SystemClock.elapsedRealtime() - begin; long scaleTime = SystemClock.elapsedRealtime() - begin;
RecordHistogram.recordTimesHistogram( RecordHistogram.recordTimesHistogram(
"Android.PhotoPicker.BitmapScalerTask", scaleTime, TimeUnit.MILLISECONDS); "Android.PhotoPicker.BitmapScalerTask", scaleTime, TimeUnit.MILLISECONDS);
......
...@@ -55,8 +55,9 @@ public class PickerBitmapViewHolder ...@@ -55,8 +55,9 @@ public class PickerBitmapViewHolder
if (mCategoryView.getLowResBitmaps().get(filePath) == null) { if (mCategoryView.getLowResBitmaps().get(filePath) == null) {
Resources resources = mItemView.getContext().getResources(); Resources resources = mItemView.getContext().getResources();
new BitmapScalerTask(mCategoryView.getLowResBitmaps(), filePath, new BitmapScalerTask(mCategoryView.getLowResBitmaps(), filePath,
resources.getDimensionPixelSize(R.dimen.photo_picker_grainy_thumbnail_size)) resources.getDimensionPixelSize(R.dimen.photo_picker_grainy_thumbnail_size),
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, bitmap); bitmap)
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} }
if (!TextUtils.equals(mBitmapDetails.getFilePath(), filePath)) { if (!TextUtils.equals(mBitmapDetails.getFilePath(), filePath)) {
......
...@@ -390,17 +390,18 @@ public class SelectFileDialog ...@@ -390,17 +390,18 @@ public class SelectFileDialog
if (photos.length == 1) { if (photos.length == 1) {
GetDisplayNameTask task = GetDisplayNameTask task =
new GetDisplayNameTask(ContextUtils.getApplicationContext(), false); new GetDisplayNameTask(ContextUtils.getApplicationContext(), false,
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, Uri.parse(photos[0])); new Uri[] {Uri.parse(photos[0])});
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
return; return;
} else { } else {
Uri[] filePathArray = new Uri[photos.length]; Uri[] filePathArray = new Uri[photos.length];
for (int i = 0; i < photos.length; ++i) { for (int i = 0; i < photos.length; ++i) {
filePathArray[i] = Uri.parse(photos[i]); filePathArray[i] = Uri.parse(photos[i]);
} }
GetDisplayNameTask task = GetDisplayNameTask task = new GetDisplayNameTask(
new GetDisplayNameTask(ContextUtils.getApplicationContext(), true); ContextUtils.getApplicationContext(), true, filePathArray);
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, filePathArray); task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} }
break; break;
...@@ -565,9 +566,9 @@ public class SelectFileDialog ...@@ -565,9 +566,9 @@ public class SelectFileDialog
for (int i = 0; i < itemCount; ++i) { for (int i = 0; i < itemCount; ++i) {
filePathArray[i] = clipData.getItemAt(i).getUri(); filePathArray[i] = clipData.getItemAt(i).getUri();
} }
GetDisplayNameTask task = GetDisplayNameTask task = new GetDisplayNameTask(
new GetDisplayNameTask(ContextUtils.getApplicationContext(), true); ContextUtils.getApplicationContext(), true, filePathArray);
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, filePathArray); task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
return; return;
} }
...@@ -577,9 +578,9 @@ public class SelectFileDialog ...@@ -577,9 +578,9 @@ public class SelectFileDialog
} }
if (ContentResolver.SCHEME_CONTENT.equals(results.getScheme())) { if (ContentResolver.SCHEME_CONTENT.equals(results.getScheme())) {
GetDisplayNameTask task = GetDisplayNameTask task = new GetDisplayNameTask(
new GetDisplayNameTask(ContextUtils.getApplicationContext(), false); ContextUtils.getApplicationContext(), false, new Uri[] {results.getData()});
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, results.getData()); task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
return; return;
} }
...@@ -699,32 +700,34 @@ public class SelectFileDialog ...@@ -699,32 +700,34 @@ public class SelectFileDialog
return count; return count;
} }
class GetDisplayNameTask extends AsyncTask<Uri, Void, String[]> { class GetDisplayNameTask extends AsyncTask<Void, Void, String[]> {
String[] mFilePaths; String[] mFilePaths;
final Context mContext; final Context mContext;
final boolean mIsMultiple; final boolean mIsMultiple;
final Uri[] mUris;
public GetDisplayNameTask(Context context, boolean isMultiple) { public GetDisplayNameTask(Context context, boolean isMultiple, Uri[] uris) {
mContext = context; mContext = context;
mIsMultiple = isMultiple; mIsMultiple = isMultiple;
mUris = uris;
} }
@Override @Override
public String[] doInBackground(Uri...uris) { public String[] doInBackground(Void... params) {
mFilePaths = new String[uris.length]; mFilePaths = new String[mUris.length];
String[] displayNames = new String[uris.length]; String[] displayNames = new String[mUris.length];
try { try {
for (int i = 0; i < uris.length; i++) { for (int i = 0; i < mUris.length; i++) {
// The selected files must be returned as a list of absolute paths. A MIUI 8.5 // The selected files must be returned as a list of absolute paths. A MIUI 8.5
// device was observed to return a file:// URI instead, so convert if necessary. // device was observed to return a file:// URI instead, so convert if necessary.
// See https://crbug.com/752834 for context. // See https://crbug.com/752834 for context.
if (ContentResolver.SCHEME_FILE.equals(uris[i].getScheme())) { if (ContentResolver.SCHEME_FILE.equals(mUris[i].getScheme())) {
mFilePaths[i] = uris[i].getSchemeSpecificPart(); mFilePaths[i] = mUris[i].getSchemeSpecificPart();
} else { } else {
mFilePaths[i] = uris[i].toString(); mFilePaths[i] = mUris[i].toString();
} }
displayNames[i] = ContentUriUtils.getDisplayName( displayNames[i] = ContentUriUtils.getDisplayName(
uris[i], mContext, MediaStore.MediaColumns.DISPLAY_NAME); mUris[i], mContext, MediaStore.MediaColumns.DISPLAY_NAME);
} }
} catch (SecurityException e) { } catch (SecurityException e) {
// Some third party apps will present themselves as being able // Some third party apps will present themselves as being able
......
...@@ -133,12 +133,12 @@ public class SelectFileDialogTest { ...@@ -133,12 +133,12 @@ public class SelectFileDialogTest {
@Test @Test
public void testMultipleFileSelectorWithFileUris() throws Throwable { public void testMultipleFileSelectorWithFileUris() throws Throwable {
SelectFileDialog selectFileDialog = new SelectFileDialog(0); SelectFileDialog selectFileDialog = new SelectFileDialog(0);
SelectFileDialog.GetDisplayNameTask task =
selectFileDialog.new GetDisplayNameTask(ContextUtils.getApplicationContext(), true);
Uri[] filePathArray = new Uri[] { Uri[] filePathArray = new Uri[] {
Uri.parse("file:///storage/emulated/0/DCIM/Camera/IMG_0.jpg"), Uri.parse("file:///storage/emulated/0/DCIM/Camera/IMG_0.jpg"),
Uri.parse("file:///storage/emulated/0/DCIM/Camera/IMG_1.jpg")}; Uri.parse("file:///storage/emulated/0/DCIM/Camera/IMG_1.jpg")};
task.doInBackground(filePathArray); SelectFileDialog.GetDisplayNameTask task = selectFileDialog.new GetDisplayNameTask(
ContextUtils.getApplicationContext(), true, filePathArray);
task.doInBackground();
assertEquals(task.mFilePaths[0].toString(), assertEquals(task.mFilePaths[0].toString(),
"///storage/emulated/0/DCIM/Camera/IMG_0.jpg"); "///storage/emulated/0/DCIM/Camera/IMG_0.jpg");
assertEquals(task.mFilePaths[1].toString(), assertEquals(task.mFilePaths[1].toString(),
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment