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

Commit 2806beb3 authored by Daichi Hirono's avatar Daichi Hirono
Browse files

Encapsulate stackTouched logic into State class.

BUG=26437613

Change-Id: I84a667cb3561d395daac4e63d9eaca589c8f1fea
parent 4d421043
Loading
Loading
Loading
Loading
+28 −45
Original line number Diff line number Diff line
@@ -198,10 +198,7 @@ public abstract class BaseActivity extends Activity {

    void onRootPicked(RootInfo root) {
        // Clear entire backstack and start in new root
        mState.stack.root = root;
        mState.stack.clear();
        mState.stackTouched = true;

        mState.onRootChanged(root);
        mSearchManager.update(root);

        // Recents is always in memory, so we just load it directly.
@@ -214,24 +211,6 @@ public abstract class BaseActivity extends Activity {
        }
    }

    void setRoot(RootInfo root) {
        // Clear entire backstack and start in new root
        mState.stack.root = root;
        mState.stack.clear();
        mState.stackTouched = false;

        mSearchManager.update(root);

        // Recents is always in memory, so we just load it directly.
        // Otherwise we delegate loading data from disk to a task
        // to ensure a responsive ui.
        if (mRoots.isRecentsRoot(root)) {
            onCurrentDirectoryChanged(ANIM_SIDE);
        } else {
            new PickRootTask(root, false).executeOnExecutor(getExecutorForCurrentDirectory());
        }
    }

    void expandMenus(Menu menu) {
        for (int i = 0; i < menu.size(); i++) {
            final MenuItem item = menu.getItem(i);
@@ -330,8 +309,7 @@ public abstract class BaseActivity extends Activity {

    void openContainerDocument(DocumentInfo doc) {
        checkArgument(doc.isContainer());
        mState.stack.push(doc);
        mState.stackTouched = true;
        mState.pushDocument(doc);
        onCurrentDirectoryChanged(ANIM_DOWN);
    }

@@ -475,7 +453,7 @@ public abstract class BaseActivity extends Activity {
            return;
        }

        if (!mState.stackTouched) {
        if (!mState.hasLocationChanged()) {
            super.onBackPressed();
            return;
        }
@@ -496,9 +474,7 @@ public abstract class BaseActivity extends Activity {
        try {
            // Update the restored stack to ensure we have freshest data
            stack.updateDocuments(getContentResolver());

            mState.stack = stack;
            mState.stackTouched = true;
            mState.setStack(stack);
            onCurrentDirectoryChanged(ANIM_SIDE);

        } catch (FileNotFoundException e) {
@@ -506,6 +482,17 @@ public abstract class BaseActivity extends Activity {
        }
    }

    private DocumentInfo getRootDocumentBlocking(RootInfo root) {
        try {
            final Uri uri = DocumentsContract.buildDocumentUri(
                    root.authority, root.documentId);
            return DocumentInfo.fromUri(getContentResolver(), uri);
        } catch (FileNotFoundException e) {
            Log.w(mTag, "Failed to find root", e);
            return null;
        }
    }

    final class PickRootTask extends AsyncTask<Void, Void, DocumentInfo> {
        private RootInfo mRoot;
        private boolean mTouched;
@@ -517,22 +504,13 @@ public abstract class BaseActivity extends Activity {

        @Override
        protected DocumentInfo doInBackground(Void... params) {
            try {
                final Uri uri = DocumentsContract.buildDocumentUri(
                        mRoot.authority, mRoot.documentId);
                return DocumentInfo.fromUri(getContentResolver(), uri);
            } catch (FileNotFoundException e) {
                Log.w(mTag, "Failed to find root", e);
                return null;
            }
            return getRootDocumentBlocking(mRoot);
        }

        @Override
        protected void onPostExecute(DocumentInfo result) {
            if (result != null) {
                mState.stack.push(result);
                mState.stackTouched = mTouched;
                onCurrentDirectoryChanged(ANIM_SIDE);
                openContainerDocument(result);
            }
        }
    }
@@ -619,9 +597,11 @@ public abstract class BaseActivity extends Activity {
    }

    final class HandleRootsChangedTask extends AsyncTask<RootInfo, Void, RootInfo> {
        DocumentInfo mHome;

        @Override
        protected RootInfo doInBackground(RootInfo... roots) {
            Preconditions.checkArgument(roots.length == 1);
            checkArgument(roots.length == 1);
            final RootInfo currentRoot = roots[0];
            final Collection<RootInfo> cachedRoots = mRoots.getRootsBlocking();
            RootInfo homeRoot = null;
@@ -635,13 +615,17 @@ public abstract class BaseActivity extends Activity {
                }
            }
            Preconditions.checkNotNull(homeRoot);
            mHome = getRootDocumentBlocking(homeRoot);
            return homeRoot;
        }

        @Override
        protected void onPostExecute(RootInfo result) {
            if (result != null) {
                setRoot(result);
        protected void onPostExecute(RootInfo homeRoot) {
            if (homeRoot != null && mHome != null) {
                // Clear entire backstack and start in new root
                mState.onRootChanged(homeRoot);
                mSearchManager.update(homeRoot);
                openContainerDocument(mHome);
            }
        }
    }
@@ -658,8 +642,7 @@ public abstract class BaseActivity extends Activity {
            }

            while (mState.stack.size() > position + 1) {
                mState.stackTouched = true;
                mState.stack.pop();
                mState.popDocument();
            }
            onCurrentDirectoryChanged(ANIM_UP);
        }
+1 −1
Original line number Diff line number Diff line
@@ -107,7 +107,7 @@ public class RecentsCreateFragment extends Fragment {
                mAdapter.update(data);

                // When launched into empty recents, show drawer
                if (mAdapter.isEmpty() && !state.stackTouched &&
                if (mAdapter.isEmpty() && !state.hasLocationChanged() &&
                        context instanceof DocumentsActivity) {
                    ((DocumentsActivity) context).setRootsDrawerOpen(true);
                }
+30 −3
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import android.util.SparseArray;
import com.android.documentsui.model.DocumentInfo;
import com.android.documentsui.model.DocumentStack;
import com.android.documentsui.model.DurableUtils;
import com.android.documentsui.model.RootInfo;

import java.util.ArrayList;
import java.util.HashMap;
@@ -49,7 +50,6 @@ public class State implements android.os.Parcelable {
    public boolean localOnly;
    public boolean forceAdvanced;
    public boolean showAdvanced;
    public boolean stackTouched;
    public boolean restored;
    public boolean directoryCopy;
    public boolean openableOnly;
@@ -87,6 +87,8 @@ public class State implements android.os.Parcelable {
    public static final int SORT_ORDER_LAST_MODIFIED = 2;
    public static final int SORT_ORDER_SIZE = 3;

    private boolean mStackTouched;

    public void initAcceptMimes(Intent intent) {
        if (intent.hasExtra(Intent.EXTRA_MIME_TYPES)) {
            acceptMimes = intent.getStringArrayExtra(Intent.EXTRA_MIME_TYPES);
@@ -96,6 +98,31 @@ public class State implements android.os.Parcelable {
        }
    }

    public void onRootChanged(RootInfo root) {
        stack.root = root;
        stack.clear();
        mStackTouched = true;
    }

    public void pushDocument(DocumentInfo info) {
        stack.add(info);
        mStackTouched = true;
    }

    public void popDocument() {
        stack.pop();
        mStackTouched = true;
    }

    public void setStack(DocumentStack stack) {
        this.stack = stack;
        mStackTouched = true;
    }

    public boolean hasLocationChanged() {
        return mStackTouched;
    }

    @Override
    public int describeContents() {
        return 0;
@@ -113,7 +140,6 @@ public class State implements android.os.Parcelable {
        out.writeInt(localOnly ? 1 : 0);
        out.writeInt(forceAdvanced ? 1 : 0);
        out.writeInt(showAdvanced ? 1 : 0);
        out.writeInt(stackTouched ? 1 : 0);
        out.writeInt(restored ? 1 : 0);
        DurableUtils.writeToParcel(out, stack);
        out.writeString(currentSearch);
@@ -121,6 +147,7 @@ public class State implements android.os.Parcelable {
        out.writeList(selectedDocumentsForCopy);
        out.writeList(excludedAuthorities);
        out.writeInt(openableOnly ? 1 : 0);
        out.writeInt(mStackTouched ? 1 : 0);
    }

    public static final Creator<State> CREATOR = new Creator<State>() {
@@ -137,7 +164,6 @@ public class State implements android.os.Parcelable {
            state.localOnly = in.readInt() != 0;
            state.forceAdvanced = in.readInt() != 0;
            state.showAdvanced = in.readInt() != 0;
            state.stackTouched = in.readInt() != 0;
            state.restored = in.readInt() != 0;
            DurableUtils.readFromParcel(in, state.stack);
            state.currentSearch = in.readString();
@@ -145,6 +171,7 @@ public class State implements android.os.Parcelable {
            in.readList(state.selectedDocumentsForCopy, null);
            in.readList(state.excludedAuthorities, null);
            state.openableOnly = in.readInt() != 0;
            state.mStackTouched = in.readInt() != 0;
            return state;
        }

+1 −1
Original line number Diff line number Diff line
@@ -411,7 +411,7 @@ public class DirectoryFragment extends Fragment implements DocumentsAdapter.Envi
                updateDisplayState();

                // When launched into empty recents, show drawer
                if (mType == TYPE_RECENT_OPEN && mModel.isEmpty() && !state.stackTouched &&
                if (mType == TYPE_RECENT_OPEN && mModel.isEmpty() && !state.hasLocationChanged() &&
                        context instanceof DocumentsActivity) {
                    ((DocumentsActivity) context).setRootsDrawerOpen(true);
                }