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

Commit 36603b27 authored by Bo Majewski's avatar Bo Majewski
Browse files

[DocsUI, Search]: Show progress while searching.

Adds a new method onSearchStarting() to the SearchManagerListener. Uses
it to inform the search manager listener created in the base activity to
indicate that the model is loading. This triggers a progress bar in the
file list view (RecyclerView). Adds ability to explicitly set th model
state to loading state. Adjusts the tests.

Bug: 412504830
Test: m DocumentsUIGoogle
Test: m SearchViewManagerTest
Flag: com.android.documentsui.flags.use_search_v2_read_only
Change-Id: I76778412d9fbce1ff9039e9add959743762e1bf8
parent a1f63535
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import static com.android.documentsui.util.FlagUtils.isUseMaterial3FlagEnabled;
import static com.android.documentsui.util.FlagUtils.isUsePeekPreviewFlagEnabled;
import static com.android.documentsui.util.FlagUtils.isVisualSignalsFlagEnabled;
import static com.android.documentsui.util.Material3Config.getRes;
import static com.android.documentsui.util.FlagUtils.isSearchV2Enabled;

import android.content.Context;
import android.content.Intent;
@@ -261,6 +262,13 @@ public abstract class BaseActivity
                }
            }

            @Override
            public void onSearchStarting() {
                if (isSearchV2Enabled()) {
                    mInjector.getModel().setLoading(true);
                }
            }

            @Override
            public void onSearchFinished() {
                // Restores menu icons state
+19 −1
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.documentsui;

import static com.android.documentsui.base.SharedMinimal.DEBUG;
import static com.android.documentsui.base.SharedMinimal.VERBOSE;
import static com.android.documentsui.util.FlagUtils.isSearchV2Enabled;

import android.app.AuthenticationRequiredException;
import android.database.Cursor;
@@ -140,7 +141,11 @@ public class Model {
        }

        final Bundle extras = mCursor.getExtras();
        if (extras != null) {
        if (extras == null) {
            if (isSearchV2Enabled()) {
                mIsLoading = false;
            }
        } else {
            info = extras.getString(DocumentsContract.EXTRA_INFO);
            error = extras.getString(DocumentsContract.EXTRA_ERROR);
            mIsLoading = extras.getBoolean(DocumentsContract.EXTRA_LOADING, false);
@@ -149,6 +154,19 @@ public class Model {
        notifyUpdateListeners();
    }

    /**
     * Explicitly sets whether this model is in the loading state or not.
     * @param loading If this model is considered to be loading new content.
     */
    public void setLoading(boolean loading) {
        if (isSearchV2Enabled()) {
            if (mIsLoading != loading) {
                mIsLoading = loading;
                notifyUpdateListeners();
            }
        }
    }

    @VisibleForTesting
    public int getItemCount() {
        return mCursorCount;
+10 −0
Original line number Diff line number Diff line
@@ -593,6 +593,9 @@ public class SearchViewManager implements
    }

    private void performSearch(String newText) {
        if (isSearchV2Enabled()) {
            mListener.onSearchStarting();
        }
        cancelQueuedSearch();
        synchronized (mSearchLock) {
            mQueuedSearchTask = createSearchTask(newText);
@@ -768,6 +771,13 @@ public class SearchViewManager implements
    public interface SearchManagerListener {
        void onSearchChanged(@Nullable String query);

        /**
         * Called when the search is about to start. There may be other tasks performed
         * before actual searching commences, such as debouncing, etc. However, this is
         * the signal that the SearchViewManager is getting ready to start searching.
         */
        void onSearchStarting();

        void onSearchFinished();

        void onSearchViewChanged(boolean opened);
+3 −28
Original line number Diff line number Diff line
@@ -39,34 +39,9 @@ public class TestSearchViewManager extends SearchViewManager {
    private boolean mShowMenuCalled;

    public TestSearchViewManager() {
        super(
                new SearchManagerListener() {
                    @Override
                    public void onSearchChanged(String query) {
                    }

                    @Override
                    public void onSearchFinished() {
                    }

                    @Override
                    public void onSearchViewChanged(boolean opened) {
                    }

                    @Override
                    public void onSearchChipStateChanged(View v) {
                    }

                    @Override
                    public void onSearchViewFocusChanged(boolean hasFocus) {
                    }

                    @Override
                    public void onSearchViewClearClicked() {
                    }
                },
                new CommandInterceptor(new TestFeatures()), mock(ViewGroup.class), mock(View.class),
                null /* savedState */);
        super(mock(SearchManagerListener.class),
                new CommandInterceptor(new TestFeatures()), mock(ViewGroup.class),
                mock(View.class), /*savedState=*/null);
    }

    @Override
+39 −9
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.platform.test.annotations.RequiresFlagsDisabled;
import android.platform.test.annotations.RequiresFlagsEnabled;
import android.provider.DocumentsContract;
import android.text.TextUtils;
import android.view.View;
@@ -55,6 +56,7 @@ import com.android.documentsui.base.EventHandler;
import com.android.documentsui.base.RootInfo;
import com.android.documentsui.flags.Flags;
import com.android.documentsui.queries.SearchViewManager.SearchManagerListener;
import com.android.documentsui.rules.CheckAndForceMaterial3Flag;
import com.android.documentsui.testing.TestEventHandler;
import com.android.documentsui.testing.TestHandler;
import com.android.documentsui.testing.TestMenu;
@@ -62,6 +64,7 @@ import com.android.documentsui.testing.TestMenuItem;
import com.android.documentsui.testing.TestTimer;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

@@ -76,6 +79,9 @@ import java.util.TimerTask;
@SmallTest
public final class SearchViewManagerTest {

    @Rule
    public final CheckAndForceMaterial3Flag mCheckFlagsRule = new CheckAndForceMaterial3Flag();

    private TestEventHandler<String> mTestEventHandler;
    private TestTimer mTestTimer;
    private TestHandler mTestHandler;
@@ -83,9 +89,9 @@ public final class SearchViewManagerTest {
    private TestMenuItem mSearchMenuItem;
    private TestableSearchViewManager mSearchViewManager;
    private SearchChipViewManager mSearchChipViewManager;
    private SearchOptionsController mSearchOptionsController;

    private boolean mListenerOnSearchChangedCalled;
    private int mOnSearchStartingCallCount;

    @Before
    public void setUp() {
@@ -93,12 +99,19 @@ public final class SearchViewManagerTest {
        mTestTimer = new TestTimer();
        mTestHandler = new TestHandler();

        mOnSearchStartingCallCount = 0;

        final SearchManagerListener searchListener = new SearchManagerListener() {
            @Override
            public void onSearchChanged(@Nullable String query) {
                mListenerOnSearchChangedCalled = true;
            }

            @Override
            public void onSearchStarting() {
                ++mOnSearchStartingCallCount;
            }

            @Override
            public void onSearchFinished() {
            }
@@ -123,9 +136,14 @@ public final class SearchViewManagerTest {
        ViewGroup chipGroup = mock(ViewGroup.class);
        mSearchChipViewManager = spy(new SearchChipViewManager(chipGroup));
        View searchOptionsView = mock(View.class);
        mSearchOptionsController = new SearchOptionsController(searchOptionsView);
        mSearchViewManager = new TestableSearchViewManager(searchListener, mTestEventHandler,
                mSearchChipViewManager, mSearchOptionsController, null /* savedState */,
        SearchOptionsController mSearchOptionsController = new SearchOptionsController(
                searchOptionsView);
        mSearchViewManager = new TestableSearchViewManager(
                searchListener,
                mTestEventHandler,
                mSearchChipViewManager,
                mSearchOptionsController,
                /*savedState=*/null,
                mTestTimer, mTestHandler);

        mTestMenu = TestMenu.create();
@@ -391,7 +409,7 @@ public final class SearchViewManagerTest {
    }

    @Test
    @RequiresFlagsDisabled(Flags.FLAG_USE_SEARCH_V2_READ_ONLY)
    @RequiresFlagsDisabled({Flags.FLAG_USE_SEARCH_V2_READ_ONLY})
    public void testBuildQueryArgs_hasMimeType() throws Exception {
        mSearchViewManager.onClick(null);
        mSearchChipViewManager.mCheckedChipItems = getFakeSearchChipDataList();
@@ -405,7 +423,7 @@ public final class SearchViewManagerTest {
    }

    @Test
    @RequiresFlagsDisabled(Flags.FLAG_USE_SEARCH_V2_READ_ONLY)
    @RequiresFlagsDisabled({Flags.FLAG_USE_SEARCH_V2_READ_ONLY})
    public void testBuildQueryArgs_hasLargeFilesSize() throws Exception {
        mSearchViewManager.onClick(null);
        mSearchChipViewManager.mCheckedChipItems = getFakeSearchChipDataList();
@@ -418,7 +436,7 @@ public final class SearchViewManagerTest {
    }

    @Test
    @RequiresFlagsDisabled(Flags.FLAG_USE_SEARCH_V2_READ_ONLY)
    @RequiresFlagsDisabled({Flags.FLAG_USE_SEARCH_V2_READ_ONLY})
    public void testBuildQueryArgs_hasWeekAgoTime() throws Exception {
        mSearchViewManager.onClick(null);
        mSearchChipViewManager.mCheckedChipItems = getFakeSearchChipDataList();
@@ -436,6 +454,7 @@ public final class SearchViewManagerTest {
    }

    @Test
    @RequiresFlagsDisabled({Flags.FLAG_USE_SEARCH_V2_READ_ONLY})
    public void testSupportsMimeTypesSearch_showChips() throws Exception {
        RootInfo root = spy(new RootInfo());
        when(root.isRecents()).thenReturn(false);
@@ -449,7 +468,7 @@ public final class SearchViewManagerTest {
    }

    @Test
    @RequiresFlagsDisabled(Flags.FLAG_USE_SEARCH_V2_READ_ONLY)
    @RequiresFlagsDisabled({Flags.FLAG_USE_SEARCH_V2_READ_ONLY})
    public void testNotSupportsMimeTypesSearch_notShowChips() throws Exception {
        RootInfo root = spy(new RootInfo());
        when(root.isRecents()).thenReturn(false);
@@ -477,7 +496,7 @@ public final class SearchViewManagerTest {
    }

    @Test
    @RequiresFlagsDisabled(Flags.FLAG_USE_SEARCH_V2_READ_ONLY)
    @RequiresFlagsDisabled({Flags.FLAG_USE_SEARCH_V2_READ_ONLY})
    public void testNotSupportsSearch_notShowMenuAndChips() throws Exception {
        RootInfo root = spy(new RootInfo());
        when(root.isRecents()).thenReturn(false);
@@ -491,6 +510,17 @@ public final class SearchViewManagerTest {
        verify(mSearchChipViewManager, times(1)).setChipsRowVisible(false);
    }

    @Test
    @RequiresFlagsEnabled({Flags.FLAG_USE_SEARCH_V2_READ_ONLY})
    public void testOnSearchStartingCalled() {
        mSearchViewManager.onClick(null);
        mTestEventHandler.nextReturn(true);
        mSearchViewManager.onQueryTextChange("q");
        assertEquals(1, mOnSearchStartingCallCount);
        mSearchViewManager.onQueryTextChange("c");
        assertEquals(2, mOnSearchStartingCallCount);
    }

    private static Set<SearchChipData> getFakeSearchChipDataList() {
        final Set<SearchChipData> chipDataList = new HashSet<>();
        chipDataList.add(new SearchChipData(MetricConsts.TYPE_CHIP_IMAGES,