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

Commit 52c6a8bb authored by Jeff DeCew's avatar Jeff DeCew
Browse files

Convert from Handlers to Executors in NotificationLockscreenUserManager

Removal of TestableLooper also means these tests can now run deviceless, so I've made that conversion as well.

Fixes: 315363596
Test: atest NotificationLockscreenUserManagerTest
Test: atest NotificationLockscreenUserManagerMainThreadTest
Test: atest SystemUiRoboTests
Flag: NA
Change-Id: Ie0adfcb9766593b2745791f040d8e3dbbd3e2240
parent 79dd6123
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -438,6 +438,7 @@ android_robolectric_test {
        "androidx.core_core-animation-testing",
        "androidx.test.ext.junit",
        "inline-mockito-robolectric-prebuilt",
        "platform-parametric-runner-lib",
    ],
    libs: [
        "android.test.runner",
+4 −15
Original line number Diff line number Diff line
@@ -42,19 +42,15 @@ import android.content.Intent;
import android.content.pm.UserInfo;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.Settings;
import android.testing.AndroidTestingRunner;
import android.testing.TestableLooper;
import android.util.SparseArray;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.SmallTest;

import com.android.internal.widget.LockPatternUtils;
import com.android.systemui.Dependency;
import com.android.systemui.SysuiTestCase;
import com.android.systemui.broadcast.BroadcastDispatcher;
import com.android.systemui.dump.DumpManager;
@@ -86,8 +82,7 @@ import java.util.Collection;
import java.util.concurrent.Executor;

@SmallTest
@RunWith(AndroidTestingRunner.class)
@TestableLooper.RunWithLooper
@RunWith(AndroidJUnit4.class)
public class NotificationLockscreenUserManagerMainThreadTest extends SysuiTestCase {
    @Mock
    private NotificationPresenter mPresenter;
@@ -128,6 +123,7 @@ public class NotificationLockscreenUserManagerMainThreadTest extends SysuiTestCa
    private NotificationEntry mSecondaryUserNotif;
    private NotificationEntry mWorkProfileNotif;
    private final FakeFeatureFlagsClassic mFakeFeatureFlags = new FakeFeatureFlagsClassic();
    private Executor mMainExecutor = Runnable::run; // Direct executor
    private Executor mBackgroundExecutor = Runnable::run; // Direct executor

    @Before
@@ -156,8 +152,6 @@ public class NotificationLockscreenUserManagerMainThreadTest extends SysuiTestCa
                mSecondaryUser));
        when(mUserManager.getProfilesIncludingCommunal(mSecondaryUser.id)).thenReturn(
                Lists.newArrayList(mSecondaryUser, mCommunalUser));
        mDependency.injectTestDependency(Dependency.MAIN_HANDLER,
                Handler.createAsync(Looper.myLooper()));

        Notification notifWithPrivateVisibility = new Notification();
        notifWithPrivateVisibility.visibility = Notification.VISIBILITY_PRIVATE;
@@ -378,28 +372,24 @@ public class NotificationLockscreenUserManagerMainThreadTest extends SysuiTestCa

        // first call explicitly sets user 0 to not public; notifies
        mLockscreenUserManager.updatePublicMode();
        TestableLooper.get(this).processAllMessages();
        assertFalse(mLockscreenUserManager.isLockscreenPublicMode(0));
        verify(listener).onNotificationStateChanged();
        clearInvocations(listener);

        // calling again has no changes; does not notify
        mLockscreenUserManager.updatePublicMode();
        TestableLooper.get(this).processAllMessages();
        assertFalse(mLockscreenUserManager.isLockscreenPublicMode(0));
        verify(listener, never()).onNotificationStateChanged();

        // Calling again with keyguard now showing makes user 0 public; notifies
        when(mKeyguardStateController.isShowing()).thenReturn(true);
        mLockscreenUserManager.updatePublicMode();
        TestableLooper.get(this).processAllMessages();
        assertTrue(mLockscreenUserManager.isLockscreenPublicMode(0));
        verify(listener).onNotificationStateChanged();
        clearInvocations(listener);

        // calling again has no changes; does not notify
        mLockscreenUserManager.updatePublicMode();
        TestableLooper.get(this).processAllMessages();
        assertTrue(mLockscreenUserManager.isLockscreenPublicMode(0));
        verify(listener, never()).onNotificationStateChanged();
    }
@@ -595,8 +585,7 @@ public class NotificationLockscreenUserManagerMainThreadTest extends SysuiTestCa
                    (() -> mOverviewProxyService),
                    NotificationLockscreenUserManagerMainThreadTest.this.mKeyguardManager,
                    mStatusBarStateController,
                    Handler.createAsync(Looper.myLooper()),
                    Handler.createAsync(Looper.myLooper()),
                    mMainExecutor,
                    mBackgroundExecutor,
                    mDeviceProvisionedController,
                    mKeyguardStateController,
+4 −8
Original line number Diff line number Diff line
@@ -29,10 +29,11 @@ import static android.os.Flags.FLAG_ALLOW_PRIVATE_PROFILE;
import static android.os.UserHandle.USER_ALL;
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.systemui.util.concurrency.MockExecutorHandlerKt.mockExecutorHandler;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
@@ -62,13 +63,11 @@ import android.platform.test.annotations.DisableFlags;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.FlagsParameterization;
import android.provider.Settings;
import android.testing.TestableLooper;
import android.util.SparseArray;

import androidx.test.filters.SmallTest;

import com.android.internal.widget.LockPatternUtils;
import com.android.systemui.Dependency;
import com.android.systemui.SysuiTestCase;
import com.android.systemui.broadcast.BroadcastDispatcher;
import com.android.systemui.dump.DumpManager;
@@ -88,6 +87,7 @@ import com.android.systemui.statusbar.policy.KeyguardStateController;
import com.android.systemui.util.concurrency.FakeExecutor;
import com.android.systemui.util.settings.FakeSettings;
import com.android.systemui.util.time.FakeSystemClock;

import com.google.android.collect.Lists;

import org.junit.After;
@@ -109,7 +109,6 @@ import platform.test.runner.parameterized.Parameters;

@SmallTest
@RunWith(ParameterizedAndroidJunit4.class)
@TestableLooper.RunWithLooper
public class NotificationLockscreenUserManagerTest extends SysuiTestCase {

    @Parameters(name = "{0}")
@@ -197,8 +196,6 @@ public class NotificationLockscreenUserManagerTest extends SysuiTestCase {
                mSecondaryUser));
        when(mUserManager.getProfilesIncludingCommunal(mSecondaryUser.id)).thenReturn(
                Lists.newArrayList(mSecondaryUser, mCommunalUser));
        mDependency.injectTestDependency(Dependency.MAIN_HANDLER,
                mockExecutorHandler(mMainExecutor));

        Notification notifWithPrivateVisibility = new Notification();
        notifWithPrivateVisibility.visibility = VISIBILITY_PRIVATE;
@@ -949,8 +946,7 @@ public class NotificationLockscreenUserManagerTest extends SysuiTestCase {
                    (() -> mOverviewProxyService),
                    NotificationLockscreenUserManagerTest.this.mKeyguardManager,
                    mStatusBarStateController,
                    mockExecutorHandler(mMainExecutor),
                    mockExecutorHandler(mBackgroundExecutor),
                    mMainExecutor,
                    mBackgroundExecutor,
                    mDeviceProvisionedController,
                    mKeyguardStateController,
+18 −22
Original line number Diff line number Diff line
@@ -15,17 +15,17 @@
 */
package com.android.systemui.statusbar;

import static android.app.Flags.keyguardPrivateNotifications;
import static android.app.StatusBarManager.ACTION_KEYGUARD_PRIVATE_NOTIFICATIONS_CHANGED;
import static android.app.StatusBarManager.EXTRA_KM_PRIVATE_NOTIFS_ALLOWED;
import static android.app.admin.DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED;
import static android.app.admin.DevicePolicyManager.KEYGUARD_DISABLE_SECURE_NOTIFICATIONS;
import static android.app.admin.DevicePolicyManager.KEYGUARD_DISABLE_UNREDACTED_NOTIFICATIONS;
import static android.os.Flags.allowPrivateProfile;
import static android.os.UserHandle.USER_ALL;
import static android.os.UserHandle.USER_NULL;
import static android.provider.Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS;
import static android.provider.Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS;
import static android.app.Flags.keyguardPrivateNotifications;
import static android.os.Flags.allowPrivateProfile;

import static com.android.systemui.DejankUtils.whitelistIpcs;

@@ -42,9 +42,8 @@ import android.content.IntentFilter;
import android.content.IntentSender;
import android.content.pm.UserInfo;
import android.database.ContentObserver;
import android.database.ExecutorContentObserver;
import android.net.Uri;
import android.os.Handler;
import android.os.HandlerExecutor;
import android.os.Looper;
import android.os.UserHandle;
import android.os.UserManager;
@@ -79,17 +78,17 @@ import com.android.systemui.statusbar.policy.KeyguardStateController;
import com.android.systemui.util.ListenerSet;
import com.android.systemui.util.settings.SecureSettings;

import dagger.Lazy;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.Objects;
import java.util.concurrent.Executor;

import javax.inject.Inject;

import dagger.Lazy;

/**
 * Handles keeping track of the current user, profiles, and various things related to hiding
 * contents, redacting notifications, and the lockscreen.
@@ -228,7 +227,7 @@ public class NotificationLockscreenUserManagerImpl implements
                updateCurrentProfilesCache();
                if (mFeatureFlags.isEnabled(Flags.NOTIF_LS_BACKGROUND_THREAD)) {
                    final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, USER_NULL);
                    mBackgroundHandler.post(() -> {
                    mBackgroundExecutor.execute(() -> {
                        initValuesForUser(userId);
                    });
                }
@@ -289,8 +288,7 @@ public class NotificationLockscreenUserManagerImpl implements
            };

    protected final Context mContext;
    private final Handler mMainHandler;
    private final Handler mBackgroundHandler;
    private final Executor mMainExecutor;
    private final Executor mBackgroundExecutor;
    /** The current user and its profiles (possibly including a communal profile). */
    protected final SparseArray<UserInfo> mCurrentProfiles = new SparseArray<>();
@@ -313,8 +311,7 @@ public class NotificationLockscreenUserManagerImpl implements
            Lazy<OverviewProxyService> overviewProxyServiceLazy,
            KeyguardManager keyguardManager,
            StatusBarStateController statusBarStateController,
            @Main Handler mainHandler,
            @Background Handler backgroundHandler,
            @Main Executor mainExecutor,
            @Background Executor backgroundExecutor,
            DeviceProvisionedController deviceProvisionedController,
            KeyguardStateController keyguardStateController,
@@ -323,8 +320,7 @@ public class NotificationLockscreenUserManagerImpl implements
            LockPatternUtils lockPatternUtils,
            FeatureFlagsClassic featureFlags) {
        mContext = context;
        mMainHandler = mainHandler;
        mBackgroundHandler = backgroundHandler;
        mMainExecutor = mainExecutor;
        mBackgroundExecutor = backgroundExecutor;
        mDevicePolicyManager = devicePolicyManager;
        mUserManager = userManager;
@@ -362,10 +358,10 @@ public class NotificationLockscreenUserManagerImpl implements
    }

    private void init() {
        mLockscreenSettingsObserver = new ContentObserver(
        mLockscreenSettingsObserver = new ExecutorContentObserver(
                mFeatureFlags.isEnabled(Flags.NOTIF_LS_BACKGROUND_THREAD)
                        ? mBackgroundHandler
                        : mMainHandler) {
                        ? mBackgroundExecutor
                        : mMainExecutor) {

            @Override
            public void onChange(boolean selfChange, Collection<Uri> uris, int flags) {
@@ -412,7 +408,7 @@ public class NotificationLockscreenUserManagerImpl implements
            }
        };

        mSettingsObserver = new ContentObserver(mMainHandler) {
        mSettingsObserver = new ExecutorContentObserver(mMainExecutor) {
            @Override
            public void onChange(boolean selfChange) {
                updateLockscreenNotificationSetting();
@@ -468,14 +464,14 @@ public class NotificationLockscreenUserManagerImpl implements
        mContext.registerReceiver(mBaseBroadcastReceiver, internalFilter, PERMISSION_SELF, null,
                Context.RECEIVER_EXPORTED_UNAUDITED);

        mUserTracker.addCallback(mUserChangedCallback, new HandlerExecutor(mMainHandler));
        mUserTracker.addCallback(mUserChangedCallback, mMainExecutor);

        mCurrentUserId = mUserTracker.getUserId(); // in case we reg'd receiver too late
        updateCurrentProfilesCache();

        if (mFeatureFlags.isEnabled(Flags.NOTIF_LS_BACKGROUND_THREAD)) {
            // Set  up
            mBackgroundHandler.post(() -> {
            mBackgroundExecutor.execute(() -> {
                @SuppressLint("MissingPermission") List<UserInfo> users = mUserManager.getUsers();
                for (int i = users.size() - 1; i >= 0; i--) {
                    initValuesForUser(users.get(i).id);
@@ -796,7 +792,7 @@ public class NotificationLockscreenUserManagerImpl implements
                }
            }
        }
        mMainHandler.post(() -> {
        mMainExecutor.execute(() -> {
            for (UserChangedListener listener : mListeners) {
                listener.onCurrentProfilesChanged(mCurrentProfiles);
            }
@@ -895,7 +891,7 @@ public class NotificationLockscreenUserManagerImpl implements

    private void notifyNotificationStateChanged() {
        if (!Looper.getMainLooper().isCurrentThread()) {
            mMainHandler.post(() -> {
            mMainExecutor.execute(() -> {
                for (NotificationStateChangedListener listener : mNotifStateChangedListeners) {
                    listener.onNotificationStateChanged();
                }