Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(105)

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/SignInPromo.java

Issue 2396523002: Unify NewTabPageItem and ItemGroup into a single tree-structured interface. (Closed)
Patch Set: review Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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.content.Context; 7 import android.content.Context;
8 import android.support.annotation.DrawableRes; 8 import android.support.annotation.DrawableRes;
9 import android.support.annotation.StringRes; 9 import android.support.annotation.StringRes;
10 import android.support.v7.widget.RecyclerView; 10 import android.support.v7.widget.RecyclerView;
11 11
12 import org.chromium.base.ContextUtils; 12 import org.chromium.base.ContextUtils;
13 import org.chromium.base.metrics.RecordUserAction; 13 import org.chromium.base.metrics.RecordUserAction;
14 import org.chromium.chrome.R; 14 import org.chromium.chrome.R;
15 import org.chromium.chrome.browser.ntp.UiConfig; 15 import org.chromium.chrome.browser.ntp.UiConfig;
16 import org.chromium.chrome.browser.ntp.snippets.SnippetArticle;
16 import org.chromium.chrome.browser.preferences.ChromePreferenceManager; 17 import org.chromium.chrome.browser.preferences.ChromePreferenceManager;
17 import org.chromium.chrome.browser.signin.AccountSigninActivity; 18 import org.chromium.chrome.browser.signin.AccountSigninActivity;
18 import org.chromium.chrome.browser.signin.SigninAccessPoint; 19 import org.chromium.chrome.browser.signin.SigninAccessPoint;
19 import org.chromium.chrome.browser.signin.SigninManager; 20 import org.chromium.chrome.browser.signin.SigninManager;
20 21
21 import java.util.Collections;
22 import java.util.List;
23
24 /** 22 /**
25 * Shows a card prompting the user to sign in. This item is also an {@link ItemG roup}, and calling 23 * Shows a card prompting the user to sign in. This item is also a {@link TreeNo de}, and calling
26 * {@link #hide()} or {@link #maybeShow()} will control its visibility. 24 * {@link #hide()} or {@link #maybeShow()} will control its visibility.
27 */ 25 */
28 public class SignInPromo implements ItemGroup, StatusCardViewHolder.DataSource { 26 public class SignInPromo extends ChildNode implements StatusCardViewHolder.DataS ource {
29 private final NewTabPageItem mItem = new Item();
30 private final List<NewTabPageItem> mItems = Collections.<NewTabPageItem>sing letonList(mItem);
31 private Observer mChangeObserver;
32
33 /** 27 /**
34 * Whether the promo should be visible, according to the parent object. 28 * Whether the promo should be visible, according to the parent object.
35 * 29 *
36 * The {@link NewTabPageAdapter} calls to {@link #maybeShow()} and {@link #h ide()} modify this 30 * The {@link NewTabPageAdapter} calls to {@link #maybeShow()} and {@link #h ide()} modify this
37 * when the sign in status changes. 31 * when the sign in status changes.
38 */ 32 */
39 private boolean mVisible; 33 private boolean mVisible;
40 34
41 /** 35 /**
42 * Whether the user has seen the promo and dismissed it at some point. When this is set, 36 * Whether the user has seen the promo and dismissed it at some point. When this is set,
43 * the promo will never be shown. 37 * the promo will never be shown.
44 */ 38 */
45 private boolean mDismissed; 39 private boolean mDismissed;
46 40
47 public SignInPromo() { 41 public SignInPromo(NodeParent parent) {
42 super(parent);
48 mDismissed = ChromePreferenceManager.getInstance(ContextUtils.getApplica tionContext()) 43 mDismissed = ChromePreferenceManager.getInstance(ContextUtils.getApplica tionContext())
49 .getNewTabPageSigninPromoDismissed(); 44 .getNewTabPageSigninPromoDismissed();
50 SigninManager signinManager = SigninManager.get(ContextUtils.getApplicat ionContext()); 45 SigninManager signinManager = SigninManager.get(ContextUtils.getApplicat ionContext());
51 mVisible = signinManager.isSignInAllowed() && !signinManager.isSignedInO nNative(); 46 mVisible = signinManager.isSignInAllowed() && !signinManager.isSignedInO nNative();
52 } 47 }
53 48
54 @Override 49 @Override
55 public List<NewTabPageItem> getItems() { 50 public int getItemCount() {
56 return isShown() ? mItems : Collections.<NewTabPageItem>emptyList(); 51 if (!isShown()) return 0;
57 }
58 52
59 private class Item implements NewTabPageItem { 53 return 1;
60 @Override
61 public int getType() {
62 return NewTabPageItem.VIEW_TYPE_PROMO;
63 }
64
65 @Override
66 public void onBindViewHolder(NewTabPageViewHolder holder) {
67 assert holder instanceof ViewHolder;
68 ((ViewHolder) holder).onBindViewHolder(SignInPromo.this);
69 }
70 } 54 }
71 55
72 @Override 56 @Override
73 public void performAction(Context context) { 57 @ItemViewType
74 AccountSigninActivity.startIfAllowed(context, SigninAccessPoint.NTP_CONT ENT_SUGGESTIONS); 58 public int getItemViewType(int position) {
75 } 59 checkIndex(position);
76 60 return ItemViewType.PROMO;
77 /** Sets the {@link Observer} that will be notified when the visibility of t he item changes. */
78 public void setObserver(Observer changeObserver) {
79 assert mChangeObserver == null;
80 this.mChangeObserver = changeObserver;
81 } 61 }
82 62
83 @Override 63 @Override
64 public void onBindViewHolder(NewTabPageViewHolder holder, int position) {
65 checkIndex(position);
66 assert holder instanceof StatusCardViewHolder;
67 ((StatusCardViewHolder) holder).onBindViewHolder(this);
68 }
69
70 @Override
71 public SnippetArticle getSuggestionAt(int position) {
72 checkIndex(position);
73 return null;
74 }
75
76 @Override
84 @StringRes 77 @StringRes
85 public int getHeader() { 78 public int getHeader() {
86 return R.string.snippets_disabled_generic_prompt; 79 return R.string.snippets_disabled_generic_prompt;
87 } 80 }
88 81
89 @Override 82 @Override
90 @StringRes 83 @StringRes
91 public int getDescription() { 84 public int getDescription() {
92 return R.string.snippets_disabled_signed_out_instructions; 85 return R.string.snippets_disabled_signed_out_instructions;
93 } 86 }
94 87
95 @Override 88 @Override
96 @StringRes 89 @StringRes
97 public int getActionLabel() { 90 public int getActionLabel() {
98 return R.string.sign_in_button; 91 return R.string.sign_in_button;
99 } 92 }
100 93
94 @Override
95 public void performAction(Context context) {
96 AccountSigninActivity.startIfAllowed(context, SigninAccessPoint.NTP_CONT ENT_SUGGESTIONS);
97 }
98
101 public boolean isShown() { 99 public boolean isShown() {
102 return !mDismissed && mVisible; 100 return !mDismissed && mVisible;
103 } 101 }
104 102
105 /** Attempts to show the sign in promo. If the user dismissed it before, it will not be shown.*/ 103 /** Attempts to show the sign in promo. If the user dismissed it before, it will not be shown.*/
106 public void maybeShow() { 104 public void maybeShow() {
107 if (mVisible) return; 105 if (mVisible) return;
108 mVisible = true; 106 mVisible = true;
109 107
110 if (mDismissed) return; 108 if (mDismissed) return;
111 109
112 RecordUserAction.record("Signin_Impression_FromNTPContentSuggestions"); 110 RecordUserAction.record("Signin_Impression_FromNTPContentSuggestions");
113 mChangeObserver.onItemRangeInserted(this, 0, 1); 111 notifyItemInserted(0);
114 } 112 }
115 113
116 /** Hides the sign in promo. */ 114 /** Hides the sign in promo. */
117 public void hide() { 115 public void hide() {
118 if (!mVisible) return; 116 if (!mVisible) return;
119 mVisible = false; 117 mVisible = false;
120 118
121 if (mDismissed) return; 119 if (mDismissed) return;
122 120
123 mChangeObserver.onItemRangeRemoved(this, 0, 1); 121 notifyItemRemoved(0);
124 } 122 }
125 123
126 /** Hides the sign in promo and sets a preference to make sure it is not sho wn again. */ 124 /** Hides the sign in promo and sets a preference to make sure it is not sho wn again. */
127 public void dismiss() { 125 public void dismiss() {
128 hide(); 126 hide();
129 mDismissed = true; 127 mDismissed = true;
130 ChromePreferenceManager.getInstance(ContextUtils.getApplicationContext() ) 128 ChromePreferenceManager.getInstance(ContextUtils.getApplicationContext() )
131 .setNewTabPageSigninPromoDismissed(true); 129 .setNewTabPageSigninPromoDismissed(true);
132 } 130 }
133 131
(...skipping 16 matching lines...) Expand all
150 if (hasCardAbove) return R.drawable.ntp_signin_promo_card_bottom; 148 if (hasCardAbove) return R.drawable.ntp_signin_promo_card_bottom;
151 return R.drawable.ntp_signin_promo_card_single; 149 return R.drawable.ntp_signin_promo_card_single;
152 } 150 }
153 151
154 @Override 152 @Override
155 public void updateLayoutParams() { 153 public void updateLayoutParams() {
156 super.updateLayoutParams(); 154 super.updateLayoutParams();
157 155
158 if (getAdapterPosition() == RecyclerView.NO_POSITION) return; 156 if (getAdapterPosition() == RecyclerView.NO_POSITION) return;
159 157
160 @NewTabPageItem.ViewType 158 @ItemViewType
161 int precedingCardType = 159 int precedingCardType =
162 getRecyclerView().getAdapter().getItemViewType(getAdapterPos ition() - 1); 160 getRecyclerView().getAdapter().getItemViewType(getAdapterPos ition() - 1);
163 161
164 // The sign in promo should stick to the articles of the preceding s ection, but have 162 // The sign in promo should stick to the articles of the preceding s ection, but have
165 // some space otherwise. 163 // some space otherwise.
166 if (precedingCardType != NewTabPageItem.VIEW_TYPE_SNIPPET) { 164 if (precedingCardType != ItemViewType.SNIPPET) {
167 getParams().topMargin = mSeparationSpaceSize; 165 getParams().topMargin = mSeparationSpaceSize;
168 } else { 166 } else {
169 getParams().topMargin = 0; 167 getParams().topMargin = 0;
170 } 168 }
171 } 169 }
172 } 170 }
171
172 private void checkIndex(int position) {
173 if (position < 0 || position >= getItemCount()) {
174 throw new IndexOutOfBoundsException(position + "/" + getItemCount()) ;
175 }
176 }
173 } 177 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698