1 /*
2  * Copyright (C) 2023 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.launcher3.allapps;
18 
19 import static com.android.launcher3.util.Executors.UI_HELPER_EXECUTOR;
20 
21 import android.os.UserHandle;
22 import android.os.UserManager;
23 
24 import androidx.annotation.IntDef;
25 
26 import com.android.launcher3.logging.StatsLogManager;
27 import com.android.launcher3.model.data.ItemInfo;
28 import com.android.launcher3.pm.UserCache;
29 
30 import java.lang.annotation.Retention;
31 import java.lang.annotation.RetentionPolicy;
32 import java.util.function.Predicate;
33 
34 /**
35  * A Generic User Profile Manager which abstract outs the common functionality required
36  * by user-profiles supported by Launcher
37  * <p>
38  * Concrete impls are
39  * {@link WorkProfileManager} which manages work profile state
40  * {@link PrivateProfileManager} which manages private profile state.
41  */
42 public abstract class UserProfileManager {
43     public static final int STATE_UNKNOWN = 0;
44     public static final int STATE_ENABLED = 1;
45     public static final int STATE_DISABLED = 2;
46     public static final int STATE_TRANSITION = 3;
47 
48     @IntDef(value = {
49             STATE_UNKNOWN,
50             STATE_ENABLED,
51             STATE_DISABLED,
52             STATE_TRANSITION
53     })
54     @Retention(RetentionPolicy.SOURCE)
55     public @interface UserProfileState { }
56 
57     protected final StatsLogManager mStatsLogManager;
58     protected final UserManager mUserManager;
59     protected final UserCache mUserCache;
60 
61     @UserProfileState
62     private int mCurrentState;
UserProfileManager(UserManager userManager, StatsLogManager statsLogManager, UserCache userCache)63     protected UserProfileManager(UserManager userManager,
64             StatsLogManager statsLogManager,
65             UserCache userCache) {
66         mUserManager = userManager;
67         mStatsLogManager = statsLogManager;
68         mUserCache = userCache;
69     }
70 
71     /** Sets quiet mode as enabled/disabled for the profile type. */
setQuietMode(boolean enabled)72     protected void setQuietMode(boolean enabled) {
73         UI_HELPER_EXECUTOR.post(() ->
74                 mUserCache.getUserProfiles()
75                         .stream()
76                         .filter(getUserMatcher())
77                         .findFirst()
78                         .ifPresent(userHandle ->
79                                 mUserManager.requestQuietModeEnabled(enabled, userHandle)));
80     }
81 
82     /** Sets current state for the profile type. */
setCurrentState(int state)83     protected void setCurrentState(int state) {
84         mCurrentState = state;
85     }
86 
87     /** Returns current state for the profile type. */
getCurrentState()88     public int getCurrentState() {
89         return mCurrentState;
90     }
91 
92     /** Returns if user profile is enabled. */
isEnabled()93     public boolean isEnabled() {
94         return mCurrentState == STATE_ENABLED;
95     }
96 
97     /** Returns the UserHandle corresponding to the profile type, null in case no matches found. */
getProfileUser()98     public UserHandle getProfileUser() {
99         return mUserCache.getUserProfiles().stream()
100                 .filter(getUserMatcher())
101                 .findAny()
102                 .orElse(null);
103     }
104 
105     /** Logs Event to StatsLogManager. */
logEvents(StatsLogManager.EventEnum event)106     protected void logEvents(StatsLogManager.EventEnum event) {
107         mStatsLogManager.logger().log(event);
108     }
109 
110     /** Returns the matcher corresponding to profile type. */
getUserMatcher()111     protected abstract Predicate<UserHandle> getUserMatcher();
112 
113     /** Returns the matcher corresponding to the profile type associated with ItemInfo. */
getItemInfoMatcher()114     protected Predicate<ItemInfo> getItemInfoMatcher() {
115         return itemInfo -> itemInfo != null && getUserMatcher().test(itemInfo.user);
116     }
117 }
118