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