| Index: chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/DisplayStyleObserverAdapter.java
|
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/DisplayStyleObserverAdapter.java b/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/DisplayStyleObserverAdapter.java
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..4fb6e42549b34a7620e471c6c548b39a4263257e
|
| --- /dev/null
|
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/DisplayStyleObserverAdapter.java
|
| @@ -0,0 +1,66 @@
|
| +// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +package org.chromium.chrome.browser.ntp.cards;
|
| +
|
| +import android.view.View;
|
| +
|
| +import org.chromium.chrome.browser.ntp.DisplayStyleObserver;
|
| +import org.chromium.chrome.browser.ntp.UiConfig;
|
| +
|
| +/**
|
| + * Implementation of {@link DisplayStyleObserver} designed to play nicely with
|
| + * {@link android.support.v7.widget.RecyclerView}. It will not notify of changes when the
|
| + * associated view is not attached to the window.
|
| + */
|
| +public class DisplayStyleObserverAdapter
|
| + implements DisplayStyleObserver, View.OnAttachStateChangeListener {
|
| + private final UiConfig mConfig;
|
| + private final DisplayStyleObserver mListener;
|
| +
|
| + private boolean mIsViewAttached;
|
| +
|
| + /**
|
| + * @param view the view whose lifecycle is tracked to determine when to not fire the
|
| + * observer.
|
| + * @param config the {@link UiConfig} object to subscribe to.
|
| + * @param listener the listener to adapt. It's {#onDisplayStyleChanged} will be called when
|
| + * the configuration changes, provided that {@code view} is attached to the
|
| + * window.
|
| + */
|
| + public DisplayStyleObserverAdapter(View view, UiConfig config, DisplayStyleObserver listener) {
|
| + mConfig = config;
|
| + mListener = listener;
|
| +
|
| + // TODO(dgn): getParent() is not a good way to test that, but isAttachedToWindow()
|
| + // requires API 19.
|
| + mIsViewAttached = view.getParent() != null;
|
| +
|
| + view.addOnAttachStateChangeListener(this);
|
| + mConfig.addObserver(this);
|
| +
|
| + refreshDisplayStyle();
|
| + }
|
| +
|
| + @Override
|
| + public void onDisplayStyleChanged(@UiConfig.DisplayStyle int newDisplayStyle) {
|
| + if (!mIsViewAttached) return;
|
| + mListener.onDisplayStyleChanged(newDisplayStyle);
|
| + }
|
| +
|
| + @Override
|
| + public void onViewAttachedToWindow(View v) {
|
| + mIsViewAttached = true;
|
| + refreshDisplayStyle();
|
| + }
|
| +
|
| + @Override
|
| + public void onViewDetachedFromWindow(View v) {
|
| + mIsViewAttached = false;
|
| + }
|
| +
|
| + private void refreshDisplayStyle() {
|
| + onDisplayStyleChanged(mConfig.getDisplayStyle());
|
| + }
|
| +}
|
|
|