1 /*
2  * Copyright (C) 2024 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.settings.fuelgauge.batteryusage;
18 
19 import android.content.Context;
20 import android.content.pm.UserInfo;
21 import android.os.UserManager;
22 
23 import androidx.annotation.Nullable;
24 
25 import java.util.ArrayList;
26 import java.util.List;
27 
28 class UserIdsSeries {
29     private final UserManager mUserManager;
30     private final int mCurrentUserId;
31     private final List<Integer> mVisibleUserIds = new ArrayList<>();
32 
33     @Nullable private UserInfo mPrivateUser = null;
34     @Nullable private UserInfo mManagedProfileUser = null;
35 
UserIdsSeries(final Context context, final boolean isNonUIRequest)36     UserIdsSeries(final Context context, final boolean isNonUIRequest) {
37         mUserManager = context.getSystemService(UserManager.class);
38         mCurrentUserId = context.getUserId();
39         List<UserInfo> aliveUsers =
40                 mUserManager != null ? mUserManager.getAliveUsers() : new ArrayList<>();
41 
42         for (UserInfo userInfo : aliveUsers) {
43             if (!mUserManager.isSameProfileGroup(mCurrentUserId, userInfo.id)) {
44                 continue;
45             }
46             if (userInfo.isManagedProfile()) {
47                 // Load data for WorkProfile mode
48                 mManagedProfileUser = userInfo;
49                 mVisibleUserIds.add(userInfo.id);
50             } else if (userInfo.isPrivateProfile()) {
51                 mPrivateUser = userInfo;
52                 // Load data for PrivateProfile if it is from UI caller and PS is unlocked.
53                 if (!isNonUIRequest && !userInfo.isQuietModeEnabled()) {
54                     mVisibleUserIds.add(userInfo.id);
55                 }
56             } else if (!userInfo.isQuietModeEnabled()) {
57                 // Load data for other profiles if it is not in quiet mode
58                 mVisibleUserIds.add(userInfo.id);
59             }
60         }
61     }
62 
getCurrentUserId()63     int getCurrentUserId() {
64         return mCurrentUserId;
65     }
66 
getVisibleUserIds()67     List<Integer> getVisibleUserIds() {
68         return mVisibleUserIds;
69     }
70 
isCurrentUserLocked()71     boolean isCurrentUserLocked() {
72         return isUserLocked(mCurrentUserId);
73     }
74 
isUserLocked(int userId)75     boolean isUserLocked(int userId) {
76         return mUserManager == null || !mUserManager.isUserUnlocked(userId);
77     }
78 
isFromOtherUsers(long userId)79     boolean isFromOtherUsers(long userId) {
80         return !mVisibleUserIds.contains((int) userId);
81     }
82 
isMainUserProfileOnly()83     boolean isMainUserProfileOnly() {
84         return mUserManager != null
85                 && mUserManager.isMainUser()
86                 && mPrivateUser == null
87                 && mManagedProfileUser == null;
88     }
89 }
90