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 static com.android.documentsui.util.FlagUtils.isUseMaterial3FlagEnabled; 20 21 import android.text.TextUtils; 22 import android.view.LayoutInflater; 23 import android.view.View; 24 import android.widget.ImageView; 25 import android.widget.LinearLayout; 26 import android.widget.TextView; 27 28 import com.android.documentsui.ActionHandler; 29 import com.android.documentsui.BaseActivity; 30 import com.android.documentsui.ConfigStore; 31 import com.android.documentsui.R; 32 import com.android.documentsui.UserIdManager; 33 import com.android.documentsui.UserManagerState; 34 import com.android.documentsui.base.State; 35 import com.android.documentsui.base.UserId; 36 import com.android.documentsui.dirlist.AppsRowItemData.AppData; 37 import com.android.documentsui.dirlist.AppsRowItemData.RootData; 38 import com.android.documentsui.sidebar.AppItem; 39 import com.android.documentsui.sidebar.Item; 40 import com.android.documentsui.sidebar.RootItem; 41 import com.android.modules.utils.build.SdkLevel; 42 43 import java.util.ArrayList; 44 import java.util.HashMap; 45 import java.util.List; 46 import java.util.Map; 47 48 /** 49 * A manager class stored apps row chip data list. Data will be synced by RootsFragment. 50 * TODO(b/379776735): Remove this after use_material3 flag is launched. 51 */ 52 public class AppsRowManager { 53 54 private final ActionHandler mActionHandler; 55 private final List<AppsRowItemData> mDataList; 56 private final boolean mMaybeShowBadge; 57 private final UserIdManager mUserIdManager; 58 private final UserManagerState mUserManagerState; 59 private final ConfigStore mConfigStore; 60 AppsRowManager(ActionHandler handler, boolean maybeShowBadge, UserIdManager userIdManager, ConfigStore configStore)61 public AppsRowManager(ActionHandler handler, boolean maybeShowBadge, 62 UserIdManager userIdManager, ConfigStore configStore) { 63 mDataList = new ArrayList<>(); 64 mActionHandler = handler; 65 mMaybeShowBadge = maybeShowBadge; 66 mUserIdManager = userIdManager; 67 mUserManagerState = null; 68 mConfigStore = configStore; 69 } 70 AppsRowManager(ActionHandler handler, boolean maybeShowBadge, UserManagerState userManagerState, ConfigStore configStore)71 public AppsRowManager(ActionHandler handler, boolean maybeShowBadge, 72 UserManagerState userManagerState, ConfigStore configStore) { 73 mDataList = new ArrayList<>(); 74 mActionHandler = handler; 75 mMaybeShowBadge = maybeShowBadge; 76 mUserIdManager = null; 77 mUserManagerState = userManagerState; 78 mConfigStore = configStore; 79 } 80 updateList(List<Item> itemList)81 public List<AppsRowItemData> updateList(List<Item> itemList) { 82 mDataList.clear(); 83 84 // If more than 1 item of the same package, show item summary (e.g. account id). 85 Map<String, Integer> packageNameCount = new HashMap<>(); 86 for (Item item : itemList) { 87 String packageName = item.getPackageName(); 88 int previousCount = packageNameCount.containsKey(packageName) 89 && !TextUtils.isEmpty(packageName) 90 ? packageNameCount.get(packageName) : 0; 91 packageNameCount.put(packageName, previousCount + 1); 92 } 93 94 for (Item item : itemList) { 95 boolean shouldShowSummary = packageNameCount.get(item.getPackageName()) > 1; 96 if (item instanceof RootItem) { 97 mDataList.add(new RootData((RootItem) item, mActionHandler, shouldShowSummary, 98 mMaybeShowBadge)); 99 } else { 100 mDataList.add(new AppData((AppItem) item, mActionHandler, shouldShowSummary, 101 mMaybeShowBadge)); 102 } 103 } 104 return mDataList; 105 } 106 shouldShow(State state, boolean isSearchExpanded)107 private boolean shouldShow(State state, boolean isSearchExpanded) { 108 if (isUseMaterial3FlagEnabled()) { 109 return false; 110 } 111 112 boolean isHiddenAction = state.action == State.ACTION_CREATE 113 || state.action == State.ACTION_OPEN_TREE 114 || state.action == State.ACTION_PICK_COPY_DESTINATION; 115 boolean isSearchExpandedAcrossProfile = getUserIds().size() > 1 116 && state.supportsCrossProfile() 117 && isSearchExpanded; 118 119 return state.stack.isRecents() && !isHiddenAction && mDataList.size() > 0 120 && !isSearchExpandedAcrossProfile; 121 } 122 updateView(BaseActivity activity)123 public void updateView(BaseActivity activity) { 124 final View appsRowLayout = activity.findViewById(R.id.apps_row); 125 126 if (!shouldShow(activity.getDisplayState(), activity.isSearchExpanded())) { 127 appsRowLayout.setVisibility(View.GONE); 128 return; 129 } 130 131 final LinearLayout appsGroup = activity.findViewById(R.id.apps_group); 132 appsGroup.removeAllViews(); 133 134 final LayoutInflater inflater = activity.getLayoutInflater(); 135 final UserId selectedUser = activity.getSelectedUser(); 136 for (AppsRowItemData data : mDataList) { 137 if (selectedUser.equals(data.getUserId())) { 138 View item = inflater.inflate(R.layout.apps_item, appsGroup, false); 139 bindView(item, data); 140 appsGroup.addView(item); 141 } 142 } 143 144 appsRowLayout.setVisibility(appsGroup.getChildCount() > 0 ? View.VISIBLE : View.GONE); 145 } 146 bindView(View view, AppsRowItemData data)147 private void bindView(View view, AppsRowItemData data) { 148 final ImageView app_icon = view.findViewById(R.id.app_icon); 149 final TextView title = view.findViewById(android.R.id.title); 150 final TextView summary = view.findViewById(R.id.summary); 151 152 app_icon.setImageDrawable(data.getIconDrawable(view.getContext())); 153 title.setText(data.getTitle()); 154 title.setContentDescription( 155 data.getUserId().getUserBadgedLabel(view.getContext(), data.getTitle())); 156 summary.setText(data.getSummary()); 157 summary.setVisibility(data.getSummary() != null ? View.VISIBLE : View.GONE); 158 view.setOnClickListener(v -> data.onClicked()); 159 } 160 getUserIds()161 private List<UserId> getUserIds() { 162 if (mConfigStore.isPrivateSpaceInDocsUIEnabled() && SdkLevel.isAtLeastS()) { 163 return mUserManagerState.getUserIds(); 164 } 165 return mUserIdManager.getUserIds(); 166 } 167 } 168