Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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.sorting; |
| 18 | |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 19 | import static com.android.documentsui.Shared.DEBUG; |
| 20 | |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 21 | import android.annotation.IntDef; |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 22 | import android.annotation.Nullable; |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 23 | import android.os.Parcel; |
| 24 | import android.os.Parcelable; |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 25 | import android.provider.DocumentsContract.Document; |
| 26 | import android.util.Log; |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 27 | import android.util.SparseArray; |
| 28 | import android.view.View; |
| 29 | |
| 30 | import com.android.documentsui.R; |
Garfield, Tan | 61f564b | 2016-08-16 13:36:15 -0700 | [diff] [blame] | 31 | import com.android.documentsui.sorting.SortDimension.SortDirection; |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 32 | |
| 33 | import java.lang.annotation.Retention; |
| 34 | import java.lang.annotation.RetentionPolicy; |
| 35 | import java.util.ArrayList; |
| 36 | import java.util.Collection; |
| 37 | import java.util.List; |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 38 | import java.util.function.Consumer; |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * Sort model that contains all columns and their sorting state. |
| 42 | */ |
| 43 | public class SortModel implements Parcelable { |
| 44 | @IntDef({ |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 45 | SORT_DIMENSION_ID_UNKNOWN, |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 46 | SORT_DIMENSION_ID_TITLE, |
| 47 | SORT_DIMENSION_ID_SUMMARY, |
| 48 | SORT_DIMENSION_ID_DATE, |
| 49 | SORT_DIMENSION_ID_SIZE |
| 50 | }) |
| 51 | @Retention(RetentionPolicy.SOURCE) |
| 52 | public @interface SortDimensionId {} |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 53 | public static final int SORT_DIMENSION_ID_UNKNOWN = 0; |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 54 | public static final int SORT_DIMENSION_ID_TITLE = android.R.id.title; |
| 55 | public static final int SORT_DIMENSION_ID_SUMMARY = android.R.id.summary; |
| 56 | public static final int SORT_DIMENSION_ID_SIZE = R.id.size; |
| 57 | public static final int SORT_DIMENSION_ID_DATE = R.id.date; |
| 58 | |
Garfield, Tan | 61f564b | 2016-08-16 13:36:15 -0700 | [diff] [blame] | 59 | @IntDef(flag = true, value = { |
| 60 | UPDATE_TYPE_NONE, |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 61 | UPDATE_TYPE_UNSPECIFIED, |
| 62 | UPDATE_TYPE_STATUS, |
| 63 | UPDATE_TYPE_VISIBILITY, |
| 64 | UPDATE_TYPE_SORTING |
| 65 | }) |
| 66 | @Retention(RetentionPolicy.SOURCE) |
| 67 | public @interface UpdateType {} |
| 68 | /** |
Garfield, Tan | 61f564b | 2016-08-16 13:36:15 -0700 | [diff] [blame] | 69 | * Default value for update type. Nothing is updated. |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 70 | */ |
Garfield, Tan | 61f564b | 2016-08-16 13:36:15 -0700 | [diff] [blame] | 71 | public static final int UPDATE_TYPE_NONE = 0; |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 72 | /** |
| 73 | * Indicates the status of sorting has changed, i.e. whether soring is enabled. |
| 74 | */ |
| 75 | public static final int UPDATE_TYPE_STATUS = 1; |
| 76 | /** |
| 77 | * Indicates the visibility of at least one dimension has changed. |
| 78 | */ |
Garfield, Tan | 61f564b | 2016-08-16 13:36:15 -0700 | [diff] [blame] | 79 | public static final int UPDATE_TYPE_VISIBILITY = 1 << 1; |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 80 | /** |
| 81 | * Indicates the sorting order has changed, either because the sorted dimension has changed or |
| 82 | * the sort direction has changed. |
| 83 | */ |
Garfield, Tan | 61f564b | 2016-08-16 13:36:15 -0700 | [diff] [blame] | 84 | public static final int UPDATE_TYPE_SORTING = 1 << 2; |
| 85 | /** |
| 86 | * Anything can be changed if the type is unspecified. |
| 87 | */ |
| 88 | public static final int UPDATE_TYPE_UNSPECIFIED = -1; |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 89 | |
| 90 | private static final String TAG = "SortModel"; |
| 91 | |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 92 | private final SparseArray<SortDimension> mDimensions; |
| 93 | |
| 94 | private transient final List<UpdateListener> mListeners; |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 95 | private transient Consumer<SortDimension> mMetricRecorder; |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 96 | |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 97 | private int mDefaultDimensionId = SORT_DIMENSION_ID_UNKNOWN; |
| 98 | private boolean mIsUserSpecified = false; |
| 99 | private @Nullable SortDimension mSortedDimension; |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 100 | |
| 101 | private boolean mIsSortEnabled = true; |
| 102 | |
| 103 | public SortModel(Collection<SortDimension> columns) { |
| 104 | mDimensions = new SparseArray<>(columns.size()); |
| 105 | |
| 106 | for (SortDimension column : columns) { |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 107 | if (column.getId() == SORT_DIMENSION_ID_UNKNOWN) { |
| 108 | throw new IllegalArgumentException( |
| 109 | "SortDimension id can't be " + SORT_DIMENSION_ID_UNKNOWN + "."); |
| 110 | } |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 111 | if (mDimensions.get(column.getId()) != null) { |
| 112 | throw new IllegalStateException( |
| 113 | "SortDimension id must be unique. Duplicate id: " + column.getId()); |
| 114 | } |
| 115 | mDimensions.put(column.getId(), column); |
| 116 | } |
| 117 | |
| 118 | mListeners = new ArrayList<>(); |
| 119 | } |
| 120 | |
| 121 | public int getSize() { |
| 122 | return mDimensions.size(); |
| 123 | } |
| 124 | |
| 125 | public SortDimension getDimensionAt(int index) { |
| 126 | return mDimensions.valueAt(index); |
| 127 | } |
| 128 | |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 129 | public @Nullable SortDimension getDimensionById(int id) { |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 130 | return mDimensions.get(id); |
| 131 | } |
| 132 | |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 133 | /** |
| 134 | * Gets the sorted dimension id. |
| 135 | * @return the sorted dimension id or {@link #SORT_DIMENSION_ID_UNKNOWN} if there is no sorted |
| 136 | * dimension. |
| 137 | */ |
| 138 | public int getSortedDimensionId() { |
| 139 | return mSortedDimension != null ? mSortedDimension.getId() : SORT_DIMENSION_ID_UNKNOWN; |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Garfield, Tan | 61f564b | 2016-08-16 13:36:15 -0700 | [diff] [blame] | 142 | public @SortDirection int getCurrentSortDirection() { |
| 143 | return mSortedDimension != null |
| 144 | ? mSortedDimension.getSortDirection() |
| 145 | : SortDimension.SORT_DIRECTION_NONE; |
| 146 | } |
| 147 | |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 148 | public void setSortEnabled(boolean enabled) { |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 149 | mIsSortEnabled = enabled; |
| 150 | |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 151 | notifyListeners(UPDATE_TYPE_STATUS); |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | public boolean isSortEnabled() { |
| 155 | return mIsSortEnabled; |
| 156 | } |
| 157 | |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 158 | /** |
| 159 | * Sort by the default direction of the given dimension if user has never specified any sort |
| 160 | * direction before. |
| 161 | * @param dimensionId the id of the dimension |
| 162 | */ |
| 163 | public void setDefaultDimension(int dimensionId) { |
| 164 | final boolean mayNeedSorting = (mDefaultDimensionId != dimensionId); |
| 165 | |
| 166 | mDefaultDimensionId = dimensionId; |
| 167 | |
| 168 | if (mayNeedSorting) { |
| 169 | sortOnDefault(); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | void setMetricRecorder(Consumer<SortDimension> metricRecorder) { |
| 174 | mMetricRecorder = metricRecorder; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Sort by given dimension and direction. Should only be used when user explicitly asks to sort |
| 179 | * docs. |
| 180 | * @param dimensionId the id of the dimension |
| 181 | * @param direction the direction to sort docs in |
| 182 | */ |
Garfield, Tan | 61f564b | 2016-08-16 13:36:15 -0700 | [diff] [blame] | 183 | public void sortByUser(int dimensionId, @SortDirection int direction) { |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 184 | if (!mIsSortEnabled) { |
| 185 | throw new IllegalStateException("Sort is not enabled."); |
| 186 | } |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 187 | |
| 188 | SortDimension dimension = mDimensions.get(dimensionId); |
| 189 | if (dimension == null) { |
| 190 | throw new IllegalArgumentException("Unknown column id: " + dimensionId); |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 193 | sortByDimension(dimension, direction); |
| 194 | |
| 195 | if (mMetricRecorder != null) { |
| 196 | mMetricRecorder.accept(dimension); |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 197 | } |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 198 | |
| 199 | mIsUserSpecified = true; |
| 200 | } |
| 201 | |
| 202 | private void sortByDimension( |
Garfield, Tan | 61f564b | 2016-08-16 13:36:15 -0700 | [diff] [blame] | 203 | SortDimension newSortedDimension, @SortDirection int direction) { |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 204 | if (newSortedDimension == mSortedDimension |
| 205 | && mSortedDimension.mSortDirection == direction) { |
| 206 | // Sort direction not changed, no need to proceed. |
| 207 | return; |
| 208 | } |
| 209 | |
| 210 | if ((newSortedDimension.getSortCapability() & direction) == 0) { |
| 211 | throw new IllegalStateException( |
| 212 | "Dimension with id: " + newSortedDimension.getId() |
| 213 | + " can't be sorted in direction:" + direction); |
| 214 | } |
| 215 | |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 216 | switch (direction) { |
| 217 | case SortDimension.SORT_DIRECTION_ASCENDING: |
| 218 | case SortDimension.SORT_DIRECTION_DESCENDING: |
| 219 | newSortedDimension.mSortDirection = direction; |
| 220 | break; |
| 221 | default: |
| 222 | throw new IllegalArgumentException("Unknown sort direction: " + direction); |
| 223 | } |
| 224 | |
| 225 | if (mSortedDimension != null && mSortedDimension != newSortedDimension) { |
| 226 | mSortedDimension.mSortDirection = SortDimension.SORT_DIRECTION_NONE; |
| 227 | } |
| 228 | |
| 229 | mSortedDimension = newSortedDimension; |
| 230 | |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 231 | notifyListeners(UPDATE_TYPE_SORTING); |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | public void setDimensionVisibility(int columnId, int visibility) { |
| 235 | assert(mDimensions.get(columnId) != null); |
| 236 | |
| 237 | mDimensions.get(columnId).mVisibility = visibility; |
| 238 | |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 239 | notifyListeners(UPDATE_TYPE_VISIBILITY); |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 242 | public @Nullable String getDocumentSortQuery() { |
| 243 | final int id = getSortedDimensionId(); |
| 244 | final String columnName; |
| 245 | switch (id) { |
Garfield, Tan | 61f564b | 2016-08-16 13:36:15 -0700 | [diff] [blame] | 246 | case SORT_DIMENSION_ID_UNKNOWN: |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 247 | return null; |
| 248 | case SortModel.SORT_DIMENSION_ID_TITLE: |
| 249 | columnName = Document.COLUMN_DISPLAY_NAME; |
| 250 | break; |
| 251 | case SortModel.SORT_DIMENSION_ID_DATE: |
| 252 | columnName = Document.COLUMN_LAST_MODIFIED; |
| 253 | break; |
| 254 | case SortModel.SORT_DIMENSION_ID_SIZE: |
| 255 | columnName = Document.COLUMN_SIZE; |
| 256 | break; |
| 257 | default: |
| 258 | throw new IllegalStateException( |
| 259 | "Unexpected sort dimension id: " + id); |
| 260 | } |
| 261 | |
| 262 | final SortDimension dimension = getDimensionById(id); |
| 263 | final String direction; |
| 264 | switch (dimension.getSortDirection()) { |
| 265 | case SortDimension.SORT_DIRECTION_ASCENDING: |
| 266 | direction = " ASC"; |
| 267 | break; |
| 268 | case SortDimension.SORT_DIRECTION_DESCENDING: |
| 269 | direction = " DESC"; |
| 270 | break; |
| 271 | default: |
| 272 | throw new IllegalStateException( |
| 273 | "Unexpected sort direction: " + dimension.getSortDirection()); |
| 274 | } |
| 275 | |
| 276 | return columnName + direction; |
| 277 | } |
| 278 | |
| 279 | private void notifyListeners(@UpdateType int updateType) { |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 280 | for (int i = mListeners.size() - 1; i >= 0; --i) { |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 281 | mListeners.get(i).onModelUpdate(this, updateType); |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
| 285 | public void addListener(UpdateListener listener) { |
| 286 | mListeners.add(listener); |
| 287 | } |
| 288 | |
| 289 | public void removeListener(UpdateListener listener) { |
| 290 | mListeners.remove(listener); |
| 291 | } |
| 292 | |
| 293 | public void clearSortDirection() { |
| 294 | if (mSortedDimension != null) { |
| 295 | mSortedDimension.mSortDirection = SortDimension.SORT_DIRECTION_NONE; |
| 296 | mSortedDimension = null; |
| 297 | } |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 298 | |
| 299 | mIsUserSpecified = false; |
| 300 | |
| 301 | sortOnDefault(); |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Sort by default dimension and direction if there is no history of user specifying a sort |
| 306 | * order. |
| 307 | */ |
| 308 | private void sortOnDefault() { |
| 309 | if (!mIsUserSpecified) { |
| 310 | SortDimension dimension = mDimensions.get(mDefaultDimensionId); |
| 311 | if (dimension == null) { |
| 312 | if (DEBUG) Log.d(TAG, "No default sort dimension."); |
| 313 | return; |
| 314 | } |
| 315 | |
| 316 | sortByDimension(dimension, dimension.getDefaultSortDirection()); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | @Override |
| 321 | public boolean equals(Object o) { |
| 322 | if (o == null || !(o instanceof SortModel)) { |
| 323 | return false; |
| 324 | } |
| 325 | |
| 326 | if (this == o) { |
| 327 | return true; |
| 328 | } |
| 329 | |
| 330 | SortModel other = (SortModel) o; |
| 331 | if (mDimensions.size() != other.mDimensions.size()) { |
| 332 | return false; |
| 333 | } |
| 334 | for (int i = 0; i < mDimensions.size(); ++i) { |
| 335 | final SortDimension dimension = mDimensions.valueAt(i); |
| 336 | final int id = dimension.getId(); |
| 337 | if (!dimension.equals(other.getDimensionById(id))) { |
| 338 | return false; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | return mDefaultDimensionId == other.mDefaultDimensionId |
| 343 | && mIsSortEnabled == other.mIsSortEnabled |
| 344 | && (mSortedDimension == other.mSortedDimension |
| 345 | || mSortedDimension.equals(other.mSortedDimension)); |
| 346 | } |
| 347 | |
| 348 | @Override |
| 349 | public String toString() { |
| 350 | return new StringBuilder() |
| 351 | .append("SortModel{") |
| 352 | .append("enabled=").append(mIsSortEnabled) |
| 353 | .append(", dimensions=").append(mDimensions) |
| 354 | .append(", defaultDimensionId=").append(mDefaultDimensionId) |
| 355 | .append(", sortedDimension=").append(mSortedDimension) |
| 356 | .append("}") |
| 357 | .toString(); |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | @Override |
| 361 | public int describeContents() { |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | @Override |
| 366 | public void writeToParcel(Parcel out, int flag) { |
| 367 | out.writeInt(mDimensions.size()); |
| 368 | for (int i = 0; i < mDimensions.size(); ++i) { |
| 369 | out.writeParcelable(mDimensions.valueAt(i), flag); |
| 370 | } |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 371 | |
| 372 | out.writeInt(mDefaultDimensionId); |
| 373 | out.writeInt(mIsSortEnabled ? 1 : 0); |
Garfield, Tan | 61f564b | 2016-08-16 13:36:15 -0700 | [diff] [blame] | 374 | out.writeInt(getSortedDimensionId()); |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | public static Parcelable.Creator<SortModel> CREATOR = new Parcelable.Creator<SortModel>() { |
| 378 | |
| 379 | @Override |
| 380 | public SortModel createFromParcel(Parcel in) { |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 381 | final int size = in.readInt(); |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 382 | Collection<SortDimension> columns = new ArrayList<>(size); |
| 383 | for (int i = 0; i < size; ++i) { |
| 384 | columns.add(in.readParcelable(getClass().getClassLoader())); |
| 385 | } |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 386 | SortModel model = new SortModel(columns); |
| 387 | |
| 388 | model.mDefaultDimensionId = in.readInt(); |
| 389 | model.mIsSortEnabled = (in.readInt() == 1); |
| 390 | model.mSortedDimension = model.getDimensionById(in.readInt()); |
| 391 | |
| 392 | return model; |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | @Override |
| 396 | public SortModel[] newArray(int size) { |
| 397 | return new SortModel[size]; |
| 398 | } |
| 399 | }; |
| 400 | |
| 401 | /** |
| 402 | * Creates a model for all other roots. |
| 403 | * |
| 404 | * TODO: move definition of columns into xml, and inflate model from it. |
| 405 | */ |
| 406 | public static SortModel createModel() { |
| 407 | List<SortDimension> dimensions = new ArrayList<>(4); |
| 408 | SortDimension.Builder builder = new SortDimension.Builder(); |
| 409 | |
| 410 | // Name column |
| 411 | dimensions.add(builder |
| 412 | .withId(SORT_DIMENSION_ID_TITLE) |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 413 | .withLabelId(R.string.sort_dimension_name) |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 414 | .withDataType(SortDimension.DATA_TYPE_STRING) |
| 415 | .withSortCapability(SortDimension.SORT_CAPABILITY_BOTH_DIRECTION) |
| 416 | .withDefaultSortDirection(SortDimension.SORT_DIRECTION_ASCENDING) |
| 417 | .withVisibility(View.VISIBLE) |
| 418 | .build() |
| 419 | ); |
| 420 | |
| 421 | // Summary column |
| 422 | // Summary is only visible in Downloads and Recents root. |
| 423 | dimensions.add(builder |
| 424 | .withId(SORT_DIMENSION_ID_SUMMARY) |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 425 | .withLabelId(R.string.sort_dimension_summary) |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 426 | .withDataType(SortDimension.DATA_TYPE_STRING) |
| 427 | .withSortCapability(SortDimension.SORT_CAPABILITY_NONE) |
| 428 | .withVisibility(View.INVISIBLE) |
| 429 | .build() |
| 430 | ); |
| 431 | |
| 432 | // Size column |
| 433 | dimensions.add(builder |
| 434 | .withId(SORT_DIMENSION_ID_SIZE) |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 435 | .withLabelId(R.string.sort_dimension_size) |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 436 | .withDataType(SortDimension.DATA_TYPE_NUMBER) |
| 437 | .withSortCapability(SortDimension.SORT_CAPABILITY_BOTH_DIRECTION) |
| 438 | .withDefaultSortDirection(SortDimension.SORT_DIRECTION_ASCENDING) |
| 439 | .withVisibility(View.VISIBLE) |
| 440 | .build() |
| 441 | ); |
| 442 | |
| 443 | // Date column |
| 444 | dimensions.add(builder |
| 445 | .withId(SORT_DIMENSION_ID_DATE) |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 446 | .withLabelId(R.string.sort_dimension_date) |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 447 | .withDataType(SortDimension.DATA_TYPE_NUMBER) |
| 448 | .withSortCapability(SortDimension.SORT_CAPABILITY_BOTH_DIRECTION) |
| 449 | .withDefaultSortDirection(SortDimension.SORT_DIRECTION_DESCENDING) |
| 450 | .withVisibility(View.VISIBLE) |
| 451 | .build() |
| 452 | ); |
| 453 | |
| 454 | return new SortModel(dimensions); |
| 455 | } |
| 456 | |
| 457 | public interface UpdateListener { |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 458 | void onModelUpdate(SortModel newModel, @UpdateType int updateType); |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 459 | } |
| 460 | } |