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 package com.android.launcher3.model; 17 18 import android.os.UserHandle; 19 import android.os.UserManager; 20 import android.util.Log; 21 import android.util.LongSparseArray; 22 import android.util.SparseBooleanArray; 23 24 import com.android.launcher3.pm.UserCache; 25 26 /** 27 * Utility class to manager store and user manager state at any particular time 28 */ 29 public class UserManagerState { 30 31 private static final String TAG = "UserManagerState"; 32 33 public final LongSparseArray<UserHandle> allUsers = new LongSparseArray<>(); 34 35 private final LongSparseArray<Boolean> mQuietUsersSerialNoMap = new LongSparseArray<>(); 36 private final SparseBooleanArray mQuietUsersHashCodeMap = new SparseBooleanArray(); 37 38 /** 39 * Initialises the state values for all users 40 */ init(UserCache userCache, UserManager userManager)41 public void init(UserCache userCache, UserManager userManager) { 42 for (UserHandle user : userManager.getUserProfiles()) { 43 long serialNo = userCache.getSerialNumberForUser(user); 44 boolean isUserQuiet = userManager.isQuietModeEnabled(user); 45 // Mapping different UserHandles to the same serialNo in allUsers could lead to losing 46 // UserHandle and cause a series of problems, such as incorrectly marking app as 47 // disabled and deleting app icons from workspace. 48 if (allUsers.get(serialNo) != null) { 49 Log.w(TAG, String.format("Override allUsers[%d]=%s with %s", 50 serialNo, allUsers.get(serialNo), user)); 51 } 52 allUsers.put(serialNo, user); 53 mQuietUsersHashCodeMap.put(user.hashCode(), isUserQuiet); 54 mQuietUsersSerialNoMap.put(serialNo, isUserQuiet); 55 } 56 } 57 58 /** 59 * Returns true if quiet mode is enabled for the provided user 60 */ isUserQuiet(long serialNo)61 public boolean isUserQuiet(long serialNo) { 62 return mQuietUsersSerialNoMap.get(serialNo); 63 } 64 65 /** 66 * Returns true if quiet mode is enabled for the provided user 67 */ isUserQuiet(UserHandle user)68 public boolean isUserQuiet(UserHandle user) { 69 return mQuietUsersHashCodeMap.get(user.hashCode()); 70 } 71 72 /** 73 * Returns true if any user profile has quiet mode enabled. 74 * <p> 75 * Do not use this for determining if a specific profile has quiet mode enabled, as their can 76 * be more than one profile in quiet mode. 77 */ isAnyProfileQuietModeEnabled()78 public boolean isAnyProfileQuietModeEnabled() { 79 for (int i = mQuietUsersHashCodeMap.size() - 1; i >= 0; i--) { 80 if (mQuietUsersHashCodeMap.valueAt(i)) { 81 return true; 82 } 83 } 84 return false; 85 } 86 } 87