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.content.Context; | |
8 import android.support.annotation.IntDef; | |
9 import android.view.View; | |
10 | |
11 import org.chromium.base.Log; | |
12 import org.chromium.ui.widget.Toast; | |
13 | |
14 import java.lang.annotation.Retention; | |
15 import java.lang.annotation.RetentionPolicy; | |
16 import java.util.ArrayList; | |
17 import java.util.List; | |
18 | |
19 /** | |
20 * Exposes general configuration info about the NTP UI. | |
21 */ | |
22 public class UiConfig { | |
23 /** The different supported UI setups. Observers can register to be notified
of changes.*/ | |
24 @Retention(RetentionPolicy.SOURCE) | |
25 @IntDef({DISPLAY_STYLE_UNDEFINED, DISPLAY_STYLE_NARROW, DISPLAY_STYLE_REGULA
R, | |
26 DISPLAY_STYLE_WIDE}) | |
27 public @interface DisplayStyle {} | |
28 public static final int DISPLAY_STYLE_UNDEFINED = -1; | |
29 public static final int DISPLAY_STYLE_NARROW = 0; | |
30 public static final int DISPLAY_STYLE_REGULAR = 1; | |
31 public static final int DISPLAY_STYLE_WIDE = 2; | |
32 | |
33 private static final int REGULAR_CARD_MIN_WIDTH_DP = 360; | |
34 private static final int WIDE_CARD_MIN_WIDTH_DP = 600; | |
35 | |
36 private static final String TAG = "Ntp"; | |
37 private static final boolean DEBUG = false; | |
38 | |
39 @DisplayStyle | |
40 private int mCurrentDisplayStyle; | |
41 | |
42 private final List<DisplayStyleObserver> mObservers = new ArrayList<>(); | |
43 private final Context mContext; | |
44 | |
45 /** | |
46 * @param referenceView the View we observe to deduce the configuration from
. | |
47 */ | |
48 public UiConfig(View referenceView) { | |
49 mContext = referenceView.getContext(); | |
50 mCurrentDisplayStyle = computeDisplayStyleForCurrentConfig(); | |
51 | |
52 referenceView.addOnAttachStateChangeListener(new View.OnAttachStateChang
eListener() { | |
53 @Override | |
54 public void onViewAttachedToWindow(View v) { | |
55 updateDisplayStyle(); | |
56 } | |
57 | |
58 @Override | |
59 public void onViewDetachedFromWindow(View v) {} | |
60 }); | |
61 } | |
62 | |
63 /** | |
64 * Registers a {@link DisplayStyleObserver}. It will be notified right away
with the current | |
65 * display style. | |
66 */ | |
67 public void addObserver(DisplayStyleObserver observer) { | |
68 mObservers.add(observer); | |
69 observer.onDisplayStyleChanged(mCurrentDisplayStyle); | |
70 } | |
71 | |
72 /** | |
73 * Refresh the display style, notify observers of changes. | |
74 */ | |
75 public void updateDisplayStyle() { | |
76 @DisplayStyle | |
77 int newDisplayStyle = computeDisplayStyleForCurrentConfig(); | |
78 | |
79 if (newDisplayStyle == mCurrentDisplayStyle) return; | |
80 | |
81 mCurrentDisplayStyle = newDisplayStyle; | |
82 for (DisplayStyleObserver observer : mObservers) { | |
83 observer.onDisplayStyleChanged(newDisplayStyle); | |
84 } | |
85 } | |
86 | |
87 @DisplayStyle | |
88 private int computeDisplayStyleForCurrentConfig() { | |
89 int widthDp = mContext.getResources().getConfiguration().screenWidthDp; | |
90 | |
91 String debugString; | |
92 | |
93 @DisplayStyle | |
94 int newDisplayStyle; | |
95 if (widthDp < REGULAR_CARD_MIN_WIDTH_DP) { | |
96 newDisplayStyle = DISPLAY_STYLE_NARROW; | |
97 if (DEBUG) debugString = String.format("DISPLAY_STYLE_NARROW (w=%ddp
)", widthDp); | |
98 } else if (widthDp >= WIDE_CARD_MIN_WIDTH_DP) { | |
99 newDisplayStyle = DISPLAY_STYLE_WIDE; | |
100 if (DEBUG) debugString = String.format("DISPLAY_STYLE_WIDE (w=%ddp)"
, widthDp); | |
101 } else { | |
102 newDisplayStyle = DISPLAY_STYLE_REGULAR; | |
103 if (DEBUG) debugString = String.format("DISPLAY_STYLE_REGULAR (w=%dd
p)", widthDp); | |
104 } | |
105 | |
106 if (DEBUG) { | |
107 Log.d(TAG, debugString); | |
108 Toast.makeText(mContext, debugString, Toast.LENGTH_SHORT).show(); | |
109 } | |
110 | |
111 return newDisplayStyle; | |
112 } | |
113 } | |
OLD | NEW |