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 androidx.annotation.Nullable;
20 import androidx.fragment.app.FragmentActivity;
21 
22 import android.view.View;
23 
24 import com.android.documentsui.BaseActivity;
25 import com.android.documentsui.Injector;
26 import com.android.documentsui.MetricConsts;
27 import com.android.documentsui.Metrics;
28 import com.android.documentsui.R;
29 import com.android.documentsui.base.State;
30 import com.android.documentsui.base.State.ViewMode;
31 
32 /**
33  * A high level controller that manages sort widgets. This is useful when sort widgets can and will
34  * appear in different locations in the UI, like the menu, above the file list (pinned) and embedded
35  * at the top of file list... and maybe other places too.
36  */
37 public final class SortController {
38 
39     private final @Nullable WidgetController mTableHeaderController;
40 
SortController(@ullable WidgetController tableHeaderController)41     public SortController(@Nullable WidgetController tableHeaderController) {
42 
43         mTableHeaderController = tableHeaderController;
44     }
45 
onViewModeChanged(@iewMode int mode)46     public void onViewModeChanged(@ViewMode int mode) {
47         // in phone layouts we only ever have the dropdown sort controller.
48         if (mTableHeaderController == null) {
49             return;
50         }
51 
52         // in tablet mode, we have fancy pants tabular header.
53         mTableHeaderController.setVisibility(mode == State.MODE_LIST ? View.VISIBLE : View.GONE);
54     }
55 
destroy()56     public void destroy() {
57         if (mTableHeaderController != null) {
58             mTableHeaderController.destroy();
59         }
60     }
61 
create( FragmentActivity activity, @ViewMode int initialMode, SortModel sortModel)62     public static SortController create(
63             FragmentActivity activity,
64             @ViewMode int initialMode,
65             SortModel sortModel) {
66 
67         final Injector<?> injector = ((BaseActivity)activity).getInjector();
68         sortModel.setMetricRecorder((SortDimension dimension) -> {
69             int sortType = MetricConsts.USER_ACTION_UNKNOWN;
70             switch (dimension.getId()) {
71                 case SortModel.SORT_DIMENSION_ID_TITLE:
72                     sortType = MetricConsts.USER_ACTION_SORT_NAME;
73                     break;
74                 case SortModel.SORT_DIMENSION_ID_SIZE:
75                     sortType = MetricConsts.USER_ACTION_SORT_SIZE;
76                     break;
77                 case SortModel.SORT_DIMENSION_ID_DATE:
78                     sortType = MetricConsts.USER_ACTION_SORT_DATE;
79                     break;
80                 case SortModel.SORT_DIMENSION_ID_FILE_TYPE:
81                     sortType = MetricConsts.USER_ACTION_SORT_TYPE;
82                     break;
83             }
84 
85             Metrics.logUserAction(sortType);
86             if (injector.pickResult != null) {
87                 injector.pickResult.increaseActionCount();
88             }
89         });
90 
91         SortController controller = new SortController(
92                 TableHeaderController.create(sortModel, activity.findViewById(R.id.table_header)));
93 
94         controller.onViewModeChanged(initialMode);
95         return controller;
96     }
97 
98     public interface WidgetController {
setVisibility(int visibility)99         void setVisibility(int visibility);
destroy()100         void destroy();
101     }
102 }
103