Search in sources :

Example 1 with DirectoryResult

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

the class DirectoryFragment method onActivityCreated.

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    final Context context = getActivity();
    final State state = getDisplayState(DirectoryFragment.this);
    root = getArguments().getParcelable(EXTRA_ROOT);
    doc = getArguments().getParcelable(EXTRA_DOC);
    if (null != root && root.isSecondaryStorage() && state.action == ACTION_BROWSE) {
        if (!doc.isWriteSupported()) {
            SAFManager.takeCardUriPermission(getActivity(), root, doc);
        }
    }
    isApp = root != null && root.isApp();
    isOperationSupported = root != null && (root.isRootedStorage() || root.isUsbStorage());
    mAdapter = new DocumentsAdapter();
    mType = getArguments().getInt(EXTRA_TYPE);
    mStateKey = buildStateKey(root, doc);
    if (mType == TYPE_RECENT_OPEN) {
        // Hide titles when showing recents for picking images/videos
        mHideGridTitles = MimePredicate.mimeMatches(MimePredicate.VISUAL_MIMES, state.acceptMimes);
    } else {
        mHideGridTitles = (doc != null) && doc.isGridTitlesHidden();
    }
    mSvelteRecents = Utils.isLowRamDevice(context) && (mType == TYPE_RECENT_OPEN);
    mCallbacks = new LoaderCallbacks<DirectoryResult>() {

        @Override
        public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) {
            final String query = getArguments().getString(EXTRA_QUERY);
            Uri contentsUri;
            switch(mType) {
                case TYPE_NORMAL:
                    contentsUri = DocumentsContract.buildChildDocumentsUri(doc.authority, doc.documentId);
                    if (state.action == ACTION_MANAGE) {
                        contentsUri = DocumentsContract.setManageMode(contentsUri);
                    }
                    return new DirectoryLoader(context, mType, root, doc, contentsUri, state.userSortOrder);
                case TYPE_SEARCH:
                    contentsUri = DocumentsContract.buildSearchDocumentsUri(root.authority, root.rootId, query);
                    if (state.action == ACTION_MANAGE) {
                        contentsUri = DocumentsContract.setManageMode(contentsUri);
                    }
                    return new DirectoryLoader(context, mType, root, doc, contentsUri, state.userSortOrder);
                case TYPE_RECENT_OPEN:
                    final RootsCache roots = DocumentsApplication.getRootsCache(context);
                    return new RecentLoader(context, roots, state);
                default:
                    throw new IllegalStateException("Unknown type " + mType);
            }
        }

        @Override
        public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) {
            if (!isAdded())
                return;
            mAdapter.swapResult(result);
            // TODO: if mode change was racing with us, don't overwrite it
            if (result.mode != MODE_UNKNOWN) {
                state.derivedMode = result.mode;
            }
            if (result.sortOrder != SORT_ORDER_UNKNOWN) {
                state.derivedSortOrder = result.sortOrder;
            }
            ((BaseActivity) context).onStateChanged();
            updateDisplayState();
            // When launched into empty recents, show drawer
            if (mType == TYPE_RECENT_OPEN && mAdapter.isEmpty() && !state.stackTouched) {
                ((BaseActivity) context).setRootsDrawerOpen(true);
            }
            if (isResumed()) {
                setListShown(true);
            } else {
                setListShownNoAnimation(true);
            }
            // Restore any previous instance state
            final SparseArray<Parcelable> container = state.dirState.remove(mStateKey);
            if (container != null && !getArguments().getBoolean(EXTRA_IGNORE_STATE, false)) {
                getView().restoreHierarchyState(container);
            } else if (mLastSortOrder != state.derivedSortOrder) {
                mListView.smoothScrollToPosition(0);
                mGridView.smoothScrollToPosition(0);
            }
            mLastSortOrder = state.derivedSortOrder;
            if (isTelevision()) {
                mCurrentView.requestFocus();
            }
        }

        @Override
        public void onLoaderReset(Loader<DirectoryResult> loader) {
            mAdapter.swapResult(null);
        }
    };
    setListAdapter(mAdapter);
    setListShown(false);
    // Kick off loader at least once
    getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
    updateDisplayState();
}
Also used : Context(android.content.Context) DirectoryLoader(dev.dworks.apps.anexplorer.loader.DirectoryLoader) Bundle(android.os.Bundle) DirectoryLoader(dev.dworks.apps.anexplorer.loader.DirectoryLoader) Loader(android.content.Loader) RecentLoader(dev.dworks.apps.anexplorer.loader.RecentLoader) DocumentInfo.getCursorString(dev.dworks.apps.anexplorer.model.DocumentInfo.getCursorString) Uri(android.net.Uri) Point(android.graphics.Point) SparseArray(android.util.SparseArray) RootsCache(dev.dworks.apps.anexplorer.misc.RootsCache) DirectoryResult(dev.dworks.apps.anexplorer.model.DirectoryResult) State(dev.dworks.apps.anexplorer.BaseActivity.State) RecentLoader(dev.dworks.apps.anexplorer.loader.RecentLoader)

Example 2 with DirectoryResult

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

the class RecentLoader method deliverResult.

@Override
public void deliverResult(DirectoryResult result) {
    if (isReset()) {
        IoUtils.closeQuietly(result);
        return;
    }
    DirectoryResult oldResult = mResult;
    mResult = result;
    if (isStarted()) {
        super.deliverResult(result);
    }
    if (oldResult != null && oldResult != result) {
        IoUtils.closeQuietly(oldResult);
    }
}
Also used : DirectoryResult(dev.dworks.apps.anexplorer.model.DirectoryResult)

Example 3 with DirectoryResult

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

the class HomeFragment method showRecents.

private void showRecents() {
    final RootInfo root = roots.getRecentsRoot();
    recents.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            openRoot(root);
        }
    });
    mRecentsAdapter = new RecentsAdapter(getActivity(), null);
    mRecentsAdapter.setOnItemClickListener(new RecentsAdapter.OnItemClickListener() {

        @Override
        public void onItemClick(RecentsAdapter.ViewHolder item, int position) {
            openDocument(item.mDocumentInfo);
        }
    });
    mRecentsRecycler.setAdapter(mRecentsAdapter);
    LinearSnapHelper helper = new LinearSnapHelper();
    helper.attachToRecyclerView(mRecentsRecycler);
    final BaseActivity.State state = getDisplayState(this);
    mCallbacks = new LoaderManager.LoaderCallbacks<DirectoryResult>() {

        @Override
        public Loader<DirectoryResult> onCreateLoader(int id, Bundle args) {
            final RootsCache roots = DocumentsApplication.getRootsCache(getActivity());
            return new RecentLoader(getActivity(), roots, state);
        }

        @Override
        public void onLoadFinished(Loader<DirectoryResult> loader, DirectoryResult result) {
            if (!isAdded())
                return;
            if (null == result.cursor || (null != result.cursor && result.cursor.getCount() == 0)) {
                recents_container.setVisibility(View.GONE);
            } else {
                // recents_container.setVisibility(View.VISIBLE);
                mRecentsAdapter.swapCursor(new LimitCursorWrapper(result.cursor, MAX_RECENT_COUNT));
            }
        }

        @Override
        public void onLoaderReset(Loader<DirectoryResult> loader) {
            mRecentsAdapter.swapCursor(null);
        }
    };
    getLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
}
Also used : Bundle(android.os.Bundle) LinearSnapHelper(android.support.v7.widget.LinearSnapHelper) RecentLoader(dev.dworks.apps.anexplorer.loader.RecentLoader) Loader(android.content.Loader) LimitCursorWrapper(dev.dworks.apps.anexplorer.cursor.LimitCursorWrapper) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) RecentsAdapter(dev.dworks.apps.anexplorer.adapter.RecentsAdapter) LoaderManager(android.app.LoaderManager) RootInfo(dev.dworks.apps.anexplorer.model.RootInfo) RootsCache(dev.dworks.apps.anexplorer.misc.RootsCache) DirectoryResult(dev.dworks.apps.anexplorer.model.DirectoryResult) BaseActivity(dev.dworks.apps.anexplorer.BaseActivity) RecentLoader(dev.dworks.apps.anexplorer.loader.RecentLoader)

Example 4 with DirectoryResult

use of dev.dworks.apps.anexplorer.model.DirectoryResult 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 5 with DirectoryResult

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

the class DirectoryLoader method deliverResult.

@Override
public void deliverResult(DirectoryResult result) {
    if (isReset()) {
        IoUtils.closeQuietly(result);
        return;
    }
    DirectoryResult oldResult = mResult;
    mResult = result;
    if (isStarted()) {
        super.deliverResult(result);
    }
    if (oldResult != null && oldResult != result) {
        IoUtils.closeQuietly(oldResult);
    }
}
Also used : DirectoryResult(dev.dworks.apps.anexplorer.model.DirectoryResult)

Aggregations

DirectoryResult (dev.dworks.apps.anexplorer.model.DirectoryResult)6 Bundle (android.os.Bundle)3 Loader (android.content.Loader)2 Cursor (android.database.Cursor)2 Uri (android.net.Uri)2 FilteringCursorWrapper (dev.dworks.apps.anexplorer.cursor.FilteringCursorWrapper)2 SortingCursorWrapper (dev.dworks.apps.anexplorer.cursor.SortingCursorWrapper)2 RecentLoader (dev.dworks.apps.anexplorer.loader.RecentLoader)2 RootsCache (dev.dworks.apps.anexplorer.misc.RootsCache)2 RootInfo (dev.dworks.apps.anexplorer.model.RootInfo)2 LoaderManager (android.app.LoaderManager)1 ContentProviderClient (android.content.ContentProviderClient)1 ContentResolver (android.content.ContentResolver)1 Context (android.content.Context)1 MatrixCursor (android.database.MatrixCursor)1 MergeCursor (android.database.MergeCursor)1 Point (android.graphics.Point)1 CancellationSignal (android.os.CancellationSignal)1 OperationCanceledException (android.os.OperationCanceledException)1 LinearSnapHelper (android.support.v7.widget.LinearSnapHelper)1