Merge "Treat document thumbnails as preemptable." into klp-dev
This commit is contained in:
@ -76,6 +76,7 @@ import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.android.documentsui.DocumentsActivity.State;
|
||||
import com.android.documentsui.ProviderExecutor.Preemptable;
|
||||
import com.android.documentsui.RecentsProvider.StateColumns;
|
||||
import com.android.documentsui.model.DocumentInfo;
|
||||
import com.android.documentsui.model.RootInfo;
|
||||
@ -528,7 +529,7 @@ public class DirectoryFragment extends Fragment {
|
||||
if (iconThumb != null) {
|
||||
final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
|
||||
if (oldTask != null) {
|
||||
oldTask.reallyCancel();
|
||||
oldTask.preempt();
|
||||
iconThumb.setTag(null);
|
||||
}
|
||||
}
|
||||
@ -794,7 +795,7 @@ public class DirectoryFragment extends Fragment {
|
||||
|
||||
final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) iconThumb.getTag();
|
||||
if (oldTask != null) {
|
||||
oldTask.reallyCancel();
|
||||
oldTask.preempt();
|
||||
iconThumb.setTag(null);
|
||||
}
|
||||
|
||||
@ -818,7 +819,7 @@ public class DirectoryFragment extends Fragment {
|
||||
final ThumbnailAsyncTask task = new ThumbnailAsyncTask(
|
||||
uri, iconMime, iconThumb, mThumbSize);
|
||||
iconThumb.setTag(task);
|
||||
task.executeOnExecutor(ProviderExecutor.forAuthority(docAuthority));
|
||||
ProviderExecutor.forAuthority(docAuthority).execute(task);
|
||||
}
|
||||
}
|
||||
|
||||
@ -988,7 +989,8 @@ public class DirectoryFragment extends Fragment {
|
||||
}
|
||||
}
|
||||
|
||||
private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap> {
|
||||
private static class ThumbnailAsyncTask extends AsyncTask<Uri, Void, Bitmap>
|
||||
implements Preemptable {
|
||||
private final Uri mUri;
|
||||
private final ImageView mIconMime;
|
||||
private final ImageView mIconThumb;
|
||||
@ -1004,7 +1006,8 @@ public class DirectoryFragment extends Fragment {
|
||||
mSignal = new CancellationSignal();
|
||||
}
|
||||
|
||||
public void reallyCancel() {
|
||||
@Override
|
||||
public void preempt() {
|
||||
cancel(false);
|
||||
mSignal.cancel();
|
||||
}
|
||||
|
@ -16,10 +16,15 @@
|
||||
|
||||
package com.android.documentsui;
|
||||
|
||||
import android.os.AsyncTask;
|
||||
|
||||
import com.android.internal.annotations.GuardedBy;
|
||||
import com.android.internal.util.Preconditions;
|
||||
import com.google.android.collect.Lists;
|
||||
import com.google.android.collect.Maps;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
@ -29,7 +34,7 @@ public class ProviderExecutor extends Thread implements Executor {
|
||||
@GuardedBy("sExecutors")
|
||||
private static HashMap<String, ProviderExecutor> sExecutors = Maps.newHashMap();
|
||||
|
||||
public static Executor forAuthority(String authority) {
|
||||
public static ProviderExecutor forAuthority(String authority) {
|
||||
synchronized (sExecutors) {
|
||||
ProviderExecutor executor = sExecutors.get(authority);
|
||||
if (executor == null) {
|
||||
@ -42,10 +47,54 @@ public class ProviderExecutor extends Thread implements Executor {
|
||||
}
|
||||
}
|
||||
|
||||
public interface Preemptable {
|
||||
void preempt();
|
||||
}
|
||||
|
||||
private final LinkedBlockingQueue<Runnable> mQueue = new LinkedBlockingQueue<Runnable>();
|
||||
|
||||
private final ArrayList<WeakReference<Preemptable>> mPreemptable = Lists.newArrayList();
|
||||
|
||||
private void preempt() {
|
||||
synchronized (mPreemptable) {
|
||||
int count = 0;
|
||||
for (WeakReference<Preemptable> ref : mPreemptable) {
|
||||
final Preemptable p = ref.get();
|
||||
if (p != null) {
|
||||
count++;
|
||||
p.preempt();
|
||||
}
|
||||
}
|
||||
mPreemptable.clear();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the given task. If given task is not {@link Preemptable}, it will
|
||||
* preempt all outstanding preemptable tasks.
|
||||
*/
|
||||
public <P> void execute(AsyncTask<P, ?, ?> task, P... params) {
|
||||
if (task instanceof Preemptable) {
|
||||
synchronized (mPreemptable) {
|
||||
mPreemptable.add(new WeakReference<Preemptable>((Preemptable) task));
|
||||
}
|
||||
task.executeOnExecutor(mNonPreemptingExecutor, params);
|
||||
} else {
|
||||
task.executeOnExecutor(this, params);
|
||||
}
|
||||
}
|
||||
|
||||
private Executor mNonPreemptingExecutor = new Executor() {
|
||||
@Override
|
||||
public void execute(Runnable command) {
|
||||
Preconditions.checkNotNull(command);
|
||||
mQueue.add(command);
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
public void execute(Runnable command) {
|
||||
preempt();
|
||||
Preconditions.checkNotNull(command);
|
||||
mQueue.add(command);
|
||||
}
|
||||
|
@ -33,6 +33,7 @@ import android.net.Uri;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.os.CancellationSignal;
|
||||
import android.os.CancellationSignal.OnCancelListener;
|
||||
import android.os.ParcelFileDescriptor;
|
||||
import android.os.SystemClock;
|
||||
import android.provider.DocumentsContract;
|
||||
@ -54,8 +55,9 @@ import java.lang.ref.WeakReference;
|
||||
public class TestDocumentsProvider extends DocumentsProvider {
|
||||
private static final String TAG = "TestDocuments";
|
||||
|
||||
private static final boolean LAG = false;
|
||||
|
||||
private static final boolean ROOTS_WEDGE = false;
|
||||
private static final boolean ROOTS_LAG = false;
|
||||
private static final boolean ROOTS_CRASH = false;
|
||||
private static final boolean ROOTS_REFRESH = false;
|
||||
|
||||
@ -105,8 +107,8 @@ public class TestDocumentsProvider extends DocumentsProvider {
|
||||
public Cursor queryRoots(String[] projection) throws FileNotFoundException {
|
||||
Log.d(TAG, "Someone asked for our roots!");
|
||||
|
||||
if (ROOTS_WEDGE) SystemClock.sleep(Integer.MAX_VALUE);
|
||||
if (ROOTS_LAG) SystemClock.sleep(3000);
|
||||
if (LAG) lagUntilCanceled(null);
|
||||
if (ROOTS_WEDGE) wedgeUntilCanceled(null);
|
||||
if (ROOTS_CRASH) System.exit(12);
|
||||
|
||||
if (ROOTS_REFRESH) {
|
||||
@ -137,6 +139,7 @@ public class TestDocumentsProvider extends DocumentsProvider {
|
||||
@Override
|
||||
public Cursor queryDocument(String documentId, String[] projection)
|
||||
throws FileNotFoundException {
|
||||
if (LAG) lagUntilCanceled(null);
|
||||
if (DOCUMENT_CRASH) System.exit(12);
|
||||
|
||||
final MatrixCursor result = new MatrixCursor(resolveDocumentProjection(projection));
|
||||
@ -209,6 +212,7 @@ public class TestDocumentsProvider extends DocumentsProvider {
|
||||
String parentDocumentId, String[] projection, String sortOrder)
|
||||
throws FileNotFoundException {
|
||||
|
||||
if (LAG) lagUntilCanceled(null);
|
||||
if (CHILD_WEDGE) SystemClock.sleep(Integer.MAX_VALUE);
|
||||
if (CHILD_CRASH) System.exit(12);
|
||||
|
||||
@ -228,7 +232,7 @@ public class TestDocumentsProvider extends DocumentsProvider {
|
||||
|
||||
if (THUMB_HUNDREDS) {
|
||||
for (int i = 0; i < 256; i++) {
|
||||
includeFile(result, "i maded u an picshure", Document.FLAG_SUPPORTS_THUMBNAIL);
|
||||
includeFile(result, "i maded u an picshure" + i, Document.FLAG_SUPPORTS_THUMBNAIL);
|
||||
}
|
||||
}
|
||||
|
||||
@ -278,7 +282,8 @@ public class TestDocumentsProvider extends DocumentsProvider {
|
||||
public Cursor queryRecentDocuments(String rootId, String[] projection)
|
||||
throws FileNotFoundException {
|
||||
|
||||
if (RECENT_WEDGE) SystemClock.sleep(Integer.MAX_VALUE);
|
||||
if (LAG) lagUntilCanceled(null);
|
||||
if (RECENT_WEDGE) wedgeUntilCanceled(null);
|
||||
|
||||
// Pretend to take a super long time to respond
|
||||
SystemClock.sleep(3000);
|
||||
@ -292,6 +297,7 @@ public class TestDocumentsProvider extends DocumentsProvider {
|
||||
@Override
|
||||
public ParcelFileDescriptor openDocument(String docId, String mode, CancellationSignal signal)
|
||||
throws FileNotFoundException {
|
||||
if (LAG) lagUntilCanceled(null);
|
||||
throw new FileNotFoundException();
|
||||
}
|
||||
|
||||
@ -299,6 +305,7 @@ public class TestDocumentsProvider extends DocumentsProvider {
|
||||
public AssetFileDescriptor openDocumentThumbnail(
|
||||
String docId, Point sizeHint, CancellationSignal signal) throws FileNotFoundException {
|
||||
|
||||
if (LAG) lagUntilCanceled(signal);
|
||||
if (THUMB_WEDGE) wedgeUntilCanceled(signal);
|
||||
if (THUMB_CRASH) System.exit(12);
|
||||
|
||||
@ -339,15 +346,34 @@ public class TestDocumentsProvider extends DocumentsProvider {
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void wedgeUntilCanceled(CancellationSignal signal) {
|
||||
if (signal != null) {
|
||||
while (true) {
|
||||
signal.throwIfCanceled();
|
||||
SystemClock.sleep(500);
|
||||
private static void lagUntilCanceled(CancellationSignal signal) {
|
||||
waitForCancelOrTimeout(signal, 1500);
|
||||
}
|
||||
} else {
|
||||
Log.w(TAG, "WEDGING WITHOUT A CANCELLATIONSIGNAL");
|
||||
SystemClock.sleep(Integer.MAX_VALUE);
|
||||
|
||||
private static void wedgeUntilCanceled(CancellationSignal signal) {
|
||||
waitForCancelOrTimeout(signal, Integer.MAX_VALUE);
|
||||
}
|
||||
|
||||
private static void waitForCancelOrTimeout(
|
||||
final CancellationSignal signal, long timeoutMillis) {
|
||||
if (signal != null) {
|
||||
final Thread blocked = Thread.currentThread();
|
||||
signal.setOnCancelListener(new OnCancelListener() {
|
||||
@Override
|
||||
public void onCancel() {
|
||||
blocked.interrupt();
|
||||
}
|
||||
});
|
||||
signal.throwIfCanceled();
|
||||
}
|
||||
|
||||
try {
|
||||
Thread.sleep(timeoutMillis);
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
|
||||
if (signal != null) {
|
||||
signal.throwIfCanceled();
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user