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.view.ContextMenu; | 10 import android.view.ContextMenu; |
11 import android.view.Menu; | 11 import android.view.Menu; |
12 import android.view.MenuItem; | 12 import android.view.MenuItem; |
13 import android.view.MenuItem.OnMenuItemClickListener; | 13 import android.view.MenuItem.OnMenuItemClickListener; |
14 import android.view.View; | 14 import android.view.View; |
15 | 15 |
16 import org.chromium.chrome.R; | 16 import org.chromium.chrome.R; |
17 import org.chromium.chrome.browser.ntp.NewTabPageView.NewTabPageManager; | 17 import org.chromium.chrome.browser.ntp.NewTabPageView.NewTabPageManager; |
18 import org.chromium.chrome.browser.ntp.snippets.SnippetsConfig; | 18 import org.chromium.chrome.browser.ntp.snippets.SnippetsConfig; |
19 import org.chromium.chrome.browser.offlinepages.OfflinePageBridge; | 19 import org.chromium.chrome.browser.offlinepages.OfflinePageBridge; |
| 20 import org.chromium.chrome.browser.tab.Tab; |
20 import org.chromium.ui.base.WindowAndroid.OnCloseContextMenuListener; | 21 import org.chromium.ui.base.WindowAndroid.OnCloseContextMenuListener; |
21 import org.chromium.ui.mojom.WindowOpenDisposition; | 22 import org.chromium.ui.mojom.WindowOpenDisposition; |
22 | 23 |
23 import java.lang.annotation.Retention; | 24 import java.lang.annotation.Retention; |
24 import java.lang.annotation.RetentionPolicy; | 25 import java.lang.annotation.RetentionPolicy; |
25 import java.util.Map; | 26 import java.util.Map; |
26 import java.util.TreeMap; | 27 import java.util.TreeMap; |
27 | 28 |
28 /** | 29 /** |
29 * Takes care of creating, closing a context menu and triaging the item clicks. | 30 * Takes care of creating, closing a context menu and triaging the item clicks. |
30 */ | 31 */ |
31 public class ContextMenuManager implements OnCloseContextMenuListener { | 32 public class ContextMenuManager implements OnCloseContextMenuListener { |
32 @IntDef({ID_OPEN_IN_NEW_WINDOW, ID_OPEN_IN_NEW_TAB, ID_OPEN_IN_INCOGNITO_TAB
, | 33 @IntDef({ID_OPEN_IN_NEW_WINDOW, ID_OPEN_IN_NEW_TAB, ID_OPEN_IN_INCOGNITO_TAB
, |
33 ID_SAVE_FOR_OFFLINE, ID_REMOVE}) | 34 ID_SAVE_FOR_OFFLINE, ID_REMOVE}) |
34 @Retention(RetentionPolicy.SOURCE) | 35 @Retention(RetentionPolicy.SOURCE) |
35 public @interface ContextMenuItemId {} | 36 public @interface ContextMenuItemId {} |
36 | 37 |
37 // The order of the items will be based on the value of their ID. So if new
items are added, | 38 // The order of the items will be based on the value of their ID. So if new
items are added, |
38 // the value of the existing ones should be modified so they stay in order. | 39 // the value of the existing ones should be modified so they stay in order. |
39 public static final int ID_OPEN_IN_NEW_WINDOW = 0; | 40 public static final int ID_OPEN_IN_NEW_WINDOW = 0; |
40 public static final int ID_OPEN_IN_NEW_TAB = 1; | 41 public static final int ID_OPEN_IN_NEW_TAB = 1; |
41 public static final int ID_OPEN_IN_INCOGNITO_TAB = 2; | 42 public static final int ID_OPEN_IN_INCOGNITO_TAB = 2; |
42 public static final int ID_SAVE_FOR_OFFLINE = 3; | 43 public static final int ID_SAVE_FOR_OFFLINE = 3; |
43 public static final int ID_REMOVE = 4; | 44 public static final int ID_REMOVE = 4; |
44 | 45 |
45 private final Activity mActivity; | |
46 private final NewTabPageManager mManager; | 46 private final NewTabPageManager mManager; |
| 47 private final Tab mTab; |
47 private final TouchDisableableView mOuterView; | 48 private final TouchDisableableView mOuterView; |
48 private boolean mContextMenuOpen; | |
49 | 49 |
50 /** Defines callback to configure the context menu and respond to user inter
action. */ | 50 /** Defines callback to configure the context menu and respond to user inter
action. */ |
51 public interface Delegate { | 51 public interface Delegate { |
52 /** Opens the current item the way specified by {@code windowDisposition
}. */ | 52 /** Opens the current item the way specified by {@code windowDisposition
}. */ |
53 void openItem(int windowDisposition); | 53 void openItem(int windowDisposition); |
54 | 54 |
55 /** Remove the current item. */ | 55 /** Remove the current item. */ |
56 void removeItem(); | 56 void removeItem(); |
57 | 57 |
58 /** @return whether the current item can be saved offline. */ | 58 /** @return whether the current item can be saved offline. */ |
59 String getUrl(); | 59 String getUrl(); |
60 | 60 |
61 /** @return whether the given menu item is supported. */ | 61 /** @return whether the given menu item is supported. */ |
62 boolean isItemSupported(@ContextMenuItemId int menuItemId); | 62 boolean isItemSupported(@ContextMenuItemId int menuItemId); |
63 } | 63 } |
64 | 64 |
65 /** Interface for a view that can be set to stop responding to touches. */ | 65 /** Interface for a view that can be set to stop responding to touches. */ |
66 public interface TouchDisableableView { void setTouchEnabled(boolean enabled
); } | 66 public interface TouchDisableableView { void setTouchEnabled(boolean enabled
); } |
67 | 67 |
68 public ContextMenuManager(Activity activity, NewTabPageManager newTabPageMan
ager, | 68 public ContextMenuManager( |
69 TouchDisableableView outerView) { | 69 NewTabPageManager newTabPageManager, Tab tab, TouchDisableableView o
uterView) { |
70 mActivity = activity; | |
71 mManager = newTabPageManager; | 70 mManager = newTabPageManager; |
| 71 mTab = tab; |
72 mOuterView = outerView; | 72 mOuterView = outerView; |
73 } | 73 } |
74 | 74 |
75 /** | 75 /** |
76 * Populates the context menu. | 76 * Populates the context menu. |
77 * @param menu The menu to populate. | 77 * @param menu The menu to populate. |
78 * @param associatedView The view that requested a context menu. | 78 * @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 | 79 * @param delegate Delegate that defines the configuration of the menu and w
hat to do when items |
80 * are tapped. | 80 * are tapped. |
81 */ | 81 */ |
(...skipping 22 matching lines...) Expand all Loading... |
104 closeContextMenu(); | 104 closeContextMenu(); |
105 view.removeOnAttachStateChangeListener(this); | 105 view.removeOnAttachStateChangeListener(this); |
106 } | 106 } |
107 }); | 107 }); |
108 | 108 |
109 // Disable touch events on the outer view while the context menu is open
. This is to | 109 // Disable touch events on the outer view while the context menu is open
. This is to |
110 // prevent the user long pressing to get the context menu then on the sa
me press scrolling | 110 // prevent the user long pressing to get the context menu then on the sa
me press scrolling |
111 // or swiping to dismiss an item (eg. https://crbug.com/638854, https://
crbug.com/638555, | 111 // or swiping to dismiss an item (eg. https://crbug.com/638854, https://
crbug.com/638555, |
112 // https://crbug.com/636296) | 112 // https://crbug.com/636296) |
113 mOuterView.setTouchEnabled(false); | 113 mOuterView.setTouchEnabled(false); |
114 mContextMenuOpen = true; | 114 |
| 115 mTab.getWindowAndroid().addContextMenuCloseListener(this); |
115 } | 116 } |
116 | 117 |
117 @Override | 118 @Override |
118 public void onContextMenuClosed() { | 119 public void onContextMenuClosed() { |
119 if (!mContextMenuOpen) return; | |
120 mOuterView.setTouchEnabled(true); | 120 mOuterView.setTouchEnabled(true); |
121 mContextMenuOpen = false; | 121 mTab.getWindowAndroid().removeContextMenuCloseListener(this); |
122 } | 122 } |
123 | 123 |
124 /** Closes the context menu, if open. */ | 124 /** Closes the context menu, if open. */ |
125 public void closeContextMenu() { | 125 public void closeContextMenu() { |
126 mActivity.closeContextMenu(); | 126 Activity activity = mTab.getWindowAndroid().getActivity().get(); |
| 127 if (activity == null) return; |
| 128 |
| 129 activity.closeContextMenu(); |
127 } | 130 } |
128 | 131 |
129 private boolean shouldShowItem(@ContextMenuItemId int itemId, Delegate deleg
ate) { | 132 private boolean shouldShowItem(@ContextMenuItemId int itemId, Delegate deleg
ate) { |
130 if (!delegate.isItemSupported(itemId)) return false; | 133 if (!delegate.isItemSupported(itemId)) return false; |
131 | 134 |
132 switch (itemId) { | 135 switch (itemId) { |
133 case ID_OPEN_IN_NEW_WINDOW: | 136 case ID_OPEN_IN_NEW_WINDOW: |
134 return mManager.isOpenInNewWindowEnabled(); | 137 return mManager.isOpenInNewWindowEnabled(); |
135 case ID_OPEN_IN_NEW_TAB: | 138 case ID_OPEN_IN_NEW_TAB: |
136 return true; | 139 return true; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 return true; | 189 return true; |
187 case ID_REMOVE: | 190 case ID_REMOVE: |
188 mDelegate.removeItem(); | 191 mDelegate.removeItem(); |
189 return true; | 192 return true; |
190 default: | 193 default: |
191 return false; | 194 return false; |
192 } | 195 } |
193 } | 196 } |
194 } | 197 } |
195 } | 198 } |
OLD | NEW |