• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.dirlist;
18 
19 import android.view.LayoutInflater;
20 import android.view.View;
21 import android.widget.ImageView;
22 import android.widget.LinearLayout;
23 import android.widget.TextView;
24 
25 import com.android.documentsui.ActionHandler;
26 import com.android.documentsui.BaseActivity;
27 import com.android.documentsui.R;
28 import com.android.documentsui.base.State;
29 import com.android.documentsui.dirlist.AppsRowItemData.AppData;
30 import com.android.documentsui.dirlist.AppsRowItemData.RootData;
31 import com.android.documentsui.sidebar.AppItem;
32 import com.android.documentsui.sidebar.Item;
33 import com.android.documentsui.sidebar.RootItem;
34 
35 import java.util.ArrayList;
36 import java.util.List;
37 
38 /**
39  * A manager class stored apps row chip data list. Data will be synced by RootsFragment.
40  */
41 public class AppsRowManager {
42 
43     private final ActionHandler mActionHandler;
44     private final List<AppsRowItemData> mDataList;
45 
AppsRowManager(ActionHandler handler)46     public AppsRowManager(ActionHandler handler) {
47         mDataList = new ArrayList<>();
48         mActionHandler = handler;
49     }
50 
updateList(List<Item> itemList)51     public List<AppsRowItemData> updateList(List<Item> itemList) {
52         mDataList.clear();
53         for (Item item : itemList) {
54             if (item instanceof RootItem) {
55                 mDataList.add(new RootData((RootItem) item, mActionHandler));
56             } else {
57                 mDataList.add(new AppData((AppItem) item, mActionHandler));
58             }
59         }
60         return mDataList;
61     }
62 
shouldShow(State state)63     private boolean shouldShow(State state) {
64         boolean isHiddenAction = state.action == State.ACTION_CREATE
65                 || state.action == State.ACTION_OPEN_TREE
66                 || state.action == State.ACTION_PICK_COPY_DESTINATION;
67         return state.stack.isRecents() && !isHiddenAction && mDataList.size() > 0;
68     }
69 
updateView(BaseActivity activity)70     public void updateView(BaseActivity activity) {
71         final View appsRowLayout = activity.findViewById(R.id.apps_row);
72 
73         if (!shouldShow(activity.getDisplayState())) {
74             appsRowLayout.setVisibility(View.GONE);
75             return;
76         }
77 
78         appsRowLayout.setVisibility(View.VISIBLE);
79         final LinearLayout appsGroup = activity.findViewById(R.id.apps_group);
80         appsGroup.removeAllViews();
81 
82         final LayoutInflater inflater = activity.getLayoutInflater();
83         for (AppsRowItemData data : mDataList) {
84             View item = inflater.inflate(R.layout.apps_item, appsGroup, false);
85             bindView(item, data);
86             appsGroup.addView(item);
87         }
88     }
89 
bindView(View view, AppsRowItemData data)90     private void bindView(View view, AppsRowItemData data) {
91         final ImageView app_icon = view.findViewById(R.id.app_icon);
92         final TextView title = view.findViewById(android.R.id.title);
93         final ImageView exit_icon = view.findViewById(R.id.exit_icon);
94 
95         app_icon.setImageDrawable(data.getIconDrawable(view.getContext()));
96         title.setText(data.getTitle());
97         exit_icon.setVisibility(data.showExitIcon() ? View.VISIBLE : View.GONE);
98         view.setOnClickListener(v -> data.onClicked());
99     }
100 }
101