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