Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bf26959b authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Automerger Merge Worker
Browse files

Merge "Further refactoring on ActionDisabledByAdminDialog classes." into...

Merge "Further refactoring on ActionDisabledByAdminDialog classes." into sc-dev am: 580476b7 am: bd9b5ccd

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/14708458

Change-Id: Ia75c9345f5aa0ef80667227af331426f3e264bb4
parents 481f5616 bd9b5ccd
Loading
Loading
Loading
Loading
+10 −4
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.settingslib.enterprise;

import android.annotation.UserIdInt;
import android.content.Context;

import androidx.annotation.Nullable;
@@ -27,11 +28,16 @@ import com.android.settingslib.RestrictedLockUtils;
 */
public interface ActionDisabledByAdminController {

    /**
     * Sets the {@link ActionDisabledLearnMoreButtonLauncher}.
     */
    void initialize(ActionDisabledLearnMoreButtonLauncher launcher);

    /**
     * Handles the adding and setting up of the learn more button. If button is not needed, then
     * this method can be left empty.
     */
    void setupLearnMoreButton(Context context, Object alertDialogBuilder);
    void setupLearnMoreButton(Context context);

    /**
     * Returns the admin support dialog's title resource id.
@@ -41,11 +47,11 @@ public interface ActionDisabledByAdminController {
    /**
     * Returns the admin support dialog's content string.
     */
    CharSequence getAdminSupportContentString(
            Context context, @Nullable CharSequence supportMessage);
    CharSequence getAdminSupportContentString(Context context,
            @Nullable CharSequence supportMessage);

    /**
     * Updates the enforced admin
     */
    void updateEnforcedAdmin(RestrictedLockUtils.EnforcedAdmin admin, int adminUserId);
    void updateEnforcedAdmin(RestrictedLockUtils.EnforcedAdmin admin, @UserIdInt int adminUserId);
}
+13 −12
Original line number Diff line number Diff line
@@ -19,29 +19,30 @@ package com.android.settingslib.enterprise;
import static android.app.admin.DevicePolicyManager.DEVICE_OWNER_TYPE_FINANCED;

import android.app.admin.DevicePolicyManager;
import android.content.Context;

/**
 * A factory that returns the relevant instance of {@link ActionDisabledByAdminController}.
 */
public class ActionDisabledByAdminControllerFactory {
public final class ActionDisabledByAdminControllerFactory {

    /**
     * Returns the relevant instance of {@link ActionDisabledByAdminController}.
     */
    public static ActionDisabledByAdminController createInstance(
            DevicePolicyManager dpm,
            ActionDisabledLearnMoreButtonLauncher helper,
            DeviceAdminStringProvider deviceAdminStringProvider) {
        if (isFinancedDevice(dpm)) {
            return new FinancedDeviceActionDisabledByAdminController(
                    helper, deviceAdminStringProvider);
        }
        return new ManagedDeviceActionDisabledByAdminController(
                helper, deviceAdminStringProvider);
    public static ActionDisabledByAdminController createInstance(Context context,
            DeviceAdminStringProvider stringProvider) {
        return isFinancedDevice(context)
                ? new FinancedDeviceActionDisabledByAdminController(stringProvider)
                : new ManagedDeviceActionDisabledByAdminController(stringProvider);
    }

    private static boolean isFinancedDevice(DevicePolicyManager dpm) {
    private static boolean isFinancedDevice(Context context) {
        DevicePolicyManager dpm = context.getSystemService(DevicePolicyManager.class);
        return dpm.isDeviceManaged() && dpm.getDeviceOwnerType(
                dpm.getDeviceOwnerComponentOnAnyUser()) == DEVICE_OWNER_TYPE_FINANCED;
    }

    private ActionDisabledByAdminControllerFactory() {
        throw new UnsupportedOperationException("provides only static methods");
    }
}
+83 −12
Original line number Diff line number Diff line
@@ -16,29 +16,100 @@

package com.android.settingslib.enterprise;

import static java.util.Objects.requireNonNull;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.UserHandle;
import android.os.UserManager;

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

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

    /**
     * Sets up a "learn more" button which shows a screen with device policy settings
     */
    void setupLearnMoreButtonToShowAdminPolicies(
            Context context,
            Object alertDialogBuilder,
            int enforcementAdminUserId,
            RestrictedLockUtils.EnforcedAdmin enforcedAdmin);
    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. Otherwise the admin package and its policies are not accessible to
        // the current user.
        if (isSameProfileGroup(context, enforcementAdminUserId)) {
            setLearnMoreButton(() -> showAdminPolicies(context, enforcedAdmin));
        }
    }

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

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

    /**
     * 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());
    }

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

    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);
    }
}
+56 −0
Original line number Diff line number Diff line
/*
 * 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.annotation.UserIdInt;

import com.android.internal.util.Preconditions;
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;

/**
 * Base class for {@link ActionDisabledByAdminController} implementations.
 */
abstract class BaseActionDisabledByAdminController
        implements ActionDisabledByAdminController {

    protected @UserIdInt int mEnforcementAdminUserId;
    protected EnforcedAdmin mEnforcedAdmin;
    protected ActionDisabledLearnMoreButtonLauncher mLauncher;
    protected final DeviceAdminStringProvider mStringProvider;

    BaseActionDisabledByAdminController(DeviceAdminStringProvider stringProvider) {
        mStringProvider = stringProvider;
    }

    @Override
    public final void initialize(ActionDisabledLearnMoreButtonLauncher launcher) {
        mLauncher = requireNonNull(launcher, "launcher cannot be null");
    }

    @Override
    public final void updateEnforcedAdmin(EnforcedAdmin admin, int adminUserId) {
        assertInitialized();
        mEnforcementAdminUserId = adminUserId;
        mEnforcedAdmin = requireNonNull(admin, "admin cannot be null");
    }

    protected final void assertInitialized() {
        Preconditions.checkState(mLauncher != null, "must call initialize() first");
    }
}
+8 −29
Original line number Diff line number Diff line
@@ -16,52 +16,31 @@

package com.android.settingslib.enterprise;

import static java.util.Objects.requireNonNull;

import android.annotation.UserIdInt;
import android.content.Context;

import androidx.annotation.Nullable;

import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;

/**
 * An {@link ActionDisabledByAdminController} to be used with financed devices.
 */
public class FinancedDeviceActionDisabledByAdminController
        implements ActionDisabledByAdminController {

    private @UserIdInt int mEnforcementAdminUserId;
    private EnforcedAdmin mEnforcedAdmin;
    private final ActionDisabledLearnMoreButtonLauncher mHelper;
    private final DeviceAdminStringProvider mDeviceAdminStringProvider;
final class FinancedDeviceActionDisabledByAdminController
        extends BaseActionDisabledByAdminController {

    FinancedDeviceActionDisabledByAdminController(
            ActionDisabledLearnMoreButtonLauncher helper,
            DeviceAdminStringProvider deviceAdminStringProvider) {
        mHelper = requireNonNull(helper, "helper cannot be null");
        mDeviceAdminStringProvider = requireNonNull(deviceAdminStringProvider,
                "deviceAdminStringProvider cannot be null");
    FinancedDeviceActionDisabledByAdminController(DeviceAdminStringProvider stringProvider) {
        super(stringProvider);
    }

    @Override
    public void updateEnforcedAdmin(EnforcedAdmin admin, int adminUserId) {
        mEnforcementAdminUserId = adminUserId;
        mEnforcedAdmin = requireNonNull(admin, "admin cannot be null");
    }
    public void setupLearnMoreButton(Context context) {
        assertInitialized();

    @Override
    public void setupLearnMoreButton(Context context, Object alertDialogBuilder) {
        mHelper.setupLearnMoreButtonToShowAdminPolicies(
                context,
                alertDialogBuilder,
                mEnforcementAdminUserId,
        mLauncher.setupLearnMoreButtonToShowAdminPolicies(context, mEnforcementAdminUserId,
                mEnforcedAdmin);
    }

    @Override
    public String getAdminSupportTitle(@Nullable String restriction) {
        return mDeviceAdminStringProvider.getDisabledByPolicyTitleForFinancedDevice();
        return mStringProvider.getDisabledByPolicyTitleForFinancedDevice();
    }

    @Override
Loading