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.content.Context; | 7 import android.content.Context; |
8 import android.content.res.Resources; | 8 import android.content.res.Resources; |
9 import android.graphics.Bitmap; | 9 import android.graphics.Bitmap; |
10 import android.graphics.BitmapFactory; | 10 import android.graphics.BitmapFactory; |
11 import android.graphics.Color; | 11 import android.graphics.Color; |
12 import android.graphics.drawable.BitmapDrawable; | 12 import android.graphics.drawable.BitmapDrawable; |
13 import android.support.annotation.Nullable; | 13 import android.support.annotation.Nullable; |
14 import android.support.v4.graphics.drawable.RoundedBitmapDrawable; | 14 import android.support.v4.graphics.drawable.RoundedBitmapDrawable; |
15 import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory; | 15 import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory; |
16 import android.view.ContextMenu; | 16 import android.view.ContextMenu; |
17 import android.view.ContextMenu.ContextMenuInfo; | 17 import android.view.ContextMenu.ContextMenuInfo; |
18 import android.view.LayoutInflater; | 18 import android.view.LayoutInflater; |
19 import android.view.View; | 19 import android.view.View; |
20 import android.view.View.OnClickListener; | 20 import android.view.View.OnClickListener; |
21 import android.view.View.OnCreateContextMenuListener; | 21 import android.view.View.OnCreateContextMenuListener; |
22 import android.view.ViewGroup; | 22 import android.view.ViewGroup; |
23 | 23 |
24 import org.chromium.base.ApiCompatibilityUtils; | 24 import org.chromium.base.ApiCompatibilityUtils; |
25 import org.chromium.base.Log; | 25 import org.chromium.base.Log; |
| 26 import org.chromium.base.VisibleForTesting; |
26 import org.chromium.chrome.R; | 27 import org.chromium.chrome.R; |
27 import org.chromium.chrome.browser.ChromeFeatureList; | 28 import org.chromium.chrome.browser.ChromeFeatureList; |
28 import org.chromium.chrome.browser.favicon.LargeIconBridge.LargeIconCallback; | 29 import org.chromium.chrome.browser.favicon.LargeIconBridge.LargeIconCallback; |
29 import org.chromium.chrome.browser.ntp.ContextMenuManager; | 30 import org.chromium.chrome.browser.ntp.ContextMenuManager; |
30 import org.chromium.chrome.browser.ntp.ContextMenuManager.ContextMenuItemId; | 31 import org.chromium.chrome.browser.ntp.ContextMenuManager.ContextMenuItemId; |
31 import org.chromium.chrome.browser.ntp.MostVisitedTileType; | 32 import org.chromium.chrome.browser.ntp.MostVisitedTileType; |
32 import org.chromium.chrome.browser.offlinepages.OfflinePageBridge; | 33 import org.chromium.chrome.browser.offlinepages.OfflinePageBridge; |
33 import org.chromium.chrome.browser.widget.RoundedIconGenerator; | 34 import org.chromium.chrome.browser.widget.RoundedIconGenerator; |
34 import org.chromium.ui.mojom.WindowOpenDisposition; | 35 import org.chromium.ui.mojom.WindowOpenDisposition; |
35 | 36 |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 * @param maxResults The maximum number of sites to retrieve. | 234 * @param maxResults The maximum number of sites to retrieve. |
234 */ | 235 */ |
235 public void startObserving(int maxResults) { | 236 public void startObserving(int maxResults) { |
236 mObserver.onLoadTaskAdded(); | 237 mObserver.onLoadTaskAdded(); |
237 mTileGroupDelegate.setMostVisitedSitesObserver(this, maxResults); | 238 mTileGroupDelegate.setMostVisitedSitesObserver(this, maxResults); |
238 } | 239 } |
239 | 240 |
240 /** | 241 /** |
241 * Renders tile views in the given {@link TileGridLayout}, reusing existing
tile views where | 242 * Renders tile views in the given {@link TileGridLayout}, reusing existing
tile views where |
242 * possible because view inflation and icon loading are slow. | 243 * possible because view inflation and icon loading are slow. |
243 * @param tileGridLayout The layout to render the tile views into. | 244 * @param parent The layout to render the tile views into. |
244 * @param trackLoadTasks Whether to track load tasks. | 245 * @param trackLoadTasks Whether to track load tasks. |
245 * @param condensed Whether to use a condensed layout. | 246 * @param condensed Whether to use a condensed layout. |
246 */ | 247 */ |
247 public void renderTileViews( | 248 public void renderTileViews(ViewGroup parent, boolean trackLoadTasks, boolea
n condensed) { |
248 TileGridLayout tileGridLayout, boolean trackLoadTasks, boolean conde
nsed) { | |
249 // Map the old tile views by url so they can be reused later. | 249 // Map the old tile views by url so they can be reused later. |
250 Map<String, TileView> oldTileViews = new HashMap<>(); | 250 Map<String, TileView> oldTileViews = new HashMap<>(); |
251 int childCount = tileGridLayout.getChildCount(); | 251 int childCount = parent.getChildCount(); |
252 for (int i = 0; i < childCount; i++) { | 252 for (int i = 0; i < childCount; i++) { |
253 TileView tileView = (TileView) tileGridLayout.getChildAt(i); | 253 TileView tileView = (TileView) parent.getChildAt(i); |
254 oldTileViews.put(tileView.getUrl(), tileView); | 254 oldTileViews.put(tileView.getUrl(), tileView); |
255 } | 255 } |
256 | 256 |
257 // Remove all views from the layout because even if they are reused late
r they'll have to be | 257 // Remove all views from the layout because even if they are reused late
r they'll have to be |
258 // added back in the correct order. | 258 // added back in the correct order. |
259 tileGridLayout.removeAllViews(); | 259 parent.removeAllViews(); |
260 | 260 |
261 for (Tile tile : mTiles) { | 261 for (Tile tile : mTiles) { |
262 TileView tileView = oldTileViews.get(tile.getUrl()); | 262 TileView tileView = oldTileViews.get(tile.getUrl()); |
263 if (tileView == null) { | 263 if (tileView == null) { |
264 tileView = buildTileView( | 264 tileView = buildTileView(tile, parent, trackLoadTasks, condensed
); |
265 tile, tileGridLayout, trackLoadTasks, mTitleLinesCount,
condensed); | |
266 } else { | 265 } else { |
267 tileView.updateIfDataChanged(tile); | 266 tileView.updateIfDataChanged(tile); |
268 } | 267 } |
269 | 268 |
270 tileGridLayout.addView(tileView); | 269 parent.addView(tileView); |
271 } | 270 } |
272 } | 271 } |
273 | 272 |
274 public Tile[] getTiles() { | 273 public Tile[] getTiles() { |
275 return Arrays.copyOf(mTiles, mTiles.length); | 274 return Arrays.copyOf(mTiles, mTiles.length); |
276 } | 275 } |
277 | 276 |
278 public boolean hasReceivedData() { | 277 public boolean hasReceivedData() { |
279 return mHasReceivedData; | 278 return mHasReceivedData; |
280 } | 279 } |
281 | 280 |
282 /** | 281 /** |
283 * Inflates a new tile view, initializes it, and loads an icon for it. | 282 * Inflates a new tile view, initializes it, and loads an icon for it. |
284 * @param tile The tile that holds the data to populate the new tile view. | 283 * @param tile The tile that holds the data to populate the new tile view. |
285 * @param parentView The parent of the new tile view. | 284 * @param parentView The parent of the new tile view. |
286 * @param trackLoadTask Whether to track a load task. | 285 * @param trackLoadTask Whether to track a load task. |
287 * @param titleLines The number of text lines to use for each tile title. | |
288 * @param condensed Whether to use a condensed layout. | 286 * @param condensed Whether to use a condensed layout. |
289 * @return The new tile view. | 287 * @return The new tile view. |
290 */ | 288 */ |
291 private TileView buildTileView(Tile tile, ViewGroup parentView, boolean trac
kLoadTask, | 289 @VisibleForTesting |
292 int titleLines, boolean condensed) { | 290 TileView buildTileView( |
| 291 Tile tile, ViewGroup parentView, boolean trackLoadTask, boolean cond
ensed) { |
293 TileView tileView = (TileView) LayoutInflater.from(parentView.getContext
()) | 292 TileView tileView = (TileView) LayoutInflater.from(parentView.getContext
()) |
294 .inflate(R.layout.tile_view, parentView, fal
se); | 293 .inflate(R.layout.tile_view, parentView, fal
se); |
295 tileView.initialize(tile, titleLines, condensed); | 294 tileView.initialize(tile, mTitleLinesCount, condensed); |
296 | 295 |
297 // Note: It is important that the callbacks below don't keep a reference
to the tile or | 296 // Note: It is important that the callbacks below don't keep a reference
to the tile or |
298 // modify them as there is no guarantee that the same tile would be used
to update the view. | 297 // modify them as there is no guarantee that the same tile would be used
to update the view. |
299 LargeIconCallback iconCallback = new LargeIconCallbackImpl(tile.getUrl()
, trackLoadTask); | 298 LargeIconCallback iconCallback = new LargeIconCallbackImpl(tile.getUrl()
, trackLoadTask); |
300 if (trackLoadTask) mObserver.onLoadTaskAdded(); | 299 if (trackLoadTask) mObserver.onLoadTaskAdded(); |
301 if (!loadWhitelistIcon(tile, iconCallback)) { | 300 if (!loadWhitelistIcon(tile, iconCallback)) { |
302 mUiDelegate.getLargeIconForUrl(tile.getUrl(), mMinIconSize, iconCall
back); | 301 mUiDelegate.getLargeIconForUrl(tile.getUrl(), mMinIconSize, iconCall
back); |
303 } | 302 } |
304 | 303 |
305 TileInteractionDelegate delegate = new TileInteractionDelegate(tile.getU
rl()); | 304 TileInteractionDelegate delegate = new TileInteractionDelegate(tile.getU
rl()); |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
442 if (oldOfflineAvailable == tile.isOfflineAvailable()) return; | 441 if (oldOfflineAvailable == tile.isOfflineAvailable()) return; |
443 mObserver.onTileOfflineBadgeVisibilityChanged(tile); | 442 mObserver.onTileOfflineBadgeVisibilityChanged(tile); |
444 } | 443 } |
445 | 444 |
446 @Override | 445 @Override |
447 public Iterable<Tile> getOfflinableSuggestions() { | 446 public Iterable<Tile> getOfflinableSuggestions() { |
448 return Arrays.asList(mTiles); | 447 return Arrays.asList(mTiles); |
449 } | 448 } |
450 } | 449 } |
451 } | 450 } |
OLD | NEW |