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.LongSparseArray; 21 import android.util.SparseBooleanArray; 22 23 import com.android.launcher3.pm.UserCache; 24 25 /** 26 * Utility class to manager store and user manager state at any particular time 27 */ 28 public class UserManagerState { 29 30 public final LongSparseArray<UserHandle> allUsers = new LongSparseArray<>(); 31 32 private final LongSparseArray<Boolean> mQuietUsersSerialNoMap = new LongSparseArray<>(); 33 private final SparseBooleanArray mQuietUsersHashCodeMap = new SparseBooleanArray(); 34 35 /** 36 * Initialises the state values for all users 37 */ init(UserCache userCache, UserManager userManager)38 public void init(UserCache userCache, UserManager userManager) { 39 for (UserHandle user : userCache.getUserProfiles()) { 40 long serialNo = userCache.getSerialNumberForUser(user); 41 boolean isUserQuiet = userManager.isQuietModeEnabled(user); 42 allUsers.put(serialNo, user); 43 mQuietUsersHashCodeMap.put(user.hashCode(), isUserQuiet); 44 mQuietUsersSerialNoMap.put(serialNo, isUserQuiet); 45 } 46 } 47 48 /** 49 * Returns true if quiet mode is enabled for the provided user 50 */ isUserQuiet(long serialNo)51 public boolean isUserQuiet(long serialNo) { 52 return mQuietUsersSerialNoMap.get(serialNo); 53 } 54 55 /** 56 * Returns true if quiet mode is enabled for the provided user 57 */ isUserQuiet(UserHandle user)58 public boolean isUserQuiet(UserHandle user) { 59 return mQuietUsersHashCodeMap.get(user.hashCode()); 60 } 61 62 /** 63 * Returns true if any user profile has quiet mode enabled. 64 */ isAnyProfileQuietModeEnabled()65 public boolean isAnyProfileQuietModeEnabled() { 66 for (int i = mQuietUsersHashCodeMap.size() - 1; i >= 0; i--) { 67 if (mQuietUsersHashCodeMap.valueAt(i)) { 68 return true; 69 } 70 } 71 return false; 72 } 73 } 74