Steve McKay | 6bbed4d | 2015-08-17 13:18:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
Felipe Leme | 9de5807 | 2018-01-19 16:40:04 -0800 | [diff] [blame] | 19 | import static com.android.documentsui.base.SharedMinimal.DEBUG; |
Aga Wronska | c32a299 | 2016-03-08 11:20:39 -0800 | [diff] [blame] | 20 | |
Jeff Sharkey | a4ff00f | 2018-07-09 14:57:51 -0600 | [diff] [blame] | 21 | import androidx.annotation.IntDef; |
Steve McKay | 6bbed4d | 2015-08-17 13:18:05 -0700 | [diff] [blame] | 22 | import android.app.Activity; |
KOUSHIK PANUGANTI | 6ca7acc | 2018-04-17 16:00:10 -0700 | [diff] [blame] | 23 | import androidx.annotation.ColorRes; |
Tony Huang | 8d8d92f | 2018-09-13 14:41:16 +0800 | [diff] [blame] | 24 | import androidx.appcompat.widget.Toolbar; |
KOUSHIK PANUGANTI | 6ca7acc | 2018-04-17 16:00:10 -0700 | [diff] [blame] | 25 | import androidx.legacy.app.ActionBarDrawerToggle; |
| 26 | import androidx.drawerlayout.widget.DrawerLayout; |
| 27 | import androidx.drawerlayout.widget.DrawerLayout.DrawerListener; |
Aga Wronska | c32a299 | 2016-03-08 11:20:39 -0800 | [diff] [blame] | 28 | import android.util.Log; |
Steve McKay | 6bbed4d | 2015-08-17 13:18:05 -0700 | [diff] [blame] | 29 | import android.view.View; |
| 30 | |
Steve McKay | d9caa6a | 2016-09-15 16:36:45 -0700 | [diff] [blame] | 31 | import com.android.documentsui.base.Display; |
| 32 | |
Aga Wronska | 441b9be | 2016-03-29 16:57:10 -0700 | [diff] [blame] | 33 | import java.lang.annotation.Retention; |
| 34 | import java.lang.annotation.RetentionPolicy; |
| 35 | |
Steve McKay | 6bbed4d | 2015-08-17 13:18:05 -0700 | [diff] [blame] | 36 | /** |
| 37 | * A facade over the various pieces comprising "roots fragment in a Drawer". |
| 38 | * |
| 39 | * @see DrawerController#create(DrawerLayout) |
| 40 | */ |
Steve McKay | 16e0c1f | 2016-09-15 12:41:13 -0700 | [diff] [blame] | 41 | public abstract class DrawerController implements DrawerListener { |
Aga Wronska | c32a299 | 2016-03-08 11:20:39 -0800 | [diff] [blame] | 42 | public static final String TAG = "DrawerController"; |
| 43 | |
Steve McKay | 16e0c1f | 2016-09-15 12:41:13 -0700 | [diff] [blame] | 44 | public abstract void update(); |
Steve McKay | 16e0c1f | 2016-09-15 12:41:13 -0700 | [diff] [blame] | 45 | public abstract void setOpen(boolean open); |
Steve McKay | 16e0c1f | 2016-09-15 12:41:13 -0700 | [diff] [blame] | 46 | public abstract boolean isPresent(); |
| 47 | public abstract boolean isOpen(); |
Steve McKay | 18d01e8 | 2016-02-03 11:15:57 -0800 | [diff] [blame] | 48 | abstract void setTitle(String title); |
Steve McKay | 6bbed4d | 2015-08-17 13:18:05 -0700 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * Returns a controller suitable for {@code Layout}. |
| 52 | */ |
Garfield Tan | 208945c | 2016-10-04 14:36:38 -0700 | [diff] [blame] | 53 | public static DrawerController create(Activity activity, ActivityConfig activityConfig) { |
Steve McKay | 6bbed4d | 2015-08-17 13:18:05 -0700 | [diff] [blame] | 54 | |
| 55 | DrawerLayout layout = (DrawerLayout) activity.findViewById(R.id.drawer_layout); |
| 56 | |
| 57 | if (layout == null) { |
| 58 | return new DummyDrawerController(); |
| 59 | } |
| 60 | |
| 61 | View drawer = activity.findViewById(R.id.drawer_roots); |
Steve McKay | 18d01e8 | 2016-02-03 11:15:57 -0800 | [diff] [blame] | 62 | Toolbar toolbar = (Toolbar) activity.findViewById(R.id.roots_toolbar); |
Ivan Chiang | b7543de | 2018-10-23 14:20:22 +0800 | [diff] [blame] | 63 | toolbar.setTitleTextColor(activity.getResources().getColor(R.color.drawer_title_color)); |
Aga Wronska | e20925f | 2016-04-06 13:57:28 -0700 | [diff] [blame] | 64 | |
Aga Wronska | c32a299 | 2016-03-08 11:20:39 -0800 | [diff] [blame] | 65 | drawer.getLayoutParams().width = calculateDrawerWidth(activity); |
| 66 | |
Steve McKay | 6bbed4d | 2015-08-17 13:18:05 -0700 | [diff] [blame] | 67 | ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( |
| 68 | activity, |
| 69 | layout, |
| 70 | R.drawable.ic_hamburger, |
| 71 | R.string.drawer_open, |
| 72 | R.string.drawer_close); |
| 73 | |
Garfield Tan | 208945c | 2016-10-04 14:36:38 -0700 | [diff] [blame] | 74 | return new RuntimeDrawerController(layout, drawer, toggle, toolbar, activityConfig); |
Steve McKay | 6bbed4d | 2015-08-17 13:18:05 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Returns a controller suitable for {@code Layout}. |
| 79 | */ |
| 80 | static DrawerController createDummy() { |
| 81 | return new DummyDrawerController(); |
| 82 | } |
| 83 | |
Aga Wronska | c32a299 | 2016-03-08 11:20:39 -0800 | [diff] [blame] | 84 | private static int calculateDrawerWidth(Activity activity) { |
| 85 | // Material design specification for navigation drawer: |
| 86 | // https://www.google.com/design/spec/patterns/navigation-drawer.html |
| 87 | float width = Display.screenWidth(activity) - Display.actionBarHeight(activity); |
| 88 | float maxWidth = activity.getResources().getDimension(R.dimen.max_drawer_width); |
| 89 | int finalWidth = (int) ((width > maxWidth ? maxWidth : width)); |
| 90 | |
| 91 | if (DEBUG) |
| 92 | Log.d(TAG, "Calculated drawer width:" + (finalWidth / Display.density(activity))); |
| 93 | |
| 94 | return finalWidth; |
| 95 | } |
| 96 | |
Steve McKay | 6bbed4d | 2015-08-17 13:18:05 -0700 | [diff] [blame] | 97 | /** |
| 98 | * Runtime controller that manages a real drawer. |
| 99 | */ |
Garfield, Tan | 7d66a86 | 2016-05-11 10:28:41 -0700 | [diff] [blame] | 100 | private static final class RuntimeDrawerController extends DrawerController |
| 101 | implements ItemDragListener.DragHost { |
Ben Lin | b1ab696 | 2017-03-14 10:48:57 -0700 | [diff] [blame] | 102 | private static final int SPRING_TIMEOUT = 750; |
Steve McKay | 6bbed4d | 2015-08-17 13:18:05 -0700 | [diff] [blame] | 103 | private final ActionBarDrawerToggle mToggle; |
| 104 | private DrawerLayout mLayout; |
| 105 | private View mDrawer; |
Steve McKay | 18d01e8 | 2016-02-03 11:15:57 -0800 | [diff] [blame] | 106 | private Toolbar mToolbar; |
Steve McKay | 6bbed4d | 2015-08-17 13:18:05 -0700 | [diff] [blame] | 107 | |
| 108 | public RuntimeDrawerController( |
Garfield Tan | 208945c | 2016-10-04 14:36:38 -0700 | [diff] [blame] | 109 | DrawerLayout layout, |
| 110 | View drawer, |
| 111 | ActionBarDrawerToggle toggle, |
| 112 | Toolbar drawerToolbar, |
| 113 | ActivityConfig activityConfig) { |
Steve McKay | 18d01e8 | 2016-02-03 11:15:57 -0800 | [diff] [blame] | 114 | mToolbar = drawerToolbar; |
Steve McKay | 0af8afd | 2016-02-25 13:34:03 -0800 | [diff] [blame] | 115 | assert(layout != null); |
Steve McKay | 6bbed4d | 2015-08-17 13:18:05 -0700 | [diff] [blame] | 116 | |
| 117 | mLayout = layout; |
| 118 | mDrawer = drawer; |
| 119 | mToggle = toggle; |
| 120 | |
| 121 | mLayout.setDrawerListener(this); |
Garfield, Tan | 7d66a86 | 2016-05-11 10:28:41 -0700 | [diff] [blame] | 122 | |
Garfield Tan | 208945c | 2016-10-04 14:36:38 -0700 | [diff] [blame] | 123 | if (activityConfig.dragAndDropEnabled()) { |
| 124 | View edge = layout.findViewById(R.id.drawer_edge); |
Ben Lin | b1ab696 | 2017-03-14 10:48:57 -0700 | [diff] [blame] | 125 | edge.setOnDragListener(new ItemDragListener<>(this, SPRING_TIMEOUT)); |
Garfield Tan | 208945c | 2016-10-04 14:36:38 -0700 | [diff] [blame] | 126 | } |
Garfield, Tan | 7d66a86 | 2016-05-11 10:28:41 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public void runOnUiThread(Runnable runnable) { |
| 131 | mDrawer.post(runnable); |
| 132 | } |
| 133 | |
| 134 | @Override |
Garfield Tan | da2c0f0 | 2017-04-11 13:47:58 -0700 | [diff] [blame] | 135 | public void setDropTargetHighlight(View v, boolean highlight) { |
Ben Lin | 693a352 | 2017-03-07 16:00:29 -0800 | [diff] [blame] | 136 | assert (v.getId() == R.id.drawer_edge); |
| 137 | |
| 138 | @ColorRes int id = highlight ? R.color.item_doc_background_selected : |
| 139 | android.R.color.transparent; |
| 140 | v.setBackgroundColor(id); |
Ben Lin | 7f72a3c | 2016-09-27 16:37:28 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | @Override |
Garfield Tan | da2c0f0 | 2017-04-11 13:47:58 -0700 | [diff] [blame] | 144 | public void onDragEntered(View v) { |
Ben Lin | 693a352 | 2017-03-07 16:00:29 -0800 | [diff] [blame] | 145 | // do nothing; let drawer only open for onViewHovered |
Garfield, Tan | 7d66a86 | 2016-05-11 10:28:41 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | @Override |
Garfield Tan | da2c0f0 | 2017-04-11 13:47:58 -0700 | [diff] [blame] | 149 | public void onDragExited(View v) { |
Ben Lin | d020212 | 2016-11-10 18:00:12 -0800 | [diff] [blame] | 150 | // do nothing |
| 151 | } |
| 152 | |
| 153 | @Override |
Garfield, Tan | 7d66a86 | 2016-05-11 10:28:41 -0700 | [diff] [blame] | 154 | public void onViewHovered(View v) { |
Ben Lin | 693a352 | 2017-03-07 16:00:29 -0800 | [diff] [blame] | 155 | assert (v.getId() == R.id.drawer_edge); |
| 156 | |
| 157 | setOpen(true); |
Steve McKay | 6bbed4d | 2015-08-17 13:18:05 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | @Override |
Garfield Tan | da2c0f0 | 2017-04-11 13:47:58 -0700 | [diff] [blame] | 161 | public void onDragEnded() { |
| 162 | // do nothing |
| 163 | } |
| 164 | |
| 165 | @Override |
Steve McKay | 16e0c1f | 2016-09-15 12:41:13 -0700 | [diff] [blame] | 166 | public void setOpen(boolean open) { |
Steve McKay | 6bbed4d | 2015-08-17 13:18:05 -0700 | [diff] [blame] | 167 | if (open) { |
| 168 | mLayout.openDrawer(mDrawer); |
| 169 | } else { |
| 170 | mLayout.closeDrawer(mDrawer); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | @Override |
Steve McKay | 16e0c1f | 2016-09-15 12:41:13 -0700 | [diff] [blame] | 175 | public boolean isOpen() { |
Steve McKay | 6bbed4d | 2015-08-17 13:18:05 -0700 | [diff] [blame] | 176 | return mLayout.isDrawerOpen(mDrawer); |
| 177 | } |
| 178 | |
| 179 | @Override |
Steve McKay | 16e0c1f | 2016-09-15 12:41:13 -0700 | [diff] [blame] | 180 | public boolean isPresent() { |
Steve McKay | 0fbfc65 | 2015-08-20 16:48:49 -0700 | [diff] [blame] | 181 | return true; |
| 182 | } |
| 183 | |
| 184 | @Override |
Steve McKay | 18d01e8 | 2016-02-03 11:15:57 -0800 | [diff] [blame] | 185 | void setTitle(String title) { |
| 186 | mToolbar.setTitle(title); |
| 187 | } |
| 188 | |
| 189 | @Override |
Steve McKay | 16e0c1f | 2016-09-15 12:41:13 -0700 | [diff] [blame] | 190 | public void update() { |
Steve McKay | 6bbed4d | 2015-08-17 13:18:05 -0700 | [diff] [blame] | 191 | mToggle.syncState(); |
| 192 | } |
| 193 | |
| 194 | @Override |
Steve McKay | 6bbed4d | 2015-08-17 13:18:05 -0700 | [diff] [blame] | 195 | public void onDrawerSlide(View drawerView, float slideOffset) { |
| 196 | mToggle.onDrawerSlide(drawerView, slideOffset); |
| 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public void onDrawerOpened(View drawerView) { |
| 201 | mToggle.onDrawerOpened(drawerView); |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public void onDrawerClosed(View drawerView) { |
| 206 | mToggle.onDrawerClosed(drawerView); |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public void onDrawerStateChanged(int newState) { |
| 211 | mToggle.onDrawerStateChanged(newState); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | /* |
| 216 | * Dummy controller useful with clients that don't host a real drawer. |
| 217 | */ |
| 218 | private static final class DummyDrawerController extends DrawerController { |
| 219 | |
| 220 | @Override |
Steve McKay | 16e0c1f | 2016-09-15 12:41:13 -0700 | [diff] [blame] | 221 | public void setOpen(boolean open) {} |
Steve McKay | 6bbed4d | 2015-08-17 13:18:05 -0700 | [diff] [blame] | 222 | |
Aga Wronska | 441b9be | 2016-03-29 16:57:10 -0700 | [diff] [blame] | 223 | @Override |
Steve McKay | 16e0c1f | 2016-09-15 12:41:13 -0700 | [diff] [blame] | 224 | public boolean isOpen() { |
Steve McKay | 0fbfc65 | 2015-08-20 16:48:49 -0700 | [diff] [blame] | 225 | return false; |
| 226 | } |
| 227 | |
| 228 | @Override |
Steve McKay | 16e0c1f | 2016-09-15 12:41:13 -0700 | [diff] [blame] | 229 | public boolean isPresent() { |
Steve McKay | 0fbfc65 | 2015-08-20 16:48:49 -0700 | [diff] [blame] | 230 | return false; |
| 231 | } |
| 232 | |
| 233 | @Override |
Steve McKay | 18d01e8 | 2016-02-03 11:15:57 -0800 | [diff] [blame] | 234 | void setTitle(String title) {} |
| 235 | |
| 236 | @Override |
Steve McKay | 16e0c1f | 2016-09-15 12:41:13 -0700 | [diff] [blame] | 237 | public void update() {} |
Steve McKay | 6bbed4d | 2015-08-17 13:18:05 -0700 | [diff] [blame] | 238 | |
| 239 | @Override |
| 240 | public void onDrawerSlide(View drawerView, float slideOffset) {} |
| 241 | |
| 242 | @Override |
| 243 | public void onDrawerOpened(View drawerView) {} |
| 244 | |
| 245 | @Override |
| 246 | public void onDrawerClosed(View drawerView) {} |
| 247 | |
| 248 | @Override |
| 249 | public void onDrawerStateChanged(int newState) {} |
Steve McKay | 6bbed4d | 2015-08-17 13:18:05 -0700 | [diff] [blame] | 250 | } |
| 251 | } |