1 /*
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 
17 package com.android.documentsui;
18 
19 import static com.android.documentsui.base.Shared.VERBOSE;
20 
21 import android.annotation.Nullable;
22 import android.graphics.drawable.Drawable;
23 import android.util.Log;
24 import android.view.View;
25 import android.widget.Toolbar;
26 
27 import com.android.documentsui.base.RootInfo;
28 import com.android.documentsui.base.State;
29 import com.android.documentsui.dirlist.AnimationView;
30 
31 import java.util.function.IntConsumer;
32 
33 /**
34  * A facade over the portions of the app and drawer toolbars.
35  */
36 public class NavigationViewManager {
37 
38     private static final String TAG = "NavigationViewManager";
39 
40     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;
45 
NavigationViewManager( DrawerController drawer, Toolbar toolbar, State state, NavigationViewManager.Environment env, Breadcrumb breadcrumb)46     public NavigationViewManager(
47             DrawerController drawer,
48             Toolbar toolbar,
49             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 
onNavigationIconClicked()69     private void onNavigationIconClicked() {
70         if (mDrawer.isPresent()) {
71             mDrawer.setOpen(true);
72         }
73     }
74 
onNavigationItemSelected(int position)75     void onNavigationItemSelected(int position) {
76         boolean changed = false;
77         while (mState.stack.size() > position + 1) {
78             changed = true;
79             mState.stack.pop();
80         }
81         if (changed) {
82             mEnv.refreshCurrentRootAndDirectory(AnimationView.ANIM_LEAVE);
83         }
84     }
85 
update()86     public void update() {
87 
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;
103             if (VERBOSE) Log.v(TAG, "New toolbar title is: " + title);
104             mToolbar.setTitle(title);
105         } else {
106             mBreadcrumb.show(true);
107             mToolbar.setTitle(null);
108             mBreadcrumb.postUpdate();
109         }
110 
111         if (VERBOSE) Log.v(TAG, "Final toolbar title is: " + mToolbar.getTitle());
112     }
113 
114     // Hamburger if drawer is present, else sad nullness.
getActionBarIcon()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 
revealRootsDrawer(boolean open)123     void revealRootsDrawer(boolean open) {
124         mDrawer.setOpen(open);
125     }
126 
127     interface Breadcrumb {
setup(Environment env, State state, IntConsumer listener)128         void setup(Environment env, State state, IntConsumer listener);
show(boolean visibility)129         void show(boolean visibility);
postUpdate()130         void postUpdate();
131     }
132 
133     interface Environment {
134         @Deprecated  // Use CommonAddones#getCurrentRoot
getCurrentRoot()135         RootInfo getCurrentRoot();
getDrawerTitle()136         String getDrawerTitle();
137         @Deprecated  // Use CommonAddones#refreshCurrentRootAndDirectory
refreshCurrentRootAndDirectory(int animation)138         void refreshCurrentRootAndDirectory(int animation);
isSearchExpanded()139         boolean isSearchExpanded();
140     }
141 }
142