OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.ntp.cards; | 5 package org.chromium.chrome.browser.ntp.cards; |
6 | 6 |
7 import android.support.annotation.CallSuper; | 7 import android.support.annotation.CallSuper; |
8 import android.support.annotation.DrawableRes; | 8 import android.support.annotation.DrawableRes; |
9 import android.support.annotation.Nullable; | 9 import android.support.annotation.Nullable; |
10 import android.support.v4.view.animation.FastOutSlowInInterpolator; | 10 import android.support.v4.view.animation.FastOutSlowInInterpolator; |
11 import android.support.v7.widget.RecyclerView; | 11 import android.support.v7.widget.RecyclerView; |
12 import android.view.ContextMenu; | 12 import android.view.ContextMenu; |
13 import android.view.ContextMenu.ContextMenuInfo; | 13 import android.view.ContextMenu.ContextMenuInfo; |
14 import android.view.LayoutInflater; | 14 import android.view.LayoutInflater; |
15 import android.view.View; | 15 import android.view.View; |
16 import android.view.View.OnAttachStateChangeListener; | 16 import android.view.View.OnAttachStateChangeListener; |
17 import android.view.View.OnClickListener; | 17 import android.view.View.OnClickListener; |
18 import android.view.ViewGroup; | 18 import android.view.ViewGroup; |
19 import android.view.animation.Interpolator; | 19 import android.view.animation.Interpolator; |
20 | 20 |
21 import org.chromium.chrome.R; | 21 import org.chromium.chrome.R; |
22 import org.chromium.chrome.browser.ntp.ContextMenuManager; | 22 import org.chromium.chrome.browser.ntp.ContextMenuManager; |
23 import org.chromium.chrome.browser.ntp.NewTabPageView.NewTabPageManager; | |
24 import org.chromium.chrome.browser.ntp.UiConfig; | 23 import org.chromium.chrome.browser.ntp.UiConfig; |
25 import org.chromium.chrome.browser.util.MathUtils; | 24 import org.chromium.chrome.browser.util.MathUtils; |
26 import org.chromium.chrome.browser.util.ViewUtils; | 25 import org.chromium.chrome.browser.util.ViewUtils; |
27 | 26 |
28 /** | 27 /** |
29 * Holder for a generic card. | 28 * Holder for a generic card. |
30 * | 29 * |
31 * Specific behaviors added to the cards: | 30 * Specific behaviors added to the cards: |
32 * | 31 * |
33 * - Cards can peek above the fold if there is enough space. | 32 * - Cards can peek above the fold if there is enough space. |
(...skipping 18 matching lines...) Expand all Loading... |
52 * Due to the card background being a 9 patch file - the card border shadow
will be part of | 51 * Due to the card background being a 9 patch file - the card border shadow
will be part of |
53 * the card width and height. This value will be used to adjust values to ac
count for the | 52 * the card width and height. This value will be used to adjust values to ac
count for the |
54 * borders. | 53 * borders. |
55 */ | 54 */ |
56 private final int mCards9PatchAdjustment; | 55 private final int mCards9PatchAdjustment; |
57 | 56 |
58 protected final NewTabPageRecyclerView mRecyclerView; | 57 protected final NewTabPageRecyclerView mRecyclerView; |
59 | 58 |
60 private final UiConfig mUiConfig; | 59 private final UiConfig mUiConfig; |
61 private final MarginResizer mMarginResizer; | 60 private final MarginResizer mMarginResizer; |
62 private final NewTabPageManager mNtpManager; | |
63 | 61 |
64 /** | 62 /** |
65 * To what extent the card is "peeking". 0 means the card is not peeking at
all and spans the | 63 * To what extent the card is "peeking". 0 means the card is not peeking at
all and spans the |
66 * full width of its parent. 1 means it is fully peeking and will be shown w
ith a margin. | 64 * full width of its parent. 1 means it is fully peeking and will be shown w
ith a margin. |
67 */ | 65 */ |
68 private float mPeekingPercentage; | 66 private float mPeekingPercentage; |
69 | 67 |
70 @DrawableRes | 68 @DrawableRes |
71 private int mBackground; | 69 private int mBackground; |
72 | 70 |
73 /** | 71 /** |
74 * @param layoutId resource id of the layout to inflate and to use as card. | 72 * @param layoutId resource id of the layout to inflate and to use as card. |
75 * @param recyclerView ViewGroup that will contain the newly created view. | 73 * @param recyclerView ViewGroup that will contain the newly created view. |
76 * @param uiConfig The NTP UI configuration object used to adjust the card U
I. | 74 * @param uiConfig The NTP UI configuration object used to adjust the card U
I. |
| 75 * @param contextMenuManager The manager responsible for the context menu. |
77 */ | 76 */ |
78 public CardViewHolder(int layoutId, final NewTabPageRecyclerView recyclerVie
w, | 77 public CardViewHolder(int layoutId, final NewTabPageRecyclerView recyclerVie
w, |
79 UiConfig uiConfig, NewTabPageManager ntpManager) { | 78 UiConfig uiConfig, final ContextMenuManager contextMenuManager) { |
80 super(inflateView(layoutId, recyclerView)); | 79 super(inflateView(layoutId, recyclerView)); |
81 | 80 |
82 mCards9PatchAdjustment = recyclerView.getResources().getDimensionPixelSi
ze( | 81 mCards9PatchAdjustment = recyclerView.getResources().getDimensionPixelSi
ze( |
83 R.dimen.snippets_card_9_patch_adjustment); | 82 R.dimen.snippets_card_9_patch_adjustment); |
84 | 83 |
85 mMaxPeekPadding = recyclerView.getResources().getDimensionPixelSize( | 84 mMaxPeekPadding = recyclerView.getResources().getDimensionPixelSize( |
86 R.dimen.snippets_padding); | 85 R.dimen.snippets_padding); |
87 | 86 |
88 mRecyclerView = recyclerView; | 87 mRecyclerView = recyclerView; |
89 | 88 |
90 itemView.setOnClickListener(new OnClickListener() { | 89 itemView.setOnClickListener(new OnClickListener() { |
91 @Override | 90 @Override |
92 public void onClick(View v) { | 91 public void onClick(View v) { |
93 if (isPeeking()) { | 92 if (isPeeking()) { |
94 recyclerView.scrollToFirstCard(); | 93 recyclerView.scrollToFirstCard(); |
95 } else { | 94 } else { |
96 onCardTapped(); | 95 onCardTapped(); |
97 } | 96 } |
98 } | 97 } |
99 }); | 98 }); |
100 | 99 |
101 itemView.setOnCreateContextMenuListener(new View.OnCreateContextMenuList
ener() { | 100 itemView.setOnCreateContextMenuListener(new View.OnCreateContextMenuList
ener() { |
102 @Override | 101 @Override |
103 public void onCreateContextMenu(ContextMenu menu, View view, Context
MenuInfo menuInfo) { | 102 public void onCreateContextMenu(ContextMenu menu, View view, Context
MenuInfo menuInfo) { |
104 if (isPeeking()) return; | 103 if (isPeeking()) return; |
105 | 104 |
106 ContextMenuManager.Delegate delegate = getContextMenuDelegate(); | 105 ContextMenuManager.Delegate delegate = getContextMenuDelegate(); |
107 if (delegate == null) return; | 106 if (delegate == null) return; |
108 | 107 |
109 mNtpManager.getContextMenuManager().createContextMenu(menu, item
View, delegate); | 108 contextMenuManager.createContextMenu(menu, itemView, delegate); |
110 } | 109 } |
111 }); | 110 }); |
112 | 111 |
113 mUiConfig = uiConfig; | 112 mUiConfig = uiConfig; |
114 | 113 |
115 mMarginResizer = MarginResizer.createWithViewAdapter(itemView, mUiConfig
); | 114 mMarginResizer = MarginResizer.createWithViewAdapter(itemView, mUiConfig
); |
116 | 115 |
117 // Configure the resizer to use negative margins on regular display to b
alance out the | 116 // Configure the resizer to use negative margins on regular display to b
alance out the |
118 // lateral shadow of the card 9-patch and avoid a rounded corner effect. | 117 // lateral shadow of the card 9-patch and avoid a rounded corner effect. |
119 mMarginResizer.setMargins(-mCards9PatchAdjustment); | 118 mMarginResizer.setMargins(-mCards9PatchAdjustment); |
120 | |
121 mNtpManager = ntpManager; | |
122 } | 119 } |
123 | 120 |
124 /** | 121 /** |
125 * Called when the NTP cards adapter is requested to update the currently vi
sible ViewHolder | 122 * Called when the NTP cards adapter is requested to update the currently vi
sible ViewHolder |
126 * with data. | 123 * with data. |
127 */ | 124 */ |
128 @CallSuper | 125 @CallSuper |
129 protected void onBindViewHolder() { | 126 protected void onBindViewHolder() { |
130 // Reset the peek status to avoid recycled view holders to be peeking at
the wrong moment. | 127 // Reset the peek status to avoid recycled view holders to be peeking at
the wrong moment. |
131 if (getAdapterPosition() != mRecyclerView.getNewTabPageAdapter().getFirs
tCardPosition()) { | 128 if (getAdapterPosition() != mRecyclerView.getNewTabPageAdapter().getFirs
tCardPosition()) { |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 if (hasCardAbove && hasCardBelow) return R.drawable.ntp_card_middle; | 288 if (hasCardAbove && hasCardBelow) return R.drawable.ntp_card_middle; |
292 if (!hasCardAbove && hasCardBelow) return R.drawable.ntp_card_top; | 289 if (!hasCardAbove && hasCardBelow) return R.drawable.ntp_card_top; |
293 if (hasCardAbove && !hasCardBelow) return R.drawable.ntp_card_bottom; | 290 if (hasCardAbove && !hasCardBelow) return R.drawable.ntp_card_bottom; |
294 return R.drawable.ntp_card_single; | 291 return R.drawable.ntp_card_single; |
295 } | 292 } |
296 | 293 |
297 protected NewTabPageRecyclerView getRecyclerView() { | 294 protected NewTabPageRecyclerView getRecyclerView() { |
298 return mRecyclerView; | 295 return mRecyclerView; |
299 } | 296 } |
300 } | 297 } |
OLD | NEW |