OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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.app.Activity; | 7 import android.app.Activity; |
8 import android.support.annotation.IntDef; | 8 import android.support.annotation.IntDef; |
9 import android.support.annotation.StringRes; | 9 import android.support.annotation.StringRes; |
10 import android.support.v13.view.ViewCompat; | |
11 import android.view.ContextMenu; | 10 import android.view.ContextMenu; |
12 import android.view.Menu; | 11 import android.view.Menu; |
13 import android.view.MenuItem; | 12 import android.view.MenuItem; |
14 import android.view.MenuItem.OnMenuItemClickListener; | 13 import android.view.MenuItem.OnMenuItemClickListener; |
15 import android.view.View; | 14 import android.view.View; |
16 | 15 |
17 import org.chromium.chrome.R; | 16 import org.chromium.chrome.R; |
18 import org.chromium.chrome.browser.ntp.NewTabPageView.NewTabPageManager; | 17 import org.chromium.chrome.browser.ntp.NewTabPageView.NewTabPageManager; |
19 import org.chromium.chrome.browser.ntp.snippets.SnippetsConfig; | 18 import org.chromium.chrome.browser.ntp.snippets.SnippetsConfig; |
20 import org.chromium.chrome.browser.offlinepages.OfflinePageBridge; | 19 import org.chromium.chrome.browser.offlinepages.OfflinePageBridge; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 } | 73 } |
75 | 74 |
76 /** | 75 /** |
77 * Populates the context menu. | 76 * Populates the context menu. |
78 * @param menu The menu to populate. | 77 * @param menu The menu to populate. |
79 * @param associatedView The view that requested a context menu. | 78 * @param associatedView The view that requested a context menu. |
80 * @param delegate Delegate that defines the configuration of the menu and w
hat to do when items | 79 * @param delegate Delegate that defines the configuration of the menu and w
hat to do when items |
81 * are tapped. | 80 * are tapped. |
82 */ | 81 */ |
83 public void createContextMenu(ContextMenu menu, View associatedView, Delegat
e delegate) { | 82 public void createContextMenu(ContextMenu menu, View associatedView, Delegat
e delegate) { |
84 OnMenuItemClickListener listener = new ItemClickListener(delegate, assoc
iatedView); | 83 OnMenuItemClickListener listener = new ItemClickListener(delegate); |
85 boolean hasItems = false; | 84 boolean hasItems = false; |
86 | 85 |
87 for (@ContextMenuItemId int itemId : MenuItemLabelMatcher.STRING_MAP.key
Set()) { | 86 for (@ContextMenuItemId int itemId : MenuItemLabelMatcher.STRING_MAP.key
Set()) { |
88 if (!shouldShowItem(itemId, delegate)) continue; | 87 if (!shouldShowItem(itemId, delegate)) continue; |
89 | 88 |
90 @StringRes | 89 @StringRes |
91 int itemString = MenuItemLabelMatcher.STRING_MAP.get(itemId); | 90 int itemString = MenuItemLabelMatcher.STRING_MAP.get(itemId); |
92 menu.add(Menu.NONE, itemId, Menu.NONE, itemString).setOnMenuItemClic
kListener(listener); | 91 menu.add(Menu.NONE, itemId, Menu.NONE, itemString).setOnMenuItemClic
kListener(listener); |
93 hasItems = true; | 92 hasItems = true; |
94 } | 93 } |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 put(ID_OPEN_IN_NEW_TAB, R.string.contextmenu_open_in_new_tab); | 160 put(ID_OPEN_IN_NEW_TAB, R.string.contextmenu_open_in_new_tab); |
162 put(ID_OPEN_IN_INCOGNITO_TAB, R.string.contextmenu_open_in_incog
nito_tab); | 161 put(ID_OPEN_IN_INCOGNITO_TAB, R.string.contextmenu_open_in_incog
nito_tab); |
163 put(ID_SAVE_FOR_OFFLINE, R.string.contextmenu_save_link); | 162 put(ID_SAVE_FOR_OFFLINE, R.string.contextmenu_save_link); |
164 put(ID_REMOVE, R.string.remove); | 163 put(ID_REMOVE, R.string.remove); |
165 } | 164 } |
166 }; | 165 }; |
167 } | 166 } |
168 | 167 |
169 private static class ItemClickListener implements OnMenuItemClickListener { | 168 private static class ItemClickListener implements OnMenuItemClickListener { |
170 private final Delegate mDelegate; | 169 private final Delegate mDelegate; |
171 private final View mAssociatedView; | |
172 | 170 |
173 ItemClickListener(Delegate delegate, View associatedView) { | 171 ItemClickListener(Delegate delegate) { |
174 mDelegate = delegate; | 172 mDelegate = delegate; |
175 mAssociatedView = associatedView; | |
176 } | 173 } |
177 | 174 |
178 @Override | 175 @Override |
179 public boolean onMenuItemClick(MenuItem item) { | 176 public boolean onMenuItemClick(MenuItem item) { |
180 // If the user clicks a snippet then immediately long presses they w
ill create a context | |
181 // menu while the snippet's URL loads in the background. This means
that when they press | |
182 // an item on context menu the NTP will not actually be open. We add
this check here to | |
183 // prevent taking any action if the user has left the NTP. (https://
crbug.com/640468) | |
184 // Although the menu is supposed to be closed when we navigate away
from the NTP, we | |
185 // have to keep this check because of race conditions. (https://crbu
g.com/668945) | |
186 if (!ViewCompat.isAttachedToWindow(mAssociatedView)) return true; | |
187 | |
188 switch (item.getItemId()) { | 177 switch (item.getItemId()) { |
189 case ID_OPEN_IN_NEW_WINDOW: | 178 case ID_OPEN_IN_NEW_WINDOW: |
190 mDelegate.openItem(WindowOpenDisposition.NEW_WINDOW); | 179 mDelegate.openItem(WindowOpenDisposition.NEW_WINDOW); |
191 return true; | 180 return true; |
192 case ID_OPEN_IN_NEW_TAB: | 181 case ID_OPEN_IN_NEW_TAB: |
193 mDelegate.openItem(WindowOpenDisposition.NEW_FOREGROUND_TAB)
; | 182 mDelegate.openItem(WindowOpenDisposition.NEW_FOREGROUND_TAB)
; |
194 return true; | 183 return true; |
195 case ID_OPEN_IN_INCOGNITO_TAB: | 184 case ID_OPEN_IN_INCOGNITO_TAB: |
196 mDelegate.openItem(WindowOpenDisposition.OFF_THE_RECORD); | 185 mDelegate.openItem(WindowOpenDisposition.OFF_THE_RECORD); |
197 return true; | 186 return true; |
198 case ID_SAVE_FOR_OFFLINE: | 187 case ID_SAVE_FOR_OFFLINE: |
199 mDelegate.openItem(WindowOpenDisposition.SAVE_TO_DISK); | 188 mDelegate.openItem(WindowOpenDisposition.SAVE_TO_DISK); |
200 return true; | 189 return true; |
201 case ID_REMOVE: | 190 case ID_REMOVE: |
202 mDelegate.removeItem(); | 191 mDelegate.removeItem(); |
203 return true; | 192 return true; |
204 default: | 193 default: |
205 return false; | 194 return false; |
206 } | 195 } |
207 } | 196 } |
208 } | 197 } |
209 } | 198 } |
OLD | NEW |