• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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.sidebar;
18 
19 import static androidx.core.util.Preconditions.checkNotNull;
20 
21 import com.android.documentsui.base.UserId;
22 
23 import com.google.common.collect.ArrayListMultimap;
24 import com.google.common.collect.Lists;
25 import com.google.common.collect.Multimap;
26 
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.List;
30 
31 /**
32  * A builder to build a list of root items to be displayed on the {@link RootsFragment}. The list
33  * should contain roots that the were added to this builder, except for those which support
34  * cross-profile.
35  *
36  * <p>If the root supports cross-profile, the list would only contain the root of the
37  * selected user.
38  *
39  * <p>If no root of the selected user was added but that of the other user was added,
40  * a stub root of that root for the selected user will be generated.
41  *
42  * <p>The builder group the roots using {@link Item#stringId} as key.
43  *
44  * <p>For example, if these items were added to the builder: itemA[0], itemA[10], itemB[0],
45  * itemC[10], itemX[0],itemY[10] where root itemX, itemY do not support cross profile.
46  *
47  * <p>When the selected user is user 0, {@link #getList()} returns itemA[0], itemB[0],
48  * stubC[0], itemX[0], itemY[10].
49  *
50  * <p>When the selected user is user 10, {@link #getList()} returns itemA[10], stubB[10],
51  * itemC[10], itemX[0], itemY[10].
52  */
53 class RootItemListBuilder {
54     private final UserId mSelectedUser;
55     private final List<UserId> mUserIds;
56     private final Multimap<String, RootItem> mItems = ArrayListMultimap.create();
57 
RootItemListBuilder(UserId selectedUser, List<UserId> userIds)58     RootItemListBuilder(UserId selectedUser, List<UserId> userIds) {
59         mSelectedUser = checkNotNull(selectedUser);
60         mUserIds = userIds;
61     }
62 
add(RootItem item)63     public void add(RootItem item) {
64         mItems.put(item.stringId, item);
65     }
66 
67     /**
68      * Returns a lists of root items generated from the provided root items.
69      */
getList()70     public List<RootItem> getList() {
71         if (mUserIds.size() < 2) {
72             // If we only have one user, simply return everything that has added to this builder.
73             return Lists.newArrayList(mItems.values());
74         }
75         List<RootItem> result = Lists.newArrayList();
76         // Iterates through items by item.stringId.
77         for (Collection<RootItem> items : mItems.asMap().values()) {
78             result.addAll(getRootItemForSelectedUser(items));
79         }
80         return result;
81     }
82 
getRootItemForSelectedUser(Collection<RootItem> items)83     private Collection<RootItem> getRootItemForSelectedUser(Collection<RootItem> items) {
84         RootItem testRootItem = items.iterator().next();
85         if (!testRootItem.root.supportsCrossProfile()) {
86             // If the root does not support cross-profile, return the entire list.
87             return items;
88         }
89 
90         // If the root supports cross-profile, we return the added root or create a stub root if
91         // it was not added for the selected user.
92         for (RootItem item : items) {
93             if (item.userId.equals(mSelectedUser)) {
94                 // If the item has and return the item if it is already in the items list.
95                 return Collections.singletonList(item);
96             }
97         }
98 
99         return Collections.singletonList(RootItem.createStubItem(testRootItem, mSelectedUser));
100     }
101 }
102