Index: chrome/android/java/src/org/chromium/chrome/browser/ntp/DisplayStyleObserver.java |
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ntp/DisplayStyleObserver.java b/chrome/android/java/src/org/chromium/chrome/browser/ntp/DisplayStyleObserver.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c1619dff0c40b3e455c7cc0f297f1d4d0f7f35c9 |
--- /dev/null |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/ntp/DisplayStyleObserver.java |
@@ -0,0 +1,74 @@ |
+// 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; |
+ |
+import android.view.View; |
+ |
+/** |
+ * Gets notified of changes in the display style. |
+ * @see UiConfig.DisplayStyle |
+ * @see UiConfig#getDisplayStyle() |
+ */ |
+public interface DisplayStyleObserver { |
+ void onDisplayStyleChanged(@UiConfig.DisplayStyle int newDisplayStyle); |
+ |
+ /** |
+ * 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 static class ViewAdapter |
Bernhard Bauer
2016/07/19 14:46:52
Pull this out into a separate class?
dgn
2016/07/19 23:33:56
Done.
|
+ implements DisplayStyleObserver, View.OnAttachStateChangeListener { |
+ private final View mView; |
+ 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 ViewAdapter(View view, UiConfig config, DisplayStyleObserver listener) { |
+ mView = view; |
+ mConfig = config; |
+ mListener = listener; |
+ |
+ // TODO(dgn): getParent() is not a good way to test that, but isAttachedToWindow() |
+ // requires API 19. |
+ mIsViewAttached = view.getParent() != null; |
+ |
+ mView.addOnAttachStateChangeListener(this); |
+ mConfig.addListener(this); |
Bernhard Bauer
2016/07/19 14:46:52
You could just have the caller attached the return
dgn
2016/07/19 23:33:56
I use it in refreshDisplayStyle() to pull the curr
Bernhard Bauer
2016/07/20 08:56:24
Hm... you could have UiConfig.addObserver() immedi
dgn
2016/07/20 11:38:39
Done.
|
+ |
+ 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()); |
+ } |
+ } |
+} |