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 | |
| 19 | import android.annotation.Nullable; |
Steve McKay | 9de0da6 | 2016-08-25 15:18:23 -0700 | [diff] [blame] | 20 | import android.app.Activity; |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 21 | import android.view.View; |
| 22 | |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 23 | import com.android.documentsui.Metrics; |
Steve McKay | 9de0da6 | 2016-08-25 15:18:23 -0700 | [diff] [blame] | 24 | import com.android.documentsui.R; |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 25 | import com.android.documentsui.State; |
Garfield, Tan | 61f564b | 2016-08-16 13:36:15 -0700 | [diff] [blame] | 26 | import com.android.documentsui.State.ViewMode; |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * A high level controller that manages sort widgets. This is useful when sort widgets can and will |
| 30 | * appear in different locations in the UI, like the menu, above the file list (pinned) and embedded |
| 31 | * at the top of file list... and maybe other places too. |
| 32 | */ |
Steve McKay | 9de0da6 | 2016-08-25 15:18:23 -0700 | [diff] [blame] | 33 | public final class SortController { |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 34 | |
Steve McKay | 9de0da6 | 2016-08-25 15:18:23 -0700 | [diff] [blame] | 35 | private final WidgetController mDropdownController; |
| 36 | private final @Nullable WidgetController mTableHeaderController; |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 37 | |
Steve McKay | 9de0da6 | 2016-08-25 15:18:23 -0700 | [diff] [blame] | 38 | public SortController( |
| 39 | WidgetController dropdownController, |
| 40 | @Nullable WidgetController tableHeaderController) { |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 41 | |
Steve McKay | 9de0da6 | 2016-08-25 15:18:23 -0700 | [diff] [blame] | 42 | assert(dropdownController != null); |
| 43 | mDropdownController = dropdownController; |
| 44 | mTableHeaderController = tableHeaderController; |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Garfield, Tan | 61f564b | 2016-08-16 13:36:15 -0700 | [diff] [blame] | 47 | public void onViewModeChanged(@ViewMode int mode) { |
Steve McKay | 9de0da6 | 2016-08-25 15:18:23 -0700 | [diff] [blame] | 48 | // in phone layouts we only ever have the dropdown sort controller. |
| 49 | if (mTableHeaderController == null) { |
Garfield, Tan | 61f564b | 2016-08-16 13:36:15 -0700 | [diff] [blame] | 50 | mDropdownController.setVisibility(View.VISIBLE); |
Steve McKay | 9de0da6 | 2016-08-25 15:18:23 -0700 | [diff] [blame] | 51 | return; |
Garfield, Tan | 61f564b | 2016-08-16 13:36:15 -0700 | [diff] [blame] | 52 | } |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 53 | |
Steve McKay | 9de0da6 | 2016-08-25 15:18:23 -0700 | [diff] [blame] | 54 | // in tablet mode, we have fancy pants tabular header. |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 55 | switch (mode) { |
| 56 | case State.MODE_GRID: |
Steve McKay | 9de0da6 | 2016-08-25 15:18:23 -0700 | [diff] [blame] | 57 | case State.MODE_UNKNOWN: |
| 58 | mTableHeaderController.setVisibility(View.GONE); |
| 59 | mDropdownController.setVisibility(View.VISIBLE); |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 60 | break; |
| 61 | case State.MODE_LIST: |
Steve McKay | 9de0da6 | 2016-08-25 15:18:23 -0700 | [diff] [blame] | 62 | mTableHeaderController.setVisibility(View.VISIBLE); |
| 63 | mDropdownController.setVisibility(View.GONE); |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 64 | break; |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
Steve McKay | 9de0da6 | 2016-08-25 15:18:23 -0700 | [diff] [blame] | 68 | public static SortController create( |
| 69 | Activity activity, |
| 70 | @ViewMode int initialMode, |
| 71 | SortModel sortModel) { |
| 72 | |
| 73 | sortModel.setMetricRecorder((SortDimension dimension) -> { |
| 74 | switch (dimension.getId()) { |
| 75 | case SortModel.SORT_DIMENSION_ID_TITLE: |
| 76 | Metrics.logUserAction(activity, Metrics.USER_ACTION_SORT_NAME); |
| 77 | break; |
| 78 | case SortModel.SORT_DIMENSION_ID_SIZE: |
| 79 | Metrics.logUserAction(activity, Metrics.USER_ACTION_SORT_SIZE); |
| 80 | break; |
| 81 | case SortModel.SORT_DIMENSION_ID_DATE: |
| 82 | Metrics.logUserAction(activity, Metrics.USER_ACTION_SORT_DATE); |
| 83 | break; |
| 84 | } |
| 85 | }); |
| 86 | |
| 87 | SortController controller = new SortController( |
| 88 | new DropdownSortWidgetController( |
| 89 | sortModel, |
| 90 | activity.findViewById(R.id.dropdown_sort_widget)), |
| 91 | TableHeaderController.create( |
| 92 | sortModel, |
| 93 | activity.findViewById(R.id.table_header))); |
| 94 | |
| 95 | controller.onViewModeChanged(initialMode); |
| 96 | return controller; |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 99 | public interface WidgetController { |
Steve McKay | 7c66209 | 2016-08-26 12:17:41 -0700 | [diff] [blame] | 100 | void setVisibility(int visibility); |
Garfield, Tan | 171e6f5 | 2016-07-29 14:44:58 -0700 | [diff] [blame] | 101 | } |
| 102 | } |