blob: 6a9d633361e6256aa2baf0bd0826916a11202d21 [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 McKayd9caa6a2016-09-15 16:36:45 -070019import static com.android.documentsui.base.Shared.DEBUG;
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;
25
Steve McKayd0805062016-09-15 14:30:38 -070026import com.android.documentsui.base.RootInfo;
Steve McKayd9caa6a2016-09-15 16:36:45 -070027import com.android.documentsui.base.State;
Ben Linb8c54e72016-06-10 12:13:27 -070028import com.android.documentsui.dirlist.AnimationView;
Ben Linb8c54e72016-06-10 12:13:27 -070029
30import java.util.function.Consumer;
31
32/**
33 * A facade over the portions of the app and drawer toolbars.
34 */
35public class NavigationViewManager {
36
37 private static final String TAG = "NavigationViewManager";
38
39 final DrawerController mDrawer;
40 final DocumentsToolbar mToolbar;
41 final State mState;
42 final NavigationViewManager.Environment mEnv;
43 final Breadcrumb mBreadcrumb;
44
45 public NavigationViewManager(
46 DrawerController drawer,
47 DocumentsToolbar toolbar,
48 State state,
49 NavigationViewManager.Environment env,
50 Breadcrumb breadcrumb) {
51
52 mToolbar = toolbar;
53 mDrawer = drawer;
54 mState = state;
55 mEnv = env;
56 mBreadcrumb = breadcrumb;
57 mBreadcrumb.setup(env, state, this::onNavigationItemSelected);
58
59 mToolbar.setNavigationOnClickListener(
60 new View.OnClickListener() {
61 @Override
62 public void onClick(View v) {
63 onNavigationIconClicked();
64 }
65 });
66 }
67
68 private void onNavigationIconClicked() {
69 if (mDrawer.isPresent()) {
70 mDrawer.setOpen(true, DrawerController.OPENED_HAMBURGER);
71 }
72 }
73
74 void onNavigationItemSelected(int position) {
75 boolean changed = false;
76 while (mState.stack.size() > position + 1) {
77 changed = true;
78 mState.popDocument();
79 }
80 if (changed) {
81 mEnv.refreshCurrentRootAndDirectory(AnimationView.ANIM_LEAVE);
82 }
83 }
84
Steve McKay16e0c1f2016-09-15 12:41:13 -070085 public void update() {
Ben Linb8c54e72016-06-10 12:13:27 -070086
87 // TODO: Looks to me like this block is never getting hit.
88 if (mEnv.isSearchExpanded()) {
89 mToolbar.setTitle(null);
90 mBreadcrumb.show(false);
91 return;
92 }
93
94 mDrawer.setTitle(mEnv.getDrawerTitle());
95
96 mToolbar.setNavigationIcon(getActionBarIcon());
97 mToolbar.setNavigationContentDescription(R.string.drawer_open);
98
99 if (mState.stack.size() <= 1) {
100 mBreadcrumb.show(false);
101 String title = mEnv.getCurrentRoot().title;
Steve McKayc88f83c2016-08-31 12:01:43 -0700102 if (DEBUG) Log.v(TAG, "New toolbar title is: " + title);
Ben Linb8c54e72016-06-10 12:13:27 -0700103 mToolbar.setTitle(title);
104 } else {
105 mBreadcrumb.show(true);
106 mToolbar.setTitle(null);
107 mBreadcrumb.postUpdate();
108 }
109
Steve McKayc88f83c2016-08-31 12:01:43 -0700110 if (DEBUG) Log.v(TAG, "Final toolbar title is: " + mToolbar.getTitle());
Ben Linb8c54e72016-06-10 12:13:27 -0700111 }
112
113 // Hamburger if drawer is present, else sad nullness.
114 private @Nullable Drawable getActionBarIcon() {
115 if (mDrawer.isPresent()) {
116 return mToolbar.getContext().getDrawable(R.drawable.ic_hamburger);
117 } else {
118 return null;
119 }
120 }
121
122 void revealRootsDrawer(boolean open) {
123 mDrawer.setOpen(open);
124 }
125
126 interface Breadcrumb {
127 void setup(Environment env, State state, Consumer<Integer> listener);
128 void show(boolean visibility);
129 void postUpdate();
130 }
131
132 interface Environment {
133 RootInfo getCurrentRoot();
134 String getDrawerTitle();
135 void refreshCurrentRootAndDirectory(int animation);
136 boolean isSearchExpanded();
137 }
138}