Refactor DreamLogger with Logger

This change refactors where DreamLogger is used to take advantage of the
Logger class.

It also adds a FakeLogBuffer class which returns a real LogMessage
object to prevent a NullPointerException when operating on the message.

Bug: 276475093
Test: atest ComplicationCollectionLiveDataTest
Test: atest DreamOverlayAnimationsControllerTest
Test: atest DreamOverlayStateControllerTest
Test: atest DreamOverlayStatusBarViewControllerTest
Change-Id: I68160c4f2c1d46abb0cf67531e749d02f297047d
This commit is contained in:
Darrell Shi
2023-07-11 23:23:25 +00:00
parent f29c879cdc
commit c4ef2ff4ca
9 changed files with 148 additions and 48 deletions

View File

@@ -16,15 +16,52 @@
package com.android.systemui.dreams
import com.android.systemui.log.LogBuffer
import com.android.systemui.log.core.LogLevel
import com.android.systemui.log.dagger.DreamLog
import javax.inject.Inject
import com.android.systemui.log.core.Logger
import com.android.systemui.log.core.MessageBuffer
/** Logs dream-related stuff to a {@link LogBuffer}. */
class DreamLogger @Inject constructor(@DreamLog private val buffer: LogBuffer) {
/** Logs a debug message to the buffer. */
fun d(tag: String, message: String) {
buffer.log(tag, LogLevel.DEBUG, { str1 = message }, { message })
}
class DreamLogger(buffer: MessageBuffer, tag: String) : Logger(buffer, tag) {
fun logDreamOverlayEnabled(enabled: Boolean) =
d({ "Dream overlay enabled: $bool1" }) { bool1 = enabled }
fun logIgnoreAddComplication(reason: String, complication: String) =
d({ "Ignore adding complication, reason: $str1, complication: $str2" }) {
str1 = reason
str2 = complication
}
fun logIgnoreRemoveComplication(reason: String, complication: String) =
d({ "Ignore removing complication, reason: $str1, complication: $str2" }) {
str1 = reason
str2 = complication
}
fun logAddComplication(complication: String) =
d({ "Add dream complication: $str1" }) { str1 = complication }
fun logRemoveComplication(complication: String) =
d({ "Remove dream complication: $str1" }) { str1 = complication }
fun logOverlayActive(active: Boolean) = d({ "Dream overlay active: $bool1" }) { bool1 = active }
fun logLowLightActive(active: Boolean) =
d({ "Low light mode active: $bool1" }) { bool1 = active }
fun logHasAssistantAttention(hasAttention: Boolean) =
d({ "Dream overlay has Assistant attention: $bool1" }) { bool1 = hasAttention }
fun logStatusBarVisible(visible: Boolean) =
d({ "Dream overlay status bar visible: $bool1" }) { bool1 = visible }
fun logAvailableComplicationTypes(types: Int) =
d({ "Available complication types: $int1" }) { int1 = types }
fun logShouldShowComplications(showComplications: Boolean) =
d({ "Dream overlay should show complications: $bool1" }) { bool1 = showComplications }
fun logShowOrHideStatusBarItem(show: Boolean, type: String) =
d({ "${if (bool1) "Showing" else "Hiding"} dream status bar item: $int1" }) {
bool1 = show
str1 = type
}
}

View File

@@ -36,6 +36,9 @@ import com.android.systemui.complication.ComplicationLayoutParams.Position
import com.android.systemui.dreams.dagger.DreamOverlayModule
import com.android.systemui.keyguard.ui.viewmodel.DreamingToLockscreenTransitionViewModel
import com.android.systemui.lifecycle.repeatWhenAttached
import com.android.systemui.log.LogBuffer
import com.android.systemui.log.core.Logger
import com.android.systemui.log.dagger.DreamLog
import com.android.systemui.statusbar.BlurUtils
import com.android.systemui.statusbar.CrossFadeHelper
import com.android.systemui.statusbar.policy.ConfigurationController
@@ -65,12 +68,14 @@ constructor(
private val mDreamInTranslationYDistance: Int,
@Named(DreamOverlayModule.DREAM_IN_TRANSLATION_Y_DURATION)
private val mDreamInTranslationYDurationMs: Long,
private val mLogger: DreamLogger,
@DreamLog logBuffer: LogBuffer,
) {
companion object {
private const val TAG = "DreamOverlayAnimationsController"
}
private val logger = Logger(logBuffer, TAG)
private var mAnimator: Animator? = null
private lateinit var view: View
@@ -179,11 +184,11 @@ constructor(
doOnEnd {
mAnimator = null
mOverlayStateController.setEntryAnimationsFinished(true)
mLogger.d(TAG, "Dream overlay entry animations finished.")
logger.d("Dream overlay entry animations finished.")
}
doOnCancel { mLogger.d(TAG, "Dream overlay entry animations canceled.") }
doOnCancel { logger.d("Dream overlay entry animations canceled.") }
start()
mLogger.d(TAG, "Dream overlay entry animations started.")
logger.d("Dream overlay entry animations started.")
}
}
@@ -242,11 +247,11 @@ constructor(
doOnEnd {
mAnimator = null
mOverlayStateController.setExitAnimationsRunning(false)
mLogger.d(TAG, "Dream overlay exit animations finished.")
logger.d("Dream overlay exit animations finished.")
}
doOnCancel { mLogger.d(TAG, "Dream overlay exit animations canceled.") }
doOnCancel { logger.d("Dream overlay exit animations canceled.") }
start()
mLogger.d(TAG, "Dream overlay exit animations started.")
logger.d("Dream overlay exit animations started.")
}
mOverlayStateController.setExitAnimationsRunning(true)
return mAnimator as AnimatorSet

View File

@@ -28,6 +28,8 @@ import com.android.systemui.dagger.SysUISingleton;
import com.android.systemui.dagger.qualifiers.Main;
import com.android.systemui.flags.FeatureFlags;
import com.android.systemui.flags.Flags;
import com.android.systemui.log.LogBuffer;
import com.android.systemui.log.dagger.DreamLog;
import com.android.systemui.statusbar.policy.CallbackController;
import java.util.ArrayList;
@@ -115,10 +117,10 @@ public class DreamOverlayStateController implements
public DreamOverlayStateController(@Main Executor executor,
@Named(DREAM_OVERLAY_ENABLED) boolean overlayEnabled,
FeatureFlags featureFlags,
DreamLogger dreamLogger) {
@DreamLog LogBuffer logBuffer) {
mExecutor = executor;
mOverlayEnabled = overlayEnabled;
mLogger = dreamLogger;
mLogger = new DreamLogger(logBuffer, TAG);
mFeatureFlags = featureFlags;
if (mFeatureFlags.isEnabled(Flags.ALWAYS_SHOW_HOME_CONTROLS_ON_DREAMS)) {
mSupportedTypes = Complication.COMPLICATION_TYPE_NONE
@@ -126,7 +128,7 @@ public class DreamOverlayStateController implements
} else {
mSupportedTypes = Complication.COMPLICATION_TYPE_NONE;
}
mLogger.d(TAG, "Dream overlay enabled: " + mOverlayEnabled);
mLogger.logDreamOverlayEnabled(mOverlayEnabled);
}
/**
@@ -134,14 +136,13 @@ public class DreamOverlayStateController implements
*/
public void addComplication(Complication complication) {
if (!mOverlayEnabled) {
mLogger.d(TAG,
"Ignoring adding complication due to overlay disabled: " + complication);
mLogger.logIgnoreAddComplication("overlay disabled", complication.toString());
return;
}
mExecutor.execute(() -> {
if (mComplications.add(complication)) {
mLogger.d(TAG, "Added dream complication: " + complication);
mLogger.logAddComplication(complication.toString());
mCallbacks.stream().forEach(callback -> callback.onComplicationsChanged());
}
});
@@ -152,14 +153,13 @@ public class DreamOverlayStateController implements
*/
public void removeComplication(Complication complication) {
if (!mOverlayEnabled) {
mLogger.d(TAG,
"Ignoring removing complication due to overlay disabled: " + complication);
mLogger.logIgnoreRemoveComplication("overlay disabled", complication.toString());
return;
}
mExecutor.execute(() -> {
if (mComplications.remove(complication)) {
mLogger.d(TAG, "Removed dream complication: " + complication);
mLogger.logRemoveComplication(complication.toString());
mCallbacks.stream().forEach(callback -> callback.onComplicationsChanged());
}
});
@@ -305,7 +305,7 @@ public class DreamOverlayStateController implements
* @param active {@code true} if overlay is active, {@code false} otherwise.
*/
public void setOverlayActive(boolean active) {
mLogger.d(TAG, "Dream overlay active: " + active);
mLogger.logOverlayActive(active);
modifyState(active ? OP_SET_STATE : OP_CLEAR_STATE, STATE_DREAM_OVERLAY_ACTIVE);
}
@@ -314,7 +314,7 @@ public class DreamOverlayStateController implements
* @param active {@code true} if low light mode is active, {@code false} otherwise.
*/
public void setLowLightActive(boolean active) {
mLogger.d(TAG, "Low light mode active: " + active);
mLogger.logLowLightActive(active);
if (isLowLightActive() && !active) {
// Notify that we're exiting low light only on the transition from active to not active.
@@ -346,7 +346,7 @@ public class DreamOverlayStateController implements
* @param hasAttention {@code true} if has the user's attention, {@code false} otherwise.
*/
public void setHasAssistantAttention(boolean hasAttention) {
mLogger.d(TAG, "Dream overlay has Assistant attention: " + hasAttention);
mLogger.logHasAssistantAttention(hasAttention);
modifyState(hasAttention ? OP_SET_STATE : OP_CLEAR_STATE, STATE_HAS_ASSISTANT_ATTENTION);
}
@@ -355,7 +355,7 @@ public class DreamOverlayStateController implements
* @param visible {@code true} if the status bar is visible, {@code false} otherwise.
*/
public void setDreamOverlayStatusBarVisible(boolean visible) {
mLogger.d(TAG, "Dream overlay status bar visible: " + visible);
mLogger.logStatusBarVisible(visible);
modifyState(
visible ? OP_SET_STATE : OP_CLEAR_STATE, STATE_DREAM_OVERLAY_STATUS_BAR_VISIBLE);
}
@@ -373,7 +373,7 @@ public class DreamOverlayStateController implements
*/
public void setAvailableComplicationTypes(@Complication.ComplicationType int types) {
mExecutor.execute(() -> {
mLogger.d(TAG, "Available complication types: " + types);
mLogger.logAvailableComplicationTypes(types);
mAvailableComplicationTypes = types;
mCallbacks.forEach(Callback::onAvailableComplicationTypesChanged);
});
@@ -391,7 +391,7 @@ public class DreamOverlayStateController implements
*/
public void setShouldShowComplications(boolean shouldShowComplications) {
mExecutor.execute(() -> {
mLogger.d(TAG, "Should show complications: " + shouldShowComplications);
mLogger.logShouldShowComplications(shouldShowComplications);
mShouldShowComplications = shouldShowComplications;
mCallbacks.forEach(Callback::onAvailableComplicationTypesChanged);
});

View File

@@ -36,6 +36,8 @@ import com.android.systemui.R;
import com.android.systemui.dagger.qualifiers.Main;
import com.android.systemui.dreams.DreamOverlayStatusBarItemsProvider.StatusBarItem;
import com.android.systemui.dreams.dagger.DreamOverlayComponent;
import com.android.systemui.log.LogBuffer;
import com.android.systemui.log.dagger.DreamLog;
import com.android.systemui.settings.UserTracker;
import com.android.systemui.statusbar.CrossFadeHelper;
import com.android.systemui.statusbar.policy.IndividualSensorPrivacyController;
@@ -161,7 +163,7 @@ public class DreamOverlayStatusBarViewController extends ViewController<DreamOve
DreamOverlayStatusBarItemsProvider statusBarItemsProvider,
DreamOverlayStateController dreamOverlayStateController,
UserTracker userTracker,
DreamLogger dreamLogger) {
@DreamLog LogBuffer logBuffer) {
super(view);
mResources = resources;
mMainExecutor = mainExecutor;
@@ -177,7 +179,7 @@ public class DreamOverlayStatusBarViewController extends ViewController<DreamOve
mZenModeController = zenModeController;
mDreamOverlayStateController = dreamOverlayStateController;
mUserTracker = userTracker;
mLogger = dreamLogger;
mLogger = new DreamLogger(logBuffer, TAG);
// Register to receive show/hide updates for the system status bar. Our custom status bar
// needs to hide when the system status bar is showing to ovoid overlapping status bars.
@@ -346,8 +348,8 @@ public class DreamOverlayStatusBarViewController extends ViewController<DreamOve
@Nullable String contentDescription) {
mMainExecutor.execute(() -> {
if (mIsAttached) {
mLogger.d(TAG, (show ? "Showing" : "Hiding") + " dream status bar item: "
+ DreamOverlayStatusBarView.getLoggableStatusIconType(iconType));
mLogger.logShowOrHideStatusBarItem(
show, DreamOverlayStatusBarView.getLoggableStatusIconType(iconType));
mView.showIcon(iconType, show, contentDescription);
}
});

View File

@@ -28,10 +28,10 @@ import androidx.lifecycle.Observer;
import androidx.test.filters.SmallTest;
import com.android.systemui.SysuiTestCase;
import com.android.systemui.dreams.DreamLogger;
import com.android.systemui.dreams.DreamOverlayStateController;
import com.android.systemui.flags.FakeFeatureFlags;
import com.android.systemui.flags.Flags;
import com.android.systemui.log.core.FakeLogBuffer;
import com.android.systemui.util.concurrency.FakeExecutor;
import com.android.systemui.util.time.FakeSystemClock;
@@ -57,8 +57,6 @@ public class ComplicationCollectionLiveDataTest extends SysuiTestCase {
private FakeFeatureFlags mFeatureFlags;
@Mock
private Observer mObserver;
@Mock
private DreamLogger mLogger;
@Before
public void setUp() {
@@ -70,7 +68,7 @@ public class ComplicationCollectionLiveDataTest extends SysuiTestCase {
mExecutor,
/* overlayEnabled= */ true,
mFeatureFlags,
mLogger);
FakeLogBuffer.Factory.Companion.create());
mLiveData = new ComplicationCollectionLiveData(mStateController);
}

View File

@@ -9,6 +9,7 @@ import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.complication.ComplicationHostViewController
import com.android.systemui.keyguard.ui.viewmodel.DreamingToLockscreenTransitionViewModel
import com.android.systemui.log.core.FakeLogBuffer
import com.android.systemui.statusbar.BlurUtils
import com.android.systemui.statusbar.policy.ConfigurationController
import com.android.systemui.util.mockito.argumentCaptor
@@ -47,7 +48,7 @@ class DreamOverlayAnimationsControllerTest : SysuiTestCase() {
@Mock private lateinit var stateController: DreamOverlayStateController
@Mock private lateinit var configController: ConfigurationController
@Mock private lateinit var transitionViewModel: DreamingToLockscreenTransitionViewModel
@Mock private lateinit var logger: DreamLogger
private val logBuffer = FakeLogBuffer.Factory.create()
private lateinit var controller: DreamOverlayAnimationsController
@Before
@@ -66,7 +67,7 @@ class DreamOverlayAnimationsControllerTest : SysuiTestCase() {
DREAM_IN_COMPLICATIONS_ANIMATION_DURATION,
DREAM_IN_TRANSLATION_Y_DISTANCE,
DREAM_IN_TRANSLATION_Y_DURATION,
logger
logBuffer
)
val mockView: View = mock()

View File

@@ -34,6 +34,8 @@ import com.android.systemui.SysuiTestCase;
import com.android.systemui.complication.Complication;
import com.android.systemui.flags.FeatureFlags;
import com.android.systemui.flags.Flags;
import com.android.systemui.log.LogBuffer;
import com.android.systemui.log.core.FakeLogBuffer;
import com.android.systemui.util.concurrency.FakeExecutor;
import com.android.systemui.util.time.FakeSystemClock;
@@ -58,8 +60,7 @@ public class DreamOverlayStateControllerTest extends SysuiTestCase {
@Mock
private FeatureFlags mFeatureFlags;
@Mock
private DreamLogger mLogger;
private final LogBuffer mLogBuffer = FakeLogBuffer.Factory.Companion.create();
final FakeExecutor mExecutor = new FakeExecutor(new FakeSystemClock());
@@ -408,6 +409,11 @@ public class DreamOverlayStateControllerTest extends SysuiTestCase {
}
private DreamOverlayStateController getDreamOverlayStateController(boolean overlayEnabled) {
return new DreamOverlayStateController(mExecutor, overlayEnabled, mFeatureFlags, mLogger);
return new DreamOverlayStateController(
mExecutor,
overlayEnabled,
mFeatureFlags,
mLogBuffer
);
}
}

View File

@@ -48,6 +48,8 @@ import androidx.test.filters.SmallTest;
import com.android.systemui.R;
import com.android.systemui.SysuiTestCase;
import com.android.systemui.log.LogBuffer;
import com.android.systemui.log.core.FakeLogBuffer;
import com.android.systemui.settings.UserTracker;
import com.android.systemui.statusbar.policy.IndividualSensorPrivacyController;
import com.android.systemui.statusbar.policy.NextAlarmController;
@@ -113,8 +115,8 @@ public class DreamOverlayStatusBarViewControllerTest extends SysuiTestCase {
DreamOverlayStateController mDreamOverlayStateController;
@Mock
UserTracker mUserTracker;
@Mock
DreamLogger mLogger;
LogBuffer mLogBuffer = FakeLogBuffer.Factory.Companion.create();
@Captor
private ArgumentCaptor<DreamOverlayStateController.Callback> mCallbackCaptor;
@@ -149,7 +151,7 @@ public class DreamOverlayStatusBarViewControllerTest extends SysuiTestCase {
mDreamOverlayStatusBarItemsProvider,
mDreamOverlayStateController,
mUserTracker,
mLogger);
mLogBuffer);
}
@Test
@@ -293,7 +295,7 @@ public class DreamOverlayStatusBarViewControllerTest extends SysuiTestCase {
mDreamOverlayStatusBarItemsProvider,
mDreamOverlayStateController,
mUserTracker,
mLogger);
mLogBuffer);
controller.onViewAttached();
verify(mView, never()).showIcon(
eq(DreamOverlayStatusBarView.STATUS_ICON_NOTIFICATIONS), eq(true), any());

View File

@@ -0,0 +1,49 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.systemui.log.core
import com.android.systemui.log.LogBuffer
import com.android.systemui.log.LogMessageImpl
import com.android.systemui.util.mockito.any
import com.android.systemui.util.mockito.mock
import com.android.systemui.util.mockito.nullable
import com.android.systemui.util.mockito.whenever
import org.mockito.Mockito.anyString
/**
* A fake [LogBuffer] used for testing that obtains a real [LogMessage] to prevent a
* [NullPointerException].
*/
class FakeLogBuffer private constructor() {
class Factory private constructor() {
companion object {
fun create(): LogBuffer {
val logBuffer = mock<LogBuffer>()
whenever(
logBuffer.obtain(
tag = anyString(),
level = any(),
messagePrinter = any(),
exception = nullable(),
)
)
.thenReturn(LogMessageImpl.Factory.create())
return logBuffer
}
}
}
}