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.ntp; |
6 | 6 |
7 import android.view.ContextMenu; | 7 import android.view.ContextMenu; |
8 import android.view.ContextMenu.ContextMenuInfo; | 8 import android.view.ContextMenu.ContextMenuInfo; |
9 import android.view.MenuItem; | 9 import android.view.MenuItem; |
10 import android.view.MenuItem.OnMenuItemClickListener; | 10 import android.view.MenuItem.OnMenuItemClickListener; |
11 import android.view.View; | 11 import android.view.View; |
12 import android.view.View.OnClickListener; | 12 import android.view.View.OnClickListener; |
13 import android.view.View.OnCreateContextMenuListener; | 13 import android.view.View.OnCreateContextMenuListener; |
14 | 14 |
15 /** | 15 /** |
16 * Displays the title, thumbnail, and favicon of a most visited page. The item c
an be clicked, or | 16 * Displays the title, thumbnail, and favicon of a most visited page. The item c
an be clicked, or |
17 * long-pressed to trigger a context menu with options to "open in new tab", "op
en in incognito | 17 * long-pressed to trigger a context menu with options to "open in new tab", "op
en in incognito |
18 * tab", or "remove". | 18 * tab", or "remove". |
19 */ | 19 */ |
20 public class MostVisitedItem implements OnCreateContextMenuListener, | 20 public class MostVisitedItem implements OnCreateContextMenuListener, |
21 MenuItem.OnMenuItemClickListener, OnClickListener { | 21 MenuItem.OnMenuItemClickListener, OnClickListener { |
22 | 22 |
23 private MostVisitedItemManager mManager; | |
24 private String mTitle; | |
25 private String mUrl; | |
26 private int mIndex; | |
27 private View mView; | |
28 | |
29 /** | 23 /** |
30 * Interface for an object that handles callbacks from a MostVisitedItem. | 24 * Interface for an object that handles callbacks from a MostVisitedItem. |
31 */ | 25 */ |
32 public interface MostVisitedItemManager { | 26 public interface MostVisitedItemManager { |
33 /** | 27 /** |
34 * Navigates to a most visited page in the existing tab. | 28 * Navigates to a most visited page in the existing tab. |
35 * @param item The most visited item to open. | 29 * @param item The most visited item to open. |
36 */ | 30 */ |
37 void open(MostVisitedItem item); | 31 void open(MostVisitedItem item); |
38 | 32 |
39 /** | 33 /** |
40 * Allows the manager to add context menu items for a given MostVisitedI
tem. | 34 * Allows the manager to add context menu items for a given MostVisitedI
tem. |
41 * @param menu The context menu that should be used to add menu items. | 35 * @param menu The context menu that should be used to add menu items. |
42 * @param listener Listener that should get the callbacks for context me
nu selections. | 36 * @param listener Listener that should get the callbacks for context me
nu selections. |
43 */ | 37 */ |
44 void onCreateContextMenu(ContextMenu menu, OnMenuItemClickListener liste
ner); | 38 void onCreateContextMenu(ContextMenu menu, OnMenuItemClickListener liste
ner); |
45 | 39 |
46 /** | 40 /** |
47 * Handles context menu item clicks. | 41 * Handles context menu item clicks. |
48 * @param menuId Id of the menu item that was selected. | 42 * @param menuId Id of the menu item that was selected. |
49 * @param item MostVisitedItem that triggered the context menu. | 43 * @param item MostVisitedItem that triggered the context menu. |
50 * @return Whether a menu item was selected successfully. | 44 * @return Whether a menu item was selected successfully. |
51 */ | 45 */ |
52 boolean onMenuItemClick(int menuId, MostVisitedItem item); | 46 boolean onMenuItemClick(int menuId, MostVisitedItem item); |
53 } | 47 } |
54 | 48 |
| 49 private MostVisitedItemManager mManager; |
| 50 private String mTitle; |
| 51 private String mUrl; |
| 52 private int mIndex; |
| 53 private int mTileType; |
| 54 private View mView; |
| 55 |
55 /** | 56 /** |
56 * Constructs a MostVisitedItem with the given manager, title, URL, index, a
nd view. | 57 * Constructs a MostVisitedItem with the given manager, title, URL, index, a
nd view. |
57 * | 58 * |
58 * @param manager The NewTabPageManager used to handle clicks and context me
nu events. | 59 * @param manager The NewTabPageManager used to handle clicks and context me
nu events. |
59 * @param title The title of the page. | 60 * @param title The title of the page. |
60 * @param url The URL of the page. | 61 * @param url The URL of the page. |
61 * @param index The index of this item in the list of most visited items. | 62 * @param index The index of this item in the list of most visited items. |
62 * @param view The View that will display the item. The MostVisitedItem will
handle clicks | |
63 * on this view. | |
64 */ | 63 */ |
65 public MostVisitedItem(MostVisitedItemManager manager, String title, String
url, int index, | 64 public MostVisitedItem(MostVisitedItemManager manager, String title, String
url, int index) { |
66 View view) { | |
67 mManager = manager; | 65 mManager = manager; |
68 mTitle = title; | 66 mTitle = title; |
69 mUrl = url; | 67 mUrl = url; |
70 mIndex = index; | 68 mIndex = index; |
| 69 mTileType = MostVisitedTileType.NONE; |
| 70 } |
| 71 |
| 72 /** |
| 73 * Sets the view that will display this item. MostVisitedItem will handle cl
icks on the view. |
| 74 * This should be called exactly once. |
| 75 */ |
| 76 public void initView(View view) { |
| 77 assert mView == null; |
71 mView = view; | 78 mView = view; |
72 mView.setOnClickListener(this); | 79 mView.setOnClickListener(this); |
73 mView.setOnCreateContextMenuListener(this); | 80 mView.setOnCreateContextMenuListener(this); |
74 } | 81 } |
75 | 82 |
76 /** | 83 /** |
77 * @return The view representing this item. | 84 * @return The view representing this item. |
78 */ | 85 */ |
79 public View getView() { | 86 public View getView() { |
80 return mView; | 87 return mView; |
(...skipping 20 matching lines...) Expand all Loading... |
101 return mIndex; | 108 return mIndex; |
102 } | 109 } |
103 | 110 |
104 /** | 111 /** |
105 * Updates this item's index in the list of most visited items. | 112 * Updates this item's index in the list of most visited items. |
106 */ | 113 */ |
107 public void setIndex(int index) { | 114 public void setIndex(int index) { |
108 mIndex = index; | 115 mIndex = index; |
109 } | 116 } |
110 | 117 |
| 118 /** |
| 119 * @return The visual type of this most visited item. Valid values are liste
d in |
| 120 * {@link MostVisitedTileType}. |
| 121 */ |
| 122 public int getTileType() { |
| 123 return mTileType; |
| 124 } |
| 125 |
| 126 /** |
| 127 * Sets the visual type of this most visited item. Valid values are listed i
n |
| 128 * {@link MostVisitedTileType}. |
| 129 */ |
| 130 public void setTileType(int type) { |
| 131 mTileType = type; |
| 132 } |
| 133 |
111 @Override | 134 @Override |
112 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo me
nuInfo) { | 135 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo me
nuInfo) { |
113 mManager.onCreateContextMenu(menu, this); | 136 mManager.onCreateContextMenu(menu, this); |
114 } | 137 } |
115 | 138 |
116 @Override | 139 @Override |
117 public boolean onMenuItemClick(MenuItem item) { | 140 public boolean onMenuItemClick(MenuItem item) { |
118 return mManager.onMenuItemClick(item.getItemId(), this); | 141 return mManager.onMenuItemClick(item.getItemId(), this); |
119 } | 142 } |
120 | 143 |
121 @Override | 144 @Override |
122 public void onClick(View v) { | 145 public void onClick(View v) { |
123 mManager.open(this); | 146 mManager.open(this); |
124 } | 147 } |
125 } | 148 } |
OLD | NEW |