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 | d9caa6a | 2016-09-15 16:36:45 -0700 | [diff] [blame] | 19 | import static com.android.documentsui.base.Shared.DEBUG; |
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; |
| 25 | |
Steve McKay | d080506 | 2016-09-15 14:30:38 -0700 | [diff] [blame] | 26 | import com.android.documentsui.base.RootInfo; |
Steve McKay | d9caa6a | 2016-09-15 16:36:45 -0700 | [diff] [blame] | 27 | import com.android.documentsui.base.State; |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 28 | import com.android.documentsui.dirlist.AnimationView; |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 29 | |
| 30 | import java.util.function.Consumer; |
| 31 | |
| 32 | /** |
| 33 | * A facade over the portions of the app and drawer toolbars. |
| 34 | */ |
| 35 | public 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; |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame^] | 78 | mState.stack.pop(); |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 79 | } |
| 80 | if (changed) { |
| 81 | mEnv.refreshCurrentRootAndDirectory(AnimationView.ANIM_LEAVE); |
| 82 | } |
| 83 | } |
| 84 | |
Steve McKay | 16e0c1f | 2016-09-15 12:41:13 -0700 | [diff] [blame] | 85 | public void update() { |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 86 | |
| 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 McKay | c88f83c | 2016-08-31 12:01:43 -0700 | [diff] [blame] | 102 | if (DEBUG) Log.v(TAG, "New toolbar title is: " + title); |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 103 | mToolbar.setTitle(title); |
| 104 | } else { |
| 105 | mBreadcrumb.show(true); |
| 106 | mToolbar.setTitle(null); |
| 107 | mBreadcrumb.postUpdate(); |
| 108 | } |
| 109 | |
Steve McKay | c88f83c | 2016-08-31 12:01:43 -0700 | [diff] [blame] | 110 | if (DEBUG) Log.v(TAG, "Final toolbar title is: " + mToolbar.getTitle()); |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 111 | } |
| 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 { |
Steve McKay | eed2f4e | 2016-10-03 20:30:52 -0700 | [diff] [blame] | 133 | @Deprecated // Use CommonAddones#getCurrentRoot |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 134 | RootInfo getCurrentRoot(); |
| 135 | String getDrawerTitle(); |
Steve McKay | 988d8a3 | 2016-09-27 09:41:17 -0700 | [diff] [blame] | 136 | @Deprecated // Use CommonAddones#refreshCurrentRootAndDirectory |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 137 | void refreshCurrentRootAndDirectory(int animation); |
| 138 | boolean isSearchExpanded(); |
| 139 | } |
| 140 | } |