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..995cbe513a56566fa00d9c79f61f842dc9a7433d |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/ntp/UiConfig.java |
| @@ -0,0 +1,106 @@ |
| +// 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; |
|
Bernhard Bauer
2016/07/20 08:56:24
Hm... having UNDEFINED as a valid style is a bit i
dgn
2016/07/20 11:38:39
Done.
|
| + |
| + private final List<DisplayStyleObserver> mObservers = new ArrayList<>(); |
| + private final Context mContext; |
| + |
| + /** |
| + * @param referenceView the View we observe to deduce the configuration from. |
| + */ |
| + public UiConfig(View referenceView) { |
| + mContext = referenceView.getContext(); |
| + |
| + referenceView.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() { |
| + @Override |
| + public void onViewAttachedToWindow(View v) { |
| + updateDisplayStyle(); |
| + } |
| + |
| + @Override |
| + public void onViewDetachedFromWindow(View v) {} |
| + }); |
| + |
| + updateDisplayStyle(); |
| + } |
| + |
| + public void addObserver(DisplayStyleObserver observer) { |
| + mObservers.add(observer); |
| + } |
| + |
| + @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 (DEBUG) { |
| + Log.d(TAG, debugString); |
| + Toast.makeText(mContext, debugString, Toast.LENGTH_SHORT).show(); |
| + } |
| + |
| + if (newDisplayStyle == mCurrentDisplayStyle) return; |
| + |
| + mCurrentDisplayStyle = newDisplayStyle; |
| + for (DisplayStyleObserver listener : mObservers) { |
| + listener.onDisplayStyleChanged(newDisplayStyle); |
| + } |
| + } |
| +} |