import androidx.loader.app.LoaderManager;
import androidx.loader.content.Loader;
+import com.android.settings.homepage.contextualcards.conditional.ConditionalCardController;
import com.android.settings.homepage.contextualcards.slices.SliceContextualCardRenderer;
import com.android.settings.overlay.FeatureFactory;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
{ContextualCard.CardType.CONDITIONAL, ContextualCard.CardType.LEGACY_SUGGESTION};
private final Context mContext;
- private final ControllerRendererPool mControllerRendererPool;
private final Lifecycle mLifecycle;
private final List<LifecycleObserver> mLifecycleObservers;
private ContextualCardUpdateListener mListener;
@VisibleForTesting
+ final ControllerRendererPool mControllerRendererPool;
+ @VisibleForTesting
final List<ContextualCard> mContextualCards;
@VisibleForTesting
long mStartTime;
public void onWindowFocusChanged(boolean hasWindowFocus) {
// Duplicate a list to avoid java.util.ConcurrentModificationException.
final List<ContextualCard> cards = new ArrayList<>(mContextualCards);
+ boolean hasConditionController = false;
for (ContextualCard card : cards) {
- final ContextualCardController controller = mControllerRendererPool
+ final ContextualCardController controller = getControllerRendererPool()
.getController(mContext, card.getCardType());
+ if (controller instanceof ConditionalCardController) {
+ hasConditionController = true;
+ }
+ if (hasWindowFocus && controller instanceof OnStart) {
+ ((OnStart) controller).onStart();
+ }
+ if (!hasWindowFocus && controller instanceof OnStop) {
+ ((OnStop) controller).onStop();
+ }
+ }
+ // Conditional cards will always be refreshed whether or not there are conditional cards
+ // in the homepage.
+ if (!hasConditionController) {
+ final ContextualCardController controller = getControllerRendererPool()
+ .getController(mContext, ContextualCard.CardType.CONDITIONAL);
if (hasWindowFocus && controller instanceof OnStart) {
((OnStart) controller).onStart();
}
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.ArgumentMatchers.nullable;
+import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import android.provider.Settings;
import android.util.ArrayMap;
+import com.android.settings.homepage.contextualcards.conditional.ConditionContextualCardController;
import com.android.settings.homepage.contextualcards.conditional.ConditionFooterContextualCard;
import com.android.settings.homepage.contextualcards.conditional.ConditionHeaderContextualCard;
import com.android.settings.homepage.contextualcards.conditional.ConditionalContextualCard;
import com.android.settings.slices.CustomSliceRegistry;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
+import com.android.settingslib.core.lifecycle.events.OnStart;
+import com.android.settingslib.core.lifecycle.events.OnStop;
import org.junit.Before;
import org.junit.Test;
}
@Test
+ public void onWindowFocusChanged_hasFocusAndNoConditional_startMonitoringConditionCard() {
+ final ContextualCardManager manager = spy(mManager);
+ final ControllerRendererPool pool = spy(mManager.mControllerRendererPool);
+ doReturn(pool).when(manager).getControllerRendererPool();
+ final ConditionContextualCardController conditionController =
+ pool.getController(mContext,
+ ContextualCard.CardType.CONDITIONAL);
+ final OnStart controller = spy((OnStart)conditionController);
+ doReturn(controller).when(pool).getController(mContext, ContextualCard.CardType.CONDITIONAL);
+
+ manager.onWindowFocusChanged(true /* hasWindowFocus */);
+
+ verify(controller, atLeast(1)).onStart();
+ }
+
+ @Test
+ public void onWindowFocusChanged_hasFocusAndHasConditionals_startMonitoringConditionCard() {
+ mManager.mContextualCards.add(new ConditionalContextualCard.Builder().build());
+ mManager.mContextualCards.add(new ConditionFooterContextualCard.Builder().build());
+ final ContextualCardManager manager = spy(mManager);
+ final ControllerRendererPool pool = spy(mManager.mControllerRendererPool);
+ doReturn(pool).when(manager).getControllerRendererPool();
+ final ConditionContextualCardController conditionController =
+ pool.getController(mContext,
+ ContextualCard.CardType.CONDITIONAL);
+ final OnStart controller = spy((OnStart)conditionController);
+ doReturn(controller).when(pool).getController(mContext, ContextualCard.CardType.CONDITIONAL);
+
+ manager.onWindowFocusChanged(true /* hasWindowFocus */);
+
+ verify(controller, atLeast(2)).onStart();
+ }
+
+ @Test
+ public void onWindowFocusChanged_loseFocusAndHasConditionals_stopMonitoringConditionCard() {
+ mManager.mContextualCards.add(new ConditionalContextualCard.Builder().build());
+ mManager.mContextualCards.add(new ConditionFooterContextualCard.Builder().build());
+ final ContextualCardManager manager = spy(mManager);
+ final ControllerRendererPool pool = spy(mManager.mControllerRendererPool);
+ doReturn(pool).when(manager).getControllerRendererPool();
+ final ConditionContextualCardController conditionController =
+ pool.getController(mContext,
+ ContextualCard.CardType.CONDITIONAL);
+ final OnStop controller = spy((OnStop) conditionController);
+ doReturn(controller).when(pool).getController(mContext, ContextualCard.CardType.CONDITIONAL);
+
+ manager.onWindowFocusChanged(false /* hasWindowFocus */);
+
+ verify(controller, atLeast(2)).onStop();
+ }
+
+ @Test
+ public void onWindowFocusChanged_loseFocusAndNoConditional_stopMonitoringConditionCard() {
+ final ContextualCardManager manager = spy(mManager);
+ final ControllerRendererPool pool = spy(mManager.mControllerRendererPool);
+ doReturn(pool).when(manager).getControllerRendererPool();
+ final ConditionContextualCardController conditionController =
+ pool.getController(mContext,
+ ContextualCard.CardType.CONDITIONAL);
+ final OnStop controller = spy((OnStop) conditionController);
+ doReturn(controller).when(pool).getController(mContext, ContextualCard.CardType.CONDITIONAL);
+
+ manager.onWindowFocusChanged(false /* hasWindowFocus */);
+
+ verify(controller, atLeast(1)).onStop();
+ }
+
+ @Test
public void getCardsWithViewType_noSuggestionCards_shouldNotHaveHalfCards() {
final List<Integer> categories = Arrays.asList(
ContextualCardProto.ContextualCard.Category.IMPORTANT_VALUE,