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