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

Commit 4e65112c authored by Matt Pietal's avatar Matt Pietal Committed by Android (Google) Code Review
Browse files

Merge "Smartspace - Check sensitivity settings" into sc-dev

parents 1ce4cdb8 5ab8c632
Loading
Loading
Loading
Loading
+99 −3
Original line number Diff line number Diff line
@@ -24,8 +24,16 @@ import android.app.WallpaperManager;
import android.app.smartspace.SmartspaceConfig;
import android.app.smartspace.SmartspaceManager;
import android.app.smartspace.SmartspaceSession;
import android.app.smartspace.SmartspaceTarget;
import android.content.Context;
import android.content.Intent;
import android.content.pm.UserInfo;
import android.content.res.Resources;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Handler;
import android.os.UserHandle;
import android.provider.Settings;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.view.View;
@@ -47,6 +55,7 @@ import com.android.systemui.plugins.BcSmartspaceDataPlugin.IntentStarter;
import com.android.systemui.plugins.ClockPlugin;
import com.android.systemui.plugins.FalsingManager;
import com.android.systemui.plugins.statusbar.StatusBarStateController;
import com.android.systemui.settings.UserTracker;
import com.android.systemui.statusbar.FeatureFlags;
import com.android.systemui.statusbar.notification.AnimatableProperty;
import com.android.systemui.statusbar.notification.PropertyAnimator;
@@ -57,6 +66,7 @@ import com.android.systemui.statusbar.phone.NotificationIconContainer;
import com.android.systemui.statusbar.policy.BatteryController;
import com.android.systemui.statusbar.policy.ConfigurationController;
import com.android.systemui.util.ViewController;
import com.android.systemui.util.settings.SecureSettings;

import java.util.Locale;
import java.util.TimeZone;
@@ -96,6 +106,14 @@ public class KeyguardClockSwitchController extends ViewController<KeyguardClockS
    private FalsingManager mFalsingManager;
    private final KeyguardUpdateMonitor mKeyguardUpdateMonitor;
    private final KeyguardBypassController mBypassController;
    private Handler mHandler;
    private UserTracker mUserTracker;
    private SecureSettings mSecureSettings;
    private ContentObserver mSettingsObserver;
    private boolean mShowSensitiveContentForCurrentUser;
    private boolean mShowSensitiveContentForManagedUser;
    private UserHandle mManagedUserHandle;
    private UserTracker.Callback mUserTrackerCallback;

    /**
     * Listener for changes to the color palette.
@@ -151,7 +169,10 @@ public class KeyguardClockSwitchController extends ViewController<KeyguardClockS
            ActivityStarter activityStarter,
            FalsingManager falsingManager,
            KeyguardUpdateMonitor keyguardUpdateMonitor,
            KeyguardBypassController bypassController) {
            KeyguardBypassController bypassController,
            @Main Handler handler,
            UserTracker userTracker,
            SecureSettings secureSettings) {
        super(keyguardClockSwitch);
        mStatusBarStateController = statusBarStateController;
        mColorExtractor = colorExtractor;
@@ -168,6 +189,9 @@ public class KeyguardClockSwitchController extends ViewController<KeyguardClockS
        mFalsingManager = falsingManager;
        mKeyguardUpdateMonitor = keyguardUpdateMonitor;
        mBypassController = bypassController;
        mHandler = handler;
        mUserTracker = userTracker;
        mSecureSettings = secureSettings;
    }

    /**
@@ -258,15 +282,74 @@ public class KeyguardClockSwitchController extends ViewController<KeyguardClockS
            mSmartspaceSession = getContext().getSystemService(SmartspaceManager.class)
                    .createSmartspaceSession(
                            new SmartspaceConfig.Builder(getContext(), "lockscreen").build());
            mSmartspaceCallback = targets -> smartspaceDataPlugin.onTargetsAvailable(targets);
            mSmartspaceCallback = targets -> {
                targets.removeIf(this::filterSmartspaceTarget);
                smartspaceDataPlugin.onTargetsAvailable(targets);
            };
            mSmartspaceSession.addOnTargetsAvailableListener(mUiExecutor, mSmartspaceCallback);
            mSmartspaceSession.requestSmartspaceUpdate();
            mSettingsObserver = new ContentObserver(mHandler) {
                @Override
                public void onChange(boolean selfChange, Uri uri) {
                    reloadSmartspace();
                }
            };

            mUserTrackerCallback = new UserTracker.Callback() {
                public void onUserChanged(int newUser, Context userContext) {
                    reloadSmartspace();
                }
            };
            mUserTracker.addCallback(mUserTrackerCallback, mUiExecutor);

            getContext().getContentResolver().registerContentObserver(
                    Settings.Secure.getUriFor(
                            Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS),
                    true, mSettingsObserver, UserHandle.USER_ALL);
            reloadSmartspace();
        }

        float dozeAmount = mStatusBarStateController.getDozeAmount();
        mStatusBarStateListener.onDozeAmountChanged(dozeAmount, dozeAmount);
    }

    @VisibleForTesting
    boolean filterSmartspaceTarget(SmartspaceTarget t) {
        if (!t.isSensitive()) return false;

        if (t.getUserHandle().equals(mUserTracker.getUserHandle())) {
            return !mShowSensitiveContentForCurrentUser;
        }
        if (t.getUserHandle().equals(mManagedUserHandle)) {
            return !mShowSensitiveContentForManagedUser;
        }

        return false;
    }

    private void reloadSmartspace() {
        mManagedUserHandle = getWorkProfileUser();
        final String setting = Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS;

        mShowSensitiveContentForCurrentUser =
                mSecureSettings.getIntForUser(setting, 0, mUserTracker.getUserId()) == 1;
        if (mManagedUserHandle != null) {
            int id = mManagedUserHandle.getIdentifier();
            mShowSensitiveContentForManagedUser =
                    mSecureSettings.getIntForUser(setting, 0, id) == 1;
        }

        mSmartspaceSession.requestSmartspaceUpdate();
    }

    private UserHandle getWorkProfileUser() {
        for (UserInfo userInfo : mUserTracker.getUserProfiles()) {
            if (userInfo.isManagedProfile()) {
                return userInfo.getUserHandle();
            }
        }
        return null;
    }

    private void updateWallpaperColor() {
        if (mSmartspaceView != null) {
            int color = Utils.getColorAttrDefaultColor(getContext(), R.attr.wallpaperTextColor);
@@ -289,6 +372,14 @@ public class KeyguardClockSwitchController extends ViewController<KeyguardClockS
        }
        mStatusBarStateController.removeCallback(mStatusBarStateListener);
        mConfigurationController.removeCallback(mConfigurationListener);

        if (mSettingsObserver != null) {
            getContext().getContentResolver().unregisterContentObserver(mSettingsObserver);
        }

        if (mUserTrackerCallback != null) {
            mUserTracker.removeCallback(mUserTrackerCallback);
        }
    }

    /**
@@ -437,4 +528,9 @@ public class KeyguardClockSwitchController extends ViewController<KeyguardClockS
    ConfigurationController.ConfigurationListener getConfigurationListener() {
        return mConfigurationListener;
    }

    @VisibleForTesting
    ContentObserver getSettingsObserver() {
        return mSettingsObserver;
    }
}
+107 −1
Original line number Diff line number Diff line
@@ -19,14 +19,21 @@ package com.android.keyguard;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.app.smartspace.SmartspaceTarget;
import android.content.Context;
import android.content.pm.UserInfo;
import android.content.res.Resources;
import android.os.Handler;
import android.os.UserHandle;
import android.test.suitebuilder.annotation.SmallTest;
import android.testing.AndroidTestingRunner;
import android.util.AttributeSet;
@@ -47,6 +54,7 @@ import com.android.systemui.plugins.BcSmartspaceDataPlugin.IntentStarter;
import com.android.systemui.plugins.ClockPlugin;
import com.android.systemui.plugins.FalsingManager;
import com.android.systemui.plugins.statusbar.StatusBarStateController;
import com.android.systemui.settings.UserTracker;
import com.android.systemui.statusbar.FeatureFlags;
import com.android.systemui.statusbar.StatusBarState;
import com.android.systemui.statusbar.phone.KeyguardBypassController;
@@ -54,6 +62,7 @@ import com.android.systemui.statusbar.phone.NotificationIconAreaController;
import com.android.systemui.statusbar.phone.NotificationIconContainer;
import com.android.systemui.statusbar.policy.BatteryController;
import com.android.systemui.statusbar.policy.ConfigurationController;
import com.android.systemui.util.settings.SecureSettings;

import org.junit.Before;
import org.junit.Test;
@@ -63,6 +72,8 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.verification.VerificationMode;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.Executor;

@SmallTest
@@ -119,10 +130,19 @@ public class KeyguardClockSwitchControllerTest extends SysuiTestCase {
    KeyguardUpdateMonitor mKeyguardUpdateMonitor;
    @Mock
    KeyguardBypassController mBypassController;
    @Mock
    Handler mHandler;
    @Mock
    UserTracker mUserTracker;
    @Mock
    SecureSettings mSecureSettings;

    private KeyguardClockSwitchController mController;
    private View mStatusArea;

    private static final int USER_ID = 5;
    private static final int MANAGED_USER_ID = 15;

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
@@ -159,7 +179,10 @@ public class KeyguardClockSwitchControllerTest extends SysuiTestCase {
                mActivityStarter,
                mFalsingManager,
                mKeyguardUpdateMonitor,
                mBypassController
                mBypassController,
                mHandler,
                mUserTracker,
                mSecureSettings
        );

        when(mStatusBarStateController.getState()).thenReturn(StatusBarState.SHADE);
@@ -260,6 +283,89 @@ public class KeyguardClockSwitchControllerTest extends SysuiTestCase {
        verify(mSmartspaceView, times(2)).setPrimaryTextColor(anyInt());
    }

    @Test
    public void doNotFilterRegularTarget() {
        setupPrimaryAndManagedUser();
        mController.init();

        when(mSecureSettings.getIntForUser(anyString(), anyInt(), eq(USER_ID))).thenReturn(0);
        when(mSecureSettings.getIntForUser(anyString(), anyInt(), eq(MANAGED_USER_ID)))
                .thenReturn(0);

        mController.getSettingsObserver().onChange(true, null);

        SmartspaceTarget t = mock(SmartspaceTarget.class);
        when(t.isSensitive()).thenReturn(false);
        when(t.getUserHandle()).thenReturn(new UserHandle(USER_ID));
        assertEquals(false, mController.filterSmartspaceTarget(t));

        reset(t);
        when(t.isSensitive()).thenReturn(false);
        when(t.getUserHandle()).thenReturn(new UserHandle(MANAGED_USER_ID));
        assertEquals(false, mController.filterSmartspaceTarget(t));
    }

    @Test
    public void filterAllSensitiveTargetsAllUsers() {
        setupPrimaryAndManagedUser();
        mController.init();

        when(mSecureSettings.getIntForUser(anyString(), anyInt(), eq(USER_ID))).thenReturn(0);
        when(mSecureSettings.getIntForUser(anyString(), anyInt(), eq(MANAGED_USER_ID)))
                .thenReturn(0);

        mController.getSettingsObserver().onChange(true, null);

        SmartspaceTarget t = mock(SmartspaceTarget.class);
        when(t.isSensitive()).thenReturn(true);
        when(t.getUserHandle()).thenReturn(new UserHandle(USER_ID));
        assertEquals(true, mController.filterSmartspaceTarget(t));

        reset(t);
        when(t.isSensitive()).thenReturn(true);
        when(t.getUserHandle()).thenReturn(new UserHandle(MANAGED_USER_ID));
        assertEquals(true, mController.filterSmartspaceTarget(t));
    }

    @Test
    public void filterSensitiveManagedUserTargets() {
        setupPrimaryAndManagedUser();
        mController.init();

        when(mSecureSettings.getIntForUser(anyString(), anyInt(), eq(USER_ID))).thenReturn(1);
        when(mSecureSettings.getIntForUser(anyString(), anyInt(), eq(MANAGED_USER_ID)))
                .thenReturn(0);

        mController.getSettingsObserver().onChange(true, null);

        SmartspaceTarget t = mock(SmartspaceTarget.class);
        when(t.isSensitive()).thenReturn(true);
        when(t.getUserHandle()).thenReturn(new UserHandle(USER_ID));
        assertEquals(false, mController.filterSmartspaceTarget(t));

        reset(t);
        when(t.isSensitive()).thenReturn(true);
        when(t.getUserHandle()).thenReturn(new UserHandle(MANAGED_USER_ID));
        assertEquals(true, mController.filterSmartspaceTarget(t));
    }

    private void setupPrimaryAndManagedUser() {
        UserInfo userInfo = mock(UserInfo.class);
        when(userInfo.isManagedProfile()).thenReturn(true);
        when(userInfo.getUserHandle()).thenReturn(new UserHandle(MANAGED_USER_ID));
        when(mUserTracker.getUserProfiles()).thenReturn(List.of(userInfo));

        when(mUserTracker.getUserId()).thenReturn(USER_ID);
        when(mUserTracker.getUserHandle()).thenReturn(new UserHandle(USER_ID));
    }

    private void setupPrimaryAndNoManagedUser() {
        when(mUserTracker.getUserProfiles()).thenReturn(Collections.emptyList());

        when(mUserTracker.getUserId()).thenReturn(USER_ID);
        when(mUserTracker.getUserHandle()).thenReturn(new UserHandle(USER_ID));
    }

    private void verifyAttachment(VerificationMode times) {
        verify(mClockManager, times).addOnClockChangedListener(
                any(ClockManager.ClockChangedListener.class));