summaryrefslogtreecommitdiff
path: root/src/com/android/documentsui/NavigationViewManager.java
blob: bbae22d4267699c8f9140ed2c556f63f855192c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.documentsui;

import static com.android.documentsui.base.SharedMinimal.VERBOSE;
import static com.android.documentsui.util.FlagUtils.isUseMaterial3FlagEnabled;
import static com.android.documentsui.util.FlagUtils.isVisualSignalsFlagEnabled;

import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Outline;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewOutlineProvider;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.ColorRes;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.selection.SelectionTracker;

import com.android.documentsui.Injector.Injected;
import com.android.documentsui.base.EventHandler;
import com.android.documentsui.base.RootInfo;
import com.android.documentsui.base.State;
import com.android.documentsui.base.UserId;
import com.android.documentsui.dirlist.AnimationView;
import com.android.documentsui.util.VersionUtils;
import com.android.modules.utils.build.SdkLevel;

import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.CollapsingToolbarLayout;

import java.util.function.IntConsumer;

/** A facade over the portions of the app and drawer toolbars. */
public class NavigationViewManager extends SelectionTracker.SelectionObserver<String>
        implements AppBarLayout.OnOffsetChangedListener {

    private static final String TAG = "NavigationViewManager";

    private final DrawerController mDrawer;
    private final Toolbar mToolbar;
    private final BaseActivity mActivity;
    private final View mHeader;
    private final State mState;
    private final NavigationViewManager.Environment mEnv;
    private final Breadcrumb mBreadcrumb;
    private final ProfileTabs mProfileTabs;
    private final View mSearchBarView;
    private final CollapsingToolbarLayout mCollapsingBarLayout;
    private final Drawable mDefaultActionBarBackground;
    private final ViewOutlineProvider mDefaultOutlineProvider;
    private final ViewOutlineProvider mSearchBarOutlineProvider;
    private final boolean mShowSearchBar;
    private final ConfigStore mConfigStore;
    @Injected private final Injector<?> mInjector;
    private boolean mIsActionModeActivated = false;
    @ColorRes private int mDefaultStatusBarColorResId;
    private MenuManager.SelectionDetails mSelectionDetails;
    private EventHandler<MenuItem> mActionMenuItemClicker;

    public NavigationViewManager(
            BaseActivity activity,
            DrawerController drawer,
            State state,
            NavigationViewManager.Environment env,
            Breadcrumb breadcrumb,
            View tabLayoutContainer,
            UserIdManager userIdManager,
            ConfigStore configStore,
            Injector injector) {
        this(
                activity,
                drawer,
                state,
                env,
                breadcrumb,
                tabLayoutContainer,
                userIdManager,
                null,
                configStore,
                injector);
    }

    public NavigationViewManager(
            BaseActivity activity,
            DrawerController drawer,
            State state,
            NavigationViewManager.Environment env,
            Breadcrumb breadcrumb,
            View tabLayoutContainer,
            UserManagerState userManagerState,
            ConfigStore configStore,
            Injector injector) {
        this(
                activity,
                drawer,
                state,
                env,
                breadcrumb,
                tabLayoutContainer,
                null,
                userManagerState,
                configStore,
                injector);
    }

    public NavigationViewManager(
            BaseActivity activity,
            DrawerController drawer,
            State state,
            NavigationViewManager.Environment env,
            Breadcrumb breadcrumb,
            View tabLayoutContainer,
            UserIdManager userIdManager,
            UserManagerState userManagerState,
            ConfigStore configStore,
            Injector injector) {

        mActivity = activity;
        mToolbar = activity.findViewById(R.id.toolbar);
        mHeader = activity.findViewById(R.id.directory_header);
        mDrawer = drawer;
        mState = state;
        mEnv = env;
        mBreadcrumb = breadcrumb;
        mBreadcrumb.setup(
                env,
                state,
                this::onNavigationItemSelected,
                isUseMaterial3FlagEnabled()
                        ? activity.findViewById(R.id.breadcrumb_top_divider)
                        : null);
        mConfigStore = configStore;
        mInjector = injector;
        mProfileTabs =
                getProfileTabs(tabLayoutContainer, userIdManager, userManagerState, activity);

        mToolbar.setNavigationOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onNavigationIconClicked();
                    }
                });
        if (isUseMaterial3FlagEnabled()) {
            mToolbar.setOnMenuItemClickListener(
                    new Toolbar.OnMenuItemClickListener() {
                        @Override
                        public boolean onMenuItemClick(MenuItem menuItem) {
                            return onToolbarMenuItemClicked(menuItem);
                        }
                    });
        }
        mSearchBarView = activity.findViewById(R.id.searchbar_title);
        mCollapsingBarLayout = activity.findViewById(R.id.collapsing_toolbar);
        mDefaultActionBarBackground = mToolbar.getBackground();
        mDefaultOutlineProvider = mToolbar.getOutlineProvider();
        mShowSearchBar = activity.getResources().getBoolean(R.bool.show_search_bar);

        final int[] styledAttrs = {android.R.attr.statusBarColor};
        TypedArray a = mActivity.obtainStyledAttributes(styledAttrs);
        mDefaultStatusBarColorResId = a.getResourceId(0, -1);
        if (mDefaultStatusBarColorResId == -1) {
            Log.w(TAG, "Retrieve statusBarColorResId from theme failed, assigned default");
            mDefaultStatusBarColorResId = R.color.app_background_color;
        }
        a.recycle();

        final Resources resources = mToolbar.getResources();
        final int radius = resources.getDimensionPixelSize(R.dimen.search_bar_radius);
        final int marginStart =
                resources.getDimensionPixelSize(R.dimen.search_bar_background_margin_start);
        final int marginEnd =
                resources.getDimensionPixelSize(R.dimen.search_bar_background_margin_end);
        mSearchBarOutlineProvider = new ViewOutlineProvider() {
            @Override
            public void getOutline(View view, Outline outline) {
                outline.setRoundRect(marginStart, 0,
                        view.getWidth() - marginEnd, view.getHeight(), radius);
            }
        };
    }

    private ProfileTabs getProfileTabs(View tabLayoutContainer, UserIdManager userIdManager,
            UserManagerState userManagerState, BaseActivity activity) {
        return mConfigStore.isPrivateSpaceInDocsUIEnabled()
                ? new ProfileTabs(tabLayoutContainer, mState, userManagerState, mEnv, activity,
                mConfigStore)
                : new ProfileTabs(tabLayoutContainer, mState, userIdManager, mEnv, activity,
                        mConfigStore);
    }

    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int offset) {
        if (!VersionUtils.isAtLeastS()) {
            return;
        }

        // For S+ Only. Change toolbar color dynamically based on scroll offset.
        // Usually this can be done in xml using app:contentScrim and app:statusBarScrim, however
        // in our case since we also put directory_header.xml inside the CollapsingToolbarLayout,
        // the scrim will also cover the directory header. Long term need to think about how to
        // move directory_header out of the AppBarLayout.

        Window window = mActivity.getWindow();
        View actionBar =
                window.getDecorView().findViewById(androidx.appcompat.R.id.action_mode_bar);
        int dynamicHeaderColor = ContextCompat.getColor(mActivity,
                offset == 0 ? mDefaultStatusBarColorResId : R.color.color_surface_header);
        if (actionBar != null) {
            // Action bar needs to be updated separately for selection mode.
            actionBar.setBackgroundColor(dynamicHeaderColor);
        }

        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(dynamicHeaderColor);
        if (shouldShowSearchBar()) {
            // Do not change search bar background.
        } else {
            mToolbar.setBackground(new ColorDrawable(dynamicHeaderColor));
        }
    }

    public void setSearchBarClickListener(View.OnClickListener listener) {
        mSearchBarView.setOnClickListener(listener);
        if (SdkLevel.isAtLeastU()) {
            try {
                mSearchBarView.setHandwritingDelegatorCallback(
                        () -> listener.onClick(mSearchBarView));
            } catch (LinkageError e) {
                // Running on a device with an older build of Android U
                // TODO(b/274154553): Remove try/catch block after Android U Beta 1 is released
            }
        }
    }

    public ProfileTabsAddons getProfileTabsAddons() {
        return mProfileTabs;
    }

    /**
     * Sets a listener to the profile tabs.
     */
    public void setProfileTabsListener(ProfileTabs.Listener listener) {
        mProfileTabs.setListener(listener);
    }

    private void onNavigationIconClicked() {
        if (isUseMaterial3FlagEnabled() && inSelectionMode()) {
            closeSelectionBar();
        } else if (mDrawer.isPresent()) {
            mDrawer.setOpen(true);
        }
    }

    private boolean onToolbarMenuItemClicked(MenuItem menuItem) {
        if (inSelectionMode()) {
            mActionMenuItemClicker.accept(menuItem);
            return true;
        }
        return mActivity.onOptionsItemSelected(menuItem);
    }

    void onNavigationItemSelected(int position) {
        boolean changed = false;
        while (mState.stack.size() > position + 1) {
            changed = true;
            mState.stack.pop();
        }
        if (changed) {
            mEnv.refreshCurrentRootAndDirectory(AnimationView.ANIM_LEAVE);
        }
    }

    public UserId getSelectedUser() {
        return mProfileTabs.getSelectedUser();
    }

    public void setActionModeActivated(boolean actionModeActivated) {
        mIsActionModeActivated = actionModeActivated;
        update();
    }

    public void update() {
        // If use_material3 flag is ON, we don't want any scroll behavior, thus skipping this logic.
        if (!isUseMaterial3FlagEnabled()) {
            updateScrollFlag();
        }
        updateToolbar();
        mProfileTabs.updateView();

        // TODO: Looks to me like this block is never getting hit.
        if (mEnv.isSearchExpanded()) {
            mToolbar.setTitle(null);
            mBreadcrumb.show(false);
            return;
        }

        mDrawer.setTitle(mEnv.getDrawerTitle());

        boolean showBurgerMenuOnToolbar = true;
        if (isUseMaterial3FlagEnabled()) {
            View navRailRoots = mActivity.findViewById(R.id.nav_rail_container_roots);
            if (navRailRoots != null) {
                // If nav rail exists, burger menu will show on the nav rail instead.
                showBurgerMenuOnToolbar = false;
            }
        }

        if (showBurgerMenuOnToolbar) {
            mToolbar.setNavigationIcon(getActionBarIcon());
            mToolbar.setNavigationContentDescription(R.string.drawer_open);
        } else {
            mToolbar.setNavigationIcon(null);
            mToolbar.setNavigationContentDescription(null);
        }

        if (shouldShowSearchBar()) {
            mBreadcrumb.show(false);
            mToolbar.setTitle(null);
            mSearchBarView.setVisibility(View.VISIBLE);
            return;
        }

        mSearchBarView.setVisibility(View.GONE);

        if (isUseMaterial3FlagEnabled()) {
            updateActionMenu();
            if (inSelectionMode()) {
                final int quantity = mInjector.selectionMgr.getSelection().size();
                final String title =
                        mToolbar.getContext()
                                .getResources()
                                .getQuantityString(R.plurals.elements_selected, quantity, quantity);
                mToolbar.setTitle(title);
                mActivity.getWindow().setTitle(title);
                mToolbar.setNavigationIcon(R.drawable.ic_cancel);
                mToolbar.setNavigationContentDescription(android.R.string.cancel);
                return;
            }
        }

        String title =
                mState.stack.size() <= 1 ? mEnv.getCurrentRoot().title : mState.stack.getTitle();
        if (VERBOSE) Log.v(TAG, "New toolbar title is: " + title);
        mToolbar.setTitle(title);
        mBreadcrumb.show(true);
        mBreadcrumb.postUpdate();
    }

    @Override
    public void onSelectionChanged() {
        update();
    }

    /** Identifies if the `NavigationViewManager` is in selection mode or not. */
    public boolean inSelectionMode() {
        return mInjector != null
                && mInjector.selectionMgr != null
                && mInjector.selectionMgr.hasSelection();
    }

    private boolean hasActionMenu() {
        return mToolbar.getMenu().findItem(R.id.action_menu_open_with) != null;
    }

    /** Updates the action menu based on whether a selection is currently being made or not. */
    public void updateActionMenu() {
        // For the first start up of the application, the menu might not exist at all but we also
        // don't want to inflate the menu multiple times. So along with checking if the expected
        // menu is already inflated, validate that a menu exists at all as well.
        boolean isMenuInflated = mToolbar.getMenu() != null && mToolbar.getMenu().size() > 0;
        if (inSelectionMode()) {
            if (!isMenuInflated || !hasActionMenu()) {
                mToolbar.getMenu().clear();
                mToolbar.inflateMenu(R.menu.action_mode_menu);
                mToolbar.invalidateMenu();
            }
            mInjector.menuManager.updateActionMenu(mToolbar.getMenu(), mSelectionDetails);
            return;
        }

        if (!isMenuInflated || hasActionMenu()) {
            mToolbar.getMenu().clear();
            mToolbar.inflateMenu(R.menu.activity);
            mToolbar.invalidateMenu();
            boolean fullBarSearch =
                    mActivity.getResources().getBoolean(R.bool.full_bar_search_view);
            boolean showSearchBar = mActivity.getResources().getBoolean(R.bool.show_search_bar);
            mInjector.searchManager.install(mToolbar.getMenu(), fullBarSearch, showSearchBar);
            if (isVisualSignalsFlagEnabled()) {
                mInjector.menuManager.instantiateJobProgress(mToolbar.getMenu());
            }
        }
        mInjector.menuManager.updateOptionMenu(mToolbar.getMenu());
        mInjector.searchManager.showMenu(mState.stack);
    }

    /** Everytime a selection is made, update the selection. */
    public void updateSelection(
            MenuManager.SelectionDetails selectionDetails,
            EventHandler<MenuItem> actionMenuItemClicker) {
        mSelectionDetails = selectionDetails;
        mActionMenuItemClicker = actionMenuItemClicker;
    }

    private void updateScrollFlag() {
        if (mCollapsingBarLayout == null) {
            return;
        }

        AppBarLayout.LayoutParams lp =
                (AppBarLayout.LayoutParams) mCollapsingBarLayout.getLayoutParams();
        lp.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL
                | AppBarLayout.LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED);
        mCollapsingBarLayout.setLayoutParams(lp);
    }

    private void updateToolbar() {
        if (mCollapsingBarLayout == null) {
            // Tablet mode does not use CollapsingBarLayout
            // (res/layout-sw720dp/directory_app_bar.xml or res/layout/fixed_layout.xml)
            if (shouldShowSearchBar()) {
                mToolbar.setBackgroundResource(R.drawable.search_bar_background);
                mToolbar.setOutlineProvider(mSearchBarOutlineProvider);
            } else {
                mToolbar.setBackground(mDefaultActionBarBackground);
                mToolbar.setOutlineProvider(null);
            }
            return;
        }

        CollapsingToolbarLayout.LayoutParams toolbarLayoutParams =
                (CollapsingToolbarLayout.LayoutParams) mToolbar.getLayoutParams();

        int headerTopOffset = 0;
        if (shouldShowSearchBar() && !mIsActionModeActivated) {
            mToolbar.setBackgroundResource(R.drawable.search_bar_background);
            mToolbar.setOutlineProvider(mSearchBarOutlineProvider);
            int searchBarMargin = mToolbar.getResources().getDimensionPixelSize(
                    R.dimen.search_bar_margin);
            toolbarLayoutParams.setMargins(searchBarMargin, searchBarMargin, searchBarMargin,
                    searchBarMargin);
            mToolbar.setLayoutParams(toolbarLayoutParams);
            mToolbar.setElevation(
                    mToolbar.getResources().getDimensionPixelSize(R.dimen.search_bar_elevation));
            headerTopOffset = toolbarLayoutParams.height + searchBarMargin * 2;
        } else {
            mToolbar.setBackground(mDefaultActionBarBackground);
            mToolbar.setOutlineProvider(mDefaultOutlineProvider);
            int actionBarMargin = mToolbar.getResources().getDimensionPixelSize(
                    R.dimen.action_bar_margin);
            toolbarLayoutParams.setMargins(0, 0, 0, /* bottom= */ actionBarMargin);
            mToolbar.setLayoutParams(toolbarLayoutParams);
            mToolbar.setElevation(
                    mToolbar.getResources().getDimensionPixelSize(R.dimen.action_bar_elevation));
            headerTopOffset = toolbarLayoutParams.height + actionBarMargin;
        }

        if (!mIsActionModeActivated) {
            // This could be either FrameLayout.LayoutParams (when use_material3 flag is OFF) or
            // LinearLayout.LayoutParams (when use_material3 flag is ON), so use the common parent
            // class instead to make it work for both scenarios.
            ViewGroup.MarginLayoutParams headerLayoutParams =
                    (ViewGroup.MarginLayoutParams) mHeader.getLayoutParams();
            headerLayoutParams.setMargins(0, /* top= */ headerTopOffset, 0, 0);
            mHeader.setLayoutParams(headerLayoutParams);
        }
    }

    private boolean shouldShowSearchBar() {
        return mState.stack.isRecents() && !mEnv.isSearchExpanded() && mShowSearchBar;
    }

    // Hamburger if drawer is present, else sad nullness.
    private @Nullable
    Drawable getActionBarIcon() {
        if (mDrawer.isPresent()) {
            return mToolbar.getContext().getDrawable(R.drawable.ic_hamburger);
        } else {
            return null;
        }
    }

    void revealRootsDrawer(boolean open) {
        mDrawer.setOpen(open);
    }

    /** Helper method to close the selection bar. */
    public void closeSelectionBar() {
        mInjector.selectionMgr.clearSelection();
    }

    interface Breadcrumb {
        void setup(Environment env, State state, IntConsumer listener, @Nullable View topDivider);

        void show(boolean visibility);

        void postUpdate();
    }

    interface Environment {
        @Deprecated
            // Use CommonAddones#getCurrentRoot
        RootInfo getCurrentRoot();

        String getDrawerTitle();

        @Deprecated
            // Use CommonAddones#refreshCurrentRootAndDirectory
        void refreshCurrentRootAndDirectory(int animation);

        boolean isSearchExpanded();
    }
}