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

Commit bab25161 authored by Garfield Tan's avatar Garfield Tan
Browse files

Initialize the location to recents if we fail to restore stack.

Bug: 36083419
Change-Id: I8da79eb9dd50d69c828472aae725e286d5e1e8a0
parent 0bfffdef
Loading
Loading
Loading
Loading
+0 −6
Original line number Diff line number Diff line
@@ -80,10 +80,6 @@ public class State implements android.os.Parcelable {
    public boolean localOnly;
    public boolean showDeviceStorageOption;
    public boolean showAdvanced;
    /*
     * Indicates handler was an external app, like photos.
     */
    public boolean external;

    // Indicates that a copy operation (or move) includes a directory.
    // Why? Directory creation isn't supported by some roots (like Downloads).
@@ -130,7 +126,6 @@ public class State implements android.os.Parcelable {
        out.writeInt(localOnly ? 1 : 0);
        out.writeInt(showDeviceStorageOption ? 1 : 0);
        out.writeInt(showAdvanced ? 1 : 0);
        out.writeInt(external ? 1 : 0);
        DurableUtils.writeToParcel(out, stack);
        out.writeMap(dirConfigs);
        out.writeList(excludedAuthorities);
@@ -153,7 +148,6 @@ public class State implements android.os.Parcelable {
            state.localOnly = in.readInt() != 0;
            state.showDeviceStorageOption = in.readInt() != 0;
            state.showAdvanced = in.readInt() != 0;
            state.external = in.readInt() != 0;
            DurableUtils.readFromParcel(in, state.stack);
            in.readMap(state.dirConfigs, loader);
            in.readList(state.excludedAuthorities, loader);
+15 −1
Original line number Diff line number Diff line
@@ -38,11 +38,13 @@ import com.android.documentsui.base.Lookup;
import com.android.documentsui.base.RootInfo;
import com.android.documentsui.base.Shared;
import com.android.documentsui.base.State;
import com.android.documentsui.dirlist.AnimationView;
import com.android.documentsui.dirlist.DocumentDetails;
import com.android.documentsui.Model;
import com.android.documentsui.picker.ActionHandler.Addons;
import com.android.documentsui.queries.SearchViewManager;
import com.android.documentsui.roots.RootsAccess;
import com.android.internal.annotations.VisibleForTesting;

import java.util.concurrent.Executor;

@@ -134,7 +136,19 @@ class ActionHandler<T extends Activity & Addons> extends AbstractActionHandler<T

    private void loadLastAccessedStack() {
        if (DEBUG) Log.d(TAG, "Attempting to load last used stack for calling package.");
        new LoadLastAccessedStackTask<>(mActivity, mState, mRoots).execute();
        new LoadLastAccessedStackTask<>(mActivity, mState, mRoots, this::onLoadedLastAccessedStack)
                .execute();
    }

    @VisibleForTesting
    void onLoadedLastAccessedStack(@Nullable DocumentStack stack) {
        if (stack == null) {
            mState.stack.changeRoot(mRoots.getRecentsRoot());
        } else {
            mState.stack.reset(stack);
        }

        mActivity.refreshCurrentRootAndDirectory(AnimationView.ANIM_NONE);
    }

    @Override
+0 −1
Original line number Diff line number Diff line
@@ -50,7 +50,6 @@ import java.util.Set;
public class LastAccessedProvider extends ContentProvider {
    private static final String TAG = "LastAccessedProvider";


    private static final String AUTHORITY = "com.android.documentsui.lastAccessed";

    private static final UriMatcher sMatcher = new UriMatcher(UriMatcher.NO_MATCH);
+28 −26
Original line number Diff line number Diff line
@@ -24,12 +24,12 @@ import android.net.Uri;
import android.util.Log;

import com.android.documentsui.AbstractActionHandler.CommonAddons;
import com.android.documentsui.base.DocumentStack;
import com.android.documentsui.base.DurableUtils;
import com.android.documentsui.base.PairedTask;
import com.android.documentsui.base.RootInfo;
import com.android.documentsui.base.Shared;
import com.android.documentsui.base.State;
import com.android.documentsui.dirlist.AnimationView;
import com.android.documentsui.picker.LastAccessedProvider.Columns;
import com.android.documentsui.roots.RootsAccess;

@@ -38,6 +38,9 @@ import libcore.io.IoUtils;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Collection;
import java.util.function.Consumer;

import javax.annotation.Nullable;

/**
 * Loads the last used path (stack) from Recents (history).
@@ -46,54 +49,54 @@ import java.util.Collection;
 * for an app like DropBox.
 */
final class LoadLastAccessedStackTask<T extends Activity & CommonAddons>
        extends PairedTask<T, Void, Void> {
        extends PairedTask<T, Void, DocumentStack> {

    private static final String TAG = "LoadLastAccessedStackTa";

    private static final String TAG = "LoadLastAccessedStackTask";
    private volatile boolean mRestoredStack;
    private volatile boolean mExternal;
    private final State mState;
    private RootsAccess mRoots;
    private final RootsAccess mRoots;
    private final Consumer<DocumentStack> mCallback;

    public LoadLastAccessedStackTask(T activity, State state, RootsAccess roots) {
    LoadLastAccessedStackTask(
            T activity, State state, RootsAccess roots, Consumer<DocumentStack> callback) {
        super(activity);
        mState = state;
        mRoots = roots;
        mState = state;
        mCallback = callback;
    }

    @Override
    protected Void run(Void... params) {
        if (DEBUG && !mState.stack.isEmpty()) {
            Log.w(TAG, "Overwriting existing stack.");
        }
    protected DocumentStack run(Void... params) {
        DocumentStack stack = null;

        String callingPackage = Shared.getCallingPackageName(mOwner);
        Uri resumeUri = LastAccessedProvider.buildLastAccessed(
                callingPackage);
        Cursor cursor = mOwner.getContentResolver().query(resumeUri, null, null, null, null);
        try {
            if (cursor.moveToFirst()) {
                mExternal = cursor.getInt(cursor.getColumnIndex(Columns.EXTERNAL)) != 0;
                stack = new DocumentStack();
                final byte[] rawStack = cursor.getBlob(
                        cursor.getColumnIndex(Columns.STACK));
                DurableUtils.readFromArray(rawStack, mState.stack);
                mRestoredStack = true;
                DurableUtils.readFromArray(rawStack, stack);
            }
        } catch (IOException e) {
            Log.w(TAG, "Failed to resume: " + e);
            Log.w(TAG, "Failed to resume: ", e);
        } finally {
            IoUtils.closeQuietly(cursor);
        }

        if (mRestoredStack) {
        if (stack != null) {
            // Update the restored stack to ensure we have freshest data
            final Collection<RootInfo> matchingRoots = mRoots.getMatchingRootsBlocking(mState);
            try {
                mState.stack.updateRoot(matchingRoots);
                mState.stack.updateDocuments(mOwner.getContentResolver());

                stack.updateRoot(matchingRoots);
                stack.updateDocuments(mOwner.getContentResolver());
                return stack;

            } catch (FileNotFoundException e) {
                Log.w(TAG, "Failed to restore stack for package: " + callingPackage
                        + " because of error: "+ e);
                mState.stack.reset();
                mRestoredStack = false;
                Log.w(TAG, "Failed to restore stack for package: " + callingPackage, e);
            }
        }

@@ -101,8 +104,7 @@ final class LoadLastAccessedStackTask<T extends Activity & CommonAddons>
    }

    @Override
    protected void finish(Void result) {
        mState.external = mExternal;
        mOwner.refreshCurrentRootAndDirectory(AnimationView.ANIM_NONE);
    protected void finish(@Nullable DocumentStack stack) {
        mCallback.accept(stack);
    }
}
+2 −0
Original line number Diff line number Diff line
@@ -50,6 +50,8 @@ public interface RootsAccess {

    RootInfo getDefaultRootBlocking(State state);

    RootInfo getRecentsRoot();

    /**
     * Returns a list of roots for the specified authority. If not found, then
     * an empty list is returned.
Loading