Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.chrome.browser.ntp; | |
| 6 | |
| 7 import android.view.View; | |
| 8 | |
| 9 /** | |
| 10 * Gets notified of changes in the display style. | |
| 11 * @see UiConfig.DisplayStyle | |
| 12 * @see UiConfig#getDisplayStyle() | |
| 13 */ | |
| 14 public interface DisplayStyleObserver { | |
| 15 void onDisplayStyleChanged(@UiConfig.DisplayStyle int newDisplayStyle); | |
| 16 | |
| 17 /** | |
| 18 * Implementation of {@link DisplayStyleObserver} designed to play nicely wi th | |
| 19 * {@link android.support.v7.widget.RecyclerView}. It will not notify of cha nges when the | |
| 20 * associated view is not attached to the window. | |
| 21 */ | |
| 22 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.
| |
| 23 implements DisplayStyleObserver, View.OnAttachStateChangeListener { | |
| 24 private final View mView; | |
| 25 private final UiConfig mConfig; | |
| 26 private final DisplayStyleObserver mListener; | |
| 27 | |
| 28 private boolean mIsViewAttached; | |
| 29 | |
| 30 /** | |
| 31 * @param view the view whose lifecycle is tracked to determine when to not fire the | |
| 32 * observer. | |
| 33 * @param config the {@link UiConfig} object to subscribe to. | |
| 34 * @param listener the listener to adapt. It's {#onDisplayStyleChanged} will be called when | |
| 35 * the configuration changes, provided that {@code view} is attached to the | |
| 36 * window. | |
| 37 */ | |
| 38 public ViewAdapter(View view, UiConfig config, DisplayStyleObserver list ener) { | |
| 39 mView = view; | |
| 40 mConfig = config; | |
| 41 mListener = listener; | |
| 42 | |
| 43 // TODO(dgn): getParent() is not a good way to test that, but isAtta chedToWindow() | |
| 44 // requires API 19. | |
| 45 mIsViewAttached = view.getParent() != null; | |
| 46 | |
| 47 mView.addOnAttachStateChangeListener(this); | |
| 48 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.
| |
| 49 | |
| 50 refreshDisplayStyle(); | |
| 51 } | |
| 52 | |
| 53 @Override | |
| 54 public void onDisplayStyleChanged(@UiConfig.DisplayStyle int newDisplayS tyle) { | |
| 55 if (!mIsViewAttached) return; | |
| 56 mListener.onDisplayStyleChanged(newDisplayStyle); | |
| 57 } | |
| 58 | |
| 59 @Override | |
| 60 public void onViewAttachedToWindow(View v) { | |
| 61 mIsViewAttached = true; | |
| 62 refreshDisplayStyle(); | |
| 63 } | |
| 64 | |
| 65 @Override | |
| 66 public void onViewDetachedFromWindow(View v) { | |
| 67 mIsViewAttached = false; | |
| 68 } | |
| 69 | |
| 70 private void refreshDisplayStyle() { | |
| 71 onDisplayStyleChanged(mConfig.getDisplayStyle()); | |
| 72 } | |
| 73 } | |
| 74 } | |
| OLD | NEW |