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

Commit 23e1ab5f authored by Zemiao Zhu's avatar Zemiao Zhu
Browse files

Add a localPreference for showHiddenFiles and use it in DirectoryLoader.

Bug: 154349585
Test: atest DocumentsUIGoogleTestst
Change-Id: I4f2f7db9b148357fba798f20b8c0aca511fa2101
parent 6754b3a3
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -29,6 +29,9 @@
    <!-- Indicates if showing as search bar design -->
    <bool name="show_search_bar">false</bool>

    <!-- Indicates if showing hidden files by default -->
    <bool name="show_hidden_files_by_default">false</bool>

    <string name="default_root_uri" translatable="false">content://com.android.providers.downloads.documents/root/downloads</string>
    <!-- The value is used in sorting process on Drawer menu. If the root's package name
     start with it, the item will have higher order than others.-->
+5 −0
Original line number Diff line number Diff line
@@ -46,6 +46,7 @@ import com.android.documentsui.base.MimeTypes;
import com.android.documentsui.base.RootInfo;
import com.android.documentsui.base.State;
import com.android.documentsui.base.UserId;
import com.android.documentsui.prefs.LocalPreferences;
import com.android.documentsui.roots.RootCursorWrapper;
import com.android.documentsui.sorting.SortModel;

@@ -178,6 +179,10 @@ public class DirectoryLoader extends AsyncTaskLoader<DirectoryResult> {
            }
            cursor.registerContentObserver(mObserver);

            boolean showHiddenFiles = LocalPreferences.getShowHiddenFiles(getContext(),
                    getContext().getResources().getBoolean(R.bool.show_hidden_files_by_default));
            cursor = new FilteringCursorWrapper(cursor, showHiddenFiles);

            if (mSearchMode && !mFeatures.isFoldersInSearchResultsEnabled()) {
                // There is no findDocumentPath API. Enable filtering on folders in search mode.
                cursor = new FilteringCursorWrapper(cursor, null, SEARCH_REJECT_MIMES);
+9 −0
Original line number Diff line number Diff line
@@ -41,6 +41,7 @@ import com.android.documentsui.base.FilteringCursorWrapper;
import com.android.documentsui.base.Lookup;
import com.android.documentsui.base.RootInfo;
import com.android.documentsui.base.State;
import com.android.documentsui.prefs.LocalPreferences;
import com.android.documentsui.roots.ProvidersAccess;
import com.android.documentsui.roots.RootCursorWrapper;

@@ -180,6 +181,14 @@ public abstract class MultiRootDocumentsLoader extends AsyncTaskLoader<Directory
                            // after a query.
                            continue;
                        }

                        boolean fallback = getContext()
                                .getResources()
                                .getBoolean(R.bool.show_hidden_files_by_default);
                        boolean showHiddenFiles =
                                LocalPreferences.getShowHiddenFiles(getContext(), fallback);
                        cursor = new FilteringCursorWrapper(cursor, showHiddenFiles);

                        final FilteringCursorWrapper filtered = new FilteringCursorWrapper(
                                cursor, mState.acceptMimes, getRejectMimes(), rejectBefore) {
                            @Override
+20 −0
Original line number Diff line number Diff line
@@ -72,6 +72,26 @@ public class FilteringCursorWrapper extends AbstractCursor {
        }
    }

    public FilteringCursorWrapper(Cursor cursor, boolean showHiddenFiles) {
        mCursor = cursor;

        final int count = cursor.getCount();
        mPosition = new int[count];

        cursor.moveToPosition(-1);
        while (cursor.moveToNext() && mCount < count) {
            final String name = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
            if (!showHiddenFiles && name.startsWith(".")) {
                continue;
            }
            mPosition[mCount++] = cursor.getPosition();
        }

        if (DEBUG && mCount != cursor.getCount()) {
            Log.d(TAG, "Before filtering " + cursor.getCount() + ", after " + mCount);
        }
    }

    @Override
    public Bundle getExtras() {
        return mCursor.getExtras();
+21 −5
Original line number Diff line number Diff line
@@ -18,11 +18,13 @@ package com.android.documentsui.prefs;

import static com.android.documentsui.base.State.MODE_UNKNOWN;

import androidx.annotation.IntDef;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import androidx.annotation.IntDef;
import androidx.annotation.VisibleForTesting;

import com.android.documentsui.base.RootInfo;
import com.android.documentsui.base.State.ViewMode;

@@ -34,23 +36,37 @@ import java.lang.annotation.RetentionPolicy;
 */
public class LocalPreferences {
    private static final String ROOT_VIEW_MODE_PREFIX = "rootViewMode-";
    private static final String SHOW_HIDDEN_FILES = "showHiddenFiles";

    public static @ViewMode int getViewMode(Context context, RootInfo root,
            @ViewMode int fallback) {
        return getPrefs(context).getInt(createKey(root), fallback);
        return getPrefs(context).getInt(createKey(ROOT_VIEW_MODE_PREFIX, root), fallback);
    }

    /** Returns if hidden files should be shown. */
    public static boolean getShowHiddenFiles(Context context, boolean fallback) {
        return getPrefs(context).getBoolean(SHOW_HIDDEN_FILES, fallback);
    }

    public static void setViewMode(Context context, RootInfo root, @ViewMode int viewMode) {
        assert(viewMode != MODE_UNKNOWN);
        getPrefs(context).edit().putInt(createKey(root), viewMode).apply();
        getPrefs(context).edit().putInt(createKey(ROOT_VIEW_MODE_PREFIX, root), viewMode).apply();
    }

    /** Sets if hidden files should be shown. */
    @VisibleForTesting
    public static void setShowHiddenFiles(Context context, boolean showHiddenFiles) {
        getPrefs(context).edit()
                .putBoolean(SHOW_HIDDEN_FILES, showHiddenFiles)
                .apply();
    }

    private static SharedPreferences getPrefs(Context context) {
        return PreferenceManager.getDefaultSharedPreferences(context);
    }

    private static String createKey(RootInfo root) {
        return ROOT_VIEW_MODE_PREFIX + root.authority + root.rootId;
    private static String createKey(String prefix, RootInfo root) {
        return prefix + root.authority + root.rootId;
    }

    public static final int PERMISSION_ASK = 0;