summaryrefslogtreecommitdiff
path: root/packages/SettingsLib/src/com/android/settingslib/enterprise/ActionDisabledLearnMoreButtonLauncher.java
blob: f9d3aaf6b38342db1f8374d9b79761392b37fad5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settingslib.enterprise;

import static java.util.Objects.requireNonNull;

import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.UserHandle;
import android.os.UserManager;

import com.android.internal.annotations.VisibleForTesting;
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;

/**
 * Helper class meant to set up the "Learn more" button in the action disabled dialog.
 */
public abstract class ActionDisabledLearnMoreButtonLauncher {

    public static ResolveActivityChecker DEFAULT_RESOLVE_ACTIVITY_CHECKER =
            (packageManager, url, userHandle) -> packageManager.resolveActivityAsUser(
                    createLearnMoreIntent(url),
                    PackageManager.MATCH_DEFAULT_ONLY,
                    userHandle.getIdentifier()) != null;

    interface ResolveActivityChecker {
        boolean canResolveActivityAsUser(
                PackageManager packageManager, String url, UserHandle userHandle);
    }

    /**
     * Sets up a "learn more" button which shows a screen with device policy settings
     */
    public final void setupLearnMoreButtonToShowAdminPolicies(Context context,
            int enforcementAdminUserId, EnforcedAdmin enforcedAdmin) {
        requireNonNull(context, "context cannot be null");
        requireNonNull(enforcedAdmin, "enforcedAdmin cannot be null");

        // The "Learn more" button appears only if the restriction is enforced by an admin in the
        // same profile group or by the device owner. Otherwise the admin package and its policies
        // are not accessible to the current user.
        if (isSameProfileGroup(context, enforcementAdminUserId)
                || isEnforcedByDeviceOwnerOnSystemUserMode(context, enforcementAdminUserId)) {
            setLearnMoreButton(() -> showAdminPolicies(context, enforcedAdmin));
        }
    }

    /**
     * Sets up a "learn more" button which launches a help page
     */
    public final void setupLearnMoreButtonToLaunchHelpPage(
            Context context, String url, UserHandle userHandle) {
        requireNonNull(context, "context cannot be null");
        requireNonNull(url, "url cannot be null");

        setLearnMoreButton(() -> showHelpPage(context, url, userHandle));
    }

    /**
     * Sets the "learning more" button.
     *
     * @param action action to be run when the button is tapped.
     */
    public abstract void setLearnMoreButton(Runnable action);

    /**
     * Launches the settings page with info about the given admin.
     */
    protected abstract void launchShowAdminPolicies(Context context, UserHandle user,
            ComponentName admin);

    /**
     * Launches the settings page that shows all admins.
     */
    protected abstract void launchShowAdminSettings(Context context);

    /**
     * Callback to finish the activity associated with the launcher.
     */
    protected void finishSelf() {
    }

    @VisibleForTesting
    protected boolean isSameProfileGroup(Context context, int enforcementAdminUserId) {
        UserManager um = context.getSystemService(UserManager.class);

        return um.isSameProfileGroup(enforcementAdminUserId, um.getUserHandle());
    }

    private boolean isEnforcedByDeviceOwnerOnSystemUserMode(
            Context context, int enforcementAdminUserId) {
        if (enforcementAdminUserId != UserHandle.USER_SYSTEM) {
            return false;
        }
        DevicePolicyManager dpm = context.getSystemService(DevicePolicyManager.class);
        return enforcementAdminUserId == dpm.getDeviceOwnerUserId();
    }

    /**
     * Shows the help page using the given {@code url}.
     */
    @VisibleForTesting
    public void showHelpPage(Context context, String url, UserHandle userHandle) {
        context.startActivityAsUser(createLearnMoreIntent(url), userHandle);
        finishSelf();
    }

    protected final boolean canLaunchHelpPage(
            PackageManager packageManager,
            String url,
            UserHandle userHandle,
            ResolveActivityChecker resolveActivityChecker) {
        return resolveActivityChecker.canResolveActivityAsUser(packageManager, url, userHandle);
    }

    private void showAdminPolicies(Context context, EnforcedAdmin enforcedAdmin) {
        if (enforcedAdmin.component != null) {
            launchShowAdminPolicies(context, enforcedAdmin.user, enforcedAdmin.component);
        } else {
            launchShowAdminSettings(context);
        }
        finishSelf();
    }

    private static Intent createLearnMoreIntent(String url) {
        return new Intent(Intent.ACTION_VIEW, Uri.parse(url)).setFlags(
                Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
    }
}