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