Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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; | 5 package org.chromium.chrome.browser.suggestions; |
| 6 | 6 |
| 7 import android.view.ContextMenu; | 7 import android.graphics.drawable.Drawable; |
| 8 import android.view.ContextMenu.ContextMenuInfo; | 8 import android.support.annotation.Nullable; |
| 9 import android.view.View; | |
| 10 import android.view.View.OnClickListener; | |
| 11 import android.view.View.OnCreateContextMenuListener; | |
| 12 | 9 |
| 13 import org.chromium.chrome.browser.ntp.ContextMenuManager.ContextMenuItemId; | 10 import org.chromium.chrome.browser.ntp.MostVisitedTileType; |
| 14 import org.chromium.chrome.browser.ntp.NewTabPageView.NewTabPageManager; | |
| 15 import org.chromium.ui.mojom.WindowOpenDisposition; | |
| 16 | 11 |
| 17 /** | 12 /** |
| 18 * Displays the title, thumbnail, and favicon of a most visited page. The item c an be clicked, or | 13 * Holds the details of a site tile. |
| 19 * long-pressed to trigger a context menu with options to "open in new tab", "op en in incognito | |
| 20 * tab", or "remove". | |
| 21 */ | 14 */ |
| 22 public class MostVisitedItem implements OnCreateContextMenuListener, OnClickList ener { | 15 public class Tile { |
| 23 /** | 16 private final String mTitle; |
| 24 * Interface for an object that handles callbacks from a MostVisitedItem. | 17 private final String mUrl; |
| 25 */ | 18 private final String mWhitelistIconPath; |
| 26 public interface MostVisitedItemManager { | 19 private final boolean mOfflineAvailable; |
| 27 void removeMostVisitedItem(MostVisitedItem item); | 20 private int mIndex; |
| 21 private int mTileType = MostVisitedTileType.NONE; | |
| 22 private final int mSource; | |
| 28 | 23 |
| 29 void openMostVisitedItem(int windowDisposition, MostVisitedItem item); | 24 @Nullable |
| 30 } | 25 private Drawable mIcon; |
| 31 | |
| 32 private NewTabPageManager mManager; | |
| 33 private String mTitle; | |
| 34 private String mUrl; | |
| 35 private String mWhitelistIconPath; | |
| 36 private boolean mOfflineAvailable; | |
| 37 private int mIndex; | |
| 38 private int mTileType; | |
| 39 private int mSource; | |
| 40 private View mView; | |
| 41 | 26 |
| 42 /** | 27 /** |
| 43 * Constructs a MostVisitedItem with the given manager, title, URL, whitelis t icon path, index, | |
| 44 * and view. | |
| 45 * | |
| 46 * @param manager The NewTabPageManager used to handle clicks and context me nu events. | |
| 47 * @param title The title of the page. | 28 * @param title The title of the page. |
| 48 * @param url The URL of the page. | 29 * @param url The URL of the page. |
| 49 * @param whitelistIconPath The path to the icon image file, if this is a wh itelisted most | 30 * @param whitelistIconPath The path to the icon image file, if this is a wh itelisted tile. |
| 50 * visited item. Empty otherwise. | 31 * Empty otherwise. |
| 51 * @param offlineAvailable Whether there is an offline copy of the URL avail able. | 32 * @param offlineAvailable Whether there is an offline copy of the URL avail able. |
| 52 * @param index The index of this item in the list of most visited items. | 33 * @param index The index of this tile in the list of tiles. |
| 53 * @param source The {@code MostVisitedSource} that generated this item. | 34 * @param source The {@code MostVisitedSource} that generated this tile. |
| 54 */ | 35 */ |
| 55 public MostVisitedItem(NewTabPageManager manager, String title, String url, | 36 public Tile(String title, String url, String whitelistIconPath, boolean offl ineAvailable, |
| 56 String whitelistIconPath, boolean offlineAvailable, int index, int s ource) { | 37 int index, int source) { |
| 57 mManager = manager; | |
| 58 mTitle = title; | 38 mTitle = title; |
| 59 mUrl = url; | 39 mUrl = url; |
| 60 mWhitelistIconPath = whitelistIconPath; | 40 mWhitelistIconPath = whitelistIconPath; |
| 61 mOfflineAvailable = offlineAvailable; | 41 mOfflineAvailable = offlineAvailable; |
| 62 mIndex = index; | 42 mIndex = index; |
| 63 mTileType = MostVisitedTileType.NONE; | |
| 64 mSource = source; | 43 mSource = source; |
| 65 } | 44 } |
| 66 | 45 |
| 67 /** | 46 /** |
| 68 * Sets the view that will display this item. MostVisitedItem will handle cl icks on the view. | 47 * @return The URL of this tile. |
| 69 * This should be called exactly once. | |
| 70 */ | |
| 71 public void initView(View view) { | |
| 72 assert mView == null; | |
| 73 mView = view; | |
| 74 mView.setOnClickListener(this); | |
| 75 mView.setOnCreateContextMenuListener(this); | |
| 76 } | |
| 77 | |
| 78 /** | |
| 79 * @return The view representing this item. | |
| 80 */ | |
| 81 public View getView() { | |
| 82 return mView; | |
| 83 } | |
| 84 | |
| 85 /** | |
| 86 * @return The URL of this most visited item. | |
| 87 */ | 48 */ |
| 88 public String getUrl() { | 49 public String getUrl() { |
| 89 return mUrl; | 50 return mUrl; |
| 90 } | 51 } |
| 91 | 52 |
| 92 /** | 53 /** |
| 93 * @return The title of this most visited item. | 54 * @return The title of this tile. |
| 94 */ | 55 */ |
| 95 public String getTitle() { | 56 public String getTitle() { |
| 96 return mTitle; | 57 return mTitle; |
| 97 } | 58 } |
| 98 | 59 |
| 99 /** | 60 /** |
| 100 * @return The path of the whitelist icon associated with the URL. | 61 * @return The path of the whitelist icon associated with the URL. |
| 101 */ | 62 */ |
| 102 public String getWhitelistIconPath() { | 63 public String getWhitelistIconPath() { |
| 103 return mWhitelistIconPath; | 64 return mWhitelistIconPath; |
| 104 } | 65 } |
| 105 | 66 |
| 106 /** | 67 /** |
| 107 * @return Whether this item is available offline. | 68 * @return Whether this tile is available offline. |
| 108 */ | 69 */ |
| 109 public boolean isOfflineAvailable() { | 70 public boolean isOfflineAvailable() { |
| 110 return mOfflineAvailable; | 71 return mOfflineAvailable; |
| 111 } | 72 } |
| 112 | 73 |
| 113 /** | 74 /** |
| 114 * @return The index of this MostVisitedItem in the list of MostVisitedItems . | 75 * @return The index of this tile in the list of tiles. |
| 115 */ | 76 */ |
| 116 public int getIndex() { | 77 public int getIndex() { |
| 117 return mIndex; | 78 return mIndex; |
| 118 } | 79 } |
| 119 | 80 |
| 120 /** | 81 /** |
| 121 * Updates this item's index in the list of most visited items. | 82 * Updates this tile's index in the list of tiles. |
| 122 */ | 83 */ |
| 123 public void setIndex(int index) { | 84 public void setIndex(int index) { |
|
dgn
2017/01/31 18:21:35
is this another of this occurrences of "index" bei
Michael van Ouwerkerk
2017/02/03 12:37:48
These are always correct, the tiles are rebuilt /
| |
| 124 mIndex = index; | 85 mIndex = index; |
| 125 } | 86 } |
| 126 | 87 |
| 127 /** | 88 /** |
| 128 * @return The visual type of this most visited item. Valid values are liste d in | 89 * @return The visual type of this tile. Valid values are listed in |
| 129 * {@link MostVisitedTileType}. | 90 * {@link MostVisitedTileType}. |
| 130 */ | 91 */ |
| 131 public int getTileType() { | 92 public int getTileType() { |
| 132 return mTileType; | 93 return mTileType; |
| 133 } | 94 } |
| 134 | 95 |
| 135 /** | 96 /** |
| 136 * Sets the visual type of this most visited item. Valid values are listed i n | 97 * Sets the visual type of this tile. Valid values are listed in |
| 137 * {@link MostVisitedTileType}. | 98 * {@link MostVisitedTileType}. |
| 138 */ | 99 */ |
| 139 public void setTileType(int type) { | 100 public void setTileType(int type) { |
| 140 mTileType = type; | 101 mTileType = type; |
| 141 } | 102 } |
| 142 | 103 |
| 143 /** | 104 /** |
| 144 * @return The source of this item. Used for metrics tracking. Valid values are listed in | 105 * @return The source of this tile. Used for metrics tracking. Valid values are listed in |
| 145 * {@code MostVisitedSource}. | 106 * {@code MostVisitedSource}. |
| 146 */ | 107 */ |
| 147 public int getSource() { | 108 public int getSource() { |
| 148 return mSource; | 109 return mSource; |
| 149 } | 110 } |
| 150 | 111 |
| 151 @Override | 112 /** |
| 152 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo me nuInfo) { | 113 * @return The icon, may be null. |
| 153 mManager.getContextMenuManager().createContextMenu( | 114 */ |
| 154 menu, v, new ContextMenuManager.Delegate() { | 115 @Nullable |
| 155 @Override | 116 public Drawable getIcon() { |
| 156 public void openItem(int windowDisposition) { | 117 return mIcon; |
| 157 mManager.openMostVisitedItem(windowDisposition, MostVisi tedItem.this); | |
| 158 } | |
| 159 | |
| 160 @Override | |
| 161 public void removeItem() { | |
| 162 mManager.removeMostVisitedItem(MostVisitedItem.this); | |
| 163 } | |
| 164 | |
| 165 @Override | |
| 166 public String getUrl() { | |
| 167 return MostVisitedItem.this.getUrl(); | |
| 168 } | |
| 169 | |
| 170 @Override | |
| 171 public boolean isItemSupported(@ContextMenuItemId int menuIt emId) { | |
| 172 return true; | |
| 173 } | |
| 174 | |
| 175 @Override | |
| 176 public void onContextMenuCreated() {} | |
| 177 }); | |
| 178 } | 118 } |
| 179 | 119 |
| 180 @Override | 120 /** |
| 181 public void onClick(View v) { | 121 * Updates the icon drawable. |
| 182 mManager.openMostVisitedItem(WindowOpenDisposition.CURRENT_TAB, MostVisi tedItem.this); | 122 */ |
| 123 public void setIcon(@Nullable Drawable icon) { | |
| 124 mIcon = icon; | |
| 183 } | 125 } |
| 184 } | 126 } |
| OLD | NEW |