blob: f0faaa47395fba89df06e6391e6e03b2db743125 [file] [log] [blame]
Ben Linb8c54e72016-06-10 12:13:27 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.documentsui;
18
Steve McKay30535bc2016-11-04 14:16:58 -070019import static com.android.documentsui.base.Shared.VERBOSE;
Ben Linb8c54e72016-06-10 12:13:27 -070020
21import android.annotation.Nullable;
22import android.graphics.drawable.Drawable;
23import android.util.Log;
24import android.view.View;
Garfield Tand2fc4cd2017-03-15 15:39:14 -070025import android.widget.Toolbar;
Ben Linb8c54e72016-06-10 12:13:27 -070026
Steve McKayd0805062016-09-15 14:30:38 -070027import com.android.documentsui.base.RootInfo;
Steve McKayd9caa6a2016-09-15 16:36:45 -070028import com.android.documentsui.base.State;
Ben Linb8c54e72016-06-10 12:13:27 -070029import com.android.documentsui.dirlist.AnimationView;
Ben Linb8c54e72016-06-10 12:13:27 -070030
Ben Line4ffd062017-01-19 15:10:49 -080031import java.util.function.IntConsumer;
Ben Linb8c54e72016-06-10 12:13:27 -070032
33/**
34 * A facade over the portions of the app and drawer toolbars.
35 */
36public class NavigationViewManager {
37
38 private static final String TAG = "NavigationViewManager";
39
Garfield Tand2fc4cd2017-03-15 15:39:14 -070040 private final DrawerController mDrawer;
41 private final Toolbar mToolbar;
42 private final State mState;
43 private final NavigationViewManager.Environment mEnv;
44 private final Breadcrumb mBreadcrumb;
Ben Linb8c54e72016-06-10 12:13:27 -070045
46 public NavigationViewManager(
47 DrawerController drawer,
Garfield Tand2fc4cd2017-03-15 15:39:14 -070048 Toolbar toolbar,
Ben Linb8c54e72016-06-10 12:13:27 -070049 State state,
50 NavigationViewManager.Environment env,
51 Breadcrumb breadcrumb) {
52
53 mToolbar = toolbar;
54 mDrawer = drawer;
55 mState = state;
56 mEnv = env;
57 mBreadcrumb = breadcrumb;
58 mBreadcrumb.setup(env, state, this::onNavigationItemSelected);
59
60 mToolbar.setNavigationOnClickListener(
61 new View.OnClickListener() {
62 @Override
63 public void onClick(View v) {
64 onNavigationIconClicked();
65 }
66 });
67 }
68
69 private void onNavigationIconClicked() {
70 if (mDrawer.isPresent()) {
71 mDrawer.setOpen(true, DrawerController.OPENED_HAMBURGER);
72 }
73 }
74
75 void onNavigationItemSelected(int position) {
76 boolean changed = false;
77 while (mState.stack.size() > position + 1) {
78 changed = true;
Garfield Tan2a837422016-10-19 11:50:45 -070079 mState.stack.pop();
Ben Linb8c54e72016-06-10 12:13:27 -070080 }
81 if (changed) {
82 mEnv.refreshCurrentRootAndDirectory(AnimationView.ANIM_LEAVE);
83 }
84 }
85
Steve McKay16e0c1f2016-09-15 12:41:13 -070086 public void update() {
Ben Linb8c54e72016-06-10 12:13:27 -070087
88 // TODO: Looks to me like this block is never getting hit.
89 if (mEnv.isSearchExpanded()) {
90 mToolbar.setTitle(null);
91 mBreadcrumb.show(false);
92 return;
93 }
94
95 mDrawer.setTitle(mEnv.getDrawerTitle());
96
97 mToolbar.setNavigationIcon(getActionBarIcon());
98 mToolbar.setNavigationContentDescription(R.string.drawer_open);
99
100 if (mState.stack.size() <= 1) {
101 mBreadcrumb.show(false);
102 String title = mEnv.getCurrentRoot().title;
Steve McKay30535bc2016-11-04 14:16:58 -0700103 if (VERBOSE) Log.v(TAG, "New toolbar title is: " + title);
Ben Linb8c54e72016-06-10 12:13:27 -0700104 mToolbar.setTitle(title);
105 } else {
106 mBreadcrumb.show(true);
107 mToolbar.setTitle(null);
108 mBreadcrumb.postUpdate();
109 }
110
Steve McKay30535bc2016-11-04 14:16:58 -0700111 if (VERBOSE) Log.v(TAG, "Final toolbar title is: " + mToolbar.getTitle());
Ben Linb8c54e72016-06-10 12:13:27 -0700112 }
113
114 // Hamburger if drawer is present, else sad nullness.
115 private @Nullable Drawable getActionBarIcon() {
116 if (mDrawer.isPresent()) {
117 return mToolbar.getContext().getDrawable(R.drawable.ic_hamburger);
118 } else {
119 return null;
120 }
121 }
122
123 void revealRootsDrawer(boolean open) {
124 mDrawer.setOpen(open);
125 }
126
127 interface Breadcrumb {
Ben Line4ffd062017-01-19 15:10:49 -0800128 void setup(Environment env, State state, IntConsumer listener);
Ben Linb8c54e72016-06-10 12:13:27 -0700129 void show(boolean visibility);
130 void postUpdate();
131 }
132
133 interface Environment {
Steve McKayeed2f4e2016-10-03 20:30:52 -0700134 @Deprecated // Use CommonAddones#getCurrentRoot
Ben Linb8c54e72016-06-10 12:13:27 -0700135 RootInfo getCurrentRoot();
136 String getDrawerTitle();
Steve McKay988d8a32016-09-27 09:41:17 -0700137 @Deprecated // Use CommonAddones#refreshCurrentRootAndDirectory
Ben Linb8c54e72016-06-10 12:13:27 -0700138 void refreshCurrentRootAndDirectory(int animation);
139 boolean isSearchExpanded();
140 }
141}