1 /*
2  * Copyright (C) 2021 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.settingslib.enterprise;
18 
19 import static java.util.Objects.requireNonNull;
20 
21 import android.content.Context;
22 import android.os.UserHandle;
23 import android.os.UserManager;
24 
25 import androidx.annotation.Nullable;
26 
27 import com.android.settingslib.enterprise.ActionDisabledLearnMoreButtonLauncher.ResolveActivityChecker;
28 
29 
30 /**
31  * An {@link ActionDisabledByAdminController} to be used with managed devices.
32  */
33 final class ManagedDeviceActionDisabledByAdminController
34         extends BaseActionDisabledByAdminController {
35 
36     interface ForegroundUserChecker {
isUserForeground(Context context, UserHandle userHandle)37         boolean isUserForeground(Context context, UserHandle userHandle);
38     }
39 
40     public final static ForegroundUserChecker DEFAULT_FOREGROUND_USER_CHECKER =
41             ManagedDeviceActionDisabledByAdminController::isUserForeground;
42 
43     /**
44      * The {@link UserHandle} which is preferred for launching the web help page in
45      * <p>If not able to launch the web help page in this user, the current user will be used as
46      * fallback instead. If the current user cannot open it either, the admin policies page will
47      * be used instead.
48      */
49     private final UserHandle mPreferredUserHandle;
50 
51     private final ForegroundUserChecker mForegroundUserChecker;
52     private final ResolveActivityChecker mResolveActivityChecker;
53 
54     /**
55      * Constructs a {@link ManagedDeviceActionDisabledByAdminController}
56      * @param preferredUserHandle - user on which to launch the help web page, if necessary
57      */
ManagedDeviceActionDisabledByAdminController( DeviceAdminStringProvider stringProvider, UserHandle preferredUserHandle, ForegroundUserChecker foregroundUserChecker, ResolveActivityChecker resolveActivityChecker)58     ManagedDeviceActionDisabledByAdminController(
59             DeviceAdminStringProvider stringProvider,
60             UserHandle preferredUserHandle,
61             ForegroundUserChecker foregroundUserChecker,
62             ResolveActivityChecker resolveActivityChecker) {
63         super(stringProvider);
64         mPreferredUserHandle = requireNonNull(preferredUserHandle);
65         mForegroundUserChecker = requireNonNull(foregroundUserChecker);
66         mResolveActivityChecker = requireNonNull(resolveActivityChecker);
67     }
68 
69     /**
70      * We don't show Learn More button in Admin-Support Dialog anymore.
71      */
72     @Override
setupLearnMoreButton(Context context)73     public void setupLearnMoreButton(Context context) {
74     }
75 
isUserForeground(Context context, UserHandle userHandle)76     private static boolean isUserForeground(Context context, UserHandle userHandle) {
77         return context.createContextAsUser(userHandle, /* flags= */ 0)
78                 .getSystemService(UserManager.class).isUserForeground();
79     }
80 
81     @Override
getAdminSupportTitle(@ullable String restriction)82     public String getAdminSupportTitle(@Nullable String restriction) {
83         return mStringProvider.getDefaultDisabledByPolicyTitle();
84     }
85 
86     @Override
getAdminSupportContentString(Context context, CharSequence supportMessage)87     public CharSequence getAdminSupportContentString(Context context, CharSequence supportMessage) {
88         return supportMessage != null
89                 ? supportMessage
90                 : mStringProvider.getDefaultDisabledByPolicyContent();
91     }
92 }
93