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

Commit f9ecf709 authored by Cassy Chun-Crogan's avatar Cassy Chun-Crogan
Browse files

[DocsUI M3] Re-layout file list on window size change

Previously only a directory load or an app bar layout change
would re-layout the file list. A lot of small, incremental
window size changes would not trigger a re-layout leading to
a strange UI or overcrowded items or excessive space.

Update the layout whenever the AnimationView (the directory
fragment) observes a size change. Introduce an OnSizeChangedListener
to trigger the re-layout.

Also use the dimens grid_width, grid_height and grid_item_margin
on the grid item (item_doc_grid.xml) as these are also used by the
DirectoryFragment.java to get the correct spacing for the layout.

See bug for demo video.

Bug: 404625076
Test: m DocumentsUIGoogle && manual inspection
Flag: com.android.documentsui.flags.use_material3
Change-Id: I5e2a94757b8effc2462f818bc2fa286fefcd4740
parent 351a3e2f
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -17,9 +17,9 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/item_root"
    android:layout_width="@dimen/grid_item_width"
    android:layout_height="@dimen/grid_item_height"
    android:layout_margin="@dimen/grid_item_layout_margin"
    android:layout_width="@dimen/grid_width"
    android:layout_height="@dimen/grid_height"
    android:layout_margin="@dimen/grid_item_margin"
    android:clickable="true"
    android:defaultFocusHighlightEnabled="false"
    android:focusable="true"
+3 −5
Original line number Diff line number Diff line
@@ -43,9 +43,10 @@
    <dimen name="progress_bar_height">4dp</dimen>
    <fraction name="grid_scale_min">85%</fraction>
    <fraction name="grid_scale_max">200%</fraction>
    <dimen name="grid_width">152dp</dimen>
    <dimen name="grid_width">150dp</dimen>
    <dimen name="grid_height">132dp</dimen>
    <dimen name="grid_section_separator_height">0dp</dimen>
    <dimen name="grid_item_margin">6dp</dimen>
    <dimen name="grid_item_margin">@dimen/space_small_1</dimen>
    <dimen name="grid_padding_horiz">4dp</dimen>
    <dimen name="grid_padding_vert">4dp</dimen>
    <dimen name="list_item_height">56dp</dimen>
@@ -71,8 +72,6 @@
    <dimen name="breadcrumb_item_arrow_size">16dp</dimen>
    <dimen name="dir_elevation">8dp</dimen>
    <dimen name="drag_shadow_size">120dp</dimen>
    <dimen name="grid_item_width">150dp</dimen>
    <dimen name="grid_item_height">132dp</dimen>
    <dimen name="grid_item_padding_start">@dimen/space_extra_small_2</dimen>
    <dimen name="grid_item_padding_end">@dimen/space_extra_small_2</dimen>
    <dimen name="grid_item_padding_top">@dimen/space_extra_small_2</dimen>
@@ -81,7 +80,6 @@
    <dimen name="grid_item_thumbnail_radius">12dp</dimen>
    <dimen name="grid_item_icon_width">64dp</dimen>
    <dimen name="grid_item_icon_height">64dp</dimen>
    <dimen name="grid_item_layout_margin">@dimen/space_small_1</dimen>
    <dimen name="grid_item_nameplate_width">142dp</dimen>
    <dimen name="grid_item_nameplate_height">44dp</dimen>
    <dimen name="grid_item_nameplate_padding">4dp</dimen>
+41 −2
Original line number Diff line number Diff line
@@ -16,19 +16,22 @@

package com.android.documentsui.dirlist;

import androidx.annotation.IntDef;
import androidx.fragment.app.FragmentTransaction;
import static com.android.documentsui.util.FlagUtils.isUseMaterial3FlagEnabled;

import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import androidx.annotation.IntDef;
import androidx.fragment.app.FragmentTransaction;

import com.android.documentsui.R;
import com.android.documentsui.base.Shared;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;

/**
 * This class exists solely to support animated transition of our directory fragment.
@@ -51,6 +54,8 @@ public class AnimationView extends LinearLayout {
    public static final int ANIM_LEAVE = 3;
    public static final int ANIM_ENTER = 4;

    private final ArrayList<OnSizeChangedListener> mOnSizeChangedListeners = new ArrayList<>();

    private float mPosition = 0f;

    // The distance the animation will cover...currently matches the height of the
@@ -65,11 +70,45 @@ public class AnimationView extends LinearLayout {
        super(context, attrs);
    }

    /**
     * A listener of the onSizeChanged method.
     */
    public interface OnSizeChangedListener {
        /**
         * Called on the View's onSizeChanged.
         */
        void onSizeChanged();
    }

    /**
     * Adds a listener of the onSizeChanged method.
     */
    public void addOnSizeChangedListener(OnSizeChangedListener listener) {
        if (isUseMaterial3FlagEnabled()) {
            mOnSizeChangedListeners.add(listener);
        }
    }

    /**
     * Removes a listener of the onSizeChanged method.
     */
    public void removeOnSizeChangedListener(OnSizeChangedListener listener) {
        if (isUseMaterial3FlagEnabled()) {
            mOnSizeChangedListeners.remove(listener);
        }
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mSpan = h;
        setPosition(mPosition);
        if (isUseMaterial3FlagEnabled()) {
            for (int i = mOnSizeChangedListeners.size() - 1; i >= 0; --i) {
                mOnSizeChangedListeners.get(i).onSizeChanged();
            }
        }
    }

    public float getPosition() {
+21 −3
Original line number Diff line number Diff line
@@ -108,6 +108,7 @@ import com.android.documentsui.clipping.ClipStore;
import com.android.documentsui.clipping.DocumentClipper;
import com.android.documentsui.clipping.UrisSupplier;
import com.android.documentsui.dirlist.AnimationView.AnimationType;
import com.android.documentsui.dirlist.AnimationView.OnSizeChangedListener;
import com.android.documentsui.picker.PickActivity;
import com.android.documentsui.services.FileOperation;
import com.android.documentsui.services.FileOperationService;
@@ -188,7 +189,7 @@ public class DirectoryFragment extends Fragment implements SwipeRefreshLayout.On
    private SelectionMetadata mSelectionMetadata;
    private KeyInputHandler mKeyListener;
    private @Nullable DragHoverListener mDragHoverListener;
    private View mRootView;
    private AnimationView mRootView;
    private IconHelper mIconHelper;
    private SwipeRefreshLayout mRefreshLayout;
    private RecyclerView mRecView;
@@ -416,13 +417,27 @@ public class DirectoryFragment extends Fragment implements SwipeRefreshLayout.On
                || Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action);
    }

    private OnSizeChangedListener mOnSizeChangedListener =
            new AnimationView.OnSizeChangedListener() {
                @Override
                public void onSizeChanged() {
                    if (isUseMaterial3FlagEnabled() && mState.derivedMode != MODE_LIST) {
                        // Update the grid layout when the window size changes.
                        updateLayout(mState.derivedMode);
                    }
                }
            };

    @Override
    public View onCreateView(
            LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        mHandler = new Handler(Looper.getMainLooper());
        mActivity = (BaseActivity) getActivity();
        mRootView = inflater.inflate(R.layout.fragment_directory, container, false);
        mRootView = (AnimationView) inflater.inflate(R.layout.fragment_directory, container, false);
        if (isUseMaterial3FlagEnabled()) {
            mRootView.addOnSizeChangedListener(mOnSizeChangedListener);
        }

        mProgressBar = mRootView.findViewById(R.id.progressbar);
        assert mProgressBar != null;
@@ -497,6 +512,10 @@ public class DirectoryFragment extends Fragment implements SwipeRefreshLayout.On
        mModel.removeUpdateListener(mAdapter.getModelUpdateListener());
        setPreDrawListenerEnabled(false);

        if (isUseMaterial3FlagEnabled()) {
            mRootView.removeOnSizeChangedListener(mOnSizeChangedListener);
        }

        super.onDestroyView();
    }

@@ -809,7 +828,6 @@ public class DirectoryFragment extends Fragment implements SwipeRefreshLayout.On
        if (mLayout != null) {
            mLayout.setSpanCount(mColumnCount);
        }

        int pad = getDirectoryPadding(mode);
        mAppBarHeight = getAppBarLayoutHeight();
        mSaveLayoutHeight = getSaveLayoutHeight();