Search in sources :

Example 1 with FilteringCursorWrapper

use of dev.dworks.apps.anexplorer.cursor.FilteringCursorWrapper 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)

Example 2 with FilteringCursorWrapper

use of dev.dworks.apps.anexplorer.cursor.FilteringCursorWrapper in project AnExplorer by 1hakr.

the class DirectoryLoader method loadInBackground.

@Override
public final DirectoryResult loadInBackground() {
    synchronized (this) {
        if (isLoadInBackgroundCanceled()) {
            throw new OperationCanceledException();
        }
        mSignal = new CancellationSignal();
    }
    final ContentResolver resolver = getContext().getContentResolver();
    final String authority = mUri.getAuthority();
    final DirectoryResult result = new DirectoryResult();
    int userMode = State.MODE_UNKNOWN;
    int userSortOrder = State.SORT_ORDER_UNKNOWN;
    // Use default document when searching
    if (mType == DirectoryFragment.TYPE_SEARCH) {
        final Uri docUri = DocumentsContract.buildDocumentUri(mRoot.authority, mRoot.documentId);
        try {
            mDoc = DocumentInfo.fromUri(resolver, docUri);
        } catch (FileNotFoundException e) {
            Log.w(TAG, "Failed to query", e);
            result.exception = e;
            CrashReportingManager.logException(e);
            return result;
        }
    }
    // Pick up any custom modes requested by user
    Cursor cursor = null;
    try {
        final Uri stateUri = RecentsProvider.buildState(mRoot.authority, mRoot.rootId, mDoc.documentId);
        cursor = resolver.query(stateUri, null, null, null, null);
        if (cursor.moveToFirst()) {
            userMode = getCursorInt(cursor, StateColumns.MODE);
            userSortOrder = getCursorInt(cursor, StateColumns.SORT_ORDER);
        }
    } finally {
        IoUtils.closeQuietly(cursor);
    }
    if (userMode != State.MODE_UNKNOWN) {
        result.mode = userMode;
    } else {
        if ((mDoc.flags & Document.FLAG_DIR_PREFERS_GRID) != 0) {
            result.mode = State.MODE_GRID;
        } else {
            result.mode = State.MODE_LIST;
        }
    }
    if (userSortOrder != State.SORT_ORDER_UNKNOWN) {
        result.sortOrder = userSortOrder;
    } else {
        if ((mDoc.flags & Document.FLAG_DIR_PREFERS_LAST_MODIFIED) != 0) {
            result.sortOrder = State.SORT_ORDER_LAST_MODIFIED;
        } else {
            result.sortOrder = State.SORT_ORDER_DISPLAY_NAME;
        }
    }
    // Search always uses ranking from provider
    if (mType == DirectoryFragment.TYPE_SEARCH) {
    // result.sortOrder = State.SORT_ORDER_UNKNOWN;
    }
    Log.d(TAG, "userMode=" + userMode + ", userSortOrder=" + userSortOrder + " --> mode=" + result.mode + ", sortOrder=" + result.sortOrder);
    ContentProviderClient client = null;
    try {
        client = DocumentsApplication.acquireUnstableProviderOrThrow(resolver, authority);
        cursor = client.query(mUri, null, null, null, getQuerySortOrder(result.sortOrder));
        cursor.registerContentObserver(mObserver);
        cursor = new RootCursorWrapper(mUri.getAuthority(), mRoot.rootId, cursor, -1);
        if (mType == DirectoryFragment.TYPE_SEARCH) {
            cursor = new SortingCursorWrapper(cursor, result.sortOrder);
            // Filter directories out of search results, for now
            cursor = new FilteringCursorWrapper(cursor, null, SEARCH_REJECT_MIMES);
        } else {
            // Normal directories should have sorting applied
            cursor = new SortingCursorWrapper(cursor, result.sortOrder);
        }
        result.client = client;
        result.cursor = cursor;
    } catch (Exception e) {
        Log.w(TAG, "Failed to query", e);
        CrashReportingManager.logException(e);
        result.exception = e;
        ContentProviderClientCompat.releaseQuietly(client);
    } finally {
        synchronized (this) {
            mSignal = null;
        }
    }
    return result;
}
Also used : RootCursorWrapper(dev.dworks.apps.anexplorer.cursor.RootCursorWrapper) OperationCanceledException(android.os.OperationCanceledException) FileNotFoundException(java.io.FileNotFoundException) Cursor(android.database.Cursor) Uri(android.net.Uri) SortingCursorWrapper(dev.dworks.apps.anexplorer.cursor.SortingCursorWrapper) OperationCanceledException(android.os.OperationCanceledException) FileNotFoundException(java.io.FileNotFoundException) ContentResolver(android.content.ContentResolver) DirectoryResult(dev.dworks.apps.anexplorer.model.DirectoryResult) FilteringCursorWrapper(dev.dworks.apps.anexplorer.cursor.FilteringCursorWrapper) CancellationSignal(android.os.CancellationSignal) ContentProviderClient(android.content.ContentProviderClient)

Aggregations

Cursor (android.database.Cursor)2 FilteringCursorWrapper (dev.dworks.apps.anexplorer.cursor.FilteringCursorWrapper)2 SortingCursorWrapper (dev.dworks.apps.anexplorer.cursor.SortingCursorWrapper)2 DirectoryResult (dev.dworks.apps.anexplorer.model.DirectoryResult)2 ContentProviderClient (android.content.ContentProviderClient)1 ContentResolver (android.content.ContentResolver)1 MatrixCursor (android.database.MatrixCursor)1 MergeCursor (android.database.MergeCursor)1 Uri (android.net.Uri)1 Bundle (android.os.Bundle)1 CancellationSignal (android.os.CancellationSignal)1 OperationCanceledException (android.os.OperationCanceledException)1 RootCursorWrapper (dev.dworks.apps.anexplorer.cursor.RootCursorWrapper)1 RootInfo (dev.dworks.apps.anexplorer.model.RootInfo)1 FileNotFoundException (java.io.FileNotFoundException)1 ArrayList (java.util.ArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutionException (java.util.concurrent.ExecutionException)1