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

Commit cbd1c5f0 authored by Irem Uguz's avatar Irem Uguz
Browse files

Use EnforcingAdmin in restricted settings

Add support for using EnforcingAdmin in restricted preferences.

Test: atest SettingsRoboTests
Flag: android.app.admin.flags.policy_transparency_refactor_enabled
Bug: 414733570

Change-Id: I910fbe34027613a7cafa85d77367b7fbd08322d4
parent ff2ccbae
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.settingslib;

import android.app.admin.EnforcingAdmin;
import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
@@ -159,6 +160,15 @@ public class PrimarySwitchPreference extends RestrictedPreference {
        setSwitchEnabled(admin == null);
    }

    /**
     * If admin is not null, disables the switch.
     * Otherwise, keep it enabled.
     */
    public void setDisabledByAdmin(EnforcingAdmin admin) {
        super.setDisabledByAdmin(admin);
        setSwitchEnabled(admin == null);
    }

    public CompoundButton getSwitch() {
        return mSwitch;
    }
+33 −0
Original line number Diff line number Diff line
@@ -779,6 +779,39 @@ public class RestrictedLockUtilsInternal extends RestrictedLockUtils {
        item.setTitle(sb);
    }

    /**
     * Set the menu item as disabled by admin by adding a restricted padlock at the end of the
     * text and set the click listener which will send an intent to show the admin support details
     * dialog. If the admin is null, remove the padlock and disabled color span. When the admin is
     * null, we also set the OnMenuItemClickListener to null, so if you want to set a custom
     * OnMenuItemClickListener, set it after calling this method.
     */
    public static void setMenuItemAsDisabledByAdmin(final Context context,
            final MenuItem item, final EnforcingAdmin admin, final String restriction) {
        SpannableStringBuilder sb = new SpannableStringBuilder(item.getTitle());
        removeExistingRestrictedSpans(sb);

        if (admin != null) {
            final int disabledColor = getColorAttrDefaultColor(context,
                    android.R.attr.textColorHint);
            sb.setSpan(new ForegroundColorSpan(disabledColor), 0, sb.length(),
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            ImageSpan image = new RestrictedLockImageSpan(context);
            sb.append(" ", image, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

            item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    sendShowAdminSupportDetailsIntent(context, admin, restriction);
                    return true;
                }
            });
        } else {
            item.setOnMenuItemClickListener(null);
        }
        item.setTitle(sb);
    }

    private static void removeExistingRestrictedSpans(SpannableStringBuilder sb) {
        final int length = sb.length();
        RestrictedLockImageSpan[] imageSpans = sb.getSpans(length - 1, length,
+11 −0
Original line number Diff line number Diff line
@@ -18,12 +18,14 @@ package com.android.settingslib;

import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;

import android.app.admin.EnforcingAdmin;
import android.content.Context;
import android.os.Process;
import android.os.UserHandle;
import android.util.AttributeSet;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.res.TypedArrayUtils;
import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceViewHolder;
@@ -149,6 +151,15 @@ public class RestrictedPreference extends TwoTargetPreference implements
        }
    }

    /**
     * Sets the admin that disabled this preference.
     */
    public void setDisabledByAdmin(@Nullable EnforcingAdmin admin) {
        if (mHelper.setDisabledByEnforcingAdmin(admin)) {
            notifyChanged();
        }
    }

    public boolean isDisabledByAdmin() {
        return mHelper.isDisabledByAdmin();
    }
+2 −1
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;

import android.annotation.UserIdInt;
import android.app.AlertDialog;
import android.app.admin.EnforcingAdmin;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
@@ -217,7 +218,7 @@ public class InputMethodPreference extends PrimarySwitchPreference
        // This preference should also be disabled in case the admin does not allow this input
        // method.
        if (isAlwaysChecked) {
            setDisabledByAdmin(null);
            setDisabledByAdmin((EnforcingAdmin) null);
            setSwitchEnabled(false);
        } else if (!mIsAllowedByOrganization) {
            EnforcedAdmin admin =
+2 −1
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

import android.app.admin.EnforcingAdmin;
import android.content.Context;
import android.view.LayoutInflater;
import android.widget.CompoundButton;
@@ -162,7 +163,7 @@ public class PrimarySwitchPreferenceTest {
        toggle.setEnabled(false);
        mPreference.onBindViewHolder(mHolder);

        mPreference.setDisabledByAdmin(null);
        mPreference.setDisabledByAdmin((EnforcingAdmin) null);
        assertThat(toggle.isEnabled()).isTrue();
    }