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 android.content.res.Resources;
20 
21 import static androidx.core.util.Preconditions.checkArgument;
22 import static androidx.core.util.Preconditions.checkNotNull;
23 
24 import androidx.annotation.VisibleForTesting;
25 
26 import com.android.documentsui.R;
27 import com.android.documentsui.base.State;
28 import com.android.documentsui.base.UserId;
29 
30 import java.util.ArrayList;
31 import java.util.List;
32 
33 /**
34  * Converts user-specific lists of items into a single merged list appropriate for displaying in the
35  * UI, including the relevant headers.
36  */
37 class UserItemsCombiner {
38 
39     private UserId mCurrentUser;
40     private final Resources mResources;
41     private final State mState;
42     private List<Item> mRootList;
43     private List<Item> mRootListOtherUser;
44 
UserItemsCombiner(Resources resources, State state)45     UserItemsCombiner(Resources resources, State state) {
46         mCurrentUser = UserId.CURRENT_USER;
47         mResources = checkNotNull(resources);
48         mState = checkNotNull(state);
49     }
50 
51     @VisibleForTesting
overrideCurrentUserForTest(UserId userId)52     UserItemsCombiner overrideCurrentUserForTest(UserId userId) {
53         mCurrentUser = checkNotNull(userId);
54         return this;
55     }
56 
setRootListForCurrentUser(List<Item> rootList)57     UserItemsCombiner setRootListForCurrentUser(List<Item> rootList) {
58         mRootList = checkNotNull(rootList);
59         return this;
60     }
61 
setRootListForOtherUser(List<Item> rootList)62     UserItemsCombiner setRootListForOtherUser(List<Item> rootList) {
63         mRootListOtherUser = checkNotNull(rootList);
64         return this;
65     }
66 
67     /**
68      * Returns a combined lists from the provided lists. {@link HeaderItem}s indicating profile
69      * will be added if the list of current user and the other user are not empty.
70      */
createPresentableList()71     public List<Item> createPresentableList() {
72         checkArgument(mRootList != null, "RootListForCurrentUser is not set");
73         checkArgument(mRootListOtherUser != null, "setRootListForOtherUser is not set");
74 
75         final List<Item> result = new ArrayList<>();
76         if (mState.supportsCrossProfile() && mState.canShareAcrossProfile) {
77             if (!mRootList.isEmpty() && !mRootListOtherUser.isEmpty()) {
78                 // Identify personal and work root list.
79                 final List<Item> personalRootList;
80                 final List<Item> workRootList;
81                 if (mCurrentUser.isSystem()) {
82                     personalRootList = mRootList;
83                     workRootList = mRootListOtherUser;
84                 } else {
85                     personalRootList = mRootListOtherUser;
86                     workRootList = mRootList;
87                 }
88                 result.add(new HeaderItem(mResources.getString(R.string.personal_tab)));
89                 result.addAll(personalRootList);
90                 result.add(new HeaderItem(mResources.getString(R.string.work_tab)));
91                 result.addAll(workRootList);
92             } else {
93                 result.addAll(mRootList);
94                 result.addAll(mRootListOtherUser);
95             }
96         } else {
97             result.addAll(mRootList);
98         }
99         return result;
100     }
101 }
102