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

Commit c29b984a authored by Pavel Grafov's avatar Pavel Grafov
Browse files

Pick default notification setting for work profile

Managed profiles cannot completely hide notifications, so
this setting should be treated as always "true" for them.

Change-Id: I9808c1e9736d83efccb0e947d9097379bda59ebb
Fixes: 78194020
Test: atest RedactionInterstitialTest
parent 27b8c610
Loading
Loading
Loading
Loading
+13 −9
Original line number Diff line number Diff line
@@ -18,6 +18,8 @@ package com.android.settings.notification;

import static android.app.admin.DevicePolicyManager.KEYGUARD_DISABLE_SECURE_NOTIFICATIONS;
import static android.app.admin.DevicePolicyManager.KEYGUARD_DISABLE_UNREDACTED_NOTIFICATIONS;
import static android.provider.Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS;
import static android.provider.Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS;

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

@@ -164,15 +166,17 @@ public class RedactionInterstitial extends SettingsActivity {
        }

        private void loadFromSettings() {
            final boolean managed = UserManager.get(getContext()).isManagedProfile(mUserId);
            final boolean enabled = !managed || Settings.Secure.getIntForUser(getContentResolver(),
                    Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 0, mUserId) != 0;
            final boolean show = Settings.Secure.getIntForUser(getContentResolver(),
                    Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 1, mUserId) != 0;
            final boolean managedProfile = UserManager.get(getContext()).isManagedProfile(mUserId);
            // Hiding all notifications is device-wide setting, managed profiles can only set
            // whether their notifications are show in full or redacted.
            final boolean showNotifications = managedProfile || Settings.Secure.getIntForUser(
                    getContentResolver(), LOCK_SCREEN_SHOW_NOTIFICATIONS, 0, mUserId) != 0;
            final boolean showUnredacted = Settings.Secure.getIntForUser(
                    getContentResolver(), LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 1, mUserId) != 0;

            int checkedButtonId = R.id.hide_all;
            if (enabled) {
                if (show && !mShowAllButton.isDisabledByAdmin()) {
            if (showNotifications) {
                if (showUnredacted && !mShowAllButton.isDisabledByAdmin()) {
                    checkedButtonId = R.id.show_all;
                } else if (!mRedactSensitiveButton.isDisabledByAdmin()) {
                    checkedButtonId = R.id.redact_sensitive;
@@ -188,9 +192,9 @@ public class RedactionInterstitial extends SettingsActivity {
            final boolean enabled = (checkedId != R.id.hide_all);

            Settings.Secure.putIntForUser(getContentResolver(),
                    Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, show ? 1 : 0, mUserId);
                    LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, show ? 1 : 0, mUserId);
            Settings.Secure.putIntForUser(getContentResolver(),
                    Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, enabled ? 1 : 0, mUserId);
                    LOCK_SCREEN_SHOW_NOTIFICATIONS, enabled ? 1 : 0, mUserId);

        }
    }
+175 −0
Original line number Diff line number Diff line
package com.android.settings.notification;

import static android.app.admin.DevicePolicyManager.KEYGUARD_DISABLE_SECURE_NOTIFICATIONS;
import static android.app.admin.DevicePolicyManager.KEYGUARD_DISABLE_UNREDACTED_NOTIFICATIONS;
import static android.provider.Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS;
import static android.provider.Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS;

import static com.google.common.truth.Truth.assertThat;

import static org.robolectric.Robolectric.buildActivity;

import android.content.ContentResolver;
import android.content.Intent;
import android.os.UserHandle;
import android.provider.Settings;
import android.view.View;
import android.widget.RadioButton;

import com.android.settings.R;
import com.android.settings.RestrictedRadioButton;
import com.android.settings.notification.RedactionInterstitial.RedactionInterstitialFragment;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.SettingsShadowResources;
import com.android.settings.testutils.shadow.SettingsShadowResourcesImpl;
import com.android.settings.testutils.shadow.ShadowRestrictedLockUtils;
import com.android.settings.testutils.shadow.ShadowUserManager;
import com.android.settings.testutils.shadow.ShadowUtils;

import org.junit.After;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;

@RunWith(SettingsRobolectricTestRunner.class)
@Config(shadows = {
        SettingsShadowResources.class,
        SettingsShadowResourcesImpl.class,
        SettingsShadowResources.SettingsShadowTheme.class,
        ShadowUtils.class,
        ShadowRestrictedLockUtils.class,
        ShadowUserManager.class,
})
public class RedactionInterstitialTest {
    private RedactionInterstitial mActivity;
    private RedactionInterstitialFragment mFragment;

    @After
    public void tearDown() {
        ShadowUserManager.getShadow().reset();
        ShadowRestrictedLockUtils.reset();
    }

    @Test
    public void primaryUserDefaultStateTest() {
        setupSettings(1 /* show */, 1 /* showUnredacted */);
        setupActivity();

        assertHideAllVisible(true);
        assertEnabledButtons(true /* all */, true /* redact */);
        assertSelectedButton(R.id.show_all);
    }

    @Test
    public void primaryUserRedactSensitiveTest() {
        setupSettings(1 /* show */, 0 /* showUnredacted */);
        setupActivity();

        assertHideAllVisible(true);
        assertEnabledButtons(true /* all */, true /* redact */);
        assertSelectedButton(R.id.redact_sensitive);
    }

    @Test
    public void primaryUserHideAllTest() {
        setupSettings(0 /* show */, 0 /* showUnredacted */);
        setupActivity();

        assertHideAllVisible(true);
        assertEnabledButtons(true /* all */, true /* redact */);
        assertSelectedButton(R.id.hide_all);
    }

    @Test
    public void primaryUserUnredactedRestrictionTest() {
        setupSettings(1 /* show */, 1 /* showUnredacted */);
        ShadowRestrictedLockUtils.setKeyguardDisabledFeatures(
                KEYGUARD_DISABLE_UNREDACTED_NOTIFICATIONS);
        setupActivity();

        assertHideAllVisible(true);
        assertEnabledButtons(false /* all */, true /* redact */);
        assertSelectedButton(R.id.redact_sensitive);
    }

    @Test
    public void primaryUserNotificationRestrictionTest() {
        setupSettings(1 /* show */, 1 /* showUnredacted */);
        ShadowRestrictedLockUtils.setKeyguardDisabledFeatures(
                KEYGUARD_DISABLE_SECURE_NOTIFICATIONS);
        setupActivity();

        assertHideAllVisible(true);
        assertEnabledButtons(false /* all */, false /* redact */);
        assertSelectedButton(R.id.hide_all);
    }

    @Test
    public void managedProfileNoRestrictionsTest() {
        setupSettings(1 /* show */, 1 /* showUnredacted */);
        ShadowUserManager.getShadow().addManagedProfile(UserHandle.myUserId());
        setupActivity();

        assertHideAllVisible(false);
        assertEnabledButtons(true /* all */, true /* redact */);
        assertSelectedButton(R.id.show_all);
    }

    @Test
    public void managedProfileUnredactedRestrictionTest() {
        setupSettings(1 /* show */, 1 /* showUnredacted */);
        ShadowUserManager.getShadow().addManagedProfile(UserHandle.myUserId());
        ShadowRestrictedLockUtils.setKeyguardDisabledFeatures(
                KEYGUARD_DISABLE_UNREDACTED_NOTIFICATIONS);
        setupActivity();

        assertHideAllVisible(false);
        assertEnabledButtons(false /* all */, true /* redact */);
        assertSelectedButton(R.id.redact_sensitive);
    }

    private void setupActivity() {
        mActivity = buildActivity(RedactionInterstitial.class, new Intent()).setup().get();
        mFragment = (RedactionInterstitialFragment)
                mActivity.getFragmentManager().findFragmentById(R.id.main_content);
        assertThat(mActivity).isNotNull();
        assertThat(mFragment).isNotNull();
    }

    private void setupSettings(int show, int showUnredacted) {
        final ContentResolver resolver = RuntimeEnvironment.application.getContentResolver();
        Settings.Secure.putIntForUser(resolver,
                LOCK_SCREEN_SHOW_NOTIFICATIONS, show, UserHandle.myUserId());
        Settings.Secure.putIntForUser(resolver,
                LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, showUnredacted, UserHandle.myUserId());
    }

    private void assertHideAllVisible(boolean visible) {
        Assert.assertEquals(visible, getButton(R.id.hide_all).getVisibility() != View.GONE);
    }

    private void assertEnabledButtons(boolean all, boolean redact) {
        Assert.assertEquals(all, buttonEnabled(R.id.show_all));
        Assert.assertEquals(redact, buttonEnabled(R.id.redact_sensitive));
    }

    private void assertSelectedButton(int resId) {
        Assert.assertEquals(resId == R.id.show_all, buttonChecked(R.id.show_all));
        Assert.assertEquals(resId == R.id.redact_sensitive, buttonChecked(R.id.redact_sensitive));
        Assert.assertEquals(resId == R.id.hide_all, buttonChecked(R.id.hide_all));
    }

    private boolean buttonChecked(int resource) {
        return getButton(resource).isChecked();
    }

    private boolean buttonEnabled(int resource) {
        return !((RestrictedRadioButton) getButton(resource)).isDisabledByAdmin();
    }

    private RadioButton getButton(int resource) {
        return (RadioButton) mFragment.getView().findViewById(resource);
    }
}
+22 −1
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@
 */
package com.android.settings.testutils.shadow;

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

import com.android.internal.util.ArrayUtils;
@@ -23,15 +24,25 @@ import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;

import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.Resetter;

@Implements(RestrictedLockUtils.class)
public class ShadowRestrictedLockUtils {
    private static boolean isRestricted;
    private static String[] restrictedPkgs;
    private static boolean adminSupportDetailsIntentLaunched;
    private static int keyguardDisabledFeatures;

    @Resetter
    public static void reset() {
        isRestricted = false;
        restrictedPkgs = null;
        adminSupportDetailsIntentLaunched = false;
        keyguardDisabledFeatures = 0;
    }

    @Implementation
    public static RestrictedLockUtils.EnforcedAdmin checkIfMeteredDataRestricted(Context context,
    public static EnforcedAdmin checkIfMeteredDataRestricted(Context context,
            String packageName, int userId) {
        if (isRestricted) {
            return new EnforcedAdmin();
@@ -47,6 +58,12 @@ public class ShadowRestrictedLockUtils {
        adminSupportDetailsIntentLaunched = true;
    }

    @Implementation
    public static EnforcedAdmin checkIfKeyguardFeaturesDisabled(Context context,
            int features, final @UserIdInt int userId) {
        return (keyguardDisabledFeatures & features) == 0 ? null : new EnforcedAdmin();
    }

    public static boolean hasAdminSupportDetailsIntentLaunched() {
        return adminSupportDetailsIntentLaunched;
    }
@@ -62,4 +79,8 @@ public class ShadowRestrictedLockUtils {
    public static void setRestrictedPkgs(String... pkgs) {
        restrictedPkgs = pkgs;
    }

    public static void setKeyguardDisabledFeatures(int features) {
        keyguardDisabledFeatures = features;
    }
}
+16 −1
Original line number Diff line number Diff line
@@ -31,8 +31,10 @@ import org.robolectric.shadow.api.Shadow;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Implements(value = UserManager.class, inheritImplementationMethods = true)
public class ShadowUserManager extends org.robolectric.shadows.ShadowUserManager {
@@ -40,12 +42,16 @@ public class ShadowUserManager extends org.robolectric.shadows.ShadowUserManager
    private SparseArray<UserInfo> mUserInfos = new SparseArray<>();
    private final List<String> mRestrictions = new ArrayList<>();
    private final Map<String, List<EnforcingUser>> mRestrictionSources = new HashMap<>();
    private List<UserInfo> mUserProfileInfos = new ArrayList<>();
    private final List<UserInfo> mUserProfileInfos = new ArrayList<>();
    private final Set<Integer> mManagedProfiles = new HashSet<>();

    @Resetter
    public void reset() {
        mUserInfos.clear();
        mRestrictions.clear();
        mUserProfileInfos.clear();
        mRestrictionSources.clear();
        mManagedProfiles.clear();
    }

    public void setUserInfo(int userHandle, UserInfo userInfo) {
@@ -95,4 +101,13 @@ public class ShadowUserManager extends org.robolectric.shadows.ShadowUserManager
            String restrictionKey, UserHandle userHandle, List<EnforcingUser> enforcers) {
        mRestrictionSources.put(restrictionKey + userHandle.getIdentifier(), enforcers);
    }

    @Implementation
    public boolean isManagedProfile(@UserIdInt int userId) {
        return mManagedProfiles.contains(userId);
    }

    public void addManagedProfile(int userId) {
        mManagedProfiles.add(userId);
    }
}