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

Commit ee6c4bac authored by Billy Huang's avatar Billy Huang
Browse files

Use disabled summary when trustagents are disabled

This makes trust agent preferences follow the behavior of other features that can be disabled by device admins, otherwise the preference is simply greyed out without context until the user clicks on the preference.

Bug: 319095039
Test: Covered by TrustAgentListPreferenceControllerTest#onResume_restrictedPreferenceShouldUseAdminDisabledSummary
Test: atest SettingsRoboTests:com.android.settings.security --host
Test: manually validated that disabled admin policy results in showing "Controlled by admin" as the summary
Flag: EXEMPT bugfix for single preference in settings
Change-Id: I89f052cf2479a120d8b5b223414162f129cde89e
parent c35fc38b
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.UserHandle;
import android.text.TextUtils;

import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;
@@ -216,7 +217,11 @@ public class TrustAgentListPreferenceController extends AbstractPreferenceContro
            trustAgentPreference.setIntent(new Intent(Intent.ACTION_MAIN)
                    .setComponent(agent.componentName));
            trustAgentPreference.setDisabledByAdmin(agent.admin);
            if (!trustAgentPreference.isDisabledByAdmin() && !hasSecurity) {
            if (trustAgentPreference.isDisabledByAdmin()) {
                // Ensure visibility by setting non-empty summary text.
                trustAgentPreference.setSummary(TextUtils.firstNotEmpty(agent.summary, " "));
                trustAgentPreference.useAdminDisabledSummary(true);
            } else if (!trustAgentPreference.isDisabledByAdmin() && !hasSecurity) {
                trustAgentPreference.setEnabled(false);
                trustAgentPreference.setSummary(R.string.disabled_because_no_backup_security);
            }
+94 −36
Original line number Diff line number Diff line
@@ -23,27 +23,39 @@ import static com.google.common.truth.Truth.assertThat;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import static java.util.Objects.requireNonNull;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceScreen;
import androidx.preference.PreferenceViewHolder;

import com.android.internal.widget.LockPatternUtils;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settings.security.SecuritySettings;
import com.android.settings.security.trustagent.TrustAgentManager.TrustAgentComponentInfo;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
import com.android.settingslib.RestrictedPreference;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.search.SearchIndexableRaw;

import com.google.common.collect.Maps;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -55,6 +67,7 @@ import org.robolectric.annotation.Config;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RunWith(RobolectricTestRunner.class)
public class TrustAgentListPreferenceControllerTest {
@@ -114,13 +127,8 @@ public class TrustAgentListPreferenceControllerTest {
        when(mCategory.findPreference(PREF_KEY_TRUST_AGENT + 0))
                .thenReturn(oldAgent)
                .thenReturn(null);
        final List<TrustAgentManager.TrustAgentComponentInfo> agents = new ArrayList<>();
        final TrustAgentManager.TrustAgentComponentInfo agent =
                mock(TrustAgentManager.TrustAgentComponentInfo.class);
        agent.title = "Test_title";
        agent.summary = "test summary";
        agent.componentName = new ComponentName("pkg", "agent");
        agent.admin = null;
        final List<TrustAgentComponentInfo> agents = new ArrayList<>();
        final TrustAgentComponentInfo agent = createTrustAgentComponentInfo(null);
        agents.add(agent);
        when(mTrustAgentManager.getActiveTrustAgents(mActivity, mLockPatternUtils))
                .thenReturn(agents);
@@ -133,13 +141,8 @@ public class TrustAgentListPreferenceControllerTest {

    @Test
    public void onResume_shouldAddNewAgents() {
        final List<TrustAgentManager.TrustAgentComponentInfo> agents = new ArrayList<>();
        final TrustAgentManager.TrustAgentComponentInfo agent =
                mock(TrustAgentManager.TrustAgentComponentInfo.class);
        agent.title = "Test_title";
        agent.summary = "test summary";
        agent.componentName = new ComponentName("pkg", "agent");
        agent.admin = null;
        final List<TrustAgentComponentInfo> agents = new ArrayList<>();
        final TrustAgentComponentInfo agent = createTrustAgentComponentInfo(null);
        agents.add(agent);
        when(mTrustAgentManager.getActiveTrustAgents(mActivity, mLockPatternUtils))
                .thenReturn(agents);
@@ -153,13 +156,8 @@ public class TrustAgentListPreferenceControllerTest {
    @Test
    @Config(qualifiers = "mcc999")
    public void onResume_ifNotAvailable_shouldNotAddNewAgents() {
        final List<TrustAgentManager.TrustAgentComponentInfo> agents = new ArrayList<>();
        final TrustAgentManager.TrustAgentComponentInfo agent =
                mock(TrustAgentManager.TrustAgentComponentInfo.class);
        agent.title = "Test_title";
        agent.summary = "test summary";
        agent.componentName = new ComponentName("pkg", "agent");
        agent.admin = null;
        final List<TrustAgentComponentInfo> agents = new ArrayList<>();
        final TrustAgentComponentInfo agent = createTrustAgentComponentInfo(null);
        agents.add(agent);
        when(mTrustAgentManager.getActiveTrustAgents(mActivity, mLockPatternUtils))
                .thenReturn(agents);
@@ -172,13 +170,8 @@ public class TrustAgentListPreferenceControllerTest {

    @Test
    public void onResume_controllerShouldHasKey() {
        final List<TrustAgentManager.TrustAgentComponentInfo> agents = new ArrayList<>();
        final TrustAgentManager.TrustAgentComponentInfo agent =
                mock(TrustAgentManager.TrustAgentComponentInfo.class);
        agent.title = "Test_title";
        agent.summary = "test summary";
        agent.componentName = new ComponentName("pkg", "agent");
        agent.admin = null;
        final List<TrustAgentComponentInfo> agents = new ArrayList<>();
        final TrustAgentComponentInfo agent = createTrustAgentComponentInfo(null);
        agents.add(agent);
        when(mTrustAgentManager.getActiveTrustAgents(mActivity, mLockPatternUtils))
                .thenReturn(agents);
@@ -190,15 +183,69 @@ public class TrustAgentListPreferenceControllerTest {
        assertThat(mController.mTrustAgentsKeyList).containsExactly(key);
    }

    @Test
    public void onResume_shouldShowDisabledByAdminRestrictedPreference() {
        final List<TrustAgentComponentInfo> agents = new ArrayList<>();
        final TrustAgentComponentInfo agent = createTrustAgentComponentInfo(new EnforcedAdmin());
        final Map<String, Preference> preferences = setUpPreferenceMap();
        agents.add(agent);
        when(mTrustAgentManager.getActiveTrustAgents(mActivity, mLockPatternUtils))
                .thenReturn(agents);

        mController.displayPreference(mScreen);
        mController.onResume();

        assertThat(preferences).hasSize(1);
        Preference preference = preferences.values().iterator().next();
        assertThat(preference).isInstanceOf(RestrictedPreference.class);
        RestrictedPreference restrictedPreference = (RestrictedPreference) preference;
        assertThat(restrictedPreference.isDisabledByAdmin()).isTrue();
    }

    @Test
    public void onResume_restrictedPreferenceShouldUseAdminDisabledSummary() {
        final List<TrustAgentComponentInfo> agents = new ArrayList<>();
        final TrustAgentComponentInfo agent = createTrustAgentComponentInfo(new EnforcedAdmin());
        final Map<String, Preference> preferences = setUpPreferenceMap();
        final LayoutInflater inflater = LayoutInflater.from(mActivity);
        agents.add(agent);
        when(mTrustAgentManager.getActiveTrustAgents(mActivity, mLockPatternUtils))
                .thenReturn(agents);
        mController.displayPreference(mScreen);
        mController.onResume();
        final RestrictedPreference restrictedPreference =
                (RestrictedPreference) preferences.values().iterator().next();
        final PreferenceViewHolder viewHolder = PreferenceViewHolder.createInstanceForTests(
                inflater.inflate(restrictedPreference.getLayoutResource(), null));

        restrictedPreference.onBindViewHolder(viewHolder);

        final TextView summaryView = (TextView) requireNonNull(
                viewHolder.findViewById(android.R.id.summary));
        assertThat(summaryView.getVisibility()).isEqualTo(View.VISIBLE);
        assertThat(summaryView.getText().toString()).isEqualTo(
                mActivity.getString(
                        com.android.settingslib.R.string.disabled_by_admin_summary_text));
    }

    private Map<String, Preference> setUpPreferenceMap() {
        final Map<String, Preference> preferences = Maps.newLinkedHashMap();
        when(mCategory.addPreference(any())).thenAnswer((invocation) -> {
            Preference preference = invocation.getArgument(0);
            preferences.put(preference.getKey(), preference);
            return true;
        });
        when(mCategory.removePreference(any())).thenAnswer((invocation) -> {
            Preference preference = invocation.getArgument(0);
            return preferences.remove(preference.getKey()) != null;
        });
        return preferences;
    }

    @Test
    public void updateDynamicRawDataToIndex_shouldIndexAgents() {
        final List<TrustAgentManager.TrustAgentComponentInfo> agents = new ArrayList<>();
        final TrustAgentManager.TrustAgentComponentInfo agent =
                mock(TrustAgentManager.TrustAgentComponentInfo.class);
        agent.title = "Test_title";
        agent.summary = "test summary";
        agent.componentName = new ComponentName("pkg", "agent");
        agent.admin = null;
        final List<TrustAgentComponentInfo> agents = new ArrayList<>();
        final TrustAgentComponentInfo agent = createTrustAgentComponentInfo(null);
        agents.add(agent);
        when(mTrustAgentManager.getActiveTrustAgents(mActivity, mLockPatternUtils))
                .thenReturn(agents);
@@ -208,4 +255,15 @@ public class TrustAgentListPreferenceControllerTest {

        assertThat(indexRaws).hasSize(1);
    }

    @NonNull
    private static TrustAgentComponentInfo createTrustAgentComponentInfo(
            @Nullable EnforcedAdmin admin) {
        final TrustAgentComponentInfo agent = new TrustAgentComponentInfo();
        agent.title = "Test_title";
        agent.summary = "test summary";
        agent.componentName = new ComponentName("pkg", "agent");
        agent.admin = admin;
        return agent;
    }
}