Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/ntp/UiConfig.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ntp/UiConfig.java b/chrome/android/java/src/org/chromium/chrome/browser/ntp/UiConfig.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1f2916c71c1add24918db29a7947ef66378a373b |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/ntp/UiConfig.java |
| @@ -0,0 +1,130 @@ |
| +// 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.content.Context; |
| +import android.support.annotation.IntDef; |
| +import android.view.View; |
| + |
| +import org.chromium.base.Log; |
| +import org.chromium.ui.widget.Toast; |
| + |
| +import java.lang.annotation.Retention; |
| +import java.lang.annotation.RetentionPolicy; |
| +import java.util.ArrayList; |
| +import java.util.List; |
| + |
| +/** |
| + * Exposes general configuration info about the NTP UI. |
| + */ |
| +public class UiConfig { |
| + /** The different supported UI setups. Listeners can register to be notified of changes.*/ |
| + @Retention(RetentionPolicy.SOURCE) |
| + @IntDef({DISPLAY_STYLE_UNDEFINED, DISPLAY_STYLE_NARROW, DISPLAY_STYLE_REGULAR, |
| + DISPLAY_STYLE_WIDE}) |
| + public @interface DisplayStyle {} |
| + public static final int DISPLAY_STYLE_UNDEFINED = -1; |
| + public static final int DISPLAY_STYLE_NARROW = 0; |
| + public static final int DISPLAY_STYLE_REGULAR = 1; |
| + public static final int DISPLAY_STYLE_WIDE = 2; |
| + |
| + private static final int REGULAR_CARD_MIN_WIDTH_DP = 360; |
| + private static final int WIDE_CARD_MIN_WIDTH_DP = 600; |
| + |
| + private static final String TAG = "Ntp"; |
| + private static final boolean DEBUG = false; |
| + |
| + @DisplayStyle |
| + private int mCurrentDisplayStyle = DISPLAY_STYLE_UNDEFINED; |
| + |
| + private final List<DisplayStyleObserver> mListeners = new ArrayList<>(); |
| + private final Context mContext; |
| + private final ViewObserver mViewObserver = new ViewObserver(); |
| + |
| + /** |
| + * Observes the relevant changes to the reference view, to let UiConfig know when it should |
| + * update the display style and notify the listeners. |
| + */ |
| + private class ViewObserver |
| + implements View.OnLayoutChangeListener, View.OnAttachStateChangeListener { |
| + @Override |
| + public void onViewAttachedToWindow(View v) { |
| + updateDisplayStyle(); |
| + } |
| + |
| + @Override |
| + public void onViewDetachedFromWindow(View v) {} |
| + |
| + @Override |
| + public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, |
| + int oldTop, int oldRight, int oldBottom) { |
| + int oldWidth = oldRight - oldLeft; |
| + int newWidth = right - left; |
| + |
| + if (oldWidth == newWidth) return; |
| + Log.d(TAG, "The viewport size changed: %d -> %d", oldWidth, newWidth); |
| + updateDisplayStyle(); |
| + } |
| + } |
| + |
| + /** |
| + * @param referenceView the View we observe to deduce the configuration from. |
| + */ |
| + public UiConfig(View referenceView) { |
| + mContext = referenceView.getContext(); |
| + |
| + referenceView.addOnLayoutChangeListener(mViewObserver); |
| + referenceView.addOnAttachStateChangeListener(mViewObserver); |
| + |
| + updateDisplayStyle(); |
| + } |
| + |
| + public void addListener(DisplayStyleObserver listener) { |
|
Bernhard Bauer
2016/07/19 14:46:53
Nit: It's an observer.
dgn
2016/07/19 23:33:56
Done.
|
| + mListeners.add(listener); |
| + } |
| + |
| + public void removeListener(DisplayStyleObserver listener) { |
| + mListeners.remove(listener); |
| + } |
| + |
| + @DisplayStyle |
| + public int getDisplayStyle() { |
| + return mCurrentDisplayStyle; |
| + } |
| + |
| + /** |
| + * Refresh the display style, notify listeners of changes. |
| + */ |
| + public void updateDisplayStyle() { |
| + int widthDp = mContext.getResources().getConfiguration().screenWidthDp; |
| + |
| + String debugString; |
| + |
| + @DisplayStyle |
| + int newDisplayStyle; |
| + if (widthDp < REGULAR_CARD_MIN_WIDTH_DP) { |
| + newDisplayStyle = DISPLAY_STYLE_NARROW; |
| + if (DEBUG) debugString = String.format("DISPLAY_STYLE_NARROW (w=%ddp)", widthDp); |
| + } else if (widthDp >= WIDE_CARD_MIN_WIDTH_DP) { |
| + newDisplayStyle = DISPLAY_STYLE_WIDE; |
| + if (DEBUG) debugString = String.format("DISPLAY_STYLE_WIDE (w=%ddp)", widthDp); |
| + } else { |
| + newDisplayStyle = DISPLAY_STYLE_REGULAR; |
| + if (DEBUG) debugString = String.format("DISPLAY_STYLE_REGULAR (w=%ddp)", widthDp); |
| + } |
| + |
| + if (newDisplayStyle == mCurrentDisplayStyle) return; |
| + |
| + if (DEBUG) { |
| + Log.d(TAG, debugString); |
| + Toast.makeText(mContext, debugString, Toast.LENGTH_SHORT).show(); |
| + } |
| + |
| + mCurrentDisplayStyle = newDisplayStyle; |
| + for (DisplayStyleObserver listener : mListeners) { |
| + listener.onDisplayStyleChanged(newDisplayStyle); |
| + } |
| + } |
| +} |