-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
NewTabPage.java
1158 lines (1011 loc) · 49.8 KB
/
NewTabPage.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2015 The Chromium Authors
// 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.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.ViewStub;
import android.widget.FrameLayout;
import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.core.view.ViewCompat;
import androidx.recyclerview.widget.RecyclerView;
import org.chromium.base.Log;
import org.chromium.base.ObserverList;
import org.chromium.base.TimeUtils;
import org.chromium.base.TraceEvent;
import org.chromium.base.metrics.RecordHistogram;
import org.chromium.base.metrics.RecordUserAction;
import org.chromium.base.supplier.Supplier;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.app.feed.FeedActionDelegateImpl;
import org.chromium.chrome.browser.bookmarks.BookmarkModel;
import org.chromium.chrome.browser.browser_controls.BrowserControlsStateProvider;
import org.chromium.chrome.browser.compositor.layouts.content.InvalidationAwareThumbnailProvider;
import org.chromium.chrome.browser.download.DownloadManagerService;
import org.chromium.chrome.browser.feature_engagement.TrackerFactory;
import org.chromium.chrome.browser.feature_guide.notifications.FeatureNotificationUtils;
import org.chromium.chrome.browser.feature_guide.notifications.FeatureType;
import org.chromium.chrome.browser.feed.FeedActionDelegate;
import org.chromium.chrome.browser.feed.FeedFeatures;
import org.chromium.chrome.browser.feed.FeedReliabilityLogger;
import org.chromium.chrome.browser.feed.FeedSurfaceCoordinator;
import org.chromium.chrome.browser.feed.FeedSurfaceDelegate;
import org.chromium.chrome.browser.feed.FeedSurfaceLifecycleManager;
import org.chromium.chrome.browser.feed.FeedSurfaceProvider;
import org.chromium.chrome.browser.feed.FeedSwipeRefreshLayout;
import org.chromium.chrome.browser.feed.NtpFeedSurfaceLifecycleManager;
import org.chromium.chrome.browser.feed.componentinterfaces.SurfaceCoordinator;
import org.chromium.chrome.browser.feedback.HelpAndFeedbackLauncherImpl;
import org.chromium.chrome.browser.flags.ChromeFeatureList;
import org.chromium.chrome.browser.lifecycle.ActivityLifecycleDispatcher;
import org.chromium.chrome.browser.lifecycle.LifecycleObserver;
import org.chromium.chrome.browser.lifecycle.PauseResumeWithNativeObserver;
import org.chromium.chrome.browser.native_page.ContextMenuManager;
import org.chromium.chrome.browser.omnibox.OmniboxFocusReason;
import org.chromium.chrome.browser.omnibox.OmniboxStub;
import org.chromium.chrome.browser.omnibox.suggestions.AutocompleteControllerProvider;
import org.chromium.chrome.browser.omnibox.voice.VoiceRecognitionHandler;
import org.chromium.chrome.browser.privacy.settings.PrivacyPreferencesManagerImpl;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.browser.profiles.ProfileKey;
import org.chromium.chrome.browser.query_tiles.QueryTileSection.QueryInfo;
import org.chromium.chrome.browser.search_engines.TemplateUrlServiceFactory;
import org.chromium.chrome.browser.search_resumption.SearchResumptionModuleCoordinator;
import org.chromium.chrome.browser.search_resumption.SearchResumptionModuleUtils;
import org.chromium.chrome.browser.share.ShareDelegate;
import org.chromium.chrome.browser.suggestions.SuggestionsMetrics;
import org.chromium.chrome.browser.suggestions.SuggestionsNavigationDelegate;
import org.chromium.chrome.browser.suggestions.SuggestionsUiDelegateImpl;
import org.chromium.chrome.browser.suggestions.tile.Tile;
import org.chromium.chrome.browser.suggestions.tile.TileGroup;
import org.chromium.chrome.browser.suggestions.tile.TileGroupDelegateImpl;
import org.chromium.chrome.browser.tab.EmptyTabObserver;
import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.browser.tab.TabHidingType;
import org.chromium.chrome.browser.tab.TabObserver;
import org.chromium.chrome.browser.tab.TabSelectionType;
import org.chromium.chrome.browser.tabmodel.TabModelSelector;
import org.chromium.chrome.browser.tabmodel.TabModelSelectorObserver;
import org.chromium.chrome.browser.tasks.HomeSurfaceTracker;
import org.chromium.chrome.browser.tasks.ReturnToChromeUtil;
import org.chromium.chrome.browser.toolbar.top.Toolbar;
import org.chromium.chrome.browser.ui.messages.snackbar.SnackbarManager;
import org.chromium.chrome.browser.ui.native_page.NativePage;
import org.chromium.chrome.browser.ui.native_page.NativePageHost;
import org.chromium.chrome.browser.util.BrowserUiUtils;
import org.chromium.chrome.browser.xsurface.feed.FeedLaunchReliabilityLogger.SurfaceType;
import org.chromium.chrome.features.start_surface.StartSurfaceConfiguration;
import org.chromium.chrome.features.tasks.SingleTabSwitcherCoordinator;
import org.chromium.components.browser_ui.bottomsheet.BottomSheetController;
import org.chromium.components.browser_ui.settings.SettingsLauncher;
import org.chromium.components.browser_ui.styles.ChromeColors;
import org.chromium.components.browser_ui.styles.SemanticColorUtils;
import org.chromium.components.embedder_support.util.UrlConstants;
import org.chromium.components.embedder_support.util.UrlUtilities;
import org.chromium.components.feature_engagement.EventConstants;
import org.chromium.components.feature_engagement.Tracker;
import org.chromium.components.search_engines.TemplateUrlService;
import org.chromium.components.search_engines.TemplateUrlService.TemplateUrlServiceObserver;
import org.chromium.content_public.browser.LoadUrlParams;
import org.chromium.content_public.browser.NavigationController;
import org.chromium.content_public.browser.NavigationEntry;
import org.chromium.ui.base.DeviceFormFactor;
import org.chromium.ui.base.WindowAndroid;
import org.chromium.ui.mojom.WindowOpenDisposition;
import java.util.List;
/**
* Provides functionality when the user interacts with the NTP.
*/
public class NewTabPage implements NativePage, InvalidationAwareThumbnailProvider,
TemplateUrlServiceObserver,
BrowserControlsStateProvider.Observer, FeedSurfaceDelegate,
VoiceRecognitionHandler.Observer {
private static final String TAG = "NewTabPage";
// Key for the scroll position data that may be stored in a navigation entry.
private static final String NAVIGATION_ENTRY_SCROLL_POSITION_KEY = "NewTabPageScrollPosition";
public static final String CONTEXT_MENU_USER_ACTION_PREFIX = "Suggestions";
protected final Tab mTab;
private final Supplier<Tab> mActivityTabProvider;
private final ActivityLifecycleDispatcher mActivityLifecycleDispatcher;
private final String mTitle;
private Context mContext;
private final int mBackgroundColor;
protected final NewTabPageManagerImpl mNewTabPageManager;
protected final TileGroup.Delegate mTileGroupDelegate;
private final boolean mIsTablet;
private boolean mIsHidden;
private final BrowserControlsStateProvider mBrowserControlsStateProvider;
private final NewTabPageUma mNewTabPageUma;
private final ContextMenuManager mContextMenuManager;
private final ObserverList<MostVisitedTileClickObserver> mMostVisitedTileClickObservers;
private final BottomSheetController mBottomSheetController;
private final SettingsLauncher mSettingsLauncher;
private FeedSurfaceProvider mFeedSurfaceProvider;
private NewTabPageLayout mNewTabPageLayout;
private TabObserver mTabObserver;
private LifecycleObserver mLifecycleObserver;
protected boolean mSearchProviderHasLogo;
protected OmniboxStub mOmniboxStub;
private VoiceRecognitionHandler mVoiceRecognitionHandler;
// The timestamp at which the constructor was called.
protected final long mConstructedTimeNs;
// The timestamp at which this NTP was last shown to the user.
private long mLastShownTimeNs;
private boolean mIsLoaded;
// Whether destroy() has been called.
private boolean mIsDestroyed;
private final int mTabStripAndToolbarHeight;
private final Supplier<Toolbar> mToolbarSupplier;
private final TabModelSelector mTabModelSelector;
private final TemplateUrlService mTemplateUrlService;
private SingleTabSwitcherCoordinator mSingleTabSwitcherCoordinator;
private ViewGroup mSingleTabCardContainer;
private final Activity mActivity;
@Nullable
private final HomeSurfaceTracker mHomeSurfaceTracker;
private final boolean mIsNtpAsHomeSurfaceEnabled;
@Nullable
private SearchResumptionModuleCoordinator mSearchResumptionModuleCoordinator;
@Override
public void onControlsOffsetChanged(int topOffset, int topControlsMinHeightOffset,
int bottomOffset, int bottomControlsMinHeightOffset, boolean needsAnimate) {
updateMargins();
}
@Override
public void onBottomControlsHeightChanged(
int bottomControlsHeight, int bottomControlsMinHeight) {
updateMargins();
}
/**
* Allows clients to listen for updates to the scroll changes of the search box on the
* NTP.
*/
public interface OnSearchBoxScrollListener {
/**
* Callback to be notified when the scroll position of the search box on the NTP has
* changed. A scroll percentage of 0, means the search box has no scroll applied and
* is in it's natural resting position. A value of 1 means the search box is scrolled
* entirely to the top of the screen viewport.
*
* @param scrollPercentage The percentage the search box has been scrolled off the page.
*/
void onNtpScrollChanged(float scrollPercentage);
}
/**
* An observer for most visited tile clicks.
*/
public interface MostVisitedTileClickObserver {
/**
* Called when a most visited tile is clicked.
* @param tile The most visited tile that was clicked.
* @param tab The tab hosting the most visited tile section.
*/
void onMostVisitedTileClicked(Tile tile, Tab tab);
}
protected class NewTabPageManagerImpl
extends SuggestionsUiDelegateImpl implements NewTabPageManager {
private final Tracker mTracker;
public NewTabPageManagerImpl(SuggestionsNavigationDelegate navigationDelegate,
Profile profile, NativePageHost nativePageHost, SnackbarManager snackbarManager) {
super(navigationDelegate, profile, nativePageHost, snackbarManager);
mTracker = TrackerFactory.getTrackerForProfile(profile);
}
@Override
public boolean isLocationBarShownInNTP() {
if (mIsDestroyed) return false;
return isInSingleUrlBarMode() && !mNewTabPageLayout.urlFocusAnimationsDisabled();
}
@Override
public boolean isVoiceSearchEnabled() {
return mVoiceRecognitionHandler != null
&& mVoiceRecognitionHandler.isVoiceSearchEnabled();
}
@Override
public void focusSearchBox(boolean beginVoiceSearch, String pastedText) {
if (mIsDestroyed) return;
FeedReliabilityLogger feedReliabilityLogger =
mFeedSurfaceProvider.getReliabilityLogger();
if (mVoiceRecognitionHandler != null && beginVoiceSearch) {
if (feedReliabilityLogger != null) {
feedReliabilityLogger.onVoiceSearch();
}
mVoiceRecognitionHandler.startVoiceRecognition(
VoiceRecognitionHandler.VoiceInteractionSource.NTP);
mTracker.notifyEvent(EventConstants.NTP_VOICE_SEARCH_BUTTON_CLICKED);
} else if (mOmniboxStub != null) {
if (feedReliabilityLogger != null) {
feedReliabilityLogger.onOmniboxFocused();
}
mOmniboxStub.setUrlBarFocus(true, pastedText,
pastedText == null ? OmniboxFocusReason.FAKE_BOX_TAP
: OmniboxFocusReason.FAKE_BOX_LONG_PRESS);
}
}
@Override
public void performSearchQuery(QueryInfo queryInfo) {
if (mOmniboxStub == null) return;
mOmniboxStub.performSearchQuery(queryInfo.queryText, queryInfo.searchParams);
}
@Override
public boolean isCurrentPage() {
if (mIsDestroyed) return false;
if (mOmniboxStub == null) return false;
return getNewTabPageForCurrentTab() == NewTabPage.this;
}
private NewTabPage getNewTabPageForCurrentTab() {
Tab currentTab = mActivityTabProvider.get();
NativePage nativePage = currentTab != null ? currentTab.getNativePage() : null;
return (nativePage instanceof NewTabPage) ? (NewTabPage) nativePage : null;
}
@Override
public void onLoadingComplete() {
if (mIsDestroyed) return;
long loadTimeMs = (System.nanoTime() - mConstructedTimeNs) / 1000000;
RecordHistogram.recordTimesHistogram("Tab.NewTabOnload", loadTimeMs);
mIsLoaded = true;
NewTabPageUma.recordNTPImpression(NewTabPageUma.NTP_IMPRESSION_REGULAR);
// If not visible when loading completes, wait until onShown is received.
if (!mTab.isHidden()) recordNTPShown();
}
}
/**
* Extends {@link TileGroupDelegateImpl} to add metrics logging that is specific to
* {@link NewTabPage}.
*/
private class NewTabPageTileGroupDelegate extends TileGroupDelegateImpl {
private NewTabPageTileGroupDelegate(Context context, Profile profile,
SuggestionsNavigationDelegate navigationDelegate, SnackbarManager snackbarManager) {
super(context, profile, navigationDelegate, snackbarManager,
BrowserUiUtils.HostSurface.NEW_TAB_PAGE);
}
@Override
public void onLoadingComplete(List<Tile> tiles) {
if (mIsDestroyed) return;
super.onLoadingComplete(tiles);
mNewTabPageLayout.onTilesLoaded();
}
@Override
public void openMostVisitedItem(int windowDisposition, Tile tile) {
if (mIsDestroyed) return;
super.openMostVisitedItem(windowDisposition, tile);
for (MostVisitedTileClickObserver observer : mMostVisitedTileClickObservers) {
observer.onMostVisitedTileClicked(tile, mTab);
}
if (windowDisposition != WindowOpenDisposition.NEW_WINDOW) {
RecordHistogram.recordMediumTimesHistogram("NewTabPage.MostVisitedTime",
(System.nanoTime() - mLastShownTimeNs)
/ TimeUtils.NANOSECONDS_PER_MILLISECOND);
}
}
}
/**
* Constructs a NewTabPage.
* @param activity The activity used for context to create the new tab page's View.
* @param browserControlsStateProvider {@link BrowserControlsStateProvider} to observe for
* offset changes.
* @param activityTabProvider Provides the current active tab.
* @param snackbarManager {@link SnackbarManager} object.
* @param lifecycleDispatcher Activity lifecycle dispatcher.
* @param tabModelSelector {@link TabModelSelector} object.
* @param isTablet {@code true} if running on a Tablet device.
* @param uma {@link NewTabPageUma} object recording user metrics.
* @param isInNightMode {@code true} if the night mode setting is on.
* @param nativePageHost The host that is showing this new tab page.
* @param tab The {@link Tab} that contains this new tab page.
* @param url The URL that launched this new tab page.
* @param bottomSheetController The controller for bottom sheets, used by the feed.
* @param shareDelegateSupplier Supplies the Delegate used to open SharingHub.
* @param windowAndroid The containing window of this page.
* @param toolbarSupplier Supplies the {@link Toolbar}.
* @param settingsLauncher {@link SettingsLauncher} object to launch settings fragments.
*/
public NewTabPage(Activity activity, BrowserControlsStateProvider browserControlsStateProvider,
Supplier<Tab> activityTabProvider, SnackbarManager snackbarManager,
ActivityLifecycleDispatcher lifecycleDispatcher, TabModelSelector tabModelSelector,
boolean isTablet, NewTabPageUma uma, boolean isInNightMode,
NativePageHost nativePageHost, Tab tab, String url,
BottomSheetController bottomSheetController,
Supplier<ShareDelegate> shareDelegateSupplier, WindowAndroid windowAndroid,
Supplier<Toolbar> toolbarSupplier, SettingsLauncher settingsLauncher,
HomeSurfaceTracker homeSurfaceTracker) {
mConstructedTimeNs = System.nanoTime();
TraceEvent.begin(TAG);
mActivity = activity;
mActivityTabProvider = activityTabProvider;
mActivityLifecycleDispatcher = lifecycleDispatcher;
mTab = tab;
mNewTabPageUma = uma;
mToolbarSupplier = toolbarSupplier;
mMostVisitedTileClickObservers = new ObserverList<>();
mBrowserControlsStateProvider = browserControlsStateProvider;
mTabModelSelector = tabModelSelector;
mBottomSheetController = bottomSheetController;
mSettingsLauncher = settingsLauncher;
mHomeSurfaceTracker = homeSurfaceTracker;
Profile profile = Profile.fromWebContents(mTab.getWebContents());
SuggestionsNavigationDelegate navigationDelegate = new SuggestionsNavigationDelegate(
activity, profile, nativePageHost, tabModelSelector, mTab);
mNewTabPageManager = new NewTabPageManagerImpl(
navigationDelegate, profile, nativePageHost, snackbarManager);
mTileGroupDelegate = new NewTabPageTileGroupDelegate(
activity, profile, navigationDelegate, snackbarManager);
mContext = activity;
mTitle = activity.getResources().getString(R.string.new_tab_title);
mBackgroundColor = SemanticColorUtils.getDefaultBgColor(mContext);
mIsTablet = isTablet;
mTemplateUrlService = TemplateUrlServiceFactory.getForProfile(profile);
mTemplateUrlService.addObserver(this);
mIsNtpAsHomeSurfaceEnabled = StartSurfaceConfiguration.isNtpAsHomeSurfaceEnabled(mIsTablet);
mTabObserver = new EmptyTabObserver() {
@Override
public void onShown(Tab tab, @TabSelectionType int type) {
// Showing the NTP is only meaningful when the page has been loaded already.
if (mIsLoaded) recordNTPShown();
mNewTabPageLayout.onSwitchToForeground();
// We update the visibility of the single tab card to false here rather than
// in function onHidden in order to keep the new tab page's thumbnail unchanged
// in the grid tab switcher. Otherwise, it would show a blank space instead of
// single tab car in the thumbnail.
if (mIsHidden && mSingleTabSwitcherCoordinator != null) {
mIsHidden = false;
if (mHomeSurfaceTracker == null
|| !mHomeSurfaceTracker.canShowHomeSurface(mTab)) {
setSingleTabCardVisibility(false);
}
}
}
@Override
public void onHidden(Tab tab, @TabHidingType int type) {
if (mIsLoaded) recordNTPHidden();
mIsHidden = true;
}
@Override
public void onLoadUrl(Tab tab, LoadUrlParams params, int loadType) {
mNewTabPageLayout.onLoadUrl(UrlUtilities.isNTPUrl(tab.getUrl()));
}
};
mTab.addObserver(mTabObserver);
mLifecycleObserver = new PauseResumeWithNativeObserver() {
@Override
public void onResumeWithNative() {}
@Override
public void onPauseWithNative() {
// Only record when this tab is the current tab.
if (mActivityTabProvider.get() == mTab) {
RecordUserAction.record("MobileNTPPaused");
}
}
};
mActivityLifecycleDispatcher.register(mLifecycleObserver);
updateSearchProviderHasLogo();
initializeMainView(activity, windowAndroid, snackbarManager, uma, isInNightMode,
shareDelegateSupplier, url);
// It is possible that the NewTabPage is created when the Tab model hasn't been initialized.
// For example, the user changes theme when a NTP is showing, which leads to the recreation
// of the ChromeTabbedActivity and showing the NTP as the last visited Tab.
if (mTabModelSelector.isTabStateInitialized()) {
mayCreateSearchResumptionModule(
profile, AutocompleteControllerProvider.from(windowAndroid));
} else {
mTabModelSelector.addObserver(new TabModelSelectorObserver() {
@Override
public void onTabStateInitialized() {
mayCreateSearchResumptionModule(
profile, AutocompleteControllerProvider.from(windowAndroid));
mTabModelSelector.removeObserver(this);
}
});
}
getView().addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
@Override
public void onViewAttachedToWindow(View view) {
updateMargins();
getView().removeOnAttachStateChangeListener(this);
}
@Override
public void onViewDetachedFromWindow(View view) {}
});
mBrowserControlsStateProvider.addObserver(this);
DownloadManagerService.getDownloadManagerService().checkForExternallyRemovedDownloads(
ProfileKey.getLastUsedRegularProfileKey());
mTabStripAndToolbarHeight =
activity.getResources().getDimensionPixelSize(R.dimen.tab_strip_and_toolbar_height);
mNewTabPageUma.recordContentSuggestionsDisplayStatus(profile);
// TODO(twellington): Move this somewhere it can be shared with NewTabPageView?
Runnable closeContextMenuCallback = activity::closeContextMenu;
mContextMenuManager = new ContextMenuManager(mNewTabPageManager.getNavigationDelegate(),
mFeedSurfaceProvider.getTouchEnabledDelegate(), closeContextMenuCallback,
NewTabPage.CONTEXT_MENU_USER_ACTION_PREFIX);
windowAndroid.addContextMenuCloseListener(mContextMenuManager);
mNewTabPageLayout.initialize(mNewTabPageManager, activity, mTileGroupDelegate,
mSearchProviderHasLogo, mTemplateUrlService.isDefaultSearchEngineGoogle(),
mFeedSurfaceProvider.getScrollDelegate(),
mFeedSurfaceProvider.getTouchEnabledDelegate(), mFeedSurfaceProvider.getUiConfig(),
lifecycleDispatcher, uma, mTab.isIncognito(), windowAndroid,
mIsNtpAsHomeSurfaceEnabled, FeedFeatures.isMultiColumnFeedEnabled(mContext));
// If new NewTabPage is created via back operations, re-show the single Tab card with the
// previously tracked Tab.
if (mHomeSurfaceTracker != null && mHomeSurfaceTracker.isHomeSurfaceTab(mTab)) {
showHomeSurfaceUi(mHomeSurfaceTracker.getLastActiveTabToTrack());
ReturnToChromeUtil.recordHomeSurfaceShown();
}
TraceEvent.end(TAG);
}
/**
* Create and initialize the main view contained in this NewTabPage.
* @param activity The activity used to initialize the view.
* @param windowAndroid Provides the current active tab.
* @param snackbarManager {@link SnackbarManager} object.
* @param uma {@link NewTabPageUma} object recording user metrics.
* @param isInNightMode {@code true} if the night mode setting is on.
* @param shareDelegateSupplier Supplies a delegate used to open SharingHub.
*/
protected void initializeMainView(Activity activity, WindowAndroid windowAndroid,
SnackbarManager snackbarManager, NewTabPageUma uma, boolean isInNightMode,
Supplier<ShareDelegate> shareDelegateSupplier, String url) {
Profile profile = Profile.fromWebContents(mTab.getWebContents());
LayoutInflater inflater = LayoutInflater.from(activity);
mNewTabPageLayout = (NewTabPageLayout) inflater.inflate(R.layout.new_tab_page_layout, null);
FeedActionDelegate actionDelegate = new FeedActionDelegateImpl(activity, snackbarManager,
mNewTabPageManager.getNavigationDelegate(), BookmarkModel.getForProfile(profile),
BrowserUiUtils.HostSurface.NEW_TAB_PAGE, mTabModelSelector) {
@Override
public void openHelpPage() {
NewTabPageUma.recordAction(NewTabPageUma.ACTION_CLICKED_LEARN_MORE);
super.openHelpPage();
}
};
FeedSurfaceCoordinator feedSurfaceCoordinator = new FeedSurfaceCoordinator(activity,
snackbarManager, windowAndroid,
new SnapScrollHelperImpl(mNewTabPageManager, mNewTabPageLayout), mNewTabPageLayout,
mBrowserControlsStateProvider.getTopControlsHeight(), isInNightMode, this, profile,
/* isPlaceholderShownInitially= */ false, mBottomSheetController,
shareDelegateSupplier, /* externalScrollableContainerDelegate= */ null,
NewTabPageUtils.decodeOriginFromNtpUrl(url),
PrivacyPreferencesManagerImpl.getInstance(), mToolbarSupplier,
SurfaceType.NEW_TAB_PAGE, mConstructedTimeNs,
FeedSwipeRefreshLayout.create(activity, R.id.toolbar_container),
/* overScrollDisabled= */ false, /* viewportView= */ null, actionDelegate,
HelpAndFeedbackLauncherImpl.getForProfile(profile), mTabModelSelector);
mFeedSurfaceProvider = feedSurfaceCoordinator;
}
/**
* Saves a single string under a given key to the navigation entry. It is up to the caller to
* extract and interpret later.
* @param tab A tab that is used to access the NavigationController and the NavigationEntry
* extras.
* @param key The key to store the data under, will need to be used to access later.
* @param value The payload to persist.
*
* TODO(https://crbug.com/941581): Refactor this to be reusable across NativePage components.
*/
public static void saveStringToNavigationEntry(Tab tab, String key, String value) {
if (tab.getWebContents() == null) return;
NavigationController controller = tab.getWebContents().getNavigationController();
int index = controller.getLastCommittedEntryIndex();
NavigationEntry entry = controller.getEntryAtIndex(index);
if (entry == null) return;
// At least under test conditions this method may be called initially for the load of the
// NTP itself, at which point the last committed entry is not for the NTP yet. This method
// will then be called a second time when the user navigates away, at which point the last
// committed entry is for the NTP. The extra data must only be set in the latter case.
if (!UrlUtilities.isNTPUrl(entry.getUrl())) return;
controller.setEntryExtraData(index, key, value);
}
/**
* @param isTablet Whether the activity is running in tablet mode.
* @param searchProviderHasLogo Whether the default search engine has logo.
* @return Whether the NTP is in single url bar mode, i.e. the url bar is shown in-line on the
* NTP.
*/
public static boolean isInSingleUrlBarMode(boolean isTablet, boolean searchProviderHasLogo) {
return !isTablet && searchProviderHasLogo;
}
/**
* Update the margins for the content when browser controls constraints or bottom control
* height are changed.
*/
private void updateMargins() {
// TODO(mdjones): can this be merged with BasicNativePage's updateMargins?
View view = getView();
ViewGroup.MarginLayoutParams layoutParams =
((ViewGroup.MarginLayoutParams) view.getLayoutParams());
if (layoutParams == null) return;
// Negative |topControlsDistanceToRest| means the controls Y position is above the rest
// position and the controls height is increasing with animation, while positive
// |topControlsDistanceToRest| means the controls Y position is below the rest position and
// the controls height is decreasing with animation. |getToolbarExtraYOffset()| returns
// the margin when the controls are at rest, so |getToolbarExtraYOffset()
// + topControlsDistanceToRest| will give the margin for the current animation frame.
final int topControlsDistanceToRest = mBrowserControlsStateProvider.getContentOffset()
- mBrowserControlsStateProvider.getTopControlsHeight();
final int topMargin = getToolbarExtraYOffset() + topControlsDistanceToRest;
final int bottomMargin = mBrowserControlsStateProvider.getBottomControlsHeight()
- mBrowserControlsStateProvider.getBottomControlOffset();
if (topMargin != layoutParams.topMargin || bottomMargin != layoutParams.bottomMargin) {
layoutParams.topMargin = topMargin;
layoutParams.bottomMargin = bottomMargin;
view.setLayoutParams(layoutParams);
}
// Apply the height of the top toolbar as the margin to the top of the N logo.
mNewTabPageLayout.setSearchProviderTopMargin(getLogoMargin(true));
mNewTabPageLayout.setSearchProviderBottomMargin(getLogoMargin(false));
}
// TODO(sinansahin): This is the same as {@link ToolbarManager#getToolbarExtraYOffset}. So, we
// should look into sharing the logic.
/**
* @return The height that is included in the top controls but not in the toolbar or the tab
* strip.
*/
private int getToolbarExtraYOffset() {
return mBrowserControlsStateProvider.getTopControlsHeight() - mTabStripAndToolbarHeight;
}
/** @return The view container for the new tab layout. */
@VisibleForTesting
public NewTabPageLayout getNewTabPageLayout() {
return mNewTabPageLayout;
}
/**
* Updates whether the NewTabPage should animate on URL focus changes.
* @param disable Whether to disable the animations.
*/
public void setUrlFocusAnimationsDisabled(boolean disable) {
mNewTabPageLayout.setUrlFocusAnimationsDisabled(disable);
}
private boolean isInSingleUrlBarMode() {
return isInSingleUrlBarMode(mIsTablet, mSearchProviderHasLogo);
}
private void updateSearchProviderHasLogo() {
mSearchProviderHasLogo = mTemplateUrlService.doesDefaultSearchEngineHaveLogo();
}
private void onSearchEngineUpdated() {
updateSearchProviderHasLogo();
setSearchProviderInfoOnView(
mSearchProviderHasLogo, mTemplateUrlService.isDefaultSearchEngineGoogle());
// TODO(https://crbug.com/1329288): Remove this call when the Feed position experiment is
// cleaned up.
updateMargins();
}
/**
* Set the search provider info on the main child view, so that it can change layouts if
* needed.
* @param hasLogo Whether the search provider has a logo.
* @param isGoogle Whether the search provider is Google.
*/
private void setSearchProviderInfoOnView(boolean hasLogo, boolean isGoogle) {
mNewTabPageLayout.setSearchProviderInfo(hasLogo, isGoogle);
}
/**
* Specifies the percentage the URL is focused during an animation. 1.0 specifies that the URL
* bar has focus and has completed the focus animation. 0 is when the URL bar is does not have
* any focus.
*
* @param percent The percentage of the URL bar focus animation.
*/
public void setUrlFocusChangeAnimationPercent(float percent) {
mNewTabPageLayout.setUrlFocusChangeAnimationPercent(percent);
}
/**
* Get the bounds of the search box in relation to the top level NewTabPage view.
*
* @param bounds The current drawing location of the search box.
* @param translation The translation applied to the search box by the parent view hierarchy up
* to the NewTabPage view.
*/
public void getSearchBoxBounds(Rect bounds, Point translation) {
mNewTabPageLayout.getSearchBoxBounds(bounds, translation, getView());
}
/**
* Updates the opacity of the search box when scrolling.
*
* @param alpha opacity (alpha) value to use.
*/
public void setSearchBoxAlpha(float alpha) {
mNewTabPageLayout.setSearchBoxAlpha(alpha);
}
/**
* Updates the opacity of the search provider logo when scrolling.
*
* @param alpha opacity (alpha) value to use.
*/
public void setSearchProviderLogoAlpha(float alpha) {
mNewTabPageLayout.setSearchProviderLogoAlpha(alpha);
}
/**
* Set the search box background drawable.
*
* @param drawable The search box background.
*/
public void setSearchBoxBackground(Drawable drawable) {
mNewTabPageLayout.setSearchBoxBackground(drawable);
}
/**
* @return Whether the location bar is shown in the NTP.
*/
public boolean isLocationBarShownInNTP() {
return mNewTabPageManager.isLocationBarShownInNTP();
}
/** @see org.chromium.chrome.browser.omnibox.NewTabPageDelegate#hasCompletedFirstLayout(). */
public boolean hasCompletedFirstLayout() {
return mNewTabPageLayout.getHeight() > 0;
}
/**
* @return Whether the location bar has been scrolled to top in the NTP.
*/
public boolean isLocationBarScrolledToTopInNtp() {
return mNewTabPageLayout.getToolbarTransitionPercentage() == 1;
}
/**
* Sets the listener for search box scroll changes.
* @param listener The listener to be notified on changes.
*/
public void setSearchBoxScrollListener(OnSearchBoxScrollListener listener) {
mNewTabPageLayout.setSearchBoxScrollListener(listener);
}
/**
* Sets the OmniboxStub that this page interacts with.
*/
public void setOmniboxStub(OmniboxStub omniboxStub) {
mOmniboxStub = omniboxStub;
if (mOmniboxStub != null) {
// The toolbar can't get the reference to the native page until its initialization is
// finished, so we can't cache it here and transfer it to the view later. We pull that
// state from the location bar when we get a reference to it as a workaround.
mNewTabPageLayout.setUrlFocusChangeAnimationPercent(
omniboxStub.isUrlBarFocused() ? 1f : 0f);
FeedReliabilityLogger feedReliabilityLogger =
mFeedSurfaceProvider.getReliabilityLogger();
if (feedReliabilityLogger != null) {
mOmniboxStub.addUrlFocusChangeListener(feedReliabilityLogger);
}
}
mVoiceRecognitionHandler = mOmniboxStub.getVoiceRecognitionHandler();
if (mVoiceRecognitionHandler != null) {
mVoiceRecognitionHandler.addObserver(this);
mNewTabPageLayout.updateActionButtonVisibility();
}
}
@Override
public void notifyHidingWithBack() {
FeedReliabilityLogger feedReliabilityLogger = mFeedSurfaceProvider.getReliabilityLogger();
if (feedReliabilityLogger != null) {
feedReliabilityLogger.onNavigateBack();
}
}
@Override
public void onVoiceAvailabilityImpacted() {
mNewTabPageLayout.updateActionButtonVisibility();
}
/** Adds an observer to be notified on most visited tile clicks. */
public void addMostVisitedTileClickObserver(MostVisitedTileClickObserver observer) {
mMostVisitedTileClickObservers.addObserver(observer);
}
/** Removes the observer. */
public void removeMostVisitedTileClickObserver(MostVisitedTileClickObserver observer) {
mMostVisitedTileClickObservers.removeObserver(observer);
}
/**
* Records UMA for the NTP being shown. This includes a fresh page load or being brought to the
* foreground.
*/
private void recordNTPShown() {
mLastShownTimeNs = System.nanoTime();
RecordUserAction.record("MobileNTPShown");
SuggestionsMetrics.recordSurfaceVisible();
FeatureNotificationUtils.registerIPHCallback(FeatureType.VOICE_SEARCH,
mNewTabPageLayout::maybeShowFeatureNotificationVoiceSearchIPH);
}
/** Records UMA for the NTP being hidden and the time spent on it. */
private void recordNTPHidden() {
RecordHistogram.recordMediumTimesHistogram("NewTabPage.TimeSpent",
(System.nanoTime() - mLastShownTimeNs) / TimeUtils.NANOSECONDS_PER_MILLISECOND);
SuggestionsMetrics.recordSurfaceHidden();
FeatureNotificationUtils.unregisterIPHCallback(FeatureType.VOICE_SEARCH);
}
/**
* Returns the value of the adapter scroll position that was stored in the last committed
* navigation entry. Returns {@code RecyclerView.NO_POSITION} if there is no last committed
* navigation entry, or if no data is found.
* @param scrollPositionKey The key under which the scroll position has been stored in the
* NavigationEntryExtraData.
* @param tab A tab that is used to access the NavigationController and the NavigationEntry
* extras.
* @return The adapter scroll position.
*/
public static int getScrollPositionFromNavigationEntry(String scrollPositionKey, Tab tab) {
return getIntFromNavigationEntry(scrollPositionKey, tab, RecyclerView.NO_POSITION);
}
/**
* Returns an arbitrary int value stored in the last committed navigation entry. If some step
* fails then the default is returned instead.
* @param key The string previously used to tag this piece of data.
* @param tab A tab that is used to access the NavigationController and the NavigationEntry
* extras.
* @param defaultValue The value to return if lookup or parsing is unsuccessful.
* @return The value for the given key.
*
* TODO(https://crbug.com/941581): Refactor this to be reusable across NativePage components.
*/
private static int getIntFromNavigationEntry(String key, Tab tab, int defaultValue) {
if (tab.getWebContents() == null) return defaultValue;
String stringValue = getStringFromNavigationEntry(tab, key);
if (stringValue == null || stringValue.isEmpty()) {
return RecyclerView.NO_POSITION;
}
try {
return Integer.parseInt(stringValue);
} catch (NumberFormatException e) {
Log.w(TAG, "Bad data found for %s : %s", key, stringValue, e);
return RecyclerView.NO_POSITION;
}
}
/**
* Returns an arbitrary string value stored in the last committed navigation entry. If the look
* up fails, an empty string is returned.
* @param tab A tab that is used to access the NavigationController and the NavigationEntry
* extras.
* @param key The string previously used to tag this piece of data.
* @return The value previously stored with the given key.
*
* TODO(https://crbug.com/941581): Refactor this to be reusable across NativePage components.
*/
public static String getStringFromNavigationEntry(Tab tab, String key) {
if (tab.getWebContents() == null) return "";
NavigationController controller = tab.getWebContents().getNavigationController();
int index = controller.getLastCommittedEntryIndex();
return controller.getEntryExtraData(index, key);
}
/**
* @return Whether the NTP has finished loaded.
*/
@VisibleForTesting
public boolean isLoadedForTests() {
return mIsLoaded;
}
// TemplateUrlServiceObserver overrides
@Override
public void onTemplateURLServiceChanged() {
onSearchEngineUpdated();
}
// NativePage overrides
@Override
public void destroy() {
assert !mIsDestroyed;
assert !ViewCompat.isAttachedToWindow(getView())
: "Destroy called before removed from window";
if (mIsLoaded && !mTab.isHidden()) recordNTPHidden();
mNewTabPageManager.onDestroy();
mTileGroupDelegate.destroy();
mTemplateUrlService.removeObserver(this);
mTab.removeObserver(mTabObserver);
mTabObserver = null;
mActivityLifecycleDispatcher.unregister(mLifecycleObserver);
mLifecycleObserver = null;
mBrowserControlsStateProvider.removeObserver(this);
FeedReliabilityLogger feedReliabilityLogger = mFeedSurfaceProvider.getReliabilityLogger();
if (mOmniboxStub != null && feedReliabilityLogger != null) {
mOmniboxStub.removeUrlFocusChangeListener(feedReliabilityLogger);
}
mFeedSurfaceProvider.destroy();
mTab.getWindowAndroid().removeContextMenuCloseListener(mContextMenuManager);
if (mVoiceRecognitionHandler != null) {
mVoiceRecognitionHandler.removeObserver(this);
}
if (mSearchResumptionModuleCoordinator != null) {
mSearchResumptionModuleCoordinator.destroy();
}
if (mSingleTabSwitcherCoordinator != null) {
destroySingleTabCard();
}
mIsDestroyed = true;
}
@Override
public String getUrl() {
return UrlConstants.NTP_URL;
}
@Override
public String getTitle() {
return mTitle;
}
@Override
public int getBackgroundColor() {
return mBackgroundColor;
}
@Override
public @ColorInt int getToolbarTextBoxBackgroundColor(@ColorInt int defaultColor) {
if (isLocationBarShownInNTP()) {
return isLocationBarScrolledToTopInNtp()
? ChromeColors.getSurfaceColor(mContext, R.dimen.toolbar_text_box_elevation)
: ChromeColors.getPrimaryBackgroundColor(mContext, false);
}
return defaultColor;
}
@Override
public @ColorInt int getToolbarSceneLayerBackground(@ColorInt int defaultColor) {
return isLocationBarShownInNTP() ? getBackgroundColor() : defaultColor;
}
@Override
public boolean needsToolbarShadow() {
return !mSearchProviderHasLogo;
}
@Override
public View getView() {
return mFeedSurfaceProvider.getView();
}
@Override
public String getHost() {
return UrlConstants.NTP_HOST;
}
@Override
public void updateForUrl(String url) {
}
@Override
public void reload() {
mFeedSurfaceProvider.reload();
}
// InvalidationAwareThumbnailProvider
@Override
public boolean shouldCaptureThumbnail() {
return mNewTabPageLayout.shouldCaptureThumbnail()
|| mFeedSurfaceProvider.shouldCaptureThumbnail();
}
@Override
public void captureThumbnail(Canvas canvas) {
mNewTabPageLayout.onPreCaptureThumbnail();
mFeedSurfaceProvider.captureThumbnail(canvas);
}
// Implements FeedSurfaceDelegate
@Override
public FeedSurfaceLifecycleManager createStreamLifecycleManager(
Activity activity, SurfaceCoordinator coordinator) {
return new NtpFeedSurfaceLifecycleManager(
activity, mTab, (FeedSurfaceCoordinator) coordinator);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {