Steve McKay | 878b271 | 2017-08-21 15:17:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.documentsui; |
| 18 | |
Riddle Hsu | 0c37598 | 2018-06-21 22:06:43 +0800 | [diff] [blame] | 19 | import android.os.Bundle; |
| 20 | import android.view.MotionEvent; |
Steve McKay | 878b271 | 2017-08-21 15:17:09 -0700 | [diff] [blame] | 21 | |
Riddle Hsu | 0c37598 | 2018-06-21 22:06:43 +0800 | [diff] [blame] | 22 | import androidx.annotation.VisibleForTesting; |
| 23 | import androidx.recyclerview.selection.ItemDetailsLookup; |
| 24 | import androidx.recyclerview.selection.ItemKeyProvider; |
| 25 | import androidx.recyclerview.selection.MutableSelection; |
| 26 | import androidx.recyclerview.selection.Selection; |
| 27 | import androidx.recyclerview.selection.SelectionTracker; |
| 28 | import androidx.recyclerview.widget.RecyclerView.AdapterDataObserver; |
Steve McKay | 878b271 | 2017-08-21 15:17:09 -0700 | [diff] [blame] | 29 | |
| 30 | import java.util.Set; |
| 31 | |
Steve McKay | 878b271 | 2017-08-21 15:17:09 -0700 | [diff] [blame] | 32 | /** |
| 33 | * DocumentsUI SelectManager implementation that creates delegate instances |
| 34 | * each time reset is called. |
| 35 | */ |
Riddle Hsu | 0c37598 | 2018-06-21 22:06:43 +0800 | [diff] [blame] | 36 | public final class DocsSelectionHelper extends SelectionTracker<String> { |
Steve McKay | 878b271 | 2017-08-21 15:17:09 -0700 | [diff] [blame] | 37 | |
Steve McKay | 8fc9d89 | 2017-08-22 13:57:45 -0700 | [diff] [blame] | 38 | private final DelegateFactory mFactory; |
Steve McKay | 878b271 | 2017-08-21 15:17:09 -0700 | [diff] [blame] | 39 | |
Steve McKay | 7bd7f0d | 2018-04-27 14:24:25 -0700 | [diff] [blame] | 40 | // initialize to a dummy object incase we get some input |
| 41 | // event drive calls before we're properly initialized. |
| 42 | // See: b/69306667. |
Riddle Hsu | 0c37598 | 2018-06-21 22:06:43 +0800 | [diff] [blame] | 43 | private SelectionTracker<String> mDelegate = new DummySelectionTracker<>(); |
Steve McKay | 8fc9d89 | 2017-08-22 13:57:45 -0700 | [diff] [blame] | 44 | |
| 45 | @VisibleForTesting |
Riddle Hsu | 0c37598 | 2018-06-21 22:06:43 +0800 | [diff] [blame] | 46 | DocsSelectionHelper(DelegateFactory factory) { |
Steve McKay | 8fc9d89 | 2017-08-22 13:57:45 -0700 | [diff] [blame] | 47 | mFactory = factory; |
Steve McKay | 878b271 | 2017-08-21 15:17:09 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Riddle Hsu | 0c37598 | 2018-06-21 22:06:43 +0800 | [diff] [blame] | 50 | public void reset(SelectionTracker<String> selectionTracker) { |
Steve McKay | 878b271 | 2017-08-21 15:17:09 -0700 | [diff] [blame] | 51 | if (mDelegate != null) { |
| 52 | mDelegate.clearSelection(); |
| 53 | } |
Riddle Hsu | 0c37598 | 2018-06-21 22:06:43 +0800 | [diff] [blame] | 54 | mDelegate = mFactory.create(selectionTracker); |
Steve McKay | 878b271 | 2017-08-21 15:17:09 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | @Override |
Steve McKay | 365e3cb | 2017-08-31 10:27:08 -0700 | [diff] [blame] | 58 | public void addObserver(SelectionObserver listener) { |
| 59 | mDelegate.addObserver(listener); |
Steve McKay | 878b271 | 2017-08-21 15:17:09 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public boolean hasSelection() { |
| 64 | return mDelegate.hasSelection(); |
| 65 | } |
| 66 | |
| 67 | @Override |
Riddle Hsu | 0c37598 | 2018-06-21 22:06:43 +0800 | [diff] [blame] | 68 | public Selection<String> getSelection() { |
Steve McKay | 878b271 | 2017-08-21 15:17:09 -0700 | [diff] [blame] | 69 | return mDelegate.getSelection(); |
| 70 | } |
| 71 | |
| 72 | @Override |
Riddle Hsu | 0c37598 | 2018-06-21 22:06:43 +0800 | [diff] [blame] | 73 | public void copySelection(MutableSelection<String> dest) { |
Steve McKay | 2d19a69 | 2017-08-22 09:05:08 -0700 | [diff] [blame] | 74 | mDelegate.copySelection(dest); |
Steve McKay | 878b271 | 2017-08-21 15:17:09 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Steve McKay | 365e3cb | 2017-08-31 10:27:08 -0700 | [diff] [blame] | 77 | @Override |
| 78 | public boolean isSelected(String id) { |
| 79 | return mDelegate.isSelected(id); |
| 80 | } |
| 81 | |
Steve McKay | 878b271 | 2017-08-21 15:17:09 -0700 | [diff] [blame] | 82 | @VisibleForTesting |
| 83 | public void replaceSelection(Iterable<String> ids) { |
Steve McKay | 2d19a69 | 2017-08-22 09:05:08 -0700 | [diff] [blame] | 84 | mDelegate.clearSelection(); |
| 85 | mDelegate.setItemsSelected(ids, true); |
Steve McKay | 878b271 | 2017-08-21 15:17:09 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | @Override |
Steve McKay | 878b271 | 2017-08-21 15:17:09 -0700 | [diff] [blame] | 89 | public boolean setItemsSelected(Iterable<String> ids, boolean selected) { |
| 90 | return mDelegate.setItemsSelected(ids, selected); |
| 91 | } |
| 92 | |
| 93 | @Override |
Riddle Hsu | 0c37598 | 2018-06-21 22:06:43 +0800 | [diff] [blame] | 94 | public boolean clearSelection() { |
| 95 | return mDelegate.clearSelection(); |
Steve McKay | 878b271 | 2017-08-21 15:17:09 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | @Override |
Steve McKay | 365e3cb | 2017-08-31 10:27:08 -0700 | [diff] [blame] | 99 | public boolean select(String modelId) { |
| 100 | return mDelegate.select(modelId); |
Steve McKay | 878b271 | 2017-08-21 15:17:09 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Override |
Steve McKay | 365e3cb | 2017-08-31 10:27:08 -0700 | [diff] [blame] | 104 | public boolean deselect(String modelId) { |
| 105 | return mDelegate.deselect(modelId); |
Steve McKay | 878b271 | 2017-08-21 15:17:09 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | @Override |
Steve McKay | 365e3cb | 2017-08-31 10:27:08 -0700 | [diff] [blame] | 109 | public void startRange(int pos) { |
| 110 | mDelegate.startRange(pos); |
Steve McKay | 878b271 | 2017-08-21 15:17:09 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | @Override |
Steve McKay | 365e3cb | 2017-08-31 10:27:08 -0700 | [diff] [blame] | 114 | public void extendRange(int pos) { |
| 115 | mDelegate.extendRange(pos); |
Steve McKay | 878b271 | 2017-08-21 15:17:09 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | @Override |
Steve McKay | 365e3cb | 2017-08-31 10:27:08 -0700 | [diff] [blame] | 119 | public void endRange() { |
| 120 | mDelegate.endRange(); |
Steve McKay | 878b271 | 2017-08-21 15:17:09 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | @Override |
Steve McKay | 365e3cb | 2017-08-31 10:27:08 -0700 | [diff] [blame] | 124 | public boolean isRangeActive() { |
| 125 | return mDelegate.isRangeActive(); |
Steve McKay | 878b271 | 2017-08-21 15:17:09 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | @Override |
Steve McKay | 365e3cb | 2017-08-31 10:27:08 -0700 | [diff] [blame] | 129 | public void anchorRange(int position) { |
| 130 | mDelegate.anchorRange(position); |
Steve McKay | 878b271 | 2017-08-21 15:17:09 -0700 | [diff] [blame] | 131 | } |
Steve McKay | 8fc9d89 | 2017-08-22 13:57:45 -0700 | [diff] [blame] | 132 | |
Riddle Hsu | 0c37598 | 2018-06-21 22:06:43 +0800 | [diff] [blame] | 133 | @Override |
| 134 | public void onSaveInstanceState(Bundle state) { |
| 135 | mDelegate.onSaveInstanceState(state); |
Steve McKay | 8fc9d89 | 2017-08-22 13:57:45 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Riddle Hsu | 0c37598 | 2018-06-21 22:06:43 +0800 | [diff] [blame] | 138 | @Override |
| 139 | public void onRestoreInstanceState(Bundle state) { |
| 140 | mDelegate.onRestoreInstanceState(state); |
| 141 | } |
| 142 | |
| 143 | // Below overridden protected methods are not used for delegation. These empty implementations |
| 144 | // are just required by abstract declaration of parent class. |
| 145 | @Override |
| 146 | protected void restoreSelection(Selection<String> selection) { |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | protected AdapterDataObserver getAdapterDataObserver() { |
| 151 | return null; |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | protected void extendProvisionalRange(int position) { |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | protected void setProvisionalSelection(Set<String> newSelection) { |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | protected void clearProvisionalSelection() { |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | protected void mergeProvisionalSelection() { |
| 168 | } |
| 169 | |
| 170 | public static DocsSelectionHelper create() { |
| 171 | return new DocsSelectionHelper(DelegateFactory.INSTANCE); |
Steve McKay | 8fc9d89 | 2017-08-22 13:57:45 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Use of a factory to create selection manager instances allows testable instances to |
| 176 | * be inject from tests. |
| 177 | */ |
| 178 | @VisibleForTesting |
| 179 | static class DelegateFactory { |
| 180 | static final DelegateFactory INSTANCE = new DelegateFactory(); |
| 181 | |
Riddle Hsu | 0c37598 | 2018-06-21 22:06:43 +0800 | [diff] [blame] | 182 | SelectionTracker<String> create(SelectionTracker<String> selectionTracker) { |
| 183 | return selectionTracker; |
Steve McKay | 8fc9d89 | 2017-08-22 13:57:45 -0700 | [diff] [blame] | 184 | } |
| 185 | } |
Steve McKay | 7bd7f0d | 2018-04-27 14:24:25 -0700 | [diff] [blame] | 186 | |
| 187 | /** |
Riddle Hsu | 0c37598 | 2018-06-21 22:06:43 +0800 | [diff] [blame] | 188 | * Facilitates the use of ItemDetailsLookup. |
Steve McKay | 7bd7f0d | 2018-04-27 14:24:25 -0700 | [diff] [blame] | 189 | */ |
Riddle Hsu | 0c37598 | 2018-06-21 22:06:43 +0800 | [diff] [blame] | 190 | public static abstract class DocDetailsLookup extends ItemDetailsLookup<String> { |
Steve McKay | 7bd7f0d | 2018-04-27 14:24:25 -0700 | [diff] [blame] | 191 | |
Riddle Hsu | 0c37598 | 2018-06-21 22:06:43 +0800 | [diff] [blame] | 192 | // Override as public for usages in other packages. |
Steve McKay | 7bd7f0d | 2018-04-27 14:24:25 -0700 | [diff] [blame] | 193 | @Override |
Riddle Hsu | 0c37598 | 2018-06-21 22:06:43 +0800 | [diff] [blame] | 194 | public boolean overItemWithSelectionKey(MotionEvent e) { |
| 195 | return super.overItemWithSelectionKey(e); |
Steve McKay | 7bd7f0d | 2018-04-27 14:24:25 -0700 | [diff] [blame] | 196 | } |
Riddle Hsu | 0c37598 | 2018-06-21 22:06:43 +0800 | [diff] [blame] | 197 | } |
Steve McKay | 7bd7f0d | 2018-04-27 14:24:25 -0700 | [diff] [blame] | 198 | |
Riddle Hsu | 0c37598 | 2018-06-21 22:06:43 +0800 | [diff] [blame] | 199 | /** |
| 200 | * Facilitates the use of stable ids. |
| 201 | */ |
| 202 | public static abstract class StableIdProvider extends ItemKeyProvider<String> { |
Steve McKay | 7bd7f0d | 2018-04-27 14:24:25 -0700 | [diff] [blame] | 203 | |
Riddle Hsu | 0c37598 | 2018-06-21 22:06:43 +0800 | [diff] [blame] | 204 | protected StableIdProvider() { |
| 205 | super(ItemKeyProvider.SCOPE_MAPPED); |
Steve McKay | 7bd7f0d | 2018-04-27 14:24:25 -0700 | [diff] [blame] | 206 | } |
| 207 | } |
Steve McKay | 878b271 | 2017-08-21 15:17:09 -0700 | [diff] [blame] | 208 | } |