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 android.content.Context; |
| 20 | import android.support.v7.widget.LinearLayoutManager; |
| 21 | import android.support.v7.widget.RecyclerView; |
| 22 | import android.util.AttributeSet; |
| 23 | import android.view.GestureDetector; |
| 24 | import android.view.LayoutInflater; |
| 25 | import android.view.MotionEvent; |
| 26 | import android.view.View; |
| 27 | import android.view.ViewGroup; |
| 28 | import android.widget.ImageView; |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 29 | |
| 30 | import com.android.documentsui.NavigationViewManager.Breadcrumb; |
| 31 | import com.android.documentsui.NavigationViewManager.Environment; |
Steve McKay | d080506 | 2016-09-15 14:30:38 -0700 | [diff] [blame] | 32 | import com.android.documentsui.base.DocumentInfo; |
| 33 | import com.android.documentsui.base.RootInfo; |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 34 | |
| 35 | import java.util.function.Consumer; |
| 36 | |
| 37 | /** |
| 38 | * Horizontal implementation of breadcrumb used for tablet / desktop device layouts |
| 39 | */ |
Ben Lin | ba1cb3d | 2016-06-20 17:08:22 -0700 | [diff] [blame] | 40 | public final class HorizontalBreadcrumb extends RecyclerView |
| 41 | implements Breadcrumb, ItemDragListener.DragHost { |
| 42 | |
| 43 | private static final int USER_NO_SCROLL_OFFSET_THRESHOLD = 5; |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 44 | |
| 45 | private LinearLayoutManager mLayoutManager; |
| 46 | private BreadcrumbAdapter mAdapter; |
| 47 | private Consumer<Integer> mListener; |
| 48 | |
| 49 | public HorizontalBreadcrumb(Context context, AttributeSet attrs, int defStyleAttr) { |
| 50 | super(context, attrs, defStyleAttr); |
| 51 | } |
| 52 | |
| 53 | public HorizontalBreadcrumb(Context context, AttributeSet attrs) { |
| 54 | super(context, attrs); |
| 55 | } |
| 56 | |
| 57 | public HorizontalBreadcrumb(Context context) { |
| 58 | super(context); |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public void setup(Environment env, |
Steve McKay | d9caa6a | 2016-09-15 16:36:45 -0700 | [diff] [blame^] | 63 | com.android.documentsui.base.State state, |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 64 | Consumer<Integer> listener) { |
| 65 | |
| 66 | mListener = listener; |
| 67 | mLayoutManager = new LinearLayoutManager( |
| 68 | getContext(), LinearLayoutManager.HORIZONTAL, false); |
Ben Lin | ba1cb3d | 2016-06-20 17:08:22 -0700 | [diff] [blame] | 69 | mAdapter = new BreadcrumbAdapter( |
| 70 | state, env, new ItemDragListener<>(this)); |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 71 | |
| 72 | setLayoutManager(mLayoutManager); |
| 73 | addOnItemTouchListener(new ClickListener(getContext(), this::onSingleTapUp)); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public void show(boolean visibility) { |
| 78 | if (visibility) { |
| 79 | setVisibility(VISIBLE); |
Ben Lin | ba1cb3d | 2016-06-20 17:08:22 -0700 | [diff] [blame] | 80 | boolean shouldScroll = !hasUserDefineScrollOffset(); |
| 81 | if (getAdapter() == null) { |
| 82 | setAdapter(mAdapter); |
| 83 | } else { |
| 84 | int currentItemCount = mAdapter.getItemCount(); |
| 85 | int lastItemCount = mAdapter.getLastItemSize(); |
| 86 | if (currentItemCount > lastItemCount) { |
| 87 | mAdapter.notifyItemRangeInserted(lastItemCount, |
| 88 | currentItemCount - lastItemCount); |
| 89 | mAdapter.notifyItemChanged(lastItemCount - 1); |
| 90 | } else if (currentItemCount < lastItemCount) { |
| 91 | mAdapter.notifyItemRangeRemoved(currentItemCount, |
| 92 | lastItemCount - currentItemCount); |
| 93 | mAdapter.notifyItemChanged(currentItemCount - 1); |
| 94 | } |
| 95 | } |
| 96 | if (shouldScroll) { |
| 97 | mLayoutManager.scrollToPosition(mAdapter.getItemCount() - 1); |
| 98 | } |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 99 | } else { |
| 100 | setVisibility(GONE); |
| 101 | setAdapter(null); |
| 102 | } |
Ben Lin | ba1cb3d | 2016-06-20 17:08:22 -0700 | [diff] [blame] | 103 | mAdapter.updateLastItemSize(); |
| 104 | } |
| 105 | |
| 106 | private boolean hasUserDefineScrollOffset() { |
| 107 | final int maxOffset = computeHorizontalScrollRange() - computeHorizontalScrollExtent(); |
| 108 | return (maxOffset - computeHorizontalScrollOffset() > USER_NO_SCROLL_OFFSET_THRESHOLD); |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public void postUpdate() { |
| 113 | } |
| 114 | |
Ben Lin | ba1cb3d | 2016-06-20 17:08:22 -0700 | [diff] [blame] | 115 | @Override |
| 116 | public void runOnUiThread(Runnable runnable) { |
| 117 | post(runnable); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public void setDropTargetHighlight(View v, boolean highlight) { |
| 122 | RecyclerView.ViewHolder vh = getChildViewHolder(v); |
| 123 | if (vh instanceof BreadcrumbHolder) { |
| 124 | ((BreadcrumbHolder) vh).setHighlighted(highlight); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public void onViewHovered(View v) { |
| 130 | int pos = getChildAdapterPosition(v); |
| 131 | if (pos != mAdapter.getItemCount() - 1) { |
| 132 | mListener.accept(pos); |
| 133 | } |
| 134 | } |
| 135 | |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 136 | private void onSingleTapUp(MotionEvent e) { |
| 137 | View itemView = findChildViewUnder(e.getX(), e.getY()); |
| 138 | int pos = getChildAdapterPosition(itemView); |
| 139 | if (pos != mAdapter.getItemCount() - 1) { |
| 140 | mListener.accept(pos); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | private static final class BreadcrumbAdapter |
| 145 | extends RecyclerView.Adapter<BreadcrumbHolder> { |
| 146 | |
Ben Lin | ba1cb3d | 2016-06-20 17:08:22 -0700 | [diff] [blame] | 147 | private final Environment mEnv; |
Steve McKay | d9caa6a | 2016-09-15 16:36:45 -0700 | [diff] [blame^] | 148 | private final com.android.documentsui.base.State mState; |
Ben Lin | ba1cb3d | 2016-06-20 17:08:22 -0700 | [diff] [blame] | 149 | private final OnDragListener mDragListener; |
| 150 | // We keep the old item size so the breadcrumb will only re-render views that are necessary |
| 151 | private int mLastItemSize; |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 152 | |
Steve McKay | d9caa6a | 2016-09-15 16:36:45 -0700 | [diff] [blame^] | 153 | public BreadcrumbAdapter(com.android.documentsui.base.State state, |
Ben Lin | ba1cb3d | 2016-06-20 17:08:22 -0700 | [diff] [blame] | 154 | Environment env, |
| 155 | OnDragListener dragListener) { |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 156 | mState = state; |
| 157 | mEnv = env; |
Ben Lin | ba1cb3d | 2016-06-20 17:08:22 -0700 | [diff] [blame] | 158 | mDragListener = dragListener; |
| 159 | mLastItemSize = mState.stack.size(); |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public BreadcrumbHolder onCreateViewHolder(ViewGroup parent, int viewType) { |
| 164 | View v = LayoutInflater.from(parent.getContext()) |
| 165 | .inflate(R.layout.navigation_breadcrumb_item, null); |
| 166 | return new BreadcrumbHolder(v); |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public void onBindViewHolder(BreadcrumbHolder holder, int position) { |
| 171 | final DocumentInfo doc = getItem(position); |
Ben Lin | 9cad635 | 2016-07-06 11:58:05 -0700 | [diff] [blame] | 172 | final int horizontalPadding = (int) holder.itemView.getResources() |
| 173 | .getDimension(R.dimen.breadcrumb_item_padding); |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 174 | |
| 175 | if (position == 0) { |
| 176 | final RootInfo root = mEnv.getCurrentRoot(); |
| 177 | holder.title.setText(root.title); |
Ben Lin | 9cad635 | 2016-07-06 11:58:05 -0700 | [diff] [blame] | 178 | holder.title.setPadding(0, 0, horizontalPadding, 0); |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 179 | } else { |
| 180 | holder.title.setText(doc.displayName); |
Ben Lin | 9cad635 | 2016-07-06 11:58:05 -0700 | [diff] [blame] | 181 | holder.title.setPadding(horizontalPadding, 0, horizontalPadding, 0); |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | if (position == getItemCount() - 1) { |
| 185 | holder.arrow.setVisibility(View.GONE); |
Ben Lin | ba1cb3d | 2016-06-20 17:08:22 -0700 | [diff] [blame] | 186 | } else { |
| 187 | holder.arrow.setVisibility(View.VISIBLE); |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 188 | } |
Ben Lin | ba1cb3d | 2016-06-20 17:08:22 -0700 | [diff] [blame] | 189 | holder.itemView.setOnDragListener(mDragListener); |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | private DocumentInfo getItem(int position) { |
| 193 | return mState.stack.get(mState.stack.size() - position - 1); |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public int getItemCount() { |
| 198 | return mState.stack.size(); |
| 199 | } |
Ben Lin | ba1cb3d | 2016-06-20 17:08:22 -0700 | [diff] [blame] | 200 | |
| 201 | public int getLastItemSize() { |
| 202 | return mLastItemSize; |
| 203 | } |
| 204 | |
| 205 | public void updateLastItemSize() { |
| 206 | mLastItemSize = mState.stack.size(); |
| 207 | } |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | private static class BreadcrumbHolder extends RecyclerView.ViewHolder { |
| 211 | |
Ben Lin | ba1cb3d | 2016-06-20 17:08:22 -0700 | [diff] [blame] | 212 | protected DragOverTextView title; |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 213 | protected ImageView arrow; |
| 214 | |
| 215 | public BreadcrumbHolder(View itemView) { |
| 216 | super(itemView); |
Ben Lin | ba1cb3d | 2016-06-20 17:08:22 -0700 | [diff] [blame] | 217 | title = (DragOverTextView) itemView.findViewById(R.id.breadcrumb_text); |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 218 | arrow = (ImageView) itemView.findViewById(R.id.breadcrumb_arrow); |
| 219 | } |
Ben Lin | ba1cb3d | 2016-06-20 17:08:22 -0700 | [diff] [blame] | 220 | |
| 221 | /** |
| 222 | * Highlights the associated item view. |
| 223 | * @param highlighted |
| 224 | */ |
| 225 | public void setHighlighted(boolean highlighted) { |
| 226 | title.setHighlight(highlighted); |
| 227 | } |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | private static final class ClickListener extends GestureDetector |
| 231 | implements OnItemTouchListener { |
| 232 | |
| 233 | public ClickListener(Context context, Consumer<MotionEvent> listener) { |
| 234 | super(context, new SimpleOnGestureListener() { |
| 235 | @Override |
| 236 | public boolean onSingleTapUp(MotionEvent e) { |
| 237 | listener.accept(e); |
| 238 | return true; |
| 239 | } |
| 240 | }); |
| 241 | } |
| 242 | |
| 243 | @Override |
| 244 | public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) { |
| 245 | onTouchEvent(e); |
| 246 | return false; |
| 247 | } |
| 248 | |
| 249 | @Override |
| 250 | public void onTouchEvent(RecyclerView rv, MotionEvent e) { |
| 251 | onTouchEvent(e); |
| 252 | } |
| 253 | |
| 254 | @Override |
| 255 | public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { |
| 256 | } |
| 257 | } |
| 258 | } |