Search in sources :

Example 26 with RootInfo

use of dev.dworks.apps.anexplorer.model.RootInfo in project AnExplorer by 1hakr.

the class RecentLoader method loadInBackground.

@Override
public DirectoryResult loadInBackground() {
    if (mFirstPassLatch == null) {
        // First time through we kick off all the recent tasks, and wait
        // around to see if everyone finishes quickly.
        final Collection<RootInfo> roots = mRoots.getMatchingRootsBlocking(mState);
        for (RootInfo root : roots) {
            if ((root.flags & Root.FLAG_SUPPORTS_RECENTS) != 0) {
                final RecentTask task = new RecentTask(new Runnable() {

                    @Override
                    public void run() {
                    }
                }, root.authority, root.rootId);
                mTasks.put(root, task);
            }
        }
        mFirstPassLatch = new CountDownLatch(mTasks.size());
        for (RecentTask task : mTasks.values()) {
            ProviderExecutor.forAuthority(task.authority).execute(task);
        }
        try {
            mFirstPassLatch.await(MAX_FIRST_PASS_WAIT_MILLIS, TimeUnit.MILLISECONDS);
            mFirstPassDone = true;
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
    final long rejectBefore = System.currentTimeMillis() - REJECT_OLDER_THAN;
    // Collect all finished tasks
    boolean allDone = true;
    List<Cursor> cursors = new ArrayList<>();
    for (RecentTask task : mTasks.values()) {
        if (task.isDone()) {
            try {
                final Cursor cursor = task.get();
                if (cursor == null)
                    continue;
                final FilteringCursorWrapper filtered = new FilteringCursorWrapper(cursor, mState.acceptMimes, RECENT_REJECT_MIMES, rejectBefore) {

                    @Override
                    public void close() {
                    // Ignored, since we manage cursor lifecycle internally
                    }
                };
                cursors.add(filtered);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } catch (ExecutionException e) {
            // We already logged on other side
            }
        } else {
            allDone = false;
        }
    }
    if (LOGD) {
        Log.d(TAG, "Found " + cursors.size() + " of " + mTasks.size() + " recent queries done");
    }
    final DirectoryResult result = new DirectoryResult();
    result.sortOrder = SORT_ORDER_LAST_MODIFIED;
    // Hint to UI if we're still loading
    final Bundle extras = new Bundle();
    if (!allDone) {
        extras.putBoolean(DocumentsContract.EXTRA_LOADING, true);
    }
    final Cursor merged;
    if (cursors.size() > 0) {
        merged = new MergeCursor(cursors.toArray(new Cursor[cursors.size()]));
    } else {
        // Return something when nobody is ready
        merged = new MatrixCursor(new String[0]);
    }
    final SortingCursorWrapper sorted = new SortingCursorWrapper(merged, result.sortOrder) {

        @Override
        public Bundle getExtras() {
            return extras;
        }
    };
    result.cursor = sorted;
    return result;
}
Also used : Bundle(android.os.Bundle) ArrayList(java.util.ArrayList) MergeCursor(android.database.MergeCursor) CountDownLatch(java.util.concurrent.CountDownLatch) Cursor(android.database.Cursor) MergeCursor(android.database.MergeCursor) MatrixCursor(android.database.MatrixCursor) SortingCursorWrapper(dev.dworks.apps.anexplorer.cursor.SortingCursorWrapper) MatrixCursor(android.database.MatrixCursor) RootInfo(dev.dworks.apps.anexplorer.model.RootInfo) DirectoryResult(dev.dworks.apps.anexplorer.model.DirectoryResult) FilteringCursorWrapper(dev.dworks.apps.anexplorer.cursor.FilteringCursorWrapper) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

RootInfo (dev.dworks.apps.anexplorer.model.RootInfo)26 Bundle (android.os.Bundle)5 View (android.view.View)5 TextView (android.widget.TextView)5 DocumentInfo (dev.dworks.apps.anexplorer.model.DocumentInfo)5 ArrayList (java.util.ArrayList)5 FragmentManager (android.app.FragmentManager)4 Uri (android.net.Uri)4 RecyclerView (android.support.v7.widget.RecyclerView)4 Intent (android.content.Intent)3 BaseActivity (dev.dworks.apps.anexplorer.BaseActivity)3 Timer (java.util.Timer)3 TargetApi (android.annotation.TargetApi)2 Context (android.content.Context)2 Loader (android.content.Loader)2 Cursor (android.database.Cursor)2 MenuItem (android.view.MenuItem)2 State (dev.dworks.apps.anexplorer.BaseActivity.State)2 RootsFragment (dev.dworks.apps.anexplorer.fragment.RootsFragment)2 SaveFragment (dev.dworks.apps.anexplorer.fragment.SaveFragment)2