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 | |
| 19 | import android.support.annotation.VisibleForTesting; |
| 20 | import android.support.v7.widget.RecyclerView; |
| 21 | |
| 22 | import com.android.documentsui.selection.BandController; |
| 23 | import com.android.documentsui.selection.DefaultSelectionManager; |
| 24 | import com.android.documentsui.selection.Selection; |
| 25 | import com.android.documentsui.selection.SelectionManager; |
| 26 | |
| 27 | import java.util.Set; |
| 28 | |
| 29 | import javax.annotation.Nullable; |
| 30 | |
| 31 | /** |
| 32 | * DocumentsUI SelectManager implementation that creates delegate instances |
| 33 | * each time reset is called. |
| 34 | */ |
| 35 | public final class DocsSelectionManager implements SelectionManager { |
| 36 | |
| 37 | private final @SelectionMode int mSelectionMode; |
| 38 | private @Nullable DefaultSelectionManager mDelegate; |
| 39 | |
| 40 | public DocsSelectionManager(@SelectionMode int mode) { |
| 41 | mSelectionMode = mode; |
| 42 | } |
| 43 | |
| 44 | public SelectionManager reset( |
| 45 | RecyclerView.Adapter<?> adapter, |
| 46 | SelectionManager.Environment idLookup, |
| 47 | SelectionManager.SelectionPredicate canSetState) { |
| 48 | |
| 49 | if (mDelegate != null) { |
| 50 | mDelegate.clearSelection(); |
| 51 | } |
| 52 | |
| 53 | mDelegate = new DefaultSelectionManager(mSelectionMode, adapter, idLookup, canSetState); |
| 54 | return this; |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public void bindContoller(BandController controller) { |
| 59 | mDelegate.bindContoller(controller); |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public void addEventListener(EventListener listener) { |
| 64 | mDelegate.addEventListener(listener); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public boolean hasSelection() { |
| 69 | return mDelegate.hasSelection(); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public Selection getSelection() { |
| 74 | return mDelegate.getSelection(); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public Selection getSelection(Selection dest) { |
| 79 | return mDelegate.getSelection(dest); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | @VisibleForTesting |
| 84 | public void replaceSelection(Iterable<String> ids) { |
| 85 | mDelegate.replaceSelection(ids); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public void restoreSelection(Selection other) { |
| 90 | mDelegate.restoreSelection(other); |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public boolean setItemsSelected(Iterable<String> ids, boolean selected) { |
| 95 | return mDelegate.setItemsSelected(ids, selected); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public void clearSelection() { |
| 100 | mDelegate.clearSelection(); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void toggleSelection(String modelId) { |
| 105 | mDelegate.toggleSelection(modelId); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public void startRangeSelection(int pos) { |
| 110 | mDelegate.startRangeSelection(pos); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public void snapRangeSelection(int pos) { |
| 115 | mDelegate.snapRangeSelection(pos); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public void snapProvisionalRangeSelection(int pos) { |
| 120 | mDelegate.snapProvisionalRangeSelection(pos); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public void formNewSelectionRange(int startPos, int endPos) { |
| 125 | mDelegate.formNewSelectionRange(startPos, endPos); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public void cancelProvisionalSelection() { |
| 130 | mDelegate.cancelProvisionalSelection(); |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public void setProvisionalSelection(Set<String> newSelection) { |
| 135 | mDelegate.setProvisionalSelection(newSelection); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public void endRangeSelection() { |
| 140 | mDelegate.endRangeSelection(); |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public boolean isRangeSelectionActive() { |
| 145 | return mDelegate.isRangeSelectionActive(); |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public void setSelectionRangeBegin(int position) { |
| 150 | mDelegate.setSelectionRangeBegin(position); |
| 151 | } |
| 152 | } |