-
Notifications
You must be signed in to change notification settings - Fork 6.7k
/
HistoryClustersCoordinator.java
271 lines (240 loc) · 12.3 KB
/
HistoryClustersCoordinator.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
// Copyright 2022 The Chromium Authors. All rights reserved.
// 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.history_clusters;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import androidx.appcompat.widget.Toolbar.OnMenuItemClickListener;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import org.chromium.chrome.browser.history_clusters.HistoryClustersItemProperties.ItemType;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.components.browser_ui.widget.selectable_list.SelectableItemView;
import org.chromium.components.browser_ui.widget.selectable_list.SelectableListLayout;
import org.chromium.components.browser_ui.widget.selectable_list.SelectableListToolbar;
import org.chromium.components.browser_ui.widget.selectable_list.SelectionDelegate;
import org.chromium.components.favicon.LargeIconBridge;
import org.chromium.components.search_engines.TemplateUrlService;
import org.chromium.ui.modelutil.MVCListAdapter.ModelList;
import org.chromium.ui.modelutil.PropertyModel;
import org.chromium.ui.modelutil.PropertyModelChangeProcessor;
import org.chromium.ui.modelutil.SimpleRecyclerViewAdapter;
import org.chromium.ui.util.AccessibilityUtil;
/**
* Root component for the HistoryClusters UI component, which displays lists of related history
* visits grouped into clusters.
*/
public class HistoryClustersCoordinator implements OnMenuItemClickListener {
private static class DisabledSelectionDelegate extends SelectionDelegate {
@Override
public boolean toggleSelectionForItem(Object o) {
return false;
}
@Override
public boolean isItemSelected(Object o) {
return false;
}
@Override
public boolean isSelectionEnabled() {
return false;
}
}
private final HistoryClustersMediator mMediator;
private final ModelList mModelList;
private final HistoryClustersDelegate mDelegate;
private SimpleRecyclerViewAdapter mAdapter;
private final Activity mActivity;
private boolean mActivityViewInflated;
private final PropertyModel mToolbarModel;
private ViewGroup mActivityContentView;
private HistoryClustersToolbar mToolbar;
private SelectionDelegate<ClusterVisit> mSelectionDelegate;
private SelectableListLayout mSelectableListLayout;
private SelectionDelegate mDisabledSelectionDelegate = new DisabledSelectionDelegate();
private RecyclerView mRecyclerView;
private final HistoryClustersMetricsLogger mMetricsLogger;
@VisibleForTesting
HistoryClustersCoordinator(@NonNull Profile profile, @NonNull Activity activity,
TemplateUrlService templateUrlService, HistoryClustersDelegate historyClustersDelegate,
HistoryClustersMetricsLogger metricsLogger,
SelectionDelegate<ClusterVisit> selectionDelegate,
AccessibilityUtil accessibilityUtil) {
mActivity = activity;
mDelegate = historyClustersDelegate;
mModelList = new ModelList();
mToolbarModel = new PropertyModel.Builder(HistoryClustersToolbarProperties.ALL_KEYS)
.with(HistoryClustersToolbarProperties.QUERY_STATE,
QueryState.forQueryless())
.build();
mMetricsLogger = metricsLogger;
mSelectionDelegate = selectionDelegate;
mMediator = new HistoryClustersMediator(HistoryClustersBridge.getForProfile(profile),
new LargeIconBridge(profile), mActivity, mActivity.getResources(), mModelList,
mToolbarModel, mDelegate, System::currentTimeMillis, templateUrlService,
mSelectionDelegate, mMetricsLogger, accessibilityUtil);
}
/**
* Construct a new HistoryClustersCoordinator.
* @param profile The profile from which the coordinator should access history data.
* @param activity Activity in which this UI resides.
* @param historyClustersDelegate Delegate that provides functionality that must be implemented
* externally, e.g. populating intents targeting activities we can't reference directly.
* @param accessibilityUtil Utility object that tells us about the current accessibility state.
*/
public HistoryClustersCoordinator(@NonNull Profile profile, @NonNull Activity activity,
TemplateUrlService templateUrlService, HistoryClustersDelegate historyClustersDelegate,
AccessibilityUtil accessibilityUtil) {
this(profile, activity, templateUrlService, historyClustersDelegate,
new HistoryClustersMetricsLogger(templateUrlService), new SelectionDelegate<>(),
accessibilityUtil);
}
public void destroy() {
mMediator.destroy();
mMetricsLogger.destroy();
if (mActivityViewInflated) {
mSelectableListLayout.onDestroyed();
}
}
/** Called when the user toggles to or from the Journeys UI in the containing history page. */
public void onToggled(boolean toggledTo) {
if (toggledTo) {
mMetricsLogger.setInitialState(HistoryClustersMetricsLogger.InitialState.SAME_DOCUMENT);
mMediator.setQueryState(QueryState.forQueryless());
} else {
mMetricsLogger.incrementToggleCount();
}
}
public void setInitialQuery(QueryState queryState) {
mMetricsLogger.setInitialState(
HistoryClustersMetricsLogger.InitialState.INDIRECT_NAVIGATION);
mMediator.setQueryState(queryState);
}
/**
* Opens the History Clusters UI. On phones this opens the History Activity; on tablets, it
* navigates to a NativePage in the active tab.
* @param query The preset query to populate when opening the UI.
*/
public void openHistoryClustersUi(String query) {
mMediator.openHistoryClustersUi(query);
}
/** Gets the root view for a "full activity" presentation of the user's history clusters. */
public ViewGroup getActivityContentView() {
if (!mActivityViewInflated) {
inflateActivityView();
}
return mActivityContentView;
}
void inflateActivityView() {
mAdapter = new SimpleRecyclerViewAdapter(mModelList);
mAdapter.registerType(
ItemType.VISIT, this::buildVisitView, HistoryClustersViewBinder::bindVisitView);
mAdapter.registerType(ItemType.CLUSTER, this::buildClusterView,
HistoryClustersViewBinder::bindClusterView);
mAdapter.registerType(ItemType.RELATED_SEARCHES, this::buildRelatedSearchesView,
HistoryClustersViewBinder::bindRelatedSearchesView);
mAdapter.registerType(
ItemType.TOGGLE, mDelegate::getToggleView, HistoryClustersViewBinder::noopBindView);
mAdapter.registerType(ItemType.PRIVACY_DISCLAIMER, mDelegate::getPrivacyDisclaimerView,
HistoryClustersViewBinder::noopBindView);
mAdapter.registerType(ItemType.CLEAR_BROWSING_DATA, mDelegate::getClearBrowsingDataView,
HistoryClustersViewBinder::noopBindView);
mAdapter.registerType(ItemType.MORE_PROGRESS, this::buildMoreProgressView,
HistoryClustersViewBinder::bindMoreProgressView);
LayoutInflater layoutInflater = LayoutInflater.from(mActivity);
mActivityContentView = (ViewGroup) layoutInflater.inflate(
R.layout.history_clusters_activity_content, null);
mSelectableListLayout = mActivityContentView.findViewById(R.id.selectable_list);
mSelectableListLayout.setEmptyViewText(R.string.history_manager_empty);
mRecyclerView = mSelectableListLayout.initializeRecyclerView(mAdapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(
mRecyclerView.getContext(), LinearLayoutManager.VERTICAL, false));
mRecyclerView.addOnScrollListener(mMediator);
mToolbar = (HistoryClustersToolbar) mSelectableListLayout.initializeToolbar(
R.layout.history_clusters_toolbar, mSelectionDelegate, R.string.menu_history,
R.id.normal_menu_group, R.id.selection_mode_menu_group, this, true);
mToolbar.initializeSearchView(
mMediator, R.string.history_clusters_search_your_journeys, R.id.search_menu_id);
mSelectableListLayout.configureWideDisplayStyle();
mToolbar.setSearchEnabled(true);
if (!mDelegate.isSeparateActivity()) {
mToolbar.getMenu().removeItem(R.id.close_menu_id);
}
mToolbar.setInfoMenuItem(R.id.info_menu_id);
mDelegate.shouldShowPrivacyDisclaimerSupplier().addObserver(
(show) -> mToolbar.updateInfoMenuItem(true, show));
PropertyModelChangeProcessor.create(
mToolbarModel, mToolbar, HistoryClustersViewBinder::bindToolbar);
PropertyModelChangeProcessor.create(
mToolbarModel, mSelectableListLayout, HistoryClustersViewBinder::bindListLayout);
mActivityViewInflated = true;
}
private View buildMoreProgressView(ViewGroup parent) {
return LayoutInflater.from(parent.getContext())
.inflate(R.layout.more_progress_button, parent, false);
}
private View buildClusterView(ViewGroup parent) {
SelectableItemView<HistoryCluster> clusterView =
(SelectableItemView<HistoryCluster>) LayoutInflater.from(parent.getContext())
.inflate(R.layout.history_cluster, parent, false);
clusterView.setSelectionDelegate(mDisabledSelectionDelegate);
return clusterView;
}
private View buildVisitView(ViewGroup parent) {
SelectableItemView<ClusterVisit> itemView =
(SelectableItemView<ClusterVisit>) LayoutInflater.from(parent.getContext())
.inflate(R.layout.history_cluster_visit, parent, false);
itemView.setSelectionDelegate(mSelectionDelegate);
return itemView;
}
private View buildRelatedSearchesView(ViewGroup parent) {
return LayoutInflater.from(parent.getContext())
.inflate(R.layout.history_clusters_related_searches_view, parent, false);
}
// OnMenuItemClickListener implementation.
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
if (menuItem.getItemId() == R.id.search_menu_id) {
mMediator.setQueryState(QueryState.forQuery("", mDelegate.getSearchEmptyString()));
return true;
} else if (menuItem.getItemId() == R.id.close_menu_id && mDelegate.isSeparateActivity()) {
mActivity.finish();
return true;
} else if (menuItem.getItemId() == R.id.selection_mode_open_in_incognito) {
mMediator.openVisitsInNewTabs(mSelectionDelegate.getSelectedItemsAsList(), true, false);
mSelectionDelegate.clearSelection();
return true;
} else if (menuItem.getItemId() == R.id.selection_mode_open_in_new_tab) {
mMediator.openVisitsInNewTabs(
mSelectionDelegate.getSelectedItemsAsList(), false, false);
mSelectionDelegate.clearSelection();
return true;
} else if (menuItem.getItemId() == R.id.info_menu_id) {
mDelegate.toggleInfoHeaderVisibility();
mToolbar.updateInfoMenuItem(
true, mDelegate.shouldShowPrivacyDisclaimerSupplier().get());
} else if (menuItem.getItemId() == R.id.selection_mode_delete_menu_id) {
mMediator.deleteVisits(mSelectionDelegate.getSelectedItemsAsList());
mSelectionDelegate.clearSelection();
return true;
} else if (menuItem.getItemId() == R.id.optout_menu_id) {
mDelegate.onOptOut();
} else if (menuItem.getItemId() == R.id.selection_mode_open_in_tab_group) {
mMediator.openVisitsInNewTabs(mSelectionDelegate.getSelectedItemsAsList(), false, true);
return true;
}
return false;
}
@VisibleForTesting
public RecyclerView getRecyclerViewFortesting() {
return mRecyclerView;
}
@VisibleForTesting
public SelectableListToolbar getToolbarForTesting() {
return mToolbar;
}
}