From bd108862880028f95f512a3e4c1994d5e661fc86 Mon Sep 17 00:00:00 2001 From: Mill Chen Date: Tue, 19 Mar 2019 18:56:11 -0700 Subject: [PATCH] Make conditional work properly when toggle it in QS Some conditionals do not work properly when users toggle its state in QS, the cause is that these conditionals don't have a way to monitor the status changed. Make RecyclerView handle onWindowFocusChanged event and make sure that conditionals can update the status when going back to Settings from QS. Bug: 118387886 Bug: 123171638 Bug: 123167705 Test: visual, robotests Change-Id: Ib3bb9bf43afaa58726502eea1e98bcf602bc3677 --- res/layout/settings_homepage.xml | 2 +- .../ContextualCardManager.java | 18 +++++++++- .../ContextualCardsFragment.java | 12 +++++-- .../contextualcards/FocusRecyclerView.java | 36 +++++++++++++++++++ 4 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 src/com/android/settings/homepage/contextualcards/FocusRecyclerView.java diff --git a/res/layout/settings_homepage.xml b/res/layout/settings_homepage.xml index e04b372aa38..6e2b302541f 100644 --- a/res/layout/settings_homepage.xml +++ b/res/layout/settings_homepage.xml @@ -21,7 +21,7 @@ android:layout_height="wrap_content" android:orientation="vertical"> - (); mControllerRendererPool = new ControllerRendererPool(); mLifecycle.addObserver(this); - if (savedInstanceState == null) { mIsFirstLaunch = true; mSavedCards = null; @@ -240,6 +241,21 @@ public class ContextualCardManager implements ContextualCardLoader.CardContentLo outState.putStringArrayList(KEY_CONTEXTUAL_CARDS, cards); } + public void onWindowFocusChanged(boolean hasWindowFocus) { + // Duplicate a list to avoid java.util.ConcurrentModificationException. + final List cards = new ArrayList<>(mContextualCards); + for (ContextualCard card : cards) { + final ContextualCardController controller = mControllerRendererPool + .getController(mContext, card.getCardType()); + if (hasWindowFocus && controller instanceof OnStart) { + ((OnStart) controller).onStart(); + } + if (!hasWindowFocus && controller instanceof OnStop) { + ((OnStop) controller).onStop(); + } + } + } + public ControllerRendererPool getControllerRendererPool() { return mControllerRendererPool; } diff --git a/src/com/android/settings/homepage/contextualcards/ContextualCardsFragment.java b/src/com/android/settings/homepage/contextualcards/ContextualCardsFragment.java index 72ddb50eafb..bd258660d4e 100644 --- a/src/com/android/settings/homepage/contextualcards/ContextualCardsFragment.java +++ b/src/com/android/settings/homepage/contextualcards/ContextualCardsFragment.java @@ -27,17 +27,17 @@ import android.view.ViewGroup; import androidx.loader.app.LoaderManager; import androidx.recyclerview.widget.GridLayoutManager; -import androidx.recyclerview.widget.RecyclerView; import com.android.settings.R; import com.android.settings.core.InstrumentedFragment; import com.android.settings.overlay.FeatureFactory; -public class ContextualCardsFragment extends InstrumentedFragment { +public class ContextualCardsFragment extends InstrumentedFragment implements + FocusRecyclerView.FocusListener { private static final String TAG = "ContextualCardsFragment"; - private RecyclerView mCardsContainer; + private FocusRecyclerView mCardsContainer; private GridLayoutManager mLayoutManager; private ContextualCardsAdapter mContextualCardsAdapter; private ContextualCardManager mContextualCardManager; @@ -72,10 +72,16 @@ public class ContextualCardsFragment extends InstrumentedFragment { this /* lifecycleOwner */, mContextualCardManager); mCardsContainer.setAdapter(mContextualCardsAdapter); mContextualCardManager.setListener(mContextualCardsAdapter); + mCardsContainer.setListener(this); return rootView; } + @Override + public void onWindowFocusChanged(boolean hasWindowFocus) { + mContextualCardManager.onWindowFocusChanged(hasWindowFocus); + } + @Override public int getMetricsCategory() { return SettingsEnums.SETTINGS_HOMEPAGE; diff --git a/src/com/android/settings/homepage/contextualcards/FocusRecyclerView.java b/src/com/android/settings/homepage/contextualcards/FocusRecyclerView.java new file mode 100644 index 00000000000..a2ec9afdf43 --- /dev/null +++ b/src/com/android/settings/homepage/contextualcards/FocusRecyclerView.java @@ -0,0 +1,36 @@ +package com.android.settings.homepage.contextualcards; + +import android.content.Context; +import android.util.AttributeSet; + +import androidx.annotation.Nullable; +import androidx.recyclerview.widget.RecyclerView; + +public class FocusRecyclerView extends RecyclerView { + + private FocusListener mListener; + + public FocusRecyclerView(Context context) { + super(context); + } + + public FocusRecyclerView(Context context, @Nullable AttributeSet attrs) { + super(context, attrs); + } + + @Override + public void onWindowFocusChanged(boolean hasWindowFocus) { + super.onWindowFocusChanged(hasWindowFocus); + if (mListener != null) { + mListener.onWindowFocusChanged(hasWindowFocus); + } + } + + public void setListener(FocusListener listener) { + mListener = listener; + } + + public interface FocusListener { + void onWindowFocusChanged(boolean hasWindowFocus); + } +}