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

Commit 33ce6d97 authored by Robert Snoeberger's avatar Robert Snoeberger
Browse files

Remove view from existing clock plugin when new plugin connects.

Assuming that the most recently connected plugin is the active
clock face. This should be updated when it is possible for
the user to select a clock face.

Bug: 118440898
Test: Added tests to KeyguardClockSwitchTest.
Change-Id: I1c38005b4d7d32a27a8d41806390338ebcab458b
parent 3749d819
Loading
Loading
Loading
Loading
+18 −4
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@ import com.android.systemui.plugins.ClockPlugin;
import com.android.systemui.plugins.PluginListener;
import com.android.systemui.shared.plugins.PluginManager;

import java.util.Objects;

/**
 * Switch to show plugin clock when plugin is connected, otherwise it will show default clock.
 */
@@ -35,6 +37,10 @@ public class KeyguardClockSwitch extends FrameLayout {
                public void onPluginConnected(ClockPlugin plugin, Context pluginContext) {
                    View view = plugin.getView();
                    if (view != null) {
                        disconnectPlugin();
                        // For now, assume that the most recently connected plugin is the
                        // selected clock face. In the future, the user should be able to
                        // pick a clock face from the available plugins.
                        mClockPlugin = plugin;
                        addView(view, -1,
                                new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
@@ -46,10 +52,8 @@ public class KeyguardClockSwitch extends FrameLayout {

                @Override
                public void onPluginDisconnected(ClockPlugin plugin) {
                    View view = plugin.getView();
                    if (view != null) {
                        mClockPlugin = null;
                        removeView(view);
                    if (Objects.equals(plugin, mClockPlugin)) {
                        disconnectPlugin();
                        mClockView.setVisibility(View.VISIBLE);
                    }
                }
@@ -148,6 +152,16 @@ public class KeyguardClockSwitch extends FrameLayout {
        }
    }

    private void disconnectPlugin() {
        if (mClockPlugin != null) {
            View view = mClockPlugin.getView();
            if (view != null) {
                removeView(view);
            }
            mClockPlugin = null;
        }
    }

    @VisibleForTesting (otherwise = VisibleForTesting.NONE)
    PluginListener getClockPluginListener() {
        return mClockPluginListener;
+72 −11
Original line number Diff line number Diff line
@@ -21,10 +21,11 @@ import static android.view.View.VISIBLE;

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

import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;

import android.graphics.Color;
@@ -34,7 +35,6 @@ import android.testing.AndroidTestingRunner;
import android.testing.TestableLooper.RunWithLooper;
import android.text.TextPaint;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.TextClock;

import com.android.systemui.SysuiTestCase;
@@ -68,6 +68,7 @@ public class KeyguardClockSwitchTest extends SysuiTestCase {
        mKeyguardClockSwitch =
                (KeyguardClockSwitch) layoutInflater.inflate(R.layout.keyguard_clock_switch, null);
        MockitoAnnotations.initMocks(this);
        when(mClockView.getPaint()).thenReturn(mock(TextPaint.class));
    }

    @Test
@@ -91,8 +92,6 @@ public class KeyguardClockSwitchTest extends SysuiTestCase {
        ClockPlugin plugin = mock(ClockPlugin.class);
        TextClock pluginView = new TextClock(getContext());
        when(plugin.getView()).thenReturn(pluginView);
        TextPaint paint = mock(TextPaint.class);
        doReturn(paint).when(mClockView).getPaint();
        PluginListener listener = mKeyguardClockSwitch.getClockPluginListener();

        listener.onPluginConnected(plugin, null);
@@ -101,23 +100,89 @@ public class KeyguardClockSwitchTest extends SysuiTestCase {
        assertThat(plugin.getView().getParent()).isEqualTo(mKeyguardClockSwitch);
    }

    @Test
    public void onPluginConnected_nullView() {
        ClockPlugin plugin = mock(ClockPlugin.class);
        PluginListener listener = mKeyguardClockSwitch.getClockPluginListener();
        listener.onPluginConnected(plugin, null);
        verify(mClockView, never()).setVisibility(GONE);
    }

    @Test
    public void onPluginConnected_showSecondPluginClock() {
        // GIVEN a plugin has already connected
        ClockPlugin plugin1 = mock(ClockPlugin.class);
        when(plugin1.getView()).thenReturn(new TextClock(getContext()));
        PluginListener listener = mKeyguardClockSwitch.getClockPluginListener();
        listener.onPluginConnected(plugin1, null);
        // WHEN a second plugin is connected
        ClockPlugin plugin2 = mock(ClockPlugin.class);
        when(plugin2.getView()).thenReturn(new TextClock(getContext()));
        listener.onPluginConnected(plugin2, null);
        // THEN only the view from the second plugin should be a child of KeyguardClockSwitch.
        assertThat(plugin2.getView().getParent()).isEqualTo(mKeyguardClockSwitch);
        assertThat(plugin1.getView().getParent()).isNull();
    }

    @Test
    public void onPluginDisconnected_showDefaultClock() {
        ClockPlugin plugin = mock(ClockPlugin.class);
        TextClock pluginView = new TextClock(getContext());
        when(plugin.getView()).thenReturn(pluginView);
        mClockView.setVisibility(GONE);
        mKeyguardClockSwitch.addView(plugin.getView(), -1,
                new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT));
        PluginListener listener = mKeyguardClockSwitch.getClockPluginListener();

        listener.onPluginConnected(plugin, null);
        listener.onPluginDisconnected(plugin);

        verify(mClockView).setVisibility(VISIBLE);
        assertThat(plugin.getView().getParent()).isNull();
    }

    @Test
    public void onPluginDisconnected_nullView() {
        ClockPlugin plugin = mock(ClockPlugin.class);
        PluginListener listener = mKeyguardClockSwitch.getClockPluginListener();
        listener.onPluginConnected(plugin, null);
        listener.onPluginDisconnected(plugin);
        verify(mClockView, never()).setVisibility(GONE);
    }

    @Test
    public void onPluginDisconnected_firstOfTwoDisconnected() {
        // GIVEN two plugins are connected
        ClockPlugin plugin1 = mock(ClockPlugin.class);
        when(plugin1.getView()).thenReturn(new TextClock(getContext()));
        PluginListener listener = mKeyguardClockSwitch.getClockPluginListener();
        listener.onPluginConnected(plugin1, null);
        ClockPlugin plugin2 = mock(ClockPlugin.class);
        when(plugin2.getView()).thenReturn(new TextClock(getContext()));
        listener.onPluginConnected(plugin2, null);
        // WHEN the first plugin is disconnected
        listener.onPluginDisconnected(plugin1);
        // THEN the view from the second plugin is still a child of KeyguardClockSwitch.
        assertThat(plugin2.getView().getParent()).isEqualTo(mKeyguardClockSwitch);
        assertThat(plugin1.getView().getParent()).isNull();
    }

    @Test
    public void onPluginDisconnected_secondOfTwoDisconnected() {
        // GIVEN two plugins are connected
        ClockPlugin plugin1 = mock(ClockPlugin.class);
        when(plugin1.getView()).thenReturn(new TextClock(getContext()));
        PluginListener listener = mKeyguardClockSwitch.getClockPluginListener();
        listener.onPluginConnected(plugin1, null);
        ClockPlugin plugin2 = mock(ClockPlugin.class);
        when(plugin2.getView()).thenReturn(new TextClock(getContext()));
        listener.onPluginConnected(plugin2, null);
        // WHEN the second plugin is disconnected
        listener.onPluginDisconnected(plugin2);
        // THEN the default clock should be shown.
        verify(mClockView).setVisibility(VISIBLE);
        assertThat(plugin1.getView().getParent()).isNull();
        assertThat(plugin2.getView().getParent()).isNull();
    }

    @Test
    public void setTextColor_defaultClockSetTextColor() {
        mKeyguardClockSwitch.setTextColor(Color.YELLOW);
@@ -130,8 +195,6 @@ public class KeyguardClockSwitchTest extends SysuiTestCase {
        ClockPlugin plugin = mock(ClockPlugin.class);
        TextClock pluginView = new TextClock(getContext());
        when(plugin.getView()).thenReturn(pluginView);
        TextPaint paint = mock(TextPaint.class);
        doReturn(paint).when(mClockView).getPaint();
        PluginListener listener = mKeyguardClockSwitch.getClockPluginListener();
        listener.onPluginConnected(plugin, null);

@@ -156,8 +219,6 @@ public class KeyguardClockSwitchTest extends SysuiTestCase {
        ClockPlugin plugin = mock(ClockPlugin.class);
        TextClock pluginView = new TextClock(getContext());
        when(plugin.getView()).thenReturn(pluginView);
        TextPaint paint = mock(TextPaint.class);
        doReturn(paint).when(mClockView).getPaint();
        Style style = mock(Style.class);
        PluginListener listener = mKeyguardClockSwitch.getClockPluginListener();
        listener.onPluginConnected(plugin, null);