diff --git a/packages/SystemUI/src/com/android/systemui/communal/CommunalManagerUpdater.java b/packages/SystemUI/src/com/android/systemui/communal/CommunalManagerUpdater.java new file mode 100644 index 0000000000000..ebe804af15e8e --- /dev/null +++ b/packages/SystemUI/src/com/android/systemui/communal/CommunalManagerUpdater.java @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2021 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.communal; + +import android.app.communal.CommunalManager; +import android.content.Context; +import android.util.Log; + +import com.android.systemui.CoreStartable; +import com.android.systemui.dagger.SysUISingleton; + +import java.lang.ref.WeakReference; + +import javax.inject.Inject; + +/** + * The {@link CommunalManagerUpdater} is responsible for forwarding state from SystemUI to + * the {@link CommunalManager} system service. + */ +@SysUISingleton +public class CommunalManagerUpdater extends CoreStartable { + private static final String TAG = "CommunalManagerUpdater"; + + private final CommunalManager mCommunalManager; + private final CommunalSourceMonitor mMonitor; + + private final CommunalSourceMonitor.Callback mSourceCallback = + new CommunalSourceMonitor.Callback() { + @Override + public void onSourceAvailable(WeakReference source) { + try { + mCommunalManager.setCommunalViewShowing( + source != null && source.get() != null); + } catch (RuntimeException e) { + Log.e(TAG, "Error updating communal manager service state", e); + } + } + }; + + @Inject + public CommunalManagerUpdater(Context context, CommunalSourceMonitor monitor) { + super(context); + mCommunalManager = context.getSystemService(CommunalManager.class); + mMonitor = monitor; + } + + @Override + public void start() { + if (mCommunalManager != null) { + mMonitor.addCallback(mSourceCallback); + } + } +} diff --git a/packages/SystemUI/src/com/android/systemui/communal/CommunalStateController.java b/packages/SystemUI/src/com/android/systemui/communal/CommunalStateController.java index 7be8eccb9e1a4..c72f5422b1f54 100644 --- a/packages/SystemUI/src/com/android/systemui/communal/CommunalStateController.java +++ b/packages/SystemUI/src/com/android/systemui/communal/CommunalStateController.java @@ -17,9 +17,6 @@ package com.android.systemui.communal; import android.annotation.NonNull; -import android.app.communal.CommunalManager; -import android.content.Context; -import android.util.Log; import com.android.internal.annotations.VisibleForTesting; import com.android.systemui.dagger.SysUISingleton; @@ -36,9 +33,7 @@ import javax.inject.Inject; @SysUISingleton public class CommunalStateController implements CallbackController { - private static final String TAG = CommunalStateController.class.getSimpleName(); private final ArrayList mCallbacks = new ArrayList<>(); - private final CommunalManager mCommunalManager; private boolean mCommunalViewOccluded; private boolean mCommunalViewShowing; @@ -61,8 +56,7 @@ public class CommunalStateController implements @VisibleForTesting @Inject - public CommunalStateController(Context context) { - mCommunalManager = context.getSystemService(CommunalManager.class); + public CommunalStateController() { } /** @@ -76,12 +70,6 @@ public class CommunalStateController implements mCommunalViewShowing = communalViewShowing; - try { - mCommunalManager.setCommunalViewShowing(communalViewShowing); - } catch (RuntimeException e) { - Log.e(TAG, "Error updating communal manager service state", e); - } - final ArrayList callbacks = new ArrayList<>(mCallbacks); for (Callback callback : callbacks) { callback.onCommunalViewShowingChanged(); diff --git a/packages/SystemUI/src/com/android/systemui/dagger/SystemUIBinder.java b/packages/SystemUI/src/com/android/systemui/dagger/SystemUIBinder.java index e5c6ab5479993..34261487cadc8 100644 --- a/packages/SystemUI/src/com/android/systemui/dagger/SystemUIBinder.java +++ b/packages/SystemUI/src/com/android/systemui/dagger/SystemUIBinder.java @@ -23,6 +23,7 @@ import com.android.systemui.SliceBroadcastRelayHandler; import com.android.systemui.accessibility.SystemActions; import com.android.systemui.accessibility.WindowMagnification; import com.android.systemui.biometrics.AuthController; +import com.android.systemui.communal.CommunalManagerUpdater; import com.android.systemui.dreams.DreamOverlayRegistrant; import com.android.systemui.dreams.appwidgets.AppWidgetOverlayPrimer; import com.android.systemui.globalactions.GlobalActionsComponent; @@ -204,4 +205,11 @@ public abstract class SystemUIBinder { @ClassKey(AppWidgetOverlayPrimer.class) public abstract CoreStartable bindAppWidgetOverlayPrimer( AppWidgetOverlayPrimer appWidgetOverlayPrimer); + + /** Inject into CommunalManagerUpdater. */ + @Binds + @IntoMap + @ClassKey(CommunalManagerUpdater.class) + public abstract CoreStartable bindCommunalManagerUpdater( + CommunalManagerUpdater communalManagerUpdater); } diff --git a/packages/SystemUI/tests/src/com/android/systemui/communal/CommunalManagerUpdaterTest.java b/packages/SystemUI/tests/src/com/android/systemui/communal/CommunalManagerUpdaterTest.java new file mode 100644 index 0000000000000..e7ae6cb1fe91d --- /dev/null +++ b/packages/SystemUI/tests/src/com/android/systemui/communal/CommunalManagerUpdaterTest.java @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2021 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.communal; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +import android.app.communal.CommunalManager; +import android.os.Handler; +import android.testing.AndroidTestingRunner; +import android.testing.TestableLooper; + +import androidx.test.filters.SmallTest; + +import com.android.systemui.SysuiTestCase; +import com.android.systemui.util.settings.FakeSettings; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +@SmallTest +@RunWith(AndroidTestingRunner.class) +@TestableLooper.RunWithLooper +public class CommunalManagerUpdaterTest extends SysuiTestCase { + private CommunalSourceMonitor mMonitor; + @Mock + private CommunalManager mCommunalManager; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + mContext.addMockSystemService(CommunalManager.class, mCommunalManager); + + mMonitor = new CommunalSourceMonitor( + Handler.createAsync(TestableLooper.get(this).getLooper()), + new FakeSettings()); + final CommunalManagerUpdater updater = new CommunalManagerUpdater(mContext, mMonitor); + updater.start(); + } + + @Test + public void testUpdateSystemService_false() { + mMonitor.setSource(null); + verify(mCommunalManager).setCommunalViewShowing(false); + } + + @Test + public void testUpdateSystemService_true() { + final CommunalSource source = mock(CommunalSource.class); + mMonitor.setSource(source); + verify(mCommunalManager).setCommunalViewShowing(true); + } +} diff --git a/packages/SystemUI/tests/src/com/android/systemui/communal/CommunalStateControllerTest.java b/packages/SystemUI/tests/src/com/android/systemui/communal/CommunalStateControllerTest.java index 42abff0cca349..7f85c35af7422 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/communal/CommunalStateControllerTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/communal/CommunalStateControllerTest.java @@ -48,13 +48,13 @@ public class CommunalStateControllerTest extends SysuiTestCase { @Test public void testDefaultCommunalViewShowingState() { // The state controller should report the communal view as not showing by default. - final CommunalStateController stateController = new CommunalStateController(getContext()); + final CommunalStateController stateController = new CommunalStateController(); assertThat(stateController.getCommunalViewShowing()).isFalse(); } @Test public void testNotifyCommunalSurfaceShow() { - final CommunalStateController stateController = new CommunalStateController(getContext()); + final CommunalStateController stateController = new CommunalStateController(); stateController.addCallback(mCallback); // Verify setting communal view to showing propagates to callback. @@ -72,7 +72,7 @@ public class CommunalStateControllerTest extends SysuiTestCase { @Test public void testCallbackRegistration() { - final CommunalStateController stateController = new CommunalStateController(getContext()); + final CommunalStateController stateController = new CommunalStateController(); stateController.addCallback(mCallback); // Verify setting communal view to showing propagates to callback.