| 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; | |
| 18 import org.chromium.chrome.browser.ntp.snippets.SnippetsConfig; | 17 import org.chromium.chrome.browser.ntp.snippets.SnippetsConfig; |
| 19 import org.chromium.chrome.browser.offlinepages.OfflinePageBridge; | 18 import org.chromium.chrome.browser.offlinepages.OfflinePageBridge; |
| 19 import org.chromium.chrome.browser.suggestions.SuggestionsNavigationDelegate; |
| 20 import org.chromium.ui.base.WindowAndroid.OnCloseContextMenuListener; | 20 import org.chromium.ui.base.WindowAndroid.OnCloseContextMenuListener; |
| 21 import org.chromium.ui.mojom.WindowOpenDisposition; | 21 import org.chromium.ui.mojom.WindowOpenDisposition; |
| 22 | 22 |
| 23 import java.lang.annotation.Retention; | 23 import java.lang.annotation.Retention; |
| 24 import java.lang.annotation.RetentionPolicy; | 24 import java.lang.annotation.RetentionPolicy; |
| 25 import java.util.Map; | 25 import java.util.Map; |
| 26 import java.util.TreeMap; | 26 import java.util.TreeMap; |
| 27 | 27 |
| 28 /** | 28 /** |
| 29 * Takes care of creating, closing a context menu and triaging the item clicks. | 29 * Takes care of creating, closing a context menu and triaging the item clicks. |
| 30 */ | 30 */ |
| 31 public class ContextMenuManager implements OnCloseContextMenuListener { | 31 public class ContextMenuManager implements OnCloseContextMenuListener { |
| 32 @IntDef({ID_OPEN_IN_NEW_WINDOW, ID_OPEN_IN_NEW_TAB, ID_OPEN_IN_INCOGNITO_TAB
, | 32 @IntDef({ID_OPEN_IN_NEW_WINDOW, ID_OPEN_IN_NEW_TAB, ID_OPEN_IN_INCOGNITO_TAB
, |
| 33 ID_SAVE_FOR_OFFLINE, ID_REMOVE}) | 33 ID_SAVE_FOR_OFFLINE, ID_REMOVE}) |
| 34 @Retention(RetentionPolicy.SOURCE) | 34 @Retention(RetentionPolicy.SOURCE) |
| 35 public @interface ContextMenuItemId {} | 35 public @interface ContextMenuItemId {} |
| 36 | 36 |
| 37 // The order of the items will be based on the value of their ID. So if new
items are added, | 37 // 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. | 38 // 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; | 39 public static final int ID_OPEN_IN_NEW_WINDOW = 0; |
| 40 public static final int ID_OPEN_IN_NEW_TAB = 1; | 40 public static final int ID_OPEN_IN_NEW_TAB = 1; |
| 41 public static final int ID_OPEN_IN_INCOGNITO_TAB = 2; | 41 public static final int ID_OPEN_IN_INCOGNITO_TAB = 2; |
| 42 public static final int ID_SAVE_FOR_OFFLINE = 3; | 42 public static final int ID_SAVE_FOR_OFFLINE = 3; |
| 43 public static final int ID_REMOVE = 4; | 43 public static final int ID_REMOVE = 4; |
| 44 | 44 |
| 45 private final Activity mActivity; | 45 private final Activity mActivity; |
| 46 private final NewTabPageManager mManager; | 46 private final SuggestionsNavigationDelegate mNavigationDelegate; |
| 47 private final TouchDisableableView mOuterView; | 47 private final TouchDisableableView mOuterView; |
| 48 private boolean mContextMenuOpen; | 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 /** | 58 /** |
| 59 * @return the URL of the current item for saving offline, or null if th
e item can't be | 59 * @return the URL of the current item for saving offline, or null if th
e item can't be |
| 60 * saved offline. | 60 * saved offline. |
| 61 */ | 61 */ |
| 62 String getUrl(); | 62 String getUrl(); |
| 63 | 63 |
| 64 /** @return whether the given menu item is supported. */ | 64 /** @return whether the given menu item is supported. */ |
| 65 boolean isItemSupported(@ContextMenuItemId int menuItemId); | 65 boolean isItemSupported(@ContextMenuItemId int menuItemId); |
| 66 | 66 |
| 67 /** Called when a context menu has been created. */ | 67 /** Called when a context menu has been created. */ |
| 68 void onContextMenuCreated(); | 68 void onContextMenuCreated(); |
| 69 } | 69 } |
| 70 | 70 |
| 71 /** Interface for a view that can be set to stop responding to touches. */ | 71 /** Interface for a view that can be set to stop responding to touches. */ |
| 72 public interface TouchDisableableView { void setTouchEnabled(boolean enabled
); } | 72 public interface TouchDisableableView { void setTouchEnabled(boolean enabled
); } |
| 73 | 73 |
| 74 public ContextMenuManager(Activity activity, NewTabPageManager newTabPageMan
ager, | 74 public ContextMenuManager(Activity activity, SuggestionsNavigationDelegate n
avigationDelegate, |
| 75 TouchDisableableView outerView) { | 75 TouchDisableableView outerView) { |
| 76 mActivity = activity; | 76 mActivity = activity; |
| 77 mManager = newTabPageManager; | 77 mNavigationDelegate = navigationDelegate; |
| 78 mOuterView = outerView; | 78 mOuterView = outerView; |
| 79 } | 79 } |
| 80 | 80 |
| 81 /** | 81 /** |
| 82 * Populates the context menu. | 82 * Populates the context menu. |
| 83 * | 83 * |
| 84 * @param menu The menu to populate. | 84 * @param menu The menu to populate. |
| 85 * @param associatedView The view that requested a context menu. | 85 * @param associatedView The view that requested a context menu. |
| 86 * @param delegate Delegate that defines the configuration of the menu and w
hat to do when items | 86 * @param delegate Delegate that defines the configuration of the menu and w
hat to do when items |
| 87 * are tapped. | 87 * are tapped. |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 /** Closes the context menu, if open. */ | 133 /** Closes the context menu, if open. */ |
| 134 public void closeContextMenu() { | 134 public void closeContextMenu() { |
| 135 mActivity.closeContextMenu(); | 135 mActivity.closeContextMenu(); |
| 136 } | 136 } |
| 137 | 137 |
| 138 private boolean shouldShowItem(@ContextMenuItemId int itemId, Delegate deleg
ate) { | 138 private boolean shouldShowItem(@ContextMenuItemId int itemId, Delegate deleg
ate) { |
| 139 if (!delegate.isItemSupported(itemId)) return false; | 139 if (!delegate.isItemSupported(itemId)) return false; |
| 140 | 140 |
| 141 switch (itemId) { | 141 switch (itemId) { |
| 142 case ID_OPEN_IN_NEW_WINDOW: | 142 case ID_OPEN_IN_NEW_WINDOW: |
| 143 return mManager.isOpenInNewWindowEnabled(); | 143 return mNavigationDelegate.isOpenInNewWindowEnabled(); |
| 144 case ID_OPEN_IN_NEW_TAB: | 144 case ID_OPEN_IN_NEW_TAB: |
| 145 return true; | 145 return true; |
| 146 case ID_OPEN_IN_INCOGNITO_TAB: | 146 case ID_OPEN_IN_INCOGNITO_TAB: |
| 147 return mManager.isOpenInIncognitoEnabled(); | 147 return mNavigationDelegate.isOpenInIncognitoEnabled(); |
| 148 case ID_SAVE_FOR_OFFLINE: { | 148 case ID_SAVE_FOR_OFFLINE: { |
| 149 if (!SnippetsConfig.isSaveToOfflineEnabled()) return false; | 149 if (!SnippetsConfig.isSaveToOfflineEnabled()) return false; |
| 150 String itemUrl = delegate.getUrl(); | 150 String itemUrl = delegate.getUrl(); |
| 151 return itemUrl != null && OfflinePageBridge.canSavePage(itemUrl)
; | 151 return itemUrl != null && OfflinePageBridge.canSavePage(itemUrl)
; |
| 152 } | 152 } |
| 153 case ID_REMOVE: | 153 case ID_REMOVE: |
| 154 return true; | 154 return true; |
| 155 | 155 |
| 156 default: | 156 default: |
| 157 assert false; | 157 assert false; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 return true; | 195 return true; |
| 196 case ID_REMOVE: | 196 case ID_REMOVE: |
| 197 mDelegate.removeItem(); | 197 mDelegate.removeItem(); |
| 198 return true; | 198 return true; |
| 199 default: | 199 default: |
| 200 return false; | 200 return false; |
| 201 } | 201 } |
| 202 } | 202 } |
| 203 } | 203 } |
| 204 } | 204 } |
| OLD | NEW |