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; |
10 import android.view.ContextMenu; | 11 import android.view.ContextMenu; |
11 import android.view.Menu; | 12 import android.view.Menu; |
12 import android.view.MenuItem; | 13 import android.view.MenuItem; |
13 import android.view.MenuItem.OnMenuItemClickListener; | 14 import android.view.MenuItem.OnMenuItemClickListener; |
14 import android.view.View; | 15 import android.view.View; |
15 | 16 |
16 import org.chromium.chrome.R; | 17 import org.chromium.chrome.R; |
17 import org.chromium.chrome.browser.ntp.NewTabPageView.NewTabPageManager; | 18 import org.chromium.chrome.browser.ntp.NewTabPageView.NewTabPageManager; |
18 import org.chromium.chrome.browser.ntp.snippets.SnippetsConfig; | 19 import org.chromium.chrome.browser.ntp.snippets.SnippetsConfig; |
19 import org.chromium.chrome.browser.offlinepages.OfflinePageBridge; | 20 import org.chromium.chrome.browser.offlinepages.OfflinePageBridge; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 } | 74 } |
74 | 75 |
75 /** | 76 /** |
76 * Populates the context menu. | 77 * Populates the context menu. |
77 * @param menu The menu to populate. | 78 * @param menu The menu to populate. |
78 * @param associatedView The view that requested a context menu. | 79 * @param associatedView The view that requested a context menu. |
79 * @param delegate Delegate that defines the configuration of the menu and w
hat to do when items | 80 * @param delegate Delegate that defines the configuration of the menu and w
hat to do when items |
80 * are tapped. | 81 * are tapped. |
81 */ | 82 */ |
82 public void createContextMenu(ContextMenu menu, View associatedView, Delegat
e delegate) { | 83 public void createContextMenu(ContextMenu menu, View associatedView, Delegat
e delegate) { |
83 OnMenuItemClickListener listener = new ItemClickListener(delegate); | 84 OnMenuItemClickListener listener = new ItemClickListener(delegate, assoc
iatedView); |
84 boolean hasItems = false; | 85 boolean hasItems = false; |
85 | 86 |
86 for (@ContextMenuItemId int itemId : MenuItemLabelMatcher.STRING_MAP.key
Set()) { | 87 for (@ContextMenuItemId int itemId : MenuItemLabelMatcher.STRING_MAP.key
Set()) { |
87 if (!shouldShowItem(itemId, delegate)) continue; | 88 if (!shouldShowItem(itemId, delegate)) continue; |
88 | 89 |
89 @StringRes | 90 @StringRes |
90 int itemString = MenuItemLabelMatcher.STRING_MAP.get(itemId); | 91 int itemString = MenuItemLabelMatcher.STRING_MAP.get(itemId); |
91 menu.add(Menu.NONE, itemId, Menu.NONE, itemString).setOnMenuItemClic
kListener(listener); | 92 menu.add(Menu.NONE, itemId, Menu.NONE, itemString).setOnMenuItemClic
kListener(listener); |
92 hasItems = true; | 93 hasItems = true; |
93 } | 94 } |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 put(ID_OPEN_IN_NEW_TAB, R.string.contextmenu_open_in_new_tab); | 161 put(ID_OPEN_IN_NEW_TAB, R.string.contextmenu_open_in_new_tab); |
161 put(ID_OPEN_IN_INCOGNITO_TAB, R.string.contextmenu_open_in_incog
nito_tab); | 162 put(ID_OPEN_IN_INCOGNITO_TAB, R.string.contextmenu_open_in_incog
nito_tab); |
162 put(ID_SAVE_FOR_OFFLINE, R.string.contextmenu_save_link); | 163 put(ID_SAVE_FOR_OFFLINE, R.string.contextmenu_save_link); |
163 put(ID_REMOVE, R.string.remove); | 164 put(ID_REMOVE, R.string.remove); |
164 } | 165 } |
165 }; | 166 }; |
166 } | 167 } |
167 | 168 |
168 private static class ItemClickListener implements OnMenuItemClickListener { | 169 private static class ItemClickListener implements OnMenuItemClickListener { |
169 private final Delegate mDelegate; | 170 private final Delegate mDelegate; |
| 171 private final View mAssociatedView; |
170 | 172 |
171 ItemClickListener(Delegate delegate) { | 173 ItemClickListener(Delegate delegate, View associatedView) { |
172 mDelegate = delegate; | 174 mDelegate = delegate; |
| 175 mAssociatedView = associatedView; |
173 } | 176 } |
174 | 177 |
175 @Override | 178 @Override |
176 public boolean onMenuItemClick(MenuItem item) { | 179 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 |
177 switch (item.getItemId()) { | 188 switch (item.getItemId()) { |
178 case ID_OPEN_IN_NEW_WINDOW: | 189 case ID_OPEN_IN_NEW_WINDOW: |
179 mDelegate.openItem(WindowOpenDisposition.NEW_WINDOW); | 190 mDelegate.openItem(WindowOpenDisposition.NEW_WINDOW); |
180 return true; | 191 return true; |
181 case ID_OPEN_IN_NEW_TAB: | 192 case ID_OPEN_IN_NEW_TAB: |
182 mDelegate.openItem(WindowOpenDisposition.NEW_FOREGROUND_TAB)
; | 193 mDelegate.openItem(WindowOpenDisposition.NEW_FOREGROUND_TAB)
; |
183 return true; | 194 return true; |
184 case ID_OPEN_IN_INCOGNITO_TAB: | 195 case ID_OPEN_IN_INCOGNITO_TAB: |
185 mDelegate.openItem(WindowOpenDisposition.OFF_THE_RECORD); | 196 mDelegate.openItem(WindowOpenDisposition.OFF_THE_RECORD); |
186 return true; | 197 return true; |
187 case ID_SAVE_FOR_OFFLINE: | 198 case ID_SAVE_FOR_OFFLINE: |
188 mDelegate.openItem(WindowOpenDisposition.SAVE_TO_DISK); | 199 mDelegate.openItem(WindowOpenDisposition.SAVE_TO_DISK); |
189 return true; | 200 return true; |
190 case ID_REMOVE: | 201 case ID_REMOVE: |
191 mDelegate.removeItem(); | 202 mDelegate.removeItem(); |
192 return true; | 203 return true; |
193 default: | 204 default: |
194 return false; | 205 return false; |
195 } | 206 } |
196 } | 207 } |
197 } | 208 } |
198 } | 209 } |
OLD | NEW |