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 | |
Steve McKay | 30535bc | 2016-11-04 14:16:58 -0700 | [diff] [blame] | 19 | import static com.android.documentsui.base.Shared.VERBOSE; |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 20 | |
| 21 | import android.annotation.Nullable; |
| 22 | import android.graphics.drawable.Drawable; |
| 23 | import android.util.Log; |
| 24 | import android.view.View; |
Garfield Tan | d2fc4cd | 2017-03-15 15:39:14 -0700 | [diff] [blame] | 25 | import android.widget.Toolbar; |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 26 | |
Steve McKay | d080506 | 2016-09-15 14:30:38 -0700 | [diff] [blame] | 27 | import com.android.documentsui.base.RootInfo; |
Steve McKay | d9caa6a | 2016-09-15 16:36:45 -0700 | [diff] [blame] | 28 | import com.android.documentsui.base.State; |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 29 | import com.android.documentsui.dirlist.AnimationView; |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 30 | |
Ben Lin | e4ffd06 | 2017-01-19 15:10:49 -0800 | [diff] [blame] | 31 | import java.util.function.IntConsumer; |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 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 | |
Garfield Tan | d2fc4cd | 2017-03-15 15:39:14 -0700 | [diff] [blame] | 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; |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 45 | |
| 46 | public NavigationViewManager( |
| 47 | DrawerController drawer, |
Garfield Tan | d2fc4cd | 2017-03-15 15:39:14 -0700 | [diff] [blame] | 48 | Toolbar toolbar, |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 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 | |
| 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 Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 79 | mState.stack.pop(); |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 80 | } |
| 81 | if (changed) { |
| 82 | mEnv.refreshCurrentRootAndDirectory(AnimationView.ANIM_LEAVE); |
| 83 | } |
| 84 | } |
| 85 | |
Steve McKay | 16e0c1f | 2016-09-15 12:41:13 -0700 | [diff] [blame] | 86 | public void update() { |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 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; |
Steve McKay | 30535bc | 2016-11-04 14:16:58 -0700 | [diff] [blame] | 103 | if (VERBOSE) Log.v(TAG, "New toolbar title is: " + title); |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 104 | mToolbar.setTitle(title); |
| 105 | } else { |
| 106 | mBreadcrumb.show(true); |
| 107 | mToolbar.setTitle(null); |
| 108 | mBreadcrumb.postUpdate(); |
| 109 | } |
| 110 | |
Steve McKay | 30535bc | 2016-11-04 14:16:58 -0700 | [diff] [blame] | 111 | if (VERBOSE) Log.v(TAG, "Final toolbar title is: " + mToolbar.getTitle()); |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 112 | } |
| 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 Lin | e4ffd06 | 2017-01-19 15:10:49 -0800 | [diff] [blame] | 128 | void setup(Environment env, State state, IntConsumer listener); |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 129 | void show(boolean visibility); |
| 130 | void postUpdate(); |
| 131 | } |
| 132 | |
| 133 | interface Environment { |
Steve McKay | eed2f4e | 2016-10-03 20:30:52 -0700 | [diff] [blame] | 134 | @Deprecated // Use CommonAddones#getCurrentRoot |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 135 | RootInfo getCurrentRoot(); |
| 136 | String getDrawerTitle(); |
Steve McKay | 988d8a3 | 2016-09-27 09:41:17 -0700 | [diff] [blame] | 137 | @Deprecated // Use CommonAddones#refreshCurrentRootAndDirectory |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 138 | void refreshCurrentRootAndDirectory(int animation); |
| 139 | boolean isSearchExpanded(); |
| 140 | } |
| 141 | } |