Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 753a3aed authored by Jeff Sharkey's avatar Jeff Sharkey
Browse files

Treat document thumbnails as preemptable.

When a more important request comes along, preempt all outstanding
thumbnail requests.

Bug: 11317901
Change-Id: I164fc8d804bb9c471e6da3f8127228043b3ca482
parent ad729b02
Loading
Loading
Loading
Loading
+8 −5
Original line number Diff line number Diff line
@@ -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();
        }
+50 −1
Original line number Diff line number Diff line
@@ -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);
    }