summaryrefslogtreecommitdiffstats
path: root/packages/DocumentsUI/src/com
diff options
context:
space:
mode:
authorJeff Sharkey <jsharkey@android.com>2013-10-22 17:09:44 -0700
committerJeff Sharkey <jsharkey@android.com>2013-10-23 09:23:13 -0700
commitb3d42635aafacd80b1e1d257338ea6abb93d22c3 (patch)
tree3a5c2e146afbaa0cc8041d1ff267cc8936de83b6 /packages/DocumentsUI/src/com
parentacc642bce9e1ca1782c8f93131d944d4a2f92be0 (diff)
downloadframeworks_base-b3d42635aafacd80b1e1d257338ea6abb93d22c3.zip
frameworks_base-b3d42635aafacd80b1e1d257338ea6abb93d22c3.tar.gz
frameworks_base-b3d42635aafacd80b1e1d257338ea6abb93d22c3.tar.bz2
Treat document thumbnails as preemptable.
When a more important request comes along, preempt all outstanding thumbnail requests. Bug: 11317901 Change-Id: I164fc8d804bb9c471e6da3f8127228043b3ca482
Diffstat (limited to 'packages/DocumentsUI/src/com')
-rw-r--r--packages/DocumentsUI/src/com/android/documentsui/DirectoryFragment.java13
-rw-r--r--packages/DocumentsUI/src/com/android/documentsui/ProviderExecutor.java51
2 files changed, 58 insertions, 6 deletions
diff --git a/packages/DocumentsUI/src/com/android/documentsui/DirectoryFragment.java b/packages/DocumentsUI/src/com/android/documentsui/DirectoryFragment.java
index 1f3901c..fa8bc9d 100644
--- a/packages/DocumentsUI/src/com/android/documentsui/DirectoryFragment.java
+++ b/packages/DocumentsUI/src/com/android/documentsui/DirectoryFragment.java
@@ -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();
}
diff --git a/packages/DocumentsUI/src/com/android/documentsui/ProviderExecutor.java b/packages/DocumentsUI/src/com/android/documentsui/ProviderExecutor.java
index 2105cb4..f94aebd 100644
--- a/packages/DocumentsUI/src/com/android/documentsui/ProviderExecutor.java
+++ b/packages/DocumentsUI/src/com/android/documentsui/ProviderExecutor.java
@@ -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);
}