Steve McKay | ef28015 | 2015-06-11 10:10:49 -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 | |
Steve McKay | f68210e | 2015-11-03 15:23:16 -0800 | [diff] [blame] | 17 | package com.android.documentsui.dirlist; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 18 | |
Steve McKay | 093d5e7 | 2015-09-03 16:49:51 -0700 | [diff] [blame] | 19 | import static com.android.documentsui.Shared.DEBUG; |
Steve McKay | 8c1b8c8 | 2015-07-28 19:20:01 -0700 | [diff] [blame] | 20 | import static com.android.internal.util.Preconditions.checkArgument; |
Steve McKay | 9459a7c | 2015-07-24 13:14:20 -0700 | [diff] [blame] | 21 | import static com.android.internal.util.Preconditions.checkNotNull; |
| 22 | import static com.android.internal.util.Preconditions.checkState; |
| 23 | |
Steve McKay | 093d5e7 | 2015-09-03 16:49:51 -0700 | [diff] [blame] | 24 | import android.graphics.Point; |
| 25 | import android.graphics.Rect; |
| 26 | import android.graphics.drawable.Drawable; |
| 27 | import android.support.annotation.Nullable; |
| 28 | import android.support.annotation.VisibleForTesting; |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 29 | import android.support.v7.widget.GridLayoutManager; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 30 | import android.support.v7.widget.RecyclerView; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 31 | import android.util.Log; |
Kyle Horimoto | d3b3d3d | 2015-08-25 21:03:38 -0700 | [diff] [blame] | 32 | import android.util.SparseArray; |
Ben Kwa | 936a7fc | 2015-12-10 15:21:18 -0800 | [diff] [blame] | 33 | import android.util.SparseBooleanArray; |
| 34 | import android.util.SparseIntArray; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 35 | import android.view.GestureDetector; |
Ben Kwa | a017bc1 | 2015-10-07 14:15:12 -0700 | [diff] [blame] | 36 | import android.view.KeyEvent; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 37 | import android.view.MotionEvent; |
| 38 | import android.view.View; |
Steve McKay | 093d5e7 | 2015-09-03 16:49:51 -0700 | [diff] [blame] | 39 | |
| 40 | import com.android.documentsui.Events.InputEvent; |
| 41 | import com.android.documentsui.Events.MotionInputEvent; |
Steve McKay | e0cba92 | 2015-11-11 16:26:59 +0900 | [diff] [blame] | 42 | import com.android.documentsui.R; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 43 | |
| 44 | import java.util.ArrayList; |
Ben Kwa | 936a7fc | 2015-12-10 15:21:18 -0800 | [diff] [blame] | 45 | import java.util.Collection; |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 46 | import java.util.Collections; |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 47 | import java.util.HashMap; |
| 48 | import java.util.HashSet; |
| 49 | import java.util.Iterator; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 50 | import java.util.List; |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 51 | import java.util.Map; |
| 52 | import java.util.Set; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 53 | |
| 54 | /** |
Steve McKay | 5739487 | 2015-08-12 14:48:34 -0700 | [diff] [blame] | 55 | * MultiSelectManager provides support traditional multi-item selection support to RecyclerView. |
| 56 | * Additionally it can be configured to restrict selection to a single element, @see |
| 57 | * #setSelectMode. |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 58 | */ |
Ben Kwa | a017bc1 | 2015-10-07 14:15:12 -0700 | [diff] [blame] | 59 | public final class MultiSelectManager implements View.OnKeyListener { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 60 | |
Steve McKay | 5739487 | 2015-08-12 14:48:34 -0700 | [diff] [blame] | 61 | /** Selection mode for multiple select. **/ |
| 62 | public static final int MODE_MULTIPLE = 0; |
| 63 | |
| 64 | /** Selection mode for multiple select. **/ |
| 65 | public static final int MODE_SINGLE = 1; |
| 66 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 67 | private static final String TAG = "MultiSelectManager"; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 68 | |
| 69 | private final Selection mSelection = new Selection(); |
Steve McKay | 8c1b8c8 | 2015-07-28 19:20:01 -0700 | [diff] [blame] | 70 | |
Steve McKay | 93d8ef4 | 2015-07-30 12:27:44 -0700 | [diff] [blame] | 71 | private Range mRanger; |
Ben Kwa | a017bc1 | 2015-10-07 14:15:12 -0700 | [diff] [blame] | 72 | private SelectionEnvironment mEnvironment; |
| 73 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 74 | private final List<MultiSelectManager.Callback> mCallbacks = new ArrayList<>(1); |
| 75 | |
Steve McKay | 5739487 | 2015-08-12 14:48:34 -0700 | [diff] [blame] | 76 | private boolean mSingleSelect; |
Steve McKay | be629f3 | 2015-09-04 14:51:16 -0700 | [diff] [blame] | 77 | |
Ben Kwa | 60d91e4 | 2015-10-20 15:02:33 -0700 | [diff] [blame] | 78 | // Payloads for notifyItemChange to distinguish between selection and other events. |
| 79 | public static final String SELECTION_CHANGED_MARKER = "Selection-Changed"; |
| 80 | |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 81 | @Nullable private BandController mBandManager; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 82 | |
| 83 | /** |
| 84 | * @param recyclerView |
Steve McKay | 5739487 | 2015-08-12 14:48:34 -0700 | [diff] [blame] | 85 | * @param mode Selection mode |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 86 | */ |
Steve McKay | 9eabc50 | 2015-10-19 12:04:21 -0700 | [diff] [blame] | 87 | public MultiSelectManager(final RecyclerView recyclerView, int mode) { |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 88 | this(new RuntimeSelectionEnvironment(recyclerView), mode); |
Ben Kwa | a017bc1 | 2015-10-07 14:15:12 -0700 | [diff] [blame] | 89 | |
Steve McKay | be629f3 | 2015-09-04 14:51:16 -0700 | [diff] [blame] | 90 | if (mode == MODE_MULTIPLE) { |
Steve McKay | 9eabc50 | 2015-10-19 12:04:21 -0700 | [diff] [blame] | 91 | mBandManager = new BandController(); |
Steve McKay | be629f3 | 2015-09-04 14:51:16 -0700 | [diff] [blame] | 92 | } |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 93 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 94 | GestureDetector.SimpleOnGestureListener listener = |
| 95 | new GestureDetector.SimpleOnGestureListener() { |
| 96 | @Override |
| 97 | public boolean onSingleTapUp(MotionEvent e) { |
Steve McKay | 093d5e7 | 2015-09-03 16:49:51 -0700 | [diff] [blame] | 98 | return MultiSelectManager.this.onSingleTapUp( |
| 99 | new MotionInputEvent(e, recyclerView)); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 100 | } |
| 101 | @Override |
| 102 | public void onLongPress(MotionEvent e) { |
Steve McKay | 093d5e7 | 2015-09-03 16:49:51 -0700 | [diff] [blame] | 103 | MultiSelectManager.this.onLongPress( |
| 104 | new MotionInputEvent(e, recyclerView)); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 105 | } |
| 106 | }; |
| 107 | |
Steve McKay | 9eabc50 | 2015-10-19 12:04:21 -0700 | [diff] [blame] | 108 | final GestureDetector detector = new GestureDetector(recyclerView.getContext(), listener); |
| 109 | detector.setOnDoubleTapListener(listener); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 110 | |
| 111 | recyclerView.addOnItemTouchListener( |
| 112 | new RecyclerView.OnItemTouchListener() { |
Steve McKay | 093d5e7 | 2015-09-03 16:49:51 -0700 | [diff] [blame] | 113 | @Override |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 114 | public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) { |
| 115 | detector.onTouchEvent(e); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 116 | |
Steve McKay | 9eabc50 | 2015-10-19 12:04:21 -0700 | [diff] [blame] | 117 | if (mBandManager != null) { |
| 118 | return mBandManager.handleEvent(new MotionInputEvent(e, recyclerView)); |
Steve McKay | be629f3 | 2015-09-04 14:51:16 -0700 | [diff] [blame] | 119 | } |
Steve McKay | 9eabc50 | 2015-10-19 12:04:21 -0700 | [diff] [blame] | 120 | return false; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 121 | } |
Steve McKay | 093d5e7 | 2015-09-03 16:49:51 -0700 | [diff] [blame] | 122 | |
| 123 | @Override |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 124 | public void onTouchEvent(RecyclerView rv, MotionEvent e) { |
Steve McKay | 9eabc50 | 2015-10-19 12:04:21 -0700 | [diff] [blame] | 125 | mBandManager.processInputEvent( |
Steve McKay | 093d5e7 | 2015-09-03 16:49:51 -0700 | [diff] [blame] | 126 | new MotionInputEvent(e, recyclerView)); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 127 | } |
Steve McKay | 093d5e7 | 2015-09-03 16:49:51 -0700 | [diff] [blame] | 128 | @Override |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 129 | public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {} |
| 130 | }); |
| 131 | } |
| 132 | |
Steve McKay | 9459a7c | 2015-07-24 13:14:20 -0700 | [diff] [blame] | 133 | /** |
| 134 | * Constructs a new instance with {@code adapter} and {@code helper}. |
Steve McKay | e0cba92 | 2015-11-11 16:26:59 +0900 | [diff] [blame] | 135 | * @param runtimeSelectionEnvironment |
Steve McKay | 9459a7c | 2015-07-24 13:14:20 -0700 | [diff] [blame] | 136 | * @hide |
| 137 | */ |
| 138 | @VisibleForTesting |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 139 | MultiSelectManager(SelectionEnvironment environment, int mode) { |
Steve McKay | e0cba92 | 2015-11-11 16:26:59 +0900 | [diff] [blame] | 140 | mEnvironment = checkNotNull(environment, "'environment' cannot be null."); |
Steve McKay | 5739487 | 2015-08-12 14:48:34 -0700 | [diff] [blame] | 141 | mSingleSelect = mode == MODE_SINGLE; |
| 142 | |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 143 | mEnvironment.registerDataObserver( |
| 144 | new RecyclerView.AdapterDataObserver() { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 145 | |
Ben Kwa | 936a7fc | 2015-12-10 15:21:18 -0800 | [diff] [blame] | 146 | private List<String> mModelIds; |
Ben Kwa | da858bf | 2015-12-09 14:33:49 -0800 | [diff] [blame] | 147 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 148 | @Override |
| 149 | public void onChanged() { |
Ben Kwa | 936a7fc | 2015-12-10 15:21:18 -0800 | [diff] [blame] | 150 | // TODO: This is causing b/22765812 |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 151 | mSelection.clear(); |
Ben Kwa | 936a7fc | 2015-12-10 15:21:18 -0800 | [diff] [blame] | 152 | mModelIds = mEnvironment.getModelIds(); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public void onItemRangeChanged( |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 157 | int startPosition, int itemCount, Object payload) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 158 | // No change in position. Ignoring. |
| 159 | } |
| 160 | |
| 161 | @Override |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 162 | public void onItemRangeInserted(int startPosition, int itemCount) { |
| 163 | mSelection.cancelProvisionalSelection(); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | @Override |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 167 | public void onItemRangeRemoved(int startPosition, int itemCount) { |
| 168 | checkState(startPosition >= 0); |
| 169 | checkState(itemCount > 0); |
Ben Kwa | 936a7fc | 2015-12-10 15:21:18 -0800 | [diff] [blame] | 170 | |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 171 | mSelection.cancelProvisionalSelection(); |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 172 | // Remove any disappeared IDs from the selection. |
Ben Kwa | 936a7fc | 2015-12-10 15:21:18 -0800 | [diff] [blame] | 173 | mSelection.intersect(mModelIds); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) { |
| 178 | throw new UnsupportedOperationException(); |
| 179 | } |
| 180 | }); |
| 181 | } |
| 182 | |
Steve McKay | 9459a7c | 2015-07-24 13:14:20 -0700 | [diff] [blame] | 183 | /** |
| 184 | * Adds {@code callback} such that it will be notified when {@code MultiSelectManager} |
| 185 | * events occur. |
| 186 | * |
| 187 | * @param callback |
| 188 | */ |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 189 | public void addCallback(MultiSelectManager.Callback callback) { |
| 190 | mCallbacks.add(callback); |
| 191 | } |
| 192 | |
Ben Kwa | 98bb6cf | 2015-10-14 08:00:27 -0700 | [diff] [blame] | 193 | public boolean hasSelection() { |
| 194 | return !mSelection.isEmpty(); |
| 195 | } |
| 196 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 197 | /** |
| 198 | * Returns a Selection object that provides a live view |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 199 | * on the current selection. |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 200 | * |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 201 | * @see #getSelection(Selection) on how to get a snapshot |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 202 | * of the selection that will not reflect future changes |
| 203 | * to selection. |
| 204 | * |
Ben Kwa | 24be5d3 | 2015-08-27 16:04:46 -0700 | [diff] [blame] | 205 | * @return The current selection. |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 206 | */ |
| 207 | public Selection getSelection() { |
| 208 | return mSelection; |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Updates {@code dest} to reflect the current selection. |
| 213 | * @param dest |
| 214 | * |
| 215 | * @return The Selection instance passed in, for convenience. |
| 216 | */ |
| 217 | public Selection getSelection(Selection dest) { |
| 218 | dest.copyFrom(mSelection); |
| 219 | return dest; |
| 220 | } |
| 221 | |
Steve McKay | 9459a7c | 2015-07-24 13:14:20 -0700 | [diff] [blame] | 222 | /** |
Steve McKay | 5739487 | 2015-08-12 14:48:34 -0700 | [diff] [blame] | 223 | * Sets the selected state of the specified items. Note that the callback will NOT |
| 224 | * be consulted to see if an item can be selected. |
| 225 | * |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 226 | * @param ids |
| 227 | * @param selected |
| 228 | * @return |
Steve McKay | 9459a7c | 2015-07-24 13:14:20 -0700 | [diff] [blame] | 229 | */ |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 230 | public boolean setItemsSelected(Iterable<String> ids, boolean selected) { |
Steve McKay | 9459a7c | 2015-07-24 13:14:20 -0700 | [diff] [blame] | 231 | boolean changed = false; |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 232 | for (String id: ids) { |
| 233 | boolean itemChanged = selected ? mSelection.add(id) : mSelection.remove(id); |
Steve McKay | 5739487 | 2015-08-12 14:48:34 -0700 | [diff] [blame] | 234 | if (itemChanged) { |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 235 | notifyItemStateChanged(id, selected); |
Steve McKay | 5739487 | 2015-08-12 14:48:34 -0700 | [diff] [blame] | 236 | } |
| 237 | changed |= itemChanged; |
Steve McKay | 9459a7c | 2015-07-24 13:14:20 -0700 | [diff] [blame] | 238 | } |
Steve McKay | 5739487 | 2015-08-12 14:48:34 -0700 | [diff] [blame] | 239 | notifySelectionChanged(); |
Steve McKay | 9459a7c | 2015-07-24 13:14:20 -0700 | [diff] [blame] | 240 | return changed; |
| 241 | } |
| 242 | |
| 243 | /** |
Steve McKay | 5739487 | 2015-08-12 14:48:34 -0700 | [diff] [blame] | 244 | * Clears the selection and notifies (even if nothing changes). |
Steve McKay | 9459a7c | 2015-07-24 13:14:20 -0700 | [diff] [blame] | 245 | */ |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 246 | public void clearSelection() { |
Steve McKay | 5739487 | 2015-08-12 14:48:34 -0700 | [diff] [blame] | 247 | clearSelectionQuietly(); |
| 248 | notifySelectionChanged(); |
| 249 | } |
| 250 | |
Kyle Horimoto | 2da6e4a | 2015-08-27 16:44:00 -0700 | [diff] [blame] | 251 | public void handleLayoutChanged() { |
Steve McKay | be629f3 | 2015-09-04 14:51:16 -0700 | [diff] [blame] | 252 | if (mBandManager != null) { |
| 253 | mBandManager.handleLayoutChanged(); |
| 254 | } |
Kyle Horimoto | 2da6e4a | 2015-08-27 16:44:00 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Steve McKay | 5739487 | 2015-08-12 14:48:34 -0700 | [diff] [blame] | 257 | /** |
| 258 | * Clears the selection, without notifying anyone. |
| 259 | */ |
| 260 | private void clearSelectionQuietly() { |
Steve McKay | 8c1b8c8 | 2015-07-28 19:20:01 -0700 | [diff] [blame] | 261 | mRanger = null; |
| 262 | |
Ben Kwa | 98bb6cf | 2015-10-14 08:00:27 -0700 | [diff] [blame] | 263 | if (!hasSelection()) { |
Steve McKay | 9459a7c | 2015-07-24 13:14:20 -0700 | [diff] [blame] | 264 | return; |
| 265 | } |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 266 | |
| 267 | Selection intermediateSelection = getSelection(new Selection()); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 268 | mSelection.clear(); |
| 269 | |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 270 | for (String id: intermediateSelection.getAll()) { |
| 271 | notifyItemStateChanged(id, false); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | |
Steve McKay | 093d5e7 | 2015-09-03 16:49:51 -0700 | [diff] [blame] | 275 | @VisibleForTesting |
| 276 | void onLongPress(InputEvent input) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 277 | if (DEBUG) Log.d(TAG, "Handling long press event."); |
| 278 | |
Steve McKay | 093d5e7 | 2015-09-03 16:49:51 -0700 | [diff] [blame] | 279 | if (!input.isOverItem()) { |
| 280 | if (DEBUG) Log.i(TAG, "Cannot handle tap. No adapter position available."); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Steve McKay | 093d5e7 | 2015-09-03 16:49:51 -0700 | [diff] [blame] | 283 | handleAdapterEvent(input); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 286 | @VisibleForTesting |
Steve McKay | 093d5e7 | 2015-09-03 16:49:51 -0700 | [diff] [blame] | 287 | boolean onSingleTapUp(InputEvent input) { |
| 288 | if (DEBUG) Log.d(TAG, "Processing tap event."); |
Ben Kwa | 98bb6cf | 2015-10-14 08:00:27 -0700 | [diff] [blame] | 289 | if (!hasSelection()) { |
Steve McKay | 93d8ef4 | 2015-07-30 12:27:44 -0700 | [diff] [blame] | 290 | // if this is a mouse click on an item, start selection mode. |
Steve McKay | 093d5e7 | 2015-09-03 16:49:51 -0700 | [diff] [blame] | 291 | // TODO: && input.isPrimaryButtonPressed(), but it is returning false. |
| 292 | if (input.isOverItem() && input.isMouseEvent()) { |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 293 | int position = input.getItemPosition(); |
| 294 | toggleSelection(position); |
| 295 | setSelectionRangeBegin(position); |
Steve McKay | 93d8ef4 | 2015-07-30 12:27:44 -0700 | [diff] [blame] | 296 | } |
| 297 | return false; |
| 298 | } |
| 299 | |
Steve McKay | 093d5e7 | 2015-09-03 16:49:51 -0700 | [diff] [blame] | 300 | if (!input.isOverItem()) { |
| 301 | if (DEBUG) Log.d(TAG, "Activity has no position. Canceling selection."); |
Steve McKay | 93d8ef4 | 2015-07-30 12:27:44 -0700 | [diff] [blame] | 302 | clearSelection(); |
| 303 | return false; |
| 304 | } |
| 305 | |
Steve McKay | 093d5e7 | 2015-09-03 16:49:51 -0700 | [diff] [blame] | 306 | handleAdapterEvent(input); |
Steve McKay | 5c86546 | 2015-09-02 15:59:28 -0700 | [diff] [blame] | 307 | return true; |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Handles a change caused by a click on the item with the given position. If the Shift key is |
| 312 | * held down, this performs a range select; otherwise, it simply toggles the item's selection |
| 313 | * state. |
| 314 | */ |
Steve McKay | 093d5e7 | 2015-09-03 16:49:51 -0700 | [diff] [blame] | 315 | private void handleAdapterEvent(InputEvent input) { |
| 316 | if (mRanger != null && input.isShiftKeyDown()) { |
| 317 | mRanger.snapSelection(input.getItemPosition()); |
Steve McKay | 93d8ef4 | 2015-07-30 12:27:44 -0700 | [diff] [blame] | 318 | |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 319 | // We're being lazy here notifying even when something might not have changed. |
| 320 | // To make this more correct, we'd need to update the Ranger class to return |
| 321 | // information about what has changed. |
| 322 | notifySelectionChanged(); |
Ben Kwa | 14a96ce | 2015-09-25 07:47:56 -0700 | [diff] [blame] | 323 | } else { |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 324 | int position = input.getItemPosition(); |
| 325 | toggleSelection(position); |
| 326 | setSelectionRangeBegin(position); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 327 | } |
Steve McKay | 93d8ef4 | 2015-07-30 12:27:44 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | /** |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 331 | * A convenience method for toggling selection by adapter position. |
Steve McKay | 8c1b8c8 | 2015-07-28 19:20:01 -0700 | [diff] [blame] | 332 | * |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 333 | * @param position Adapter position to toggle. |
Steve McKay | 8c1b8c8 | 2015-07-28 19:20:01 -0700 | [diff] [blame] | 334 | */ |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 335 | private void toggleSelection(int position) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 336 | // Position may be special "no position" during certain |
| 337 | // transitional phases. If so, skip handling of the event. |
| 338 | if (position == RecyclerView.NO_POSITION) { |
| 339 | if (DEBUG) Log.d(TAG, "Ignoring toggle for element with no position."); |
Ben Kwa | 14a96ce | 2015-09-25 07:47:56 -0700 | [diff] [blame] | 340 | return; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 341 | } |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 342 | toggleSelection(mEnvironment.getModelIdFromAdapterPosition(position)); |
| 343 | } |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 344 | |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 345 | /** |
| 346 | * Toggles selection on the item with the given model ID. |
| 347 | * |
| 348 | * @param modelId |
| 349 | */ |
| 350 | public void toggleSelection(String modelId) { |
Steve McKay | 5739487 | 2015-08-12 14:48:34 -0700 | [diff] [blame] | 351 | boolean changed = false; |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 352 | if (mSelection.contains(modelId)) { |
| 353 | changed = attemptDeselect(modelId); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 354 | } else { |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 355 | boolean canSelect = notifyBeforeItemStateChange(modelId, true); |
Steve McKay | 5739487 | 2015-08-12 14:48:34 -0700 | [diff] [blame] | 356 | if (!canSelect) { |
Ben Kwa | 14a96ce | 2015-09-25 07:47:56 -0700 | [diff] [blame] | 357 | return; |
Steve McKay | 5739487 | 2015-08-12 14:48:34 -0700 | [diff] [blame] | 358 | } |
Ben Kwa | 98bb6cf | 2015-10-14 08:00:27 -0700 | [diff] [blame] | 359 | if (mSingleSelect && hasSelection()) { |
Steve McKay | 5739487 | 2015-08-12 14:48:34 -0700 | [diff] [blame] | 360 | clearSelectionQuietly(); |
| 361 | } |
| 362 | |
Steve McKay | 8c1b8c8 | 2015-07-28 19:20:01 -0700 | [diff] [blame] | 363 | // Here we're already in selection mode. In that case |
| 364 | // When a simple click/tap (without SHIFT) creates causes |
| 365 | // an item to be selected. |
| 366 | // By recreating Ranger at this point, we allow the user to create |
| 367 | // multiple separate contiguous ranges with SHIFT+Click & Click. |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 368 | selectAndNotify(modelId); |
Steve McKay | 5739487 | 2015-08-12 14:48:34 -0700 | [diff] [blame] | 369 | changed = true; |
Steve McKay | 8c1b8c8 | 2015-07-28 19:20:01 -0700 | [diff] [blame] | 370 | } |
Steve McKay | 5739487 | 2015-08-12 14:48:34 -0700 | [diff] [blame] | 371 | |
Ben Kwa | 14a96ce | 2015-09-25 07:47:56 -0700 | [diff] [blame] | 372 | if (changed) { |
| 373 | notifySelectionChanged(); |
| 374 | } |
Steve McKay | 8c1b8c8 | 2015-07-28 19:20:01 -0700 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | /** |
Steve McKay | 93d8ef4 | 2015-07-30 12:27:44 -0700 | [diff] [blame] | 378 | * Sets the magic location at which a selection range begins. This |
| 379 | * value is consulted when determining how to extend, and modify |
| 380 | * selection ranges. |
| 381 | * |
| 382 | * @throws IllegalStateException if {@code position} is not already be selected |
| 383 | * @param position |
| 384 | */ |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 385 | void setSelectionRangeBegin(int position) { |
| 386 | if (mSelection.contains(mEnvironment.getModelIdFromAdapterPosition(position))) { |
| 387 | mRanger = new Range(position); |
| 388 | } |
Steve McKay | 93d8ef4 | 2015-07-30 12:27:44 -0700 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | /** |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 392 | * Try to set selection state for all elements in range. Not that callbacks can cancel selection |
| 393 | * of specific items, so some or even all items may not reflect the desired state after the |
| 394 | * update is complete. |
Steve McKay | 8c1b8c8 | 2015-07-28 19:20:01 -0700 | [diff] [blame] | 395 | * |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 396 | * @param begin Adapter position for range start (inclusive). |
| 397 | * @param end Adapter position for range end (inclusive). |
| 398 | * @param selected New selection state. |
Steve McKay | 8c1b8c8 | 2015-07-28 19:20:01 -0700 | [diff] [blame] | 399 | */ |
| 400 | private void updateRange(int begin, int end, boolean selected) { |
| 401 | checkState(end >= begin); |
Steve McKay | 8c1b8c8 | 2015-07-28 19:20:01 -0700 | [diff] [blame] | 402 | for (int i = begin; i <= end; i++) { |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 403 | String id = mEnvironment.getModelIdFromAdapterPosition(i); |
Steve McKay | 8c1b8c8 | 2015-07-28 19:20:01 -0700 | [diff] [blame] | 404 | if (selected) { |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 405 | boolean canSelect = notifyBeforeItemStateChange(id, true); |
Steve McKay | 5739487 | 2015-08-12 14:48:34 -0700 | [diff] [blame] | 406 | if (canSelect) { |
Ben Kwa | 98bb6cf | 2015-10-14 08:00:27 -0700 | [diff] [blame] | 407 | if (mSingleSelect && hasSelection()) { |
Steve McKay | 5739487 | 2015-08-12 14:48:34 -0700 | [diff] [blame] | 408 | clearSelectionQuietly(); |
| 409 | } |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 410 | selectAndNotify(id); |
Steve McKay | 5739487 | 2015-08-12 14:48:34 -0700 | [diff] [blame] | 411 | } |
Steve McKay | 8c1b8c8 | 2015-07-28 19:20:01 -0700 | [diff] [blame] | 412 | } else { |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 413 | attemptDeselect(id); |
Steve McKay | 8c1b8c8 | 2015-07-28 19:20:01 -0700 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | /** |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 419 | * @param modelId |
Steve McKay | 8c1b8c8 | 2015-07-28 19:20:01 -0700 | [diff] [blame] | 420 | * @return True if the update was applied. |
| 421 | */ |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 422 | private boolean selectAndNotify(String modelId) { |
| 423 | boolean changed = mSelection.add(modelId); |
Steve McKay | 5739487 | 2015-08-12 14:48:34 -0700 | [diff] [blame] | 424 | if (changed) { |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 425 | notifyItemStateChanged(modelId, true); |
Steve McKay | 8c1b8c8 | 2015-07-28 19:20:01 -0700 | [diff] [blame] | 426 | } |
Steve McKay | 5739487 | 2015-08-12 14:48:34 -0700 | [diff] [blame] | 427 | return changed; |
Steve McKay | 8c1b8c8 | 2015-07-28 19:20:01 -0700 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | /** |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 431 | * @param id |
Steve McKay | 8c1b8c8 | 2015-07-28 19:20:01 -0700 | [diff] [blame] | 432 | * @return True if the update was applied. |
| 433 | */ |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 434 | private boolean attemptDeselect(String id) { |
| 435 | if (notifyBeforeItemStateChange(id, false)) { |
| 436 | mSelection.remove(id); |
| 437 | notifyItemStateChanged(id, false); |
Steve McKay | 8c1b8c8 | 2015-07-28 19:20:01 -0700 | [diff] [blame] | 438 | if (DEBUG) Log.d(TAG, "Selection after deselect: " + mSelection); |
| 439 | return true; |
| 440 | } else { |
| 441 | if (DEBUG) Log.d(TAG, "Select cancelled by listener."); |
| 442 | return false; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 443 | } |
| 444 | } |
| 445 | |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 446 | private boolean notifyBeforeItemStateChange(String id, boolean nextState) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 447 | int lastListener = mCallbacks.size() - 1; |
| 448 | for (int i = lastListener; i > -1; i--) { |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 449 | if (!mCallbacks.get(i).onBeforeItemStateChange(id, nextState)) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 450 | return false; |
| 451 | } |
| 452 | } |
| 453 | return true; |
| 454 | } |
| 455 | |
| 456 | /** |
Steve McKay | 5739487 | 2015-08-12 14:48:34 -0700 | [diff] [blame] | 457 | * Notifies registered listeners when the selection status of a single item |
| 458 | * (identified by {@code position}) changes. |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 459 | */ |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 460 | private void notifyItemStateChanged(String id, boolean selected) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 461 | int lastListener = mCallbacks.size() - 1; |
| 462 | for (int i = lastListener; i > -1; i--) { |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 463 | mCallbacks.get(i).onItemStateChanged(id, selected); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 464 | } |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame^] | 465 | mEnvironment.notifyItemChanged(id); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | /** |
Steve McKay | 5739487 | 2015-08-12 14:48:34 -0700 | [diff] [blame] | 469 | * Notifies registered listeners when the selection has changed. This |
| 470 | * notification should be sent only once a full series of changes |
| 471 | * is complete, e.g. clearingSelection, or updating the single |
| 472 | * selection from one item to another. |
| 473 | */ |
| 474 | private void notifySelectionChanged() { |
| 475 | int lastListener = mCallbacks.size() - 1; |
| 476 | for (int i = lastListener; i > -1; i--) { |
| 477 | mCallbacks.get(i).onSelectionChanged(); |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | /** |
Steve McKay | 8c1b8c8 | 2015-07-28 19:20:01 -0700 | [diff] [blame] | 482 | * Class providing support for managing range selections. |
| 483 | */ |
Steve McKay | 93d8ef4 | 2015-07-30 12:27:44 -0700 | [diff] [blame] | 484 | private final class Range { |
Steve McKay | 8c1b8c8 | 2015-07-28 19:20:01 -0700 | [diff] [blame] | 485 | private static final int UNDEFINED = -1; |
| 486 | |
| 487 | final int mBegin; |
| 488 | int mEnd = UNDEFINED; |
| 489 | |
Steve McKay | 93d8ef4 | 2015-07-30 12:27:44 -0700 | [diff] [blame] | 490 | public Range(int begin) { |
Steve McKay | 5739487 | 2015-08-12 14:48:34 -0700 | [diff] [blame] | 491 | if (DEBUG) Log.d(TAG, "New Ranger created beginning @ " + begin); |
Steve McKay | 8c1b8c8 | 2015-07-28 19:20:01 -0700 | [diff] [blame] | 492 | mBegin = begin; |
| 493 | } |
| 494 | |
Steve McKay | 93d8ef4 | 2015-07-30 12:27:44 -0700 | [diff] [blame] | 495 | private void snapSelection(int position) { |
Steve McKay | 8c1b8c8 | 2015-07-28 19:20:01 -0700 | [diff] [blame] | 496 | checkState(mRanger != null); |
| 497 | checkArgument(position != RecyclerView.NO_POSITION); |
| 498 | |
| 499 | if (mEnd == UNDEFINED || mEnd == mBegin) { |
| 500 | // Reset mEnd so it can be established in establishRange. |
| 501 | mEnd = UNDEFINED; |
| 502 | establishRange(position); |
| 503 | } else { |
| 504 | reviseRange(position); |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | private void establishRange(int position) { |
| 509 | checkState(mRanger.mEnd == UNDEFINED); |
| 510 | |
| 511 | if (position == mBegin) { |
| 512 | mEnd = position; |
| 513 | } |
| 514 | |
| 515 | if (position > mBegin) { |
| 516 | updateRange(mBegin + 1, position, true); |
| 517 | } else if (position < mBegin) { |
| 518 | updateRange(position, mBegin - 1, true); |
| 519 | } |
| 520 | |
| 521 | mEnd = position; |
| 522 | } |
| 523 | |
| 524 | private void reviseRange(int position) { |
| 525 | checkState(mEnd != UNDEFINED); |
| 526 | checkState(mBegin != mEnd); |
| 527 | |
| 528 | if (position == mEnd) { |
| 529 | if (DEBUG) Log.i(TAG, "Skipping no-op revision click on mEndRange."); |
| 530 | } |
| 531 | |
| 532 | if (mEnd > mBegin) { |
| 533 | reviseAscendingRange(position); |
| 534 | } else if (mEnd < mBegin) { |
| 535 | reviseDescendingRange(position); |
| 536 | } |
| 537 | // the "else" case is covered by checkState at beginning of method. |
| 538 | |
| 539 | mEnd = position; |
| 540 | } |
| 541 | |
| 542 | /** |
| 543 | * Updates an existing ascending seleciton. |
| 544 | * @param position |
| 545 | */ |
| 546 | private void reviseAscendingRange(int position) { |
| 547 | // Reducing or reversing the range.... |
| 548 | if (position < mEnd) { |
| 549 | if (position < mBegin) { |
| 550 | updateRange(mBegin + 1, mEnd, false); |
| 551 | updateRange(position, mBegin -1, true); |
| 552 | } else { |
| 553 | updateRange(position + 1, mEnd, false); |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | // Extending the range... |
| 558 | else if (position > mEnd) { |
| 559 | updateRange(mEnd + 1, position, true); |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | private void reviseDescendingRange(int position) { |
| 564 | // Reducing or reversing the range.... |
| 565 | if (position > mEnd) { |
| 566 | if (position > mBegin) { |
| 567 | updateRange(mEnd, mBegin - 1, false); |
| 568 | updateRange(mBegin + 1, position, true); |
| 569 | } else { |
| 570 | updateRange(mEnd, position - 1, false); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | // Extending the range... |
| 575 | else if (position < mEnd) { |
| 576 | updateRange(position, mEnd - 1, true); |
| 577 | } |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | /** |
Steve McKay | 9459a7c | 2015-07-24 13:14:20 -0700 | [diff] [blame] | 582 | * Object representing the current selection. Provides read only access |
| 583 | * public access, and private write access. |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 584 | */ |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 585 | public static final class Selection { |
| 586 | |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 587 | // This class tracks selected items by managing two sets: the saved selection, and the total |
| 588 | // selection. Saved selections are those which have been completed by tapping an item or by |
| 589 | // completing a band select operation. Provisional selections are selections which have been |
| 590 | // temporarily created by an in-progress band select operation (once the user releases the |
| 591 | // mouse button during a band select operation, the selected items become saved). The total |
| 592 | // selection is the combination of both the saved selection and the provisional |
| 593 | // selection. Tracking both separately is necessary to ensure that saved selections do not |
| 594 | // become deselected when they are removed from the provisional selection; for example, if |
| 595 | // item A is tapped (and selected), then an in-progress band select covers A then uncovers |
| 596 | // A, A should still be selected as it has been saved. To ensure this behavior, the saved |
| 597 | // selection must be tracked separately. |
| 598 | private Set<String> mSavedSelection = new HashSet<>(); |
| 599 | private Set<String> mTotalSelection = new HashSet<>(); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 600 | |
Steve McKay | 68a8bb8 | 2015-10-21 14:38:54 -0700 | [diff] [blame] | 601 | @VisibleForTesting |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 602 | public Selection(String... ids) { |
| 603 | for (int i = 0; i < ids.length; i++) { |
| 604 | add(ids[i]); |
Steve McKay | 68a8bb8 | 2015-10-21 14:38:54 -0700 | [diff] [blame] | 605 | } |
| 606 | } |
| 607 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 608 | /** |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 609 | * @param id |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 610 | * @return true if the position is currently selected. |
| 611 | */ |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 612 | public boolean contains(String id) { |
| 613 | return mTotalSelection.contains(id); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | /** |
Steve McKay | 68a8bb8 | 2015-10-21 14:38:54 -0700 | [diff] [blame] | 617 | * Returns an unordered array of selected positions. |
| 618 | */ |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 619 | public String[] getAll() { |
| 620 | return mTotalSelection.toArray(new String[0]); |
Steve McKay | 68a8bb8 | 2015-10-21 14:38:54 -0700 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | /** |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 624 | * @return size of the selection. |
| 625 | */ |
| 626 | public int size() { |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 627 | return mTotalSelection.size(); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 628 | } |
| 629 | |
Steve McKay | 9459a7c | 2015-07-24 13:14:20 -0700 | [diff] [blame] | 630 | /** |
| 631 | * @return true if the selection is empty. |
| 632 | */ |
| 633 | public boolean isEmpty() { |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 634 | return mTotalSelection.isEmpty(); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | /** |
| 638 | * Sets the provisional selection, which is a temporary selection that can be saved, |
| 639 | * canceled, or adjusted at a later time. When a new provision selection is applied, the old |
| 640 | * one (if it exists) is abandoned. |
| 641 | * @return Array with entry for each position added or removed. Entries which were added |
| 642 | * contain a value of true, and entries which were removed contain a value of false. |
| 643 | */ |
| 644 | @VisibleForTesting |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 645 | protected Map<String, Boolean> setProvisionalSelection(Set<String> provisionalSelection) { |
| 646 | Map<String, Boolean> delta = new HashMap<>(); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 647 | |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 648 | for (String id: mTotalSelection) { |
| 649 | // Mark each item that used to be in the selection but is unsaved and not in the new |
| 650 | // provisional selection. |
| 651 | if (!provisionalSelection.contains(id) && !mSavedSelection.contains(id)) { |
| 652 | delta.put(id, false); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 653 | } |
| 654 | } |
| 655 | |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 656 | for (String id: provisionalSelection) { |
| 657 | // Mark each item that was not previously in the selection but is in the new |
| 658 | // provisional selection. |
| 659 | if (!mTotalSelection.contains(id)) { |
| 660 | delta.put(id, true); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 661 | } |
| 662 | } |
| 663 | |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 664 | // Now, iterate through the changes and actually add/remove them to/from the current |
| 665 | // selection. This could not be done in the previous loops because changing the size of |
| 666 | // the selection mid-iteration changes iteration order erroneously. |
| 667 | for (Map.Entry<String, Boolean> entry: delta.entrySet()) { |
| 668 | String id = entry.getKey(); |
| 669 | if (entry.getValue()) { |
| 670 | mTotalSelection.add(id); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 671 | } else { |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 672 | mTotalSelection.remove(id); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 673 | } |
| 674 | } |
| 675 | |
| 676 | return delta; |
| 677 | } |
| 678 | |
| 679 | /** |
| 680 | * Saves the existing provisional selection. Once the provisional selection is saved, |
| 681 | * subsequent provisional selections which are different from this existing one cannot |
| 682 | * cause items in this existing provisional selection to become deselected. |
| 683 | */ |
| 684 | @VisibleForTesting |
| 685 | protected void applyProvisionalSelection() { |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 686 | mSavedSelection = new HashSet<>(mTotalSelection); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | /** |
| 690 | * Abandons the existing provisional selection so that all items provisionally selected are |
| 691 | * now deselected. |
| 692 | */ |
| 693 | @VisibleForTesting |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 694 | void cancelProvisionalSelection() { |
| 695 | mTotalSelection = new HashSet<>(mSavedSelection); |
Steve McKay | 9459a7c | 2015-07-24 13:14:20 -0700 | [diff] [blame] | 696 | } |
| 697 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 698 | /** @hide */ |
| 699 | @VisibleForTesting |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 700 | boolean add(String id) { |
| 701 | if (!mTotalSelection.contains(id)) { |
| 702 | mTotalSelection.add(id); |
| 703 | mSavedSelection.add(id); |
Steve McKay | 9459a7c | 2015-07-24 13:14:20 -0700 | [diff] [blame] | 704 | return true; |
| 705 | } |
| 706 | return false; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | /** @hide */ |
| 710 | @VisibleForTesting |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 711 | boolean remove(String id) { |
| 712 | if (mTotalSelection.contains(id)) { |
| 713 | mTotalSelection.remove(id); |
| 714 | mSavedSelection.remove(id); |
Steve McKay | 9459a7c | 2015-07-24 13:14:20 -0700 | [diff] [blame] | 715 | return true; |
| 716 | } |
| 717 | return false; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 718 | } |
| 719 | |
Steve McKay | f68210e | 2015-11-03 15:23:16 -0800 | [diff] [blame] | 720 | public void clear() { |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 721 | mSavedSelection.clear(); |
| 722 | mTotalSelection.clear(); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 723 | } |
| 724 | |
Ben Kwa | 936a7fc | 2015-12-10 15:21:18 -0800 | [diff] [blame] | 725 | /** |
| 726 | * Trims this selection to be the intersection of itself with the set of given IDs. |
| 727 | */ |
| 728 | public void intersect(Collection<String> ids) { |
| 729 | mSavedSelection.retainAll(ids); |
| 730 | mTotalSelection.retainAll(ids); |
| 731 | } |
| 732 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 733 | @VisibleForTesting |
| 734 | void copyFrom(Selection source) { |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 735 | mSavedSelection = new HashSet<>(source.mSavedSelection); |
| 736 | mTotalSelection = new HashSet<>(source.mTotalSelection); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | @Override |
| 740 | public String toString() { |
| 741 | if (size() <= 0) { |
| 742 | return "size=0, items=[]"; |
| 743 | } |
| 744 | |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 745 | StringBuilder buffer = new StringBuilder(mTotalSelection.size() * 28); |
Steve McKay | 5739487 | 2015-08-12 14:48:34 -0700 | [diff] [blame] | 746 | buffer.append("{size=") |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 747 | .append(mTotalSelection.size()) |
Steve McKay | 5739487 | 2015-08-12 14:48:34 -0700 | [diff] [blame] | 748 | .append(", ") |
| 749 | .append("items=["); |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 750 | for (Iterator<String> i = mTotalSelection.iterator(); i.hasNext(); ) { |
| 751 | buffer.append(i.next()); |
| 752 | if (i.hasNext()) { |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 753 | buffer.append(", "); |
| 754 | } |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 755 | } |
| 756 | buffer.append("]}"); |
| 757 | return buffer.toString(); |
| 758 | } |
| 759 | |
| 760 | @Override |
Steve McKay | f4c06ab | 2015-07-22 11:42:14 -0700 | [diff] [blame] | 761 | public int hashCode() { |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 762 | return mSavedSelection.hashCode() ^ mTotalSelection.hashCode(); |
Steve McKay | f4c06ab | 2015-07-22 11:42:14 -0700 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | @Override |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 766 | public boolean equals(Object that) { |
| 767 | if (this == that) { |
| 768 | return true; |
| 769 | } |
| 770 | |
Steve McKay | f4c06ab | 2015-07-22 11:42:14 -0700 | [diff] [blame] | 771 | if (!(that instanceof Selection)) { |
| 772 | return false; |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 773 | } |
Steve McKay | f4c06ab | 2015-07-22 11:42:14 -0700 | [diff] [blame] | 774 | |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 775 | return mSavedSelection.equals(((Selection) that).mSavedSelection) && |
| 776 | mTotalSelection.equals(((Selection) that).mTotalSelection); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 777 | } |
| 778 | } |
| 779 | |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 780 | /** |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 781 | * Provides functionality for BandController. Exists primarily to tests that are |
| 782 | * fully isolated from RecyclerView. |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 783 | */ |
Ben Kwa | a017bc1 | 2015-10-07 14:15:12 -0700 | [diff] [blame] | 784 | interface SelectionEnvironment { |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 785 | void showBand(Rect rect); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 786 | void hideBand(); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 787 | void addOnScrollListener(RecyclerView.OnScrollListener listener); |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 788 | void removeOnScrollListener(RecyclerView.OnScrollListener listener); |
| 789 | void scrollBy(int dy); |
| 790 | int getHeight(); |
| 791 | void invalidateView(); |
| 792 | void runAtNextFrame(Runnable r); |
| 793 | void removeCallback(Runnable r); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 794 | Point createAbsolutePoint(Point relativePoint); |
| 795 | Rect getAbsoluteRectForChildViewAt(int index); |
Ben Kwa | 936a7fc | 2015-12-10 15:21:18 -0800 | [diff] [blame] | 796 | int getAdapterPositionAt(int index); |
Ben Kwa | a017bc1 | 2015-10-07 14:15:12 -0700 | [diff] [blame] | 797 | int getAdapterPositionForChildView(View view); |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 798 | int getColumnCount(); |
| 799 | int getRowCount(); |
| 800 | int getChildCount(); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 801 | int getVisibleChildCount(); |
Ben Kwa | a017bc1 | 2015-10-07 14:15:12 -0700 | [diff] [blame] | 802 | void focusItem(int position); |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 803 | String getModelIdFromAdapterPosition(int position); |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 804 | int getItemCount(); |
Ben Kwa | 936a7fc | 2015-12-10 15:21:18 -0800 | [diff] [blame] | 805 | List<String> getModelIds(); |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame^] | 806 | void notifyItemChanged(String id); |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 807 | void registerDataObserver(RecyclerView.AdapterDataObserver observer); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 808 | } |
| 809 | |
Steve McKay | e0cba92 | 2015-11-11 16:26:59 +0900 | [diff] [blame] | 810 | /** Recycler view facade implementation backed by good ol' RecyclerView. */ |
Ben Kwa | a017bc1 | 2015-10-07 14:15:12 -0700 | [diff] [blame] | 811 | private static final class RuntimeSelectionEnvironment implements SelectionEnvironment { |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 812 | |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 813 | private final RecyclerView mView; |
| 814 | private final Drawable mBand; |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 815 | |
| 816 | private boolean mIsOverlayShown = false; |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 817 | private DirectoryFragment.DocumentsAdapter mAdapter; |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 818 | |
Ben Kwa | a017bc1 | 2015-10-07 14:15:12 -0700 | [diff] [blame] | 819 | RuntimeSelectionEnvironment(RecyclerView rv) { |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 820 | mView = rv; |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 821 | mAdapter = (DirectoryFragment.DocumentsAdapter) rv.getAdapter(); |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 822 | mBand = mView.getContext().getTheme().getDrawable(R.drawable.band_select_overlay); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | @Override |
Ben Kwa | a017bc1 | 2015-10-07 14:15:12 -0700 | [diff] [blame] | 826 | public int getAdapterPositionForChildView(View view) { |
| 827 | if (view.getParent() == mView) { |
| 828 | return mView.getChildAdapterPosition(view); |
| 829 | } else { |
| 830 | return RecyclerView.NO_POSITION; |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | @Override |
Ben Kwa | 936a7fc | 2015-12-10 15:21:18 -0800 | [diff] [blame] | 835 | public int getAdapterPositionAt(int index) { |
| 836 | return getAdapterPositionForChildView(mView.getChildAt(index)); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | @Override |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 840 | public String getModelIdFromAdapterPosition(int position) { |
| 841 | return mAdapter.getModelId(position); |
| 842 | } |
| 843 | |
| 844 | @Override |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 845 | public void addOnScrollListener(RecyclerView.OnScrollListener listener) { |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 846 | mView.addOnScrollListener(listener); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | @Override |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 850 | public void removeOnScrollListener(RecyclerView.OnScrollListener listener) { |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 851 | mView.removeOnScrollListener(listener); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | @Override |
| 855 | public Point createAbsolutePoint(Point relativePoint) { |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 856 | return new Point(relativePoint.x + mView.computeHorizontalScrollOffset(), |
| 857 | relativePoint.y + mView.computeVerticalScrollOffset()); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | @Override |
| 861 | public Rect getAbsoluteRectForChildViewAt(int index) { |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 862 | final View child = mView.getChildAt(index); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 863 | final Rect childRect = new Rect(); |
| 864 | child.getHitRect(childRect); |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 865 | childRect.left += mView.computeHorizontalScrollOffset(); |
| 866 | childRect.right += mView.computeHorizontalScrollOffset(); |
| 867 | childRect.top += mView.computeVerticalScrollOffset(); |
| 868 | childRect.bottom += mView.computeVerticalScrollOffset(); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 869 | return childRect; |
| 870 | } |
| 871 | |
| 872 | @Override |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 873 | public int getChildCount() { |
| 874 | return mView.getAdapter().getItemCount(); |
| 875 | } |
| 876 | |
| 877 | @Override |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 878 | public int getVisibleChildCount() { |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 879 | return mView.getChildCount(); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | @Override |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 883 | public int getColumnCount() { |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 884 | RecyclerView.LayoutManager layoutManager = mView.getLayoutManager(); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 885 | if (layoutManager instanceof GridLayoutManager) { |
| 886 | return ((GridLayoutManager) layoutManager).getSpanCount(); |
| 887 | } |
| 888 | |
| 889 | // Otherwise, it is a list with 1 column. |
| 890 | return 1; |
| 891 | } |
| 892 | |
| 893 | @Override |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 894 | public int getRowCount() { |
| 895 | int numFullColumns = getChildCount() / getColumnCount(); |
| 896 | boolean hasPartiallyFullColumn = getChildCount() % getColumnCount() != 0; |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 897 | return numFullColumns + (hasPartiallyFullColumn ? 1 : 0); |
| 898 | } |
| 899 | |
| 900 | @Override |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 901 | public int getHeight() { |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 902 | return mView.getHeight(); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | @Override |
| 906 | public void invalidateView() { |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 907 | mView.invalidate(); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | @Override |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 911 | public void runAtNextFrame(Runnable r) { |
| 912 | mView.postOnAnimation(r); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | @Override |
| 916 | public void removeCallback(Runnable r) { |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 917 | mView.removeCallbacks(r); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | @Override |
| 921 | public void scrollBy(int dy) { |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 922 | mView.scrollBy(0, dy); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | @Override |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 926 | public void showBand(Rect rect) { |
| 927 | mBand.setBounds(rect); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 928 | |
| 929 | if (!mIsOverlayShown) { |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 930 | mView.getOverlay().add(mBand); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 931 | } |
| 932 | } |
| 933 | |
| 934 | @Override |
| 935 | public void hideBand() { |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 936 | mView.getOverlay().remove(mBand); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 937 | } |
Ben Kwa | a017bc1 | 2015-10-07 14:15:12 -0700 | [diff] [blame] | 938 | |
| 939 | @Override |
| 940 | public void focusItem(final int pos) { |
| 941 | // If the item is already in view, focus it; otherwise, scroll to it and focus it. |
| 942 | RecyclerView.ViewHolder vh = mView.findViewHolderForAdapterPosition(pos); |
| 943 | if (vh != null) { |
| 944 | vh.itemView.requestFocus(); |
| 945 | } else { |
| 946 | // Don't smooth scroll; that taxes the system unnecessarily and makes the scroll |
| 947 | // handling logic below more complicated. See b/24865658. |
| 948 | mView.scrollToPosition(pos); |
| 949 | // Set a one-time listener to request focus when the scroll has completed. |
| 950 | mView.addOnScrollListener( |
| 951 | new RecyclerView.OnScrollListener() { |
| 952 | @Override |
| 953 | public void onScrolled(RecyclerView view, int dx, int dy) { |
| 954 | view.findViewHolderForAdapterPosition(pos).itemView.requestFocus(); |
| 955 | view.removeOnScrollListener(this); |
| 956 | } |
| 957 | }); |
| 958 | } |
| 959 | } |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 960 | |
| 961 | @Override |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame^] | 962 | public void notifyItemChanged(String id) { |
| 963 | mAdapter.notifyItemChanged(id, SELECTION_CHANGED_MARKER); |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | @Override |
| 967 | public int getItemCount() { |
| 968 | return mAdapter.getItemCount(); |
| 969 | } |
| 970 | |
| 971 | @Override |
| 972 | public void registerDataObserver(RecyclerView.AdapterDataObserver observer) { |
| 973 | mAdapter.registerAdapterDataObserver(observer); |
| 974 | } |
Ben Kwa | 936a7fc | 2015-12-10 15:21:18 -0800 | [diff] [blame] | 975 | |
| 976 | @Override |
| 977 | public List<String> getModelIds() { |
| 978 | return mAdapter.getModelIds(); |
| 979 | } |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 980 | } |
| 981 | |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 982 | public interface Callback { |
| 983 | /** |
| 984 | * Called when an item is selected or unselected while in selection mode. |
| 985 | * |
| 986 | * @param position Adapter position of the item that was checked or unchecked |
| 987 | * @param selected <code>true</code> if the item is now selected, <code>false</code> |
| 988 | * if the item is now unselected. |
| 989 | */ |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 990 | public void onItemStateChanged(String id, boolean selected); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 991 | |
| 992 | /** |
Steve McKay | 5739487 | 2015-08-12 14:48:34 -0700 | [diff] [blame] | 993 | * Called prior to an item changing state. Callbacks can cancel |
| 994 | * the change at {@code position} by returning {@code false}. |
| 995 | * |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 996 | * @param id Adapter position of the item that was checked or unchecked |
Steve McKay | 5739487 | 2015-08-12 14:48:34 -0700 | [diff] [blame] | 997 | * @param selected <code>true</code> if the item is to be selected, <code>false</code> |
| 998 | * if the item is to be unselected. |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 999 | */ |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 1000 | public boolean onBeforeItemStateChange(String id, boolean selected); |
Steve McKay | 5739487 | 2015-08-12 14:48:34 -0700 | [diff] [blame] | 1001 | |
| 1002 | /** |
| 1003 | * Called immediately after completion of any set of changes. |
| 1004 | */ |
| 1005 | public void onSelectionChanged(); |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | /** |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1009 | * Provides mouse driven band-select support when used in conjunction with {@link RecyclerView} |
| 1010 | * and {@link MultiSelectManager}. This class is responsible for rendering the band select |
| 1011 | * overlay and selecting overlaid items via MultiSelectManager. |
| 1012 | */ |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 1013 | public class BandController extends RecyclerView.OnScrollListener |
| 1014 | implements GridModel.OnSelectionChangedListener { |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1015 | |
| 1016 | private static final int NOT_SET = -1; |
| 1017 | |
Steve McKay | 093d5e7 | 2015-09-03 16:49:51 -0700 | [diff] [blame] | 1018 | private final Runnable mModelBuilder; |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1019 | |
Steve McKay | 093d5e7 | 2015-09-03 16:49:51 -0700 | [diff] [blame] | 1020 | @Nullable private Rect mBounds; |
| 1021 | @Nullable private Point mCurrentPosition; |
| 1022 | @Nullable private Point mOrigin; |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 1023 | @Nullable private GridModel mModel; |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1024 | |
| 1025 | // The time at which the current band selection-induced scroll began. If no scroll is in |
| 1026 | // progress, the value is NOT_SET. |
| 1027 | private long mScrollStartTime = NOT_SET; |
| 1028 | private final Runnable mViewScroller = new ViewScroller(); |
| 1029 | |
Steve McKay | 9eabc50 | 2015-10-19 12:04:21 -0700 | [diff] [blame] | 1030 | public BandController() { |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 1031 | mEnvironment.addOnScrollListener(this); |
Steve McKay | 093d5e7 | 2015-09-03 16:49:51 -0700 | [diff] [blame] | 1032 | |
| 1033 | mModelBuilder = new Runnable() { |
| 1034 | @Override |
| 1035 | public void run() { |
Ben Kwa | a017bc1 | 2015-10-07 14:15:12 -0700 | [diff] [blame] | 1036 | mModel = new GridModel(mEnvironment); |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 1037 | mModel.addOnSelectionChangedListener(BandController.this); |
Steve McKay | 093d5e7 | 2015-09-03 16:49:51 -0700 | [diff] [blame] | 1038 | } |
| 1039 | }; |
| 1040 | } |
| 1041 | |
Steve McKay | 9eabc50 | 2015-10-19 12:04:21 -0700 | [diff] [blame] | 1042 | public boolean handleEvent(MotionInputEvent e) { |
| 1043 | // b/23793622 notes the fact that we *never* receive ACTION_DOWN |
| 1044 | // events in onTouchEvent. Where it not for this issue, we'd |
| 1045 | // push start handling down into handleInputEvent. |
| 1046 | if (mBandManager.shouldStart(e)) { |
| 1047 | // endBandSelect is handled in handleInputEvent. |
| 1048 | mBandManager.startBandSelect(e.getOrigin()); |
| 1049 | } else if (mBandManager.isActive() |
| 1050 | && e.isMouseEvent() |
| 1051 | && e.isActionUp()) { |
| 1052 | // Same issue here w b/23793622. The ACTION_UP event |
| 1053 | // is only evert dispatched to onTouchEvent when |
| 1054 | // there is some associated motion. If a user taps |
| 1055 | // mouse, but doesn't move, then band select gets |
| 1056 | // started BUT not ended. Causing phantom |
| 1057 | // bands to appear when the user later clicks to start |
| 1058 | // band select. |
| 1059 | mBandManager.processInputEvent(e); |
| 1060 | } |
| 1061 | |
| 1062 | return isActive(); |
| 1063 | } |
| 1064 | |
Steve McKay | 093d5e7 | 2015-09-03 16:49:51 -0700 | [diff] [blame] | 1065 | private boolean isActive() { |
| 1066 | return mModel != null; |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | /** |
Kyle Horimoto | 2da6e4a | 2015-08-27 16:44:00 -0700 | [diff] [blame] | 1070 | * Handle a change in layout by cleaning up and getting rid of the old model and creating |
| 1071 | * a new model which will track the new layout. |
| 1072 | */ |
| 1073 | public void handleLayoutChanged() { |
Steve McKay | 093d5e7 | 2015-09-03 16:49:51 -0700 | [diff] [blame] | 1074 | if (mModel != null) { |
| 1075 | mModel.removeOnSelectionChangedListener(this); |
| 1076 | mModel.stopListening(); |
Kyle Horimoto | 2da6e4a | 2015-08-27 16:44:00 -0700 | [diff] [blame] | 1077 | |
Steve McKay | 093d5e7 | 2015-09-03 16:49:51 -0700 | [diff] [blame] | 1078 | // build a new model, all fresh and happy. |
| 1079 | mModelBuilder.run(); |
| 1080 | } |
| 1081 | } |
| 1082 | |
Steve McKay | 9eabc50 | 2015-10-19 12:04:21 -0700 | [diff] [blame] | 1083 | boolean shouldStart(MotionInputEvent e) { |
Steve McKay | 093d5e7 | 2015-09-03 16:49:51 -0700 | [diff] [blame] | 1084 | return !isActive() |
Steve McKay | 9eabc50 | 2015-10-19 12:04:21 -0700 | [diff] [blame] | 1085 | && e.isMouseEvent() // a mouse |
| 1086 | && e.isActionDown() // the initial button press |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 1087 | && mEnvironment.getItemCount() > 0 |
Steve McKay | 9eabc50 | 2015-10-19 12:04:21 -0700 | [diff] [blame] | 1088 | && e.getItemPosition() == RecyclerView.NO_ID; // in empty space |
Steve McKay | 093d5e7 | 2015-09-03 16:49:51 -0700 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | boolean shouldStop(InputEvent input) { |
| 1092 | return isActive() |
| 1093 | && input.isMouseEvent() |
| 1094 | && input.isActionUp(); |
Kyle Horimoto | 2da6e4a | 2015-08-27 16:44:00 -0700 | [diff] [blame] | 1095 | } |
| 1096 | |
| 1097 | /** |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1098 | * Processes a MotionEvent by starting, ending, or resizing the band select overlay. |
Steve McKay | 093d5e7 | 2015-09-03 16:49:51 -0700 | [diff] [blame] | 1099 | * @param input |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1100 | */ |
Steve McKay | 9eabc50 | 2015-10-19 12:04:21 -0700 | [diff] [blame] | 1101 | private void processInputEvent(InputEvent input) { |
Steve McKay | 093d5e7 | 2015-09-03 16:49:51 -0700 | [diff] [blame] | 1102 | checkArgument(input.isMouseEvent()); |
| 1103 | |
| 1104 | if (shouldStop(input)) { |
Steve McKay | be629f3 | 2015-09-04 14:51:16 -0700 | [diff] [blame] | 1105 | endBandSelect(); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1106 | return; |
| 1107 | } |
| 1108 | |
Steve McKay | 093d5e7 | 2015-09-03 16:49:51 -0700 | [diff] [blame] | 1109 | // We shouldn't get any events in this method when band select is not active, |
| 1110 | // but it turns some guests show up late to the party. |
| 1111 | if (!isActive()) { |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1112 | return; |
| 1113 | } |
| 1114 | |
Steve McKay | 093d5e7 | 2015-09-03 16:49:51 -0700 | [diff] [blame] | 1115 | mCurrentPosition = input.getOrigin(); |
| 1116 | mModel.resizeSelection(input.getOrigin()); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1117 | scrollViewIfNecessary(); |
| 1118 | resizeBandSelectRectangle(); |
| 1119 | } |
| 1120 | |
| 1121 | /** |
| 1122 | * Starts band select by adding the drawable to the RecyclerView's overlay. |
| 1123 | */ |
Steve McKay | 093d5e7 | 2015-09-03 16:49:51 -0700 | [diff] [blame] | 1124 | private void startBandSelect(Point origin) { |
| 1125 | if (DEBUG) Log.d(TAG, "Starting band select @ " + origin); |
| 1126 | |
| 1127 | mOrigin = origin; |
| 1128 | mModelBuilder.run(); // Creates a new selection model. |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1129 | mModel.startSelection(mOrigin); |
| 1130 | } |
| 1131 | |
| 1132 | /** |
| 1133 | * Scrolls the view if necessary. |
| 1134 | */ |
| 1135 | private void scrollViewIfNecessary() { |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 1136 | mEnvironment.removeCallback(mViewScroller); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1137 | mViewScroller.run(); |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 1138 | mEnvironment.invalidateView(); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | /** |
| 1142 | * Resizes the band select rectangle by using the origin and the current pointer position as |
| 1143 | * two opposite corners of the selection. |
| 1144 | */ |
| 1145 | private void resizeBandSelectRectangle() { |
Steve McKay | 093d5e7 | 2015-09-03 16:49:51 -0700 | [diff] [blame] | 1146 | mBounds = new Rect(Math.min(mOrigin.x, mCurrentPosition.x), |
| 1147 | Math.min(mOrigin.y, mCurrentPosition.y), |
| 1148 | Math.max(mOrigin.x, mCurrentPosition.x), |
| 1149 | Math.max(mOrigin.y, mCurrentPosition.y)); |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 1150 | mEnvironment.showBand(mBounds); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | /** |
| 1154 | * Ends band select by removing the overlay. |
| 1155 | */ |
| 1156 | private void endBandSelect() { |
| 1157 | if (DEBUG) Log.d(TAG, "Ending band select."); |
Steve McKay | 093d5e7 | 2015-09-03 16:49:51 -0700 | [diff] [blame] | 1158 | |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 1159 | mEnvironment.hideBand(); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1160 | mSelection.applyProvisionalSelection(); |
| 1161 | mModel.endSelection(); |
Ben Kwa | 936a7fc | 2015-12-10 15:21:18 -0800 | [diff] [blame] | 1162 | int firstSelected = mModel.getPositionNearestOrigin(); |
| 1163 | if (!mSelection.contains(mEnvironment.getModelIdFromAdapterPosition(firstSelected))) { |
Steve McKay | 093d5e7 | 2015-09-03 16:49:51 -0700 | [diff] [blame] | 1164 | Log.w(TAG, "First selected by band is NOT in selection!"); |
| 1165 | // Sadly this is really happening. Need to figure out what's going on. |
Ben Kwa | 936a7fc | 2015-12-10 15:21:18 -0800 | [diff] [blame] | 1166 | } else if (firstSelected != NOT_SET) { |
| 1167 | // TODO: firstSelected should really be lastSelected, we want to anchor the item |
| 1168 | // where the mouse-up occurred. |
| 1169 | setSelectionRangeBegin(firstSelected); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1170 | } |
Steve McKay | 093d5e7 | 2015-09-03 16:49:51 -0700 | [diff] [blame] | 1171 | |
| 1172 | mModel = null; |
| 1173 | mOrigin = null; |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1174 | } |
| 1175 | |
| 1176 | @Override |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 1177 | public void onSelectionChanged(Set<String> updatedSelection) { |
| 1178 | Map<String, Boolean> delta = mSelection.setProvisionalSelection(updatedSelection); |
| 1179 | for (Map.Entry<String, Boolean> entry: delta.entrySet()) { |
| 1180 | notifyItemStateChanged(entry.getKey(), entry.getValue()); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1181 | } |
| 1182 | notifySelectionChanged(); |
| 1183 | } |
| 1184 | |
| 1185 | private class ViewScroller implements Runnable { |
| 1186 | /** |
| 1187 | * The number of milliseconds of scrolling at which scroll speed continues to increase. |
| 1188 | * At first, the scroll starts slowly; then, the rate of scrolling increases until it |
| 1189 | * reaches its maximum value at after this many milliseconds. |
| 1190 | */ |
| 1191 | private static final long SCROLL_ACCELERATION_LIMIT_TIME_MS = 2000; |
| 1192 | |
| 1193 | @Override |
| 1194 | public void run() { |
| 1195 | // Compute the number of pixels the pointer's y-coordinate is past the view. |
| 1196 | // Negative values mean the pointer is at or before the top of the view, and |
| 1197 | // positive values mean that the pointer is at or after the bottom of the view. Note |
| 1198 | // that one additional pixel is added here so that the view still scrolls when the |
| 1199 | // pointer is exactly at the top or bottom. |
| 1200 | int pixelsPastView = 0; |
Steve McKay | 093d5e7 | 2015-09-03 16:49:51 -0700 | [diff] [blame] | 1201 | if (mCurrentPosition.y <= 0) { |
| 1202 | pixelsPastView = mCurrentPosition.y - 1; |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 1203 | } else if (mCurrentPosition.y >= mEnvironment.getHeight() - 1) { |
| 1204 | pixelsPastView = mCurrentPosition.y - mEnvironment.getHeight() + 1; |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1205 | } |
| 1206 | |
Steve McKay | 093d5e7 | 2015-09-03 16:49:51 -0700 | [diff] [blame] | 1207 | if (!isActive() || pixelsPastView == 0) { |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1208 | // If band selection is inactive, or if it is active but not at the edge of the |
| 1209 | // view, no scrolling is necessary. |
| 1210 | mScrollStartTime = NOT_SET; |
| 1211 | return; |
| 1212 | } |
| 1213 | |
| 1214 | if (mScrollStartTime == NOT_SET) { |
| 1215 | // If the pointer was previously not at the edge of the view but now is, set the |
| 1216 | // start time for the scroll. |
| 1217 | mScrollStartTime = System.currentTimeMillis(); |
| 1218 | } |
| 1219 | |
| 1220 | // Compute the number of pixels to scroll, and scroll that many pixels. |
| 1221 | final int numPixels = computeScrollDistance( |
| 1222 | pixelsPastView, System.currentTimeMillis() - mScrollStartTime); |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 1223 | mEnvironment.scrollBy(numPixels); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1224 | |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 1225 | mEnvironment.removeCallback(mViewScroller); |
| 1226 | mEnvironment.runAtNextFrame(this); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | /** |
| 1230 | * Computes the number of pixels to scroll based on how far the pointer is past the end |
| 1231 | * of the view and how long it has been there. Roughly based on ItemTouchHelper's |
| 1232 | * algorithm for computing the number of pixels to scroll when an item is dragged to the |
| 1233 | * end of a {@link RecyclerView}. |
| 1234 | * @param pixelsPastView |
| 1235 | * @param scrollDuration |
| 1236 | * @return |
| 1237 | */ |
| 1238 | private int computeScrollDistance(int pixelsPastView, long scrollDuration) { |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 1239 | final int maxScrollStep = mEnvironment.getHeight(); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1240 | final int direction = (int) Math.signum(pixelsPastView); |
| 1241 | final int absPastView = Math.abs(pixelsPastView); |
| 1242 | |
| 1243 | // Calculate the ratio of how far out of the view the pointer currently resides to |
| 1244 | // the entire height of the view. |
| 1245 | final float outOfBoundsRatio = Math.min( |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 1246 | 1.0f, (float) absPastView / mEnvironment.getHeight()); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1247 | // Interpolate this ratio and use it to compute the maximum scroll that should be |
| 1248 | // possible for this step. |
| 1249 | final float cappedScrollStep = |
| 1250 | direction * maxScrollStep * smoothOutOfBoundsRatio(outOfBoundsRatio); |
| 1251 | |
| 1252 | // Likewise, calculate the ratio of the time spent in the scroll to the limit. |
| 1253 | final float timeRatio = Math.min( |
| 1254 | 1.0f, (float) scrollDuration / SCROLL_ACCELERATION_LIMIT_TIME_MS); |
| 1255 | // Interpolate this ratio and use it to compute the final number of pixels to |
| 1256 | // scroll. |
| 1257 | final int numPixels = (int) (cappedScrollStep * smoothTimeRatio(timeRatio)); |
| 1258 | |
| 1259 | // If the final number of pixels to scroll ends up being 0, the view should still |
| 1260 | // scroll at least one pixel. |
| 1261 | return numPixels != 0 ? numPixels : direction; |
| 1262 | } |
| 1263 | |
| 1264 | /** |
| 1265 | * Interpolates the given out of bounds ratio on a curve which starts at (0,0) and ends |
| 1266 | * at (1,1) and quickly approaches 1 near the start of that interval. This ensures that |
| 1267 | * drags that are at the edge or barely past the edge of the view still cause sufficient |
| 1268 | * scrolling. The equation y=(x-1)^5+1 is used, but this could also be tweaked if |
| 1269 | * needed. |
| 1270 | * @param ratio A ratio which is in the range [0, 1]. |
| 1271 | * @return A "smoothed" value, also in the range [0, 1]. |
| 1272 | */ |
| 1273 | private float smoothOutOfBoundsRatio(float ratio) { |
| 1274 | return (float) Math.pow(ratio - 1.0f, 5) + 1.0f; |
| 1275 | } |
| 1276 | |
| 1277 | /** |
| 1278 | * Interpolates the given time ratio on a curve which starts at (0,0) and ends at (1,1) |
| 1279 | * and stays close to 0 for most input values except those very close to 1. This ensures |
| 1280 | * that scrolls start out very slowly but speed up drastically after the scroll has been |
| 1281 | * in progress close to SCROLL_ACCELERATION_LIMIT_TIME_MS. The equation y=x^5 is used, |
| 1282 | * but this could also be tweaked if needed. |
| 1283 | * @param ratio A ratio which is in the range [0, 1]. |
| 1284 | * @return A "smoothed" value, also in the range [0, 1]. |
| 1285 | */ |
| 1286 | private float smoothTimeRatio(float ratio) { |
| 1287 | return (float) Math.pow(ratio, 5); |
| 1288 | } |
| 1289 | }; |
Kyle Horimoto | 237b740 | 2015-08-25 17:12:42 -0700 | [diff] [blame] | 1290 | |
| 1291 | @Override |
| 1292 | public void onScrolled(RecyclerView recyclerView, int dx, int dy) { |
Steve McKay | 093d5e7 | 2015-09-03 16:49:51 -0700 | [diff] [blame] | 1293 | if (!isActive()) { |
Kyle Horimoto | 237b740 | 2015-08-25 17:12:42 -0700 | [diff] [blame] | 1294 | return; |
| 1295 | } |
| 1296 | |
| 1297 | // Adjust the y-coordinate of the origin the opposite number of pixels so that the |
| 1298 | // origin remains in the same place relative to the view's items. |
| 1299 | mOrigin.y -= dy; |
| 1300 | resizeBandSelectRectangle(); |
| 1301 | } |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | /** |
| 1305 | * Provides a band selection item model for views within a RecyclerView. This class queries the |
| 1306 | * RecyclerView to determine where its items are placed; then, once band selection is underway, |
| 1307 | * it alerts listeners of which items are covered by the selections. |
| 1308 | */ |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 1309 | public static final class GridModel extends RecyclerView.OnScrollListener { |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1310 | |
| 1311 | public static final int NOT_SET = -1; |
| 1312 | |
| 1313 | // Enum values used to determine the corner at which the origin is located within the |
| 1314 | private static final int UPPER = 0x00; |
| 1315 | private static final int LOWER = 0x01; |
| 1316 | private static final int LEFT = 0x00; |
| 1317 | private static final int RIGHT = 0x02; |
| 1318 | private static final int UPPER_LEFT = UPPER | LEFT; |
| 1319 | private static final int UPPER_RIGHT = UPPER | RIGHT; |
| 1320 | private static final int LOWER_LEFT = LOWER | LEFT; |
| 1321 | private static final int LOWER_RIGHT = LOWER | RIGHT; |
| 1322 | |
Ben Kwa | a017bc1 | 2015-10-07 14:15:12 -0700 | [diff] [blame] | 1323 | private final SelectionEnvironment mHelper; |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 1324 | private final List<OnSelectionChangedListener> mOnSelectionChangedListeners = |
| 1325 | new ArrayList<>(); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1326 | |
Kyle Horimoto | d3b3d3d | 2015-08-25 21:03:38 -0700 | [diff] [blame] | 1327 | // Map from the x-value of the left side of a SparseBooleanArray of adapter positions, keyed |
| 1328 | // by their y-offset. For example, if the first column of the view starts at an x-value of 5, |
| 1329 | // mColumns.get(5) would return an array of positions in that column. Within that array, the |
| 1330 | // value for key y is the adapter position for the item whose y-offset is y. |
Ben Kwa | 936a7fc | 2015-12-10 15:21:18 -0800 | [diff] [blame] | 1331 | private final SparseArray<SparseIntArray> mColumns = new SparseArray<>(); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1332 | |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 1333 | // List of limits along the x-axis (columns). |
| 1334 | // This list is sorted from furthest left to furthest right. |
| 1335 | private final List<Limits> mColumnBounds = new ArrayList<>(); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1336 | |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 1337 | // List of limits along the y-axis (rows). Note that this list only contains items which |
| 1338 | // have been in the viewport. |
| 1339 | private final List<Limits> mRowBounds = new ArrayList<>(); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1340 | |
| 1341 | // The adapter positions which have been recorded so far. |
Ben Kwa | 936a7fc | 2015-12-10 15:21:18 -0800 | [diff] [blame] | 1342 | private final SparseBooleanArray mKnownPositions = new SparseBooleanArray(); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1343 | |
| 1344 | // Array passed to registered OnSelectionChangedListeners. One array is created and reused |
| 1345 | // throughout the lifetime of the object. |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 1346 | private final Set<String> mSelection = new HashSet<>(); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1347 | |
| 1348 | // The current pointer (in absolute positioning from the top of the view). |
| 1349 | private Point mPointer = null; |
| 1350 | |
| 1351 | // The bounds of the band selection. |
| 1352 | private RelativePoint mRelativeOrigin; |
| 1353 | private RelativePoint mRelativePointer; |
| 1354 | |
| 1355 | private boolean mIsActive; |
| 1356 | |
| 1357 | // Tracks where the band select originated from. This is used to determine where selections |
| 1358 | // should expand from when Shift+click is used. |
Ben Kwa | 936a7fc | 2015-12-10 15:21:18 -0800 | [diff] [blame] | 1359 | private int mPositionNearestOrigin = NOT_SET; |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1360 | |
Ben Kwa | a017bc1 | 2015-10-07 14:15:12 -0700 | [diff] [blame] | 1361 | GridModel(SelectionEnvironment helper) { |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1362 | mHelper = helper; |
| 1363 | mHelper.addOnScrollListener(this); |
| 1364 | } |
| 1365 | |
| 1366 | /** |
| 1367 | * Stops listening to the view's scrolls. Call this function before discarding a |
| 1368 | * BandSelecModel object to prevent memory leaks. |
| 1369 | */ |
| 1370 | void stopListening() { |
| 1371 | mHelper.removeOnScrollListener(this); |
| 1372 | } |
| 1373 | |
| 1374 | /** |
| 1375 | * Start a band select operation at the given point. |
| 1376 | * @param relativeOrigin The origin of the band select operation, relative to the viewport. |
| 1377 | * For example, if the view is scrolled to the bottom, the top-left of the viewport |
| 1378 | * would have a relative origin of (0, 0), even though its absolute point has a higher |
| 1379 | * y-value. |
| 1380 | */ |
| 1381 | void startSelection(Point relativeOrigin) { |
| 1382 | mIsActive = true; |
| 1383 | mPointer = mHelper.createAbsolutePoint(relativeOrigin); |
| 1384 | |
| 1385 | recordVisibleChildren(); |
| 1386 | mRelativeOrigin = new RelativePoint(mPointer); |
| 1387 | mRelativePointer = new RelativePoint(mPointer); |
| 1388 | computeCurrentSelection(); |
| 1389 | notifyListeners(); |
| 1390 | } |
| 1391 | |
| 1392 | /** |
| 1393 | * Resizes the selection by adjusting the pointer (i.e., the corner of the selection |
| 1394 | * opposite the origin. |
| 1395 | * @param relativePointer The pointer (opposite of the origin) of the band select operation, |
| 1396 | * relative to the viewport. For example, if the view is scrolled to the bottom, the |
| 1397 | * top-left of the viewport would have a relative origin of (0, 0), even though its |
| 1398 | * absolute point has a higher y-value. |
| 1399 | */ |
| 1400 | void resizeSelection(Point relativePointer) { |
| 1401 | mPointer = mHelper.createAbsolutePoint(relativePointer); |
| 1402 | updateModel(); |
| 1403 | } |
| 1404 | |
| 1405 | /** |
| 1406 | * Ends the band selection. |
| 1407 | */ |
| 1408 | void endSelection() { |
| 1409 | mIsActive = false; |
| 1410 | } |
| 1411 | |
| 1412 | /** |
| 1413 | * @return The adapter position for the item nearest the origin corresponding to the latest |
| 1414 | * band select operation, or NOT_SET if the selection did not cover any items. |
| 1415 | */ |
Ben Kwa | 936a7fc | 2015-12-10 15:21:18 -0800 | [diff] [blame] | 1416 | int getPositionNearestOrigin() { |
| 1417 | return mPositionNearestOrigin; |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1418 | } |
| 1419 | |
| 1420 | @Override |
| 1421 | public void onScrolled(RecyclerView recyclerView, int dx, int dy) { |
| 1422 | if (!mIsActive) { |
| 1423 | return; |
| 1424 | } |
| 1425 | |
| 1426 | mPointer.x += dx; |
| 1427 | mPointer.y += dy; |
| 1428 | recordVisibleChildren(); |
| 1429 | updateModel(); |
| 1430 | } |
| 1431 | |
| 1432 | /** |
| 1433 | * Queries the view for all children and records their location metadata. |
| 1434 | */ |
| 1435 | private void recordVisibleChildren() { |
| 1436 | for (int i = 0; i < mHelper.getVisibleChildCount(); i++) { |
Ben Kwa | 936a7fc | 2015-12-10 15:21:18 -0800 | [diff] [blame] | 1437 | int adapterPosition = mHelper.getAdapterPositionAt(i); |
| 1438 | if (!mKnownPositions.get(adapterPosition)) { |
| 1439 | mKnownPositions.put(adapterPosition, true); |
| 1440 | recordItemData(mHelper.getAbsoluteRectForChildViewAt(i), adapterPosition); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1441 | } |
| 1442 | } |
| 1443 | } |
| 1444 | |
| 1445 | /** |
| 1446 | * Updates the limits lists and column map with the given item metadata. |
| 1447 | * @param absoluteChildRect The absolute rectangle for the child view being processed. |
| 1448 | * @param adapterPosition The position of the child view being processed. |
| 1449 | */ |
Ben Kwa | 936a7fc | 2015-12-10 15:21:18 -0800 | [diff] [blame] | 1450 | private void recordItemData(Rect absoluteChildRect, int adapterPosition) { |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 1451 | if (mColumnBounds.size() != mHelper.getColumnCount()) { |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1452 | // If not all x-limits have been recorded, record this one. |
| 1453 | recordLimits( |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 1454 | mColumnBounds, new Limits(absoluteChildRect.left, absoluteChildRect.right)); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1455 | } |
| 1456 | |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 1457 | if (mRowBounds.size() != mHelper.getRowCount()) { |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1458 | // If not all y-limits have been recorded, record this one. |
| 1459 | recordLimits( |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 1460 | mRowBounds, new Limits(absoluteChildRect.top, absoluteChildRect.bottom)); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1461 | } |
| 1462 | |
Ben Kwa | 936a7fc | 2015-12-10 15:21:18 -0800 | [diff] [blame] | 1463 | SparseIntArray columnList = mColumns.get(absoluteChildRect.left); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1464 | if (columnList == null) { |
Ben Kwa | 936a7fc | 2015-12-10 15:21:18 -0800 | [diff] [blame] | 1465 | columnList = new SparseIntArray(); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1466 | mColumns.put(absoluteChildRect.left, columnList); |
| 1467 | } |
Ben Kwa | 936a7fc | 2015-12-10 15:21:18 -0800 | [diff] [blame] | 1468 | columnList.put(absoluteChildRect.top, adapterPosition); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1469 | } |
| 1470 | |
| 1471 | /** |
| 1472 | * Ensures limits exists within the sorted list limitsList, and adds it to the list if it |
| 1473 | * does not exist. |
| 1474 | */ |
| 1475 | private void recordLimits(List<Limits> limitsList, Limits limits) { |
| 1476 | int index = Collections.binarySearch(limitsList, limits); |
| 1477 | if (index < 0) { |
| 1478 | limitsList.add(~index, limits); |
| 1479 | } |
| 1480 | } |
| 1481 | |
| 1482 | /** |
| 1483 | * Handles a moved pointer; this function determines whether the pointer movement resulted |
| 1484 | * in a selection change and, if it has, notifies listeners of this change. |
| 1485 | */ |
| 1486 | private void updateModel() { |
| 1487 | RelativePoint old = mRelativePointer; |
| 1488 | mRelativePointer = new RelativePoint(mPointer); |
| 1489 | if (old != null && mRelativePointer.equals(old)) { |
| 1490 | return; |
| 1491 | } |
| 1492 | |
| 1493 | computeCurrentSelection(); |
| 1494 | notifyListeners(); |
| 1495 | } |
| 1496 | |
| 1497 | /** |
| 1498 | * Computes the currently-selected items. |
| 1499 | */ |
| 1500 | private void computeCurrentSelection() { |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 1501 | if (areItemsCoveredByBand(mRelativePointer, mRelativeOrigin)) { |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1502 | updateSelection(computeBounds()); |
| 1503 | } else { |
| 1504 | mSelection.clear(); |
Ben Kwa | 936a7fc | 2015-12-10 15:21:18 -0800 | [diff] [blame] | 1505 | mPositionNearestOrigin = NOT_SET; |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1506 | } |
| 1507 | } |
| 1508 | |
| 1509 | /** |
| 1510 | * Notifies all listeners of a selection change. Note that this function simply passes |
| 1511 | * mSelection, so computeCurrentSelection() should be called before this |
| 1512 | * function. |
| 1513 | */ |
| 1514 | private void notifyListeners() { |
| 1515 | for (OnSelectionChangedListener listener : mOnSelectionChangedListeners) { |
| 1516 | listener.onSelectionChanged(mSelection); |
| 1517 | } |
| 1518 | } |
| 1519 | |
| 1520 | /** |
| 1521 | * @param rect Rectangle including all covered items. |
| 1522 | */ |
| 1523 | private void updateSelection(Rect rect) { |
| 1524 | int columnStartIndex = |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 1525 | Collections.binarySearch(mColumnBounds, new Limits(rect.left, rect.left)); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1526 | checkState(columnStartIndex >= 0); |
| 1527 | int columnEndIndex = columnStartIndex; |
| 1528 | |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 1529 | for (int i = columnStartIndex; i < mColumnBounds.size() |
| 1530 | && mColumnBounds.get(i).lowerLimit <= rect.right; i++) { |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1531 | columnEndIndex = i; |
| 1532 | } |
| 1533 | |
Ben Kwa | 936a7fc | 2015-12-10 15:21:18 -0800 | [diff] [blame] | 1534 | SparseIntArray firstColumn = |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 1535 | mColumns.get(mColumnBounds.get(columnStartIndex).lowerLimit); |
Kyle Horimoto | d3b3d3d | 2015-08-25 21:03:38 -0700 | [diff] [blame] | 1536 | int rowStartIndex = firstColumn.indexOfKey(rect.top); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1537 | if (rowStartIndex < 0) { |
Ben Kwa | 936a7fc | 2015-12-10 15:21:18 -0800 | [diff] [blame] | 1538 | mPositionNearestOrigin = NOT_SET; |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1539 | return; |
| 1540 | } |
| 1541 | |
| 1542 | int rowEndIndex = rowStartIndex; |
| 1543 | for (int i = rowStartIndex; |
Kyle Horimoto | d3b3d3d | 2015-08-25 21:03:38 -0700 | [diff] [blame] | 1544 | i < firstColumn.size() && firstColumn.keyAt(i) <= rect.bottom; i++) { |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1545 | rowEndIndex = i; |
| 1546 | } |
| 1547 | |
| 1548 | updateSelection(columnStartIndex, columnEndIndex, rowStartIndex, rowEndIndex); |
| 1549 | } |
| 1550 | |
| 1551 | /** |
| 1552 | * Computes the selection given the previously-computed start- and end-indices for each |
| 1553 | * row and column. |
| 1554 | */ |
| 1555 | private void updateSelection( |
| 1556 | int columnStartIndex, int columnEndIndex, int rowStartIndex, int rowEndIndex) { |
| 1557 | mSelection.clear(); |
| 1558 | for (int column = columnStartIndex; column <= columnEndIndex; column++) { |
Ben Kwa | 936a7fc | 2015-12-10 15:21:18 -0800 | [diff] [blame] | 1559 | SparseIntArray items = mColumns.get(mColumnBounds.get(column).lowerLimit); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1560 | for (int row = rowStartIndex; row <= rowEndIndex; row++) { |
Ben Kwa | 936a7fc | 2015-12-10 15:21:18 -0800 | [diff] [blame] | 1561 | int position = items.get(items.keyAt(row)); |
| 1562 | String id = mHelper.getModelIdFromAdapterPosition(position); |
| 1563 | if (id != null) { |
| 1564 | // The adapter inserts items for UI layout purposes that aren't associated |
| 1565 | // with files. Those will have a null model ID. Don't select them. |
| 1566 | mSelection.add(id); |
| 1567 | } |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1568 | if (isPossiblePositionNearestOrigin(column, columnStartIndex, columnEndIndex, |
| 1569 | row, rowStartIndex, rowEndIndex)) { |
| 1570 | // If this is the position nearest the origin, record it now so that it |
| 1571 | // can be returned by endSelection() later. |
Ben Kwa | 936a7fc | 2015-12-10 15:21:18 -0800 | [diff] [blame] | 1572 | mPositionNearestOrigin = position; |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1573 | } |
| 1574 | } |
| 1575 | } |
| 1576 | } |
| 1577 | |
| 1578 | /** |
| 1579 | * @return Returns true if the position is the nearest to the origin, or, in the case of the |
| 1580 | * lower-right corner, whether it is possible that the position is the nearest to the |
| 1581 | * origin. See comment below for reasoning for this special case. |
| 1582 | */ |
| 1583 | private boolean isPossiblePositionNearestOrigin(int columnIndex, int columnStartIndex, |
| 1584 | int columnEndIndex, int rowIndex, int rowStartIndex, int rowEndIndex) { |
| 1585 | int corner = computeCornerNearestOrigin(); |
| 1586 | switch (corner) { |
| 1587 | case UPPER_LEFT: |
| 1588 | return columnIndex == columnStartIndex && rowIndex == rowStartIndex; |
| 1589 | case UPPER_RIGHT: |
| 1590 | return columnIndex == columnEndIndex && rowIndex == rowStartIndex; |
| 1591 | case LOWER_LEFT: |
| 1592 | return columnIndex == columnStartIndex && rowIndex == rowEndIndex; |
| 1593 | case LOWER_RIGHT: |
| 1594 | // Note that in some cases, the last row will not have as many items as there |
| 1595 | // are columns (e.g., if there are 4 items and 3 columns, the second row will |
| 1596 | // only have one item in the first column). This function is invoked for each |
| 1597 | // position from left to right, so return true for any position in the bottom |
| 1598 | // row and only the right-most position in the bottom row will be recorded. |
| 1599 | return rowIndex == rowEndIndex; |
| 1600 | default: |
| 1601 | throw new RuntimeException("Invalid corner type."); |
| 1602 | } |
| 1603 | } |
| 1604 | |
| 1605 | /** |
| 1606 | * Listener for changes in which items have been band selected. |
| 1607 | */ |
| 1608 | static interface OnSelectionChangedListener { |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 1609 | public void onSelectionChanged(Set<String> updatedSelection); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1610 | } |
| 1611 | |
| 1612 | void addOnSelectionChangedListener(OnSelectionChangedListener listener) { |
| 1613 | mOnSelectionChangedListeners.add(listener); |
| 1614 | } |
| 1615 | |
| 1616 | void removeOnSelectionChangedListener(OnSelectionChangedListener listener) { |
| 1617 | mOnSelectionChangedListeners.remove(listener); |
| 1618 | } |
| 1619 | |
| 1620 | /** |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1621 | * Limits of a view item. For example, if an item's left side is at x-value 5 and its right side |
| 1622 | * is at x-value 10, the limits would be from 5 to 10. Used to record the left- and right sides |
| 1623 | * of item columns and the top- and bottom sides of item rows so that it can be determined |
| 1624 | * whether the pointer is located within the bounds of an item. |
| 1625 | */ |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 1626 | private static class Limits implements Comparable<Limits> { |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1627 | int lowerLimit; |
| 1628 | int upperLimit; |
| 1629 | |
| 1630 | Limits(int lowerLimit, int upperLimit) { |
| 1631 | this.lowerLimit = lowerLimit; |
| 1632 | this.upperLimit = upperLimit; |
| 1633 | } |
| 1634 | |
| 1635 | @Override |
| 1636 | public int compareTo(Limits other) { |
| 1637 | return lowerLimit - other.lowerLimit; |
| 1638 | } |
| 1639 | |
| 1640 | @Override |
| 1641 | public boolean equals(Object other) { |
| 1642 | if (!(other instanceof Limits)) { |
| 1643 | return false; |
| 1644 | } |
| 1645 | |
| 1646 | return ((Limits) other).lowerLimit == lowerLimit && |
| 1647 | ((Limits) other).upperLimit == upperLimit; |
| 1648 | } |
| 1649 | } |
| 1650 | |
| 1651 | /** |
| 1652 | * The location of a coordinate relative to items. This class represents a general area of the |
| 1653 | * view as it relates to band selection rather than an explicit point. For example, two |
| 1654 | * different points within an item are considered to have the same "location" because band |
| 1655 | * selection originating within the item would select the same items no matter which point |
| 1656 | * was used. Same goes for points between items as well as those at the very beginning or end |
| 1657 | * of the view. |
| 1658 | * |
| 1659 | * Tracking a coordinate (e.g., an x-value) as a CoordinateLocation instead of as an int has the |
| 1660 | * advantage of tying the value to the Limits of items along that axis. This allows easy |
| 1661 | * selection of items within those Limits as opposed to a search through every item to see if a |
| 1662 | * given coordinate value falls within those Limits. |
| 1663 | */ |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 1664 | private static class RelativeCoordinate |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1665 | implements Comparable<RelativeCoordinate> { |
| 1666 | /** |
| 1667 | * Location describing points after the last known item. |
| 1668 | */ |
| 1669 | static final int AFTER_LAST_ITEM = 0; |
| 1670 | |
| 1671 | /** |
| 1672 | * Location describing points before the first known item. |
| 1673 | */ |
| 1674 | static final int BEFORE_FIRST_ITEM = 1; |
| 1675 | |
| 1676 | /** |
| 1677 | * Location describing points between two items. |
| 1678 | */ |
| 1679 | static final int BETWEEN_TWO_ITEMS = 2; |
| 1680 | |
| 1681 | /** |
| 1682 | * Location describing points within the limits of one item. |
| 1683 | */ |
| 1684 | static final int WITHIN_LIMITS = 3; |
| 1685 | |
| 1686 | /** |
| 1687 | * The type of this coordinate, which is one of AFTER_LAST_ITEM, BEFORE_FIRST_ITEM, |
| 1688 | * BETWEEN_TWO_ITEMS, or WITHIN_LIMITS. |
| 1689 | */ |
| 1690 | final int type; |
| 1691 | |
| 1692 | /** |
| 1693 | * The limits before the coordinate; only populated when type == WITHIN_LIMITS or type == |
| 1694 | * BETWEEN_TWO_ITEMS. |
| 1695 | */ |
| 1696 | Limits limitsBeforeCoordinate; |
| 1697 | |
| 1698 | /** |
| 1699 | * The limits after the coordinate; only populated when type == BETWEEN_TWO_ITEMS. |
| 1700 | */ |
| 1701 | Limits limitsAfterCoordinate; |
| 1702 | |
| 1703 | // Limits of the first known item; only populated when type == BEFORE_FIRST_ITEM. |
| 1704 | Limits mFirstKnownItem; |
| 1705 | // Limits of the last known item; only populated when type == AFTER_LAST_ITEM. |
| 1706 | Limits mLastKnownItem; |
| 1707 | |
| 1708 | /** |
| 1709 | * @param limitsList The sorted limits list for the coordinate type. If this |
| 1710 | * CoordinateLocation is an x-value, mXLimitsList should be passed; otherwise, |
| 1711 | * mYLimitsList should be pased. |
| 1712 | * @param value The coordinate value. |
| 1713 | */ |
| 1714 | RelativeCoordinate(List<Limits> limitsList, int value) { |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 1715 | int index = Collections.binarySearch(limitsList, new Limits(value, value)); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1716 | |
| 1717 | if (index >= 0) { |
| 1718 | this.type = WITHIN_LIMITS; |
| 1719 | this.limitsBeforeCoordinate = limitsList.get(index); |
| 1720 | } else if (~index == 0) { |
| 1721 | this.type = BEFORE_FIRST_ITEM; |
| 1722 | this.mFirstKnownItem = limitsList.get(0); |
| 1723 | } else if (~index == limitsList.size()) { |
| 1724 | Limits lastLimits = limitsList.get(limitsList.size() - 1); |
| 1725 | if (lastLimits.lowerLimit <= value && value <= lastLimits.upperLimit) { |
| 1726 | this.type = WITHIN_LIMITS; |
| 1727 | this.limitsBeforeCoordinate = lastLimits; |
| 1728 | } else { |
| 1729 | this.type = AFTER_LAST_ITEM; |
| 1730 | this.mLastKnownItem = lastLimits; |
| 1731 | } |
| 1732 | } else { |
| 1733 | Limits limitsBeforeIndex = limitsList.get(~index - 1); |
| 1734 | if (limitsBeforeIndex.lowerLimit <= value && value <= limitsBeforeIndex.upperLimit) { |
| 1735 | this.type = WITHIN_LIMITS; |
| 1736 | this.limitsBeforeCoordinate = limitsList.get(~index - 1); |
| 1737 | } else { |
| 1738 | this.type = BETWEEN_TWO_ITEMS; |
| 1739 | this.limitsBeforeCoordinate = limitsList.get(~index - 1); |
| 1740 | this.limitsAfterCoordinate = limitsList.get(~index); |
| 1741 | } |
| 1742 | } |
| 1743 | } |
| 1744 | |
| 1745 | int toComparisonValue() { |
| 1746 | if (type == BEFORE_FIRST_ITEM) { |
| 1747 | return mFirstKnownItem.lowerLimit - 1; |
| 1748 | } else if (type == AFTER_LAST_ITEM) { |
| 1749 | return mLastKnownItem.upperLimit + 1; |
| 1750 | } else if (type == BETWEEN_TWO_ITEMS) { |
| 1751 | return limitsBeforeCoordinate.upperLimit + 1; |
| 1752 | } else { |
| 1753 | return limitsBeforeCoordinate.lowerLimit; |
| 1754 | } |
| 1755 | } |
| 1756 | |
| 1757 | @Override |
| 1758 | public boolean equals(Object other) { |
| 1759 | if (!(other instanceof RelativeCoordinate)) { |
| 1760 | return false; |
| 1761 | } |
| 1762 | |
| 1763 | RelativeCoordinate otherCoordinate = (RelativeCoordinate) other; |
| 1764 | return toComparisonValue() == otherCoordinate.toComparisonValue(); |
| 1765 | } |
| 1766 | |
| 1767 | @Override |
| 1768 | public int compareTo(RelativeCoordinate other) { |
| 1769 | return toComparisonValue() - other.toComparisonValue(); |
| 1770 | } |
| 1771 | } |
| 1772 | |
| 1773 | /** |
| 1774 | * The location of a point relative to the Limits of nearby items; consists of both an x- and |
| 1775 | * y-RelativeCoordinateLocation. |
| 1776 | */ |
| 1777 | private class RelativePoint { |
| 1778 | final RelativeCoordinate xLocation; |
| 1779 | final RelativeCoordinate yLocation; |
| 1780 | |
| 1781 | RelativePoint(Point point) { |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 1782 | this.xLocation = new RelativeCoordinate(mColumnBounds, point.x); |
| 1783 | this.yLocation = new RelativeCoordinate(mRowBounds, point.y); |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1784 | } |
| 1785 | |
| 1786 | @Override |
| 1787 | public boolean equals(Object other) { |
| 1788 | if (!(other instanceof RelativePoint)) { |
| 1789 | return false; |
| 1790 | } |
| 1791 | |
| 1792 | RelativePoint otherPoint = (RelativePoint) other; |
| 1793 | return xLocation.equals(otherPoint.xLocation) && yLocation.equals(otherPoint.yLocation); |
| 1794 | } |
| 1795 | } |
| 1796 | |
| 1797 | /** |
| 1798 | * Generates a rectangle which contains the items selected by the pointer and origin. |
| 1799 | * @return The rectangle, or null if no items were selected. |
| 1800 | */ |
| 1801 | private Rect computeBounds() { |
| 1802 | Rect rect = new Rect(); |
| 1803 | rect.left = getCoordinateValue( |
| 1804 | min(mRelativeOrigin.xLocation, mRelativePointer.xLocation), |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 1805 | mColumnBounds, |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1806 | true); |
| 1807 | rect.right = getCoordinateValue( |
| 1808 | max(mRelativeOrigin.xLocation, mRelativePointer.xLocation), |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 1809 | mColumnBounds, |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1810 | false); |
| 1811 | rect.top = getCoordinateValue( |
| 1812 | min(mRelativeOrigin.yLocation, mRelativePointer.yLocation), |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 1813 | mRowBounds, |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1814 | true); |
| 1815 | rect.bottom = getCoordinateValue( |
| 1816 | max(mRelativeOrigin.yLocation, mRelativePointer.yLocation), |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 1817 | mRowBounds, |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1818 | false); |
| 1819 | return rect; |
| 1820 | } |
| 1821 | |
| 1822 | /** |
| 1823 | * Computes the corner of the selection nearest the origin. |
| 1824 | * @return |
| 1825 | */ |
| 1826 | private int computeCornerNearestOrigin() { |
| 1827 | int cornerValue = 0; |
| 1828 | |
| 1829 | if (mRelativeOrigin.yLocation == |
| 1830 | min(mRelativeOrigin.yLocation, mRelativePointer.yLocation)) { |
| 1831 | cornerValue |= UPPER; |
| 1832 | } else { |
| 1833 | cornerValue |= LOWER; |
| 1834 | } |
| 1835 | |
| 1836 | if (mRelativeOrigin.xLocation == |
| 1837 | min(mRelativeOrigin.xLocation, mRelativePointer.xLocation)) { |
| 1838 | cornerValue |= LEFT; |
| 1839 | } else { |
| 1840 | cornerValue |= RIGHT; |
| 1841 | } |
| 1842 | |
| 1843 | return cornerValue; |
| 1844 | } |
| 1845 | |
| 1846 | private RelativeCoordinate min(RelativeCoordinate first, RelativeCoordinate second) { |
| 1847 | return first.compareTo(second) < 0 ? first : second; |
| 1848 | } |
| 1849 | |
| 1850 | private RelativeCoordinate max(RelativeCoordinate first, RelativeCoordinate second) { |
| 1851 | return first.compareTo(second) > 0 ? first : second; |
| 1852 | } |
| 1853 | |
| 1854 | /** |
| 1855 | * @return The absolute coordinate (i.e., the x- or y-value) of the given relative |
| 1856 | * coordinate. |
| 1857 | */ |
| 1858 | private int getCoordinateValue(RelativeCoordinate coordinate, |
| 1859 | List<Limits> limitsList, boolean isStartOfRange) { |
| 1860 | switch (coordinate.type) { |
| 1861 | case RelativeCoordinate.BEFORE_FIRST_ITEM: |
| 1862 | return limitsList.get(0).lowerLimit; |
| 1863 | case RelativeCoordinate.AFTER_LAST_ITEM: |
| 1864 | return limitsList.get(limitsList.size() - 1).upperLimit; |
| 1865 | case RelativeCoordinate.BETWEEN_TWO_ITEMS: |
| 1866 | if (isStartOfRange) { |
| 1867 | return coordinate.limitsAfterCoordinate.lowerLimit; |
| 1868 | } else { |
| 1869 | return coordinate.limitsBeforeCoordinate.upperLimit; |
| 1870 | } |
| 1871 | case RelativeCoordinate.WITHIN_LIMITS: |
| 1872 | return coordinate.limitsBeforeCoordinate.lowerLimit; |
| 1873 | } |
| 1874 | |
| 1875 | throw new RuntimeException("Invalid coordinate value."); |
| 1876 | } |
| 1877 | |
Steve McKay | 3755016 | 2015-09-08 17:15:25 -0700 | [diff] [blame] | 1878 | private boolean areItemsCoveredByBand( |
Kyle Horimoto | 62a7fd0 | 2015-08-18 13:25:29 -0700 | [diff] [blame] | 1879 | RelativePoint first, RelativePoint second) { |
| 1880 | return doesCoordinateLocationCoverItems(first.xLocation, second.xLocation) && |
| 1881 | doesCoordinateLocationCoverItems(first.yLocation, second.yLocation); |
| 1882 | } |
| 1883 | |
| 1884 | private boolean doesCoordinateLocationCoverItems( |
| 1885 | RelativeCoordinate pointerCoordinate, |
| 1886 | RelativeCoordinate originCoordinate) { |
| 1887 | if (pointerCoordinate.type == RelativeCoordinate.BEFORE_FIRST_ITEM && |
| 1888 | originCoordinate.type == RelativeCoordinate.BEFORE_FIRST_ITEM) { |
| 1889 | return false; |
| 1890 | } |
| 1891 | |
| 1892 | if (pointerCoordinate.type == RelativeCoordinate.AFTER_LAST_ITEM && |
| 1893 | originCoordinate.type == RelativeCoordinate.AFTER_LAST_ITEM) { |
| 1894 | return false; |
| 1895 | } |
| 1896 | |
| 1897 | if (pointerCoordinate.type == RelativeCoordinate.BETWEEN_TWO_ITEMS && |
| 1898 | originCoordinate.type == RelativeCoordinate.BETWEEN_TWO_ITEMS && |
| 1899 | pointerCoordinate.limitsBeforeCoordinate.equals( |
| 1900 | originCoordinate.limitsBeforeCoordinate) && |
| 1901 | pointerCoordinate.limitsAfterCoordinate.equals( |
| 1902 | originCoordinate.limitsAfterCoordinate)) { |
| 1903 | return false; |
| 1904 | } |
| 1905 | |
| 1906 | return true; |
| 1907 | } |
| 1908 | } |
Ben Kwa | a017bc1 | 2015-10-07 14:15:12 -0700 | [diff] [blame] | 1909 | |
| 1910 | // TODO: Might have to move this to a more global level. e.g. What should happen if the |
| 1911 | // user taps a file and then presses shift-down? Currently the RecyclerView never even sees |
| 1912 | // the key event. Perhaps install a global key handler to catch those events while in |
| 1913 | // selection mode? |
| 1914 | @Override |
| 1915 | public boolean onKey(View view, int keyCode, KeyEvent event) { |
| 1916 | // Listen for key-down events. This allows the handler to respond appropriately when |
| 1917 | // the user holds down the arrow keys for navigation. |
| 1918 | if (event.getAction() != KeyEvent.ACTION_DOWN) { |
| 1919 | return false; |
| 1920 | } |
| 1921 | |
Steve McKay | e0cba92 | 2015-11-11 16:26:59 +0900 | [diff] [blame] | 1922 | // Here we unpack information from the event and pass it to an more |
| 1923 | // easily tested method....basically eliminating the need to synthesize |
| 1924 | // events and views and so on in our tests. |
| 1925 | int position = findTargetPosition(view, keyCode); |
| 1926 | if (position == RecyclerView.NO_POSITION) { |
| 1927 | // If there is no valid navigation target, don't handle the keypress. |
| 1928 | return false; |
| 1929 | } |
| 1930 | |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 1931 | return attemptChangeFocus(position, event.isShiftPressed()); |
Steve McKay | e0cba92 | 2015-11-11 16:26:59 +0900 | [diff] [blame] | 1932 | } |
| 1933 | |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 1934 | /** |
| 1935 | * @param targetPosition The adapter position to focus. |
| 1936 | * @param extendSelection |
| 1937 | */ |
Steve McKay | e0cba92 | 2015-11-11 16:26:59 +0900 | [diff] [blame] | 1938 | @VisibleForTesting |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 1939 | boolean attemptChangeFocus(int targetPosition, boolean extendSelection) { |
Steve McKay | e0cba92 | 2015-11-11 16:26:59 +0900 | [diff] [blame] | 1940 | // Focus the new file. |
| 1941 | mEnvironment.focusItem(targetPosition); |
| 1942 | |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 1943 | if (extendSelection) { |
Steve McKay | e0cba92 | 2015-11-11 16:26:59 +0900 | [diff] [blame] | 1944 | if (!hasSelection()) { |
| 1945 | // If there is no selection, start a selection when the user presses shift-arrow. |
| 1946 | toggleSelection(targetPosition); |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 1947 | setSelectionRangeBegin(targetPosition); |
Steve McKay | e0cba92 | 2015-11-11 16:26:59 +0900 | [diff] [blame] | 1948 | } else if (!mSingleSelect) { |
| 1949 | mRanger.snapSelection(targetPosition); |
| 1950 | notifySelectionChanged(); |
| 1951 | } else { |
| 1952 | // We're in single select and have an existing selection. |
| 1953 | // Our best guess as to what the user would expect is to advance the selection. |
| 1954 | clearSelection(); |
| 1955 | toggleSelection(targetPosition); |
| 1956 | } |
| 1957 | } |
| 1958 | |
| 1959 | return true; |
| 1960 | } |
| 1961 | |
| 1962 | /** |
| 1963 | * Returns the adapter position that the key combo is targeted at. |
| 1964 | */ |
| 1965 | private int findTargetPosition(View view, int keyCode) { |
| 1966 | int position = RecyclerView.NO_POSITION; |
Ben Kwa | a017bc1 | 2015-10-07 14:15:12 -0700 | [diff] [blame] | 1967 | if (keyCode == KeyEvent.KEYCODE_MOVE_HOME) { |
Steve McKay | e0cba92 | 2015-11-11 16:26:59 +0900 | [diff] [blame] | 1968 | position = 0; |
Ben Kwa | a017bc1 | 2015-10-07 14:15:12 -0700 | [diff] [blame] | 1969 | } else if (keyCode == KeyEvent.KEYCODE_MOVE_END) { |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 1970 | position = mEnvironment.getItemCount() - 1; |
Ben Kwa | a017bc1 | 2015-10-07 14:15:12 -0700 | [diff] [blame] | 1971 | } else { |
| 1972 | // Find a navigation target based on the arrow key that the user pressed. Ignore |
| 1973 | // navigation targets that aren't items in the recycler view. |
| 1974 | int searchDir = -1; |
| 1975 | switch (keyCode) { |
| 1976 | case KeyEvent.KEYCODE_DPAD_UP: |
| 1977 | searchDir = View.FOCUS_UP; |
| 1978 | break; |
| 1979 | case KeyEvent.KEYCODE_DPAD_DOWN: |
| 1980 | searchDir = View.FOCUS_DOWN; |
| 1981 | break; |
| 1982 | case KeyEvent.KEYCODE_DPAD_LEFT: |
| 1983 | searchDir = View.FOCUS_LEFT; |
| 1984 | break; |
| 1985 | case KeyEvent.KEYCODE_DPAD_RIGHT: |
| 1986 | searchDir = View.FOCUS_RIGHT; |
| 1987 | break; |
| 1988 | } |
| 1989 | if (searchDir != -1) { |
| 1990 | View targetView = view.focusSearch(searchDir); |
Ben Kwa | 98bb6cf | 2015-10-14 08:00:27 -0700 | [diff] [blame] | 1991 | // TargetView can be null, for example, if the user pressed <down> at the bottom of |
| 1992 | // the list. |
| 1993 | if (targetView != null) { |
Steve McKay | e0cba92 | 2015-11-11 16:26:59 +0900 | [diff] [blame] | 1994 | position = mEnvironment.getAdapterPositionForChildView(targetView); |
Ben Kwa | 98bb6cf | 2015-10-14 08:00:27 -0700 | [diff] [blame] | 1995 | } |
Ben Kwa | a017bc1 | 2015-10-07 14:15:12 -0700 | [diff] [blame] | 1996 | } |
| 1997 | } |
Steve McKay | e0cba92 | 2015-11-11 16:26:59 +0900 | [diff] [blame] | 1998 | return position; |
Ben Kwa | a017bc1 | 2015-10-07 14:15:12 -0700 | [diff] [blame] | 1999 | } |
Steve McKay | ef28015 | 2015-06-11 10:10:49 -0700 | [diff] [blame] | 2000 | } |