OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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.suggestions; | 5 package org.chromium.chrome.browser.suggestions; |
6 | 6 |
7 import android.annotation.SuppressLint; | 7 import android.annotation.SuppressLint; |
8 import android.support.v7.widget.RecyclerView; | 8 import android.support.v7.widget.RecyclerView; |
9 import android.support.v7.widget.RecyclerView.OnScrollListener; | 9 import android.support.v7.widget.RecyclerView.OnScrollListener; |
10 import android.view.LayoutInflater; | 10 import android.view.LayoutInflater; |
(...skipping 15 matching lines...) Expand all Loading... |
26 import org.chromium.chrome.browser.ntp.snippets.SuggestionsSource; | 26 import org.chromium.chrome.browser.ntp.snippets.SuggestionsSource; |
27 import org.chromium.chrome.browser.offlinepages.OfflinePageBridge; | 27 import org.chromium.chrome.browser.offlinepages.OfflinePageBridge; |
28 import org.chromium.chrome.browser.omnibox.LocationBar; | 28 import org.chromium.chrome.browser.omnibox.LocationBar; |
29 import org.chromium.chrome.browser.profiles.Profile; | 29 import org.chromium.chrome.browser.profiles.Profile; |
30 import org.chromium.chrome.browser.snackbar.SnackbarManager; | 30 import org.chromium.chrome.browser.snackbar.SnackbarManager; |
31 import org.chromium.chrome.browser.tabmodel.TabModelSelector; | 31 import org.chromium.chrome.browser.tabmodel.TabModelSelector; |
32 import org.chromium.chrome.browser.widget.FadingShadow; | 32 import org.chromium.chrome.browser.widget.FadingShadow; |
33 import org.chromium.chrome.browser.widget.FadingShadowView; | 33 import org.chromium.chrome.browser.widget.FadingShadowView; |
34 import org.chromium.chrome.browser.widget.bottomsheet.BottomSheet; | 34 import org.chromium.chrome.browser.widget.bottomsheet.BottomSheet; |
35 import org.chromium.chrome.browser.widget.bottomsheet.BottomSheetContentControll
er; | 35 import org.chromium.chrome.browser.widget.bottomsheet.BottomSheetContentControll
er; |
36 import org.chromium.chrome.browser.widget.bottomsheet.BottomSheetObserver; | |
37 import org.chromium.chrome.browser.widget.bottomsheet.EmptyBottomSheetObserver; | |
38 import org.chromium.chrome.browser.widget.displaystyle.UiConfig; | 36 import org.chromium.chrome.browser.widget.displaystyle.UiConfig; |
39 import org.chromium.ui.widget.Toast; | 37 import org.chromium.ui.widget.Toast; |
40 | 38 |
41 import java.util.List; | 39 import java.util.List; |
42 import java.util.Locale; | 40 import java.util.Locale; |
43 | 41 |
44 /** | 42 /** |
45 * Provides content to be displayed inside of the Home tab of bottom sheet. | 43 * Provides content to be displayed inside of the Home tab of bottom sheet. |
46 */ | 44 */ |
47 public class SuggestionsBottomSheetContent implements BottomSheet.BottomSheetCon
tent { | 45 public class SuggestionsBottomSheetContent implements BottomSheet.BottomSheetCon
tent { |
48 private static SuggestionsSource sSuggestionsSourceForTesting; | 46 private static SuggestionsSource sSuggestionsSourceForTesting; |
49 private static SuggestionsEventReporter sEventReporterForTesting; | 47 private static SuggestionsEventReporter sEventReporterForTesting; |
50 | 48 |
51 private final View mView; | 49 private final View mView; |
52 private final FadingShadowView mShadowView; | 50 private final FadingShadowView mShadowView; |
53 private final SuggestionsRecyclerView mRecyclerView; | 51 private final SuggestionsRecyclerView mRecyclerView; |
54 private final ContextMenuManager mContextMenuManager; | 52 private final ContextMenuManager mContextMenuManager; |
55 private final SuggestionsUiDelegateImpl mSuggestionsUiDelegate; | 53 private final SuggestionsUiDelegateImpl mSuggestionsUiDelegate; |
56 private final TileGroup.Delegate mTileGroupDelegate; | 54 private final TileGroup.Delegate mTileGroupDelegate; |
57 private final BottomSheet mBottomSheet; | 55 private final SuggestionsSheetVisibilityChangeObserver mBottomSheetObserver; |
58 private final BottomSheetObserver mBottomSheetObserver; | |
59 | 56 |
60 public SuggestionsBottomSheetContent(final ChromeActivity activity, final Bo
ttomSheet sheet, | 57 public SuggestionsBottomSheetContent(final ChromeActivity activity, final Bo
ttomSheet sheet, |
61 TabModelSelector tabModelSelector, SnackbarManager snackbarManager)
{ | 58 TabModelSelector tabModelSelector, SnackbarManager snackbarManager)
{ |
62 Profile profile = Profile.getLastUsedProfile(); | 59 Profile profile = Profile.getLastUsedProfile(); |
63 SuggestionsNavigationDelegate navigationDelegate = | 60 SuggestionsNavigationDelegate navigationDelegate = |
64 new SuggestionsNavigationDelegateImpl(activity, profile, sheet,
tabModelSelector); | 61 new SuggestionsNavigationDelegateImpl(activity, profile, sheet,
tabModelSelector); |
65 mTileGroupDelegate = new TileGroupDelegateImpl( | 62 mTileGroupDelegate = new TileGroupDelegateImpl( |
66 activity, profile, tabModelSelector, navigationDelegate, snackba
rManager); | 63 activity, profile, tabModelSelector, navigationDelegate, snackba
rManager); |
67 mSuggestionsUiDelegate = createSuggestionsDelegate( | 64 mSuggestionsUiDelegate = createSuggestionsDelegate( |
68 profile, navigationDelegate, sheet, activity.getReferencePool())
; | 65 profile, navigationDelegate, sheet, activity.getReferencePool())
; |
(...skipping 18 matching lines...) Expand all Loading... |
87 } | 84 } |
88 }); | 85 }); |
89 | 86 |
90 UiConfig uiConfig = new UiConfig(mRecyclerView); | 87 UiConfig uiConfig = new UiConfig(mRecyclerView); |
91 | 88 |
92 final NewTabPageAdapter adapter = new NewTabPageAdapter(mSuggestionsUiDe
legate, | 89 final NewTabPageAdapter adapter = new NewTabPageAdapter(mSuggestionsUiDe
legate, |
93 /* aboveTheFoldView = */ null, uiConfig, OfflinePageBridge.getFo
rProfile(profile), | 90 /* aboveTheFoldView = */ null, uiConfig, OfflinePageBridge.getFo
rProfile(profile), |
94 mContextMenuManager, mTileGroupDelegate); | 91 mContextMenuManager, mTileGroupDelegate); |
95 mRecyclerView.init(uiConfig, mContextMenuManager, adapter); | 92 mRecyclerView.init(uiConfig, mContextMenuManager, adapter); |
96 | 93 |
97 mBottomSheetObserver = new EmptyBottomSheetObserver() { | 94 mBottomSheetObserver = new SuggestionsSheetVisibilityChangeObserver(this
, activity) { |
98 @Override | 95 @Override |
99 public void onSheetOpened() { | 96 public void onSheetOpened() { |
100 mRecyclerView.scrollToPosition(0); | 97 mRecyclerView.scrollToPosition(0); |
101 prepareSuggestionsForReveal(adapter); | 98 adapter.refreshSuggestions(); |
102 | 99 mSuggestionsUiDelegate.getEventReporter().onSurfaceOpened(); |
103 mRecyclerView.getScrollEventReporter().reset(); | 100 mRecyclerView.getScrollEventReporter().reset(); |
104 | 101 |
105 if (ChromeFeatureList.isEnabled( | 102 if (ChromeFeatureList.isEnabled( |
106 ChromeFeatureList.CONTEXTUAL_SUGGESTIONS_CAROUSEL))
{ | 103 ChromeFeatureList.CONTEXTUAL_SUGGESTIONS_CAROUSEL))
{ |
107 updateContextualSuggestions(mBottomSheet.getActiveTab().getU
rl()); | 104 updateContextualSuggestions(sheet.getActiveTab().getUrl()); |
108 } | 105 } |
| 106 |
| 107 super.onSheetOpened(); |
109 } | 108 } |
110 | 109 |
111 @Override | 110 @Override |
112 public void onSheetClosed() { | 111 public void onContentShown() { |
| 112 SuggestionsMetrics.recordSurfaceVisible(); |
| 113 } |
| 114 |
| 115 @Override |
| 116 public void onContentHidden() { |
113 SuggestionsMetrics.recordSurfaceHidden(); | 117 SuggestionsMetrics.recordSurfaceHidden(); |
114 } | 118 } |
115 | 119 |
| 120 @Override |
| 121 public void onContentStateChanged(@BottomSheet.SheetState int conten
tState) { |
| 122 if (contentState == BottomSheet.SHEET_STATE_HALF) { |
| 123 SuggestionsMetrics.recordSurfaceHalfVisible(); |
| 124 } else if (contentState == BottomSheet.SHEET_STATE_FULL) { |
| 125 SuggestionsMetrics.recordSurfaceFullyVisible(); |
| 126 } |
| 127 } |
116 }; | 128 }; |
117 mBottomSheet = activity.getBottomSheet(); | |
118 mBottomSheet.addObserver(mBottomSheetObserver); | |
119 | |
120 if (mBottomSheet.isSheetOpen()) prepareSuggestionsForReveal(adapter); | |
121 | 129 |
122 mShadowView = (FadingShadowView) mView.findViewById(R.id.shadow); | 130 mShadowView = (FadingShadowView) mView.findViewById(R.id.shadow); |
123 mShadowView.init( | 131 mShadowView.init( |
124 ApiCompatibilityUtils.getColor(mView.getResources(), R.color.too
lbar_shadow_color), | 132 ApiCompatibilityUtils.getColor(mView.getResources(), R.color.too
lbar_shadow_color), |
125 FadingShadow.POSITION_TOP); | 133 FadingShadow.POSITION_TOP); |
126 | 134 |
127 mRecyclerView.addOnScrollListener(new OnScrollListener() { | 135 mRecyclerView.addOnScrollListener(new OnScrollListener() { |
128 @Override | 136 @Override |
129 public void onScrolled(RecyclerView recyclerView, int dx, int dy) { | 137 public void onScrolled(RecyclerView recyclerView, int dx, int dy) { |
130 boolean shadowVisible = mRecyclerView.canScrollVertically(-1); | 138 boolean shadowVisible = mRecyclerView.canScrollVertically(-1); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 return false; | 170 return false; |
163 } | 171 } |
164 | 172 |
165 @Override | 173 @Override |
166 public int getVerticalScrollOffset() { | 174 public int getVerticalScrollOffset() { |
167 return mRecyclerView.computeVerticalScrollOffset(); | 175 return mRecyclerView.computeVerticalScrollOffset(); |
168 } | 176 } |
169 | 177 |
170 @Override | 178 @Override |
171 public void destroy() { | 179 public void destroy() { |
172 mBottomSheet.removeObserver(mBottomSheetObserver); | 180 mBottomSheetObserver.onDestroy(); |
173 mSuggestionsUiDelegate.onDestroy(); | 181 mSuggestionsUiDelegate.onDestroy(); |
174 mTileGroupDelegate.destroy(); | 182 mTileGroupDelegate.destroy(); |
175 } | 183 } |
176 | 184 |
177 @Override | 185 @Override |
178 public int getType() { | 186 public int getType() { |
179 return BottomSheetContentController.TYPE_SUGGESTIONS; | 187 return BottomSheetContentController.TYPE_SUGGESTIONS; |
180 } | 188 } |
181 | 189 |
182 /** Called when the UI is revlealed, prepares the list of suggestions. */ | |
183 private void prepareSuggestionsForReveal(NewTabPageAdapter adapter) { | |
184 adapter.refreshSuggestions(); | |
185 mSuggestionsUiDelegate.getEventReporter().onSurfaceOpened(); | |
186 SuggestionsMetrics.recordSurfaceVisible(); | |
187 } | |
188 | |
189 private void updateContextualSuggestions(String url) { | 190 private void updateContextualSuggestions(String url) { |
190 mSuggestionsUiDelegate.getSuggestionsSource().fetchContextualSuggestions
( | 191 mSuggestionsUiDelegate.getSuggestionsSource().fetchContextualSuggestions
( |
191 url, new Callback<List<SnippetArticle>>() { | 192 url, new Callback<List<SnippetArticle>>() { |
192 @Override | 193 @Override |
193 public void onResult(List<SnippetArticle> result) { | 194 public void onResult(List<SnippetArticle> result) { |
194 String text = String.format( | 195 String text = String.format( |
195 Locale.US, "Received %d contextual suggestions",
result.size()); | 196 Locale.US, "Received %d contextual suggestions",
result.size()); |
196 Toast.makeText(mRecyclerView.getContext(), text, Toast.L
ENGTH_SHORT).show(); | 197 Toast.makeText(mRecyclerView.getContext(), text, Toast.L
ENGTH_SHORT).show(); |
197 } | 198 } |
198 }); | 199 }); |
(...skipping 27 matching lines...) Expand all Loading... |
226 eventReporter = sEventReporterForTesting; | 227 eventReporter = sEventReporterForTesting; |
227 } | 228 } |
228 | 229 |
229 SuggestionsUiDelegateImpl delegate = new SuggestionsUiDelegateImpl( | 230 SuggestionsUiDelegateImpl delegate = new SuggestionsUiDelegateImpl( |
230 suggestionsSource, eventReporter, navigationDelegate, profile, h
ost, referencePool); | 231 suggestionsSource, eventReporter, navigationDelegate, profile, h
ost, referencePool); |
231 if (snippetsBridge != null) delegate.addDestructionObserver(snippetsBrid
ge); | 232 if (snippetsBridge != null) delegate.addDestructionObserver(snippetsBrid
ge); |
232 | 233 |
233 return delegate; | 234 return delegate; |
234 } | 235 } |
235 } | 236 } |
OLD | NEW |