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