blob: 927ff7e81aa4d88fab6b5ae07a74ccfa9c71b8b2 [file] [log] [blame]
Garfield, Tan171e6f52016-07-29 14:44:58 -07001/*
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
17package com.android.documentsui.sorting;
18
19import android.annotation.Nullable;
Steve McKay9de0da62016-08-25 15:18:23 -070020import android.app.Activity;
Garfield, Tan171e6f52016-07-29 14:44:58 -070021import android.view.View;
22
Garfield, Tan11d23482016-08-05 09:33:29 -070023import com.android.documentsui.Metrics;
Steve McKay9de0da62016-08-25 15:18:23 -070024import com.android.documentsui.R;
Steve McKayd9caa6a2016-09-15 16:36:45 -070025import com.android.documentsui.base.State;
26import com.android.documentsui.base.State.ViewMode;
Garfield, Tan171e6f52016-07-29 14:44:58 -070027
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 McKay9de0da62016-08-25 15:18:23 -070033public final class SortController {
Garfield, Tan171e6f52016-07-29 14:44:58 -070034
Steve McKay9de0da62016-08-25 15:18:23 -070035 private final WidgetController mDropdownController;
36 private final @Nullable WidgetController mTableHeaderController;
Garfield, Tan171e6f52016-07-29 14:44:58 -070037
Steve McKay9de0da62016-08-25 15:18:23 -070038 public SortController(
39 WidgetController dropdownController,
40 @Nullable WidgetController tableHeaderController) {
Garfield, Tan171e6f52016-07-29 14:44:58 -070041
Steve McKay9de0da62016-08-25 15:18:23 -070042 assert(dropdownController != null);
43 mDropdownController = dropdownController;
44 mTableHeaderController = tableHeaderController;
Garfield, Tan11d23482016-08-05 09:33:29 -070045 }
46
Garfield, Tan61f564b2016-08-16 13:36:15 -070047 public void onViewModeChanged(@ViewMode int mode) {
Steve McKay9de0da62016-08-25 15:18:23 -070048 // in phone layouts we only ever have the dropdown sort controller.
49 if (mTableHeaderController == null) {
Garfield, Tan61f564b2016-08-16 13:36:15 -070050 mDropdownController.setVisibility(View.VISIBLE);
Steve McKay9de0da62016-08-25 15:18:23 -070051 return;
Garfield, Tan61f564b2016-08-16 13:36:15 -070052 }
Garfield, Tan171e6f52016-07-29 14:44:58 -070053
Steve McKay9de0da62016-08-25 15:18:23 -070054 // in tablet mode, we have fancy pants tabular header.
Garfield, Tan171e6f52016-07-29 14:44:58 -070055 switch (mode) {
56 case State.MODE_GRID:
Steve McKay9de0da62016-08-25 15:18:23 -070057 case State.MODE_UNKNOWN:
58 mTableHeaderController.setVisibility(View.GONE);
59 mDropdownController.setVisibility(View.VISIBLE);
Garfield, Tan171e6f52016-07-29 14:44:58 -070060 break;
61 case State.MODE_LIST:
Steve McKay9de0da62016-08-25 15:18:23 -070062 mTableHeaderController.setVisibility(View.VISIBLE);
63 mDropdownController.setVisibility(View.GONE);
Garfield, Tan171e6f52016-07-29 14:44:58 -070064 break;
Garfield, Tan171e6f52016-07-29 14:44:58 -070065 }
66 }
67
Steve McKay9de0da62016-08-25 15:18:23 -070068 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, Tan11d23482016-08-05 09:33:29 -070097 }
98
Garfield, Tan171e6f52016-07-29 14:44:58 -070099 public interface WidgetController {
Steve McKay7c662092016-08-26 12:17:41 -0700100 void setVisibility(int visibility);
Garfield, Tan171e6f52016-07-29 14:44:58 -0700101 }
102}