Steve McKay | 18d01e8 | 2016-02-03 11:15:57 -0800 | [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 android.view.View.GONE; |
| 20 | import static android.view.View.VISIBLE; |
| 21 | import static com.android.documentsui.Shared.DEBUG; |
Steve McKay | 18d01e8 | 2016-02-03 11:15:57 -0800 | [diff] [blame] | 22 | |
| 23 | import android.annotation.Nullable; |
| 24 | import android.graphics.drawable.Drawable; |
| 25 | import android.util.Log; |
| 26 | import android.view.LayoutInflater; |
| 27 | import android.view.View; |
| 28 | import android.view.ViewGroup; |
| 29 | import android.widget.AdapterView; |
| 30 | import android.widget.AdapterView.OnItemSelectedListener; |
| 31 | import android.widget.BaseAdapter; |
Steve McKay | 18d01e8 | 2016-02-03 11:15:57 -0800 | [diff] [blame] | 32 | import android.widget.Spinner; |
| 33 | import android.widget.TextView; |
| 34 | |
Steve McKay | fb4fd2f | 2016-03-11 10:49:32 -0800 | [diff] [blame] | 35 | import com.android.documentsui.dirlist.AnimationView; |
Steve McKay | 18d01e8 | 2016-02-03 11:15:57 -0800 | [diff] [blame] | 36 | import com.android.documentsui.model.DocumentInfo; |
| 37 | import com.android.documentsui.model.RootInfo; |
| 38 | |
| 39 | /** |
| 40 | * A facade over the portions of the app and drawer toolbars. |
| 41 | */ |
| 42 | class NavigationView { |
| 43 | |
| 44 | private static final String TAG = "NavigationView"; |
| 45 | |
| 46 | private final DrawerController mDrawer; |
| 47 | private final DocumentsToolbar mToolbar; |
| 48 | private final Spinner mBreadcrumb; |
| 49 | private final State mState; |
| 50 | private final NavigationView.Environment mEnv; |
| 51 | private final BreadcrumbAdapter mBreadcrumbAdapter; |
| 52 | |
| 53 | private boolean mIgnoreNextNavigation; |
| 54 | |
| 55 | public NavigationView( |
| 56 | DrawerController drawer, |
| 57 | DocumentsToolbar toolbar, |
| 58 | Spinner breadcrumb, |
| 59 | State state, |
| 60 | NavigationView.Environment env) { |
| 61 | |
| 62 | mToolbar = toolbar; |
| 63 | mBreadcrumb = breadcrumb; |
| 64 | mDrawer = drawer; |
| 65 | mState = state; |
| 66 | mEnv = env; |
| 67 | |
| 68 | mBreadcrumbAdapter = new BreadcrumbAdapter(mState, mEnv); |
| 69 | mToolbar.setNavigationOnClickListener( |
| 70 | new View.OnClickListener() { |
| 71 | @Override |
| 72 | public void onClick(View v) { |
| 73 | onNavigationIconClicked(); |
| 74 | } |
| 75 | |
| 76 | }); |
| 77 | |
| 78 | mBreadcrumb.setOnItemSelectedListener( |
| 79 | new OnItemSelectedListener() { |
| 80 | @Override |
| 81 | public void onItemSelected( |
| 82 | AdapterView<?> parent, View view, int position, long id) { |
| 83 | onBreadcrumbItemSelected(position); |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public void onNothingSelected(AdapterView<?> parent) {} |
| 88 | }); |
| 89 | |
| 90 | } |
| 91 | |
| 92 | private void onNavigationIconClicked() { |
| 93 | if (mDrawer.isPresent()) { |
Aga Wronska | 441b9be | 2016-03-29 16:57:10 -0700 | [diff] [blame] | 94 | mDrawer.setOpen(true, DrawerController.OPENED_HAMBURGER); |
Steve McKay | 18d01e8 | 2016-02-03 11:15:57 -0800 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
| 98 | private void onBreadcrumbItemSelected(int position) { |
| 99 | if (mIgnoreNextNavigation) { |
| 100 | mIgnoreNextNavigation = false; |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | while (mState.stack.size() > position + 1) { |
| 105 | mState.popDocument(); |
| 106 | } |
Steve McKay | fb4fd2f | 2016-03-11 10:49:32 -0800 | [diff] [blame] | 107 | mEnv.refreshCurrentRootAndDirectory(AnimationView.ANIM_LEAVE); |
Steve McKay | 18d01e8 | 2016-02-03 11:15:57 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | void update() { |
| 111 | |
| 112 | // TODO: Looks to me like this block is never getting hit. |
| 113 | if (mEnv.isSearchExpanded()) { |
| 114 | mToolbar.setTitle(null); |
| 115 | mBreadcrumb.setVisibility(View.GONE); |
| 116 | mBreadcrumb.setAdapter(null); |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | mDrawer.setTitle(mEnv.getDrawerTitle()); |
| 121 | |
| 122 | mToolbar.setNavigationIcon(getActionBarIcon()); |
| 123 | mToolbar.setNavigationContentDescription(R.string.drawer_open); |
| 124 | |
| 125 | if (mState.stack.size() <= 1) { |
| 126 | showBreadcrumb(false); |
| 127 | String title = mEnv.getCurrentRoot().title; |
| 128 | if (DEBUG) Log.d(TAG, "New toolbar title is: " + title); |
| 129 | mToolbar.setTitle(title); |
| 130 | } else { |
| 131 | showBreadcrumb(true); |
| 132 | mToolbar.setTitle(null); |
| 133 | mIgnoreNextNavigation = true; |
Tomasz Mikolajewski | 0113028 | 2016-02-09 19:48:49 +0900 | [diff] [blame] | 134 | mBreadcrumb.setSelection(mBreadcrumbAdapter.getCount() - 1, false); |
Steve McKay | 18d01e8 | 2016-02-03 11:15:57 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | if (DEBUG) Log.d(TAG, "Final toolbar title is: " + mToolbar.getTitle()); |
| 138 | } |
| 139 | |
| 140 | private void showBreadcrumb(boolean visibility) { |
| 141 | if (visibility) { |
| 142 | mBreadcrumb.setVisibility(VISIBLE); |
| 143 | mBreadcrumb.setAdapter(mBreadcrumbAdapter); |
| 144 | } else { |
| 145 | mBreadcrumb.setVisibility(GONE); |
| 146 | mBreadcrumb.setAdapter(null); |
| 147 | } |
| 148 | } |
| 149 | |
Tomasz Mikolajewski | 8e0c4e3 | 2016-04-12 12:56:57 +0900 | [diff] [blame] | 150 | // Hamburger if drawer is present, else sad nullness. |
Steve McKay | 18d01e8 | 2016-02-03 11:15:57 -0800 | [diff] [blame] | 151 | private @Nullable Drawable getActionBarIcon() { |
| 152 | if (mDrawer.isPresent()) { |
| 153 | return mToolbar.getContext().getDrawable(R.drawable.ic_hamburger); |
| 154 | } else { |
Tomasz Mikolajewski | 8e0c4e3 | 2016-04-12 12:56:57 +0900 | [diff] [blame] | 155 | return null; |
Steve McKay | 18d01e8 | 2016-02-03 11:15:57 -0800 | [diff] [blame] | 156 | } |
Steve McKay | 18d01e8 | 2016-02-03 11:15:57 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | void revealRootsDrawer(boolean open) { |
| 160 | mDrawer.setOpen(open); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Class providing toolbar with runtime access to useful activity data. |
| 165 | */ |
| 166 | static final class BreadcrumbAdapter extends BaseAdapter { |
| 167 | |
| 168 | private Environment mEnv; |
| 169 | private State mState; |
| 170 | |
| 171 | public BreadcrumbAdapter(State state, Environment env) { |
| 172 | mState = state; |
| 173 | mEnv = env; |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public int getCount() { |
| 178 | return mState.stack.size(); |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public DocumentInfo getItem(int position) { |
| 183 | return mState.stack.get(mState.stack.size() - position - 1); |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public long getItemId(int position) { |
| 188 | return position; |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public View getView(int position, View convertView, ViewGroup parent) { |
| 193 | if (convertView == null) { |
| 194 | convertView = LayoutInflater.from(parent.getContext()) |
| 195 | .inflate(R.layout.item_subdir_title, parent, false); |
| 196 | } |
| 197 | |
| 198 | final TextView title = (TextView) convertView.findViewById(android.R.id.title); |
| 199 | final DocumentInfo doc = getItem(position); |
| 200 | |
| 201 | if (position == 0) { |
| 202 | final RootInfo root = mEnv.getCurrentRoot(); |
| 203 | title.setText(root.title); |
| 204 | } else { |
| 205 | title.setText(doc.displayName); |
| 206 | } |
| 207 | |
| 208 | return convertView; |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public View getDropDownView(int position, View convertView, ViewGroup parent) { |
| 213 | if (convertView == null) { |
| 214 | convertView = LayoutInflater.from(parent.getContext()) |
| 215 | .inflate(R.layout.item_subdir, parent, false); |
| 216 | } |
| 217 | |
Steve McKay | 18d01e8 | 2016-02-03 11:15:57 -0800 | [diff] [blame] | 218 | final TextView title = (TextView) convertView.findViewById(android.R.id.title); |
| 219 | final DocumentInfo doc = getItem(position); |
| 220 | |
| 221 | if (position == 0) { |
| 222 | final RootInfo root = mEnv.getCurrentRoot(); |
| 223 | title.setText(root.title); |
Steve McKay | 18d01e8 | 2016-02-03 11:15:57 -0800 | [diff] [blame] | 224 | } else { |
| 225 | title.setText(doc.displayName); |
Steve McKay | 18d01e8 | 2016-02-03 11:15:57 -0800 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | return convertView; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | interface Environment { |
| 233 | RootInfo getCurrentRoot(); |
| 234 | String getDrawerTitle(); |
| 235 | void refreshCurrentRootAndDirectory(int animation); |
| 236 | boolean isSearchExpanded(); |
| 237 | } |
| 238 | } |