OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.appmenu; |
| 6 |
| 7 import android.text.TextUtils; |
| 8 import android.view.Menu; |
| 9 import android.view.MenuItem; |
| 10 |
| 11 import com.google.android.apps.chrome.R; |
| 12 |
| 13 import org.chromium.chrome.browser.BookmarkUtils; |
| 14 import org.chromium.chrome.browser.BookmarksBridge; |
| 15 import org.chromium.chrome.browser.ChromeActivity; |
| 16 import org.chromium.chrome.browser.ChromeBrowserProviderClient; |
| 17 import org.chromium.chrome.browser.Tab; |
| 18 import org.chromium.chrome.browser.UrlConstants; |
| 19 import org.chromium.chrome.browser.preferences.ManagedPreferencesUtils; |
| 20 import org.chromium.chrome.browser.preferences.PrefServiceBridge; |
| 21 import org.chromium.chrome.browser.share.ShareHelper; |
| 22 import org.chromium.chrome.browser.util.FeatureUtilities; |
| 23 import org.chromium.components.dom_distiller.core.DomDistillerUrlUtils; |
| 24 import org.chromium.printing.PrintingController; |
| 25 |
| 26 /** |
| 27 * App Menu helper that handles hiding and showing menu items based on activity
state. |
| 28 */ |
| 29 public class ChromeAppMenuPropertiesDelegate implements AppMenuPropertiesDelegat
e { |
| 30 // Indices for different levels in drawable.btn_reload_stop. |
| 31 // Used only when preparing menu and refresh reload button in menu when tab |
| 32 // page load status changes. |
| 33 private static final int RELOAD_BUTTON_LEVEL_RELOAD = 0; |
| 34 private static final int RELOAD_BUTTON_LEVEL_STOP_LOADING = 1; |
| 35 |
| 36 protected MenuItem mReloadMenuItem; |
| 37 |
| 38 protected final ChromeActivity mActivity; |
| 39 |
| 40 public ChromeAppMenuPropertiesDelegate(ChromeActivity activity) { |
| 41 mActivity = activity; |
| 42 } |
| 43 |
| 44 @Override |
| 45 public boolean shouldShowAppMenu() { |
| 46 return mActivity.shouldShowAppMenu(); |
| 47 } |
| 48 |
| 49 /** |
| 50 * Called every time the menu is shown. This assumes that the provided menu
contains all the |
| 51 * items expected in the application menu (i.e. that the main menu has been
inflated into it). |
| 52 */ |
| 53 @Override |
| 54 public void prepareMenu(Menu menu) { |
| 55 // Exactly one of these will be true, depending on the type of menu show
ing. |
| 56 boolean isPageMenu; |
| 57 boolean isOverviewMenu; |
| 58 boolean isTabletEmptyModeMenu; |
| 59 |
| 60 boolean isOverview = mActivity.isInOverviewMode(); |
| 61 boolean isIncognito = mActivity.getCurrentTabModel().isIncognito(); |
| 62 Tab currentTab = mActivity.getActivityTab(); |
| 63 |
| 64 // Determine which menu to show. |
| 65 if (mActivity.isTablet()) { |
| 66 isPageMenu = !isOverview && mActivity.getCurrentTabModel().getCount(
) != 0; |
| 67 isOverviewMenu = isOverview && mActivity.getCurrentTabModel().getCou
nt() != 0; |
| 68 isTabletEmptyModeMenu = !isPageMenu && mActivity.getCurrentTabModel(
).getCount() == 0; |
| 69 } else { |
| 70 isPageMenu = !isOverview; |
| 71 isOverviewMenu = isOverview; |
| 72 isTabletEmptyModeMenu = false; |
| 73 } |
| 74 |
| 75 menu.setGroupVisible(R.id.PAGE_MENU, isPageMenu); |
| 76 menu.setGroupVisible(R.id.OVERVIEW_MODE_MENU, isOverviewMenu); |
| 77 menu.setGroupVisible(R.id.TABLET_EMPTY_MODE_MENU, isTabletEmptyModeMenu)
; |
| 78 |
| 79 if (isPageMenu && currentTab != null) { |
| 80 String url = currentTab.getUrl(); |
| 81 boolean isChromeScheme = url.startsWith(UrlConstants.CHROME_SCHEME) |
| 82 || url.startsWith(UrlConstants.CHROME_NATIVE_SCHEME); |
| 83 |
| 84 // Update the icon row items (not shown on tablet). |
| 85 menu.findItem(R.id.icon_row_menu_id).setVisible(!mActivity.isTablet(
)); |
| 86 if (!mActivity.isTablet()) { |
| 87 // Disable the "Forward" menu item if there is no page to go to. |
| 88 MenuItem forwardMenuItem = menu.findItem(R.id.forward_menu_id); |
| 89 forwardMenuItem.setEnabled(currentTab.canGoForward()); |
| 90 |
| 91 mReloadMenuItem = menu.findItem(R.id.reload_menu_id); |
| 92 mReloadMenuItem.setIcon(R.drawable.btn_reload_stop); |
| 93 |
| 94 // Update the loading state of mReloadMenuItem. |
| 95 if (currentTab.isLoading()) loadingStateChanged(true); |
| 96 MenuItem bookmarkMenuItem = menu.findItem(R.id.bookmark_this_pag
e_id); |
| 97 bookmarkMenuItem.setEnabled(BookmarksBridge.isEditBookmarksEnabl
ed( |
| 98 mActivity.getActivityTab().getProfile().getOriginalProfi
le())); |
| 99 if (currentTab.getBookmarkId() != ChromeBrowserProviderClient.IN
VALID_BOOKMARK_ID) { |
| 100 bookmarkMenuItem.setIcon(R.drawable.btn_star_filled); |
| 101 bookmarkMenuItem.setChecked(true); |
| 102 bookmarkMenuItem.setTitleCondensed(mActivity.getString(R.str
ing.edit_bookmark)); |
| 103 } else { |
| 104 bookmarkMenuItem.setIcon(R.drawable.btn_star); |
| 105 bookmarkMenuItem.setChecked(false); |
| 106 bookmarkMenuItem.setTitleCondensed(null); |
| 107 } |
| 108 } |
| 109 |
| 110 // Hide "Recent tabs" in incognito mode or when sync can't be enable
d. |
| 111 MenuItem recentTabsMenuItem = menu.findItem(R.id.recent_tabs_menu_id
); |
| 112 recentTabsMenuItem.setVisible(!isIncognito && FeatureUtilities.canAl
lowSync(mActivity)); |
| 113 recentTabsMenuItem.setTitle(R.string.menu_recent_tabs); |
| 114 |
| 115 // Don't allow "chrome://" pages to be shared. |
| 116 menu.findItem(R.id.share_row_menu_id).setVisible(!isChromeScheme); |
| 117 |
| 118 ShareHelper.configureDirectShareMenuItem( |
| 119 mActivity, menu.findItem(R.id.direct_share_menu_id)); |
| 120 |
| 121 // Disable find in page on the native NTP. |
| 122 menu.findItem(R.id.find_in_page_id).setVisible( |
| 123 !currentTab.isNativePage() && currentTab.getWebContents() !=
null); |
| 124 |
| 125 // Hide 'Add to homescreen' on all chrome:// pages -- Android doesn'
t know how to direct |
| 126 // those URLs. Also hide it on incognito pages to avoid problems wh
ere users create |
| 127 // shortcuts in incognito mode and then open the webapp in regular m
ode. Also check if |
| 128 // creating shortcuts is supported at all. |
| 129 MenuItem homescreenItem = menu.findItem(R.id.add_to_homescreen_id); |
| 130 boolean canAddShortcutToHomescreen = |
| 131 BookmarkUtils.isAddToHomeIntentSupported(mActivity); |
| 132 homescreenItem.setVisible( |
| 133 canAddShortcutToHomescreen && !isChromeScheme && !isIncognit
o); |
| 134 |
| 135 // Hide request desktop site on all chrome:// pages except for the N
TP. Check request |
| 136 // desktop site if it's activated on this page. |
| 137 MenuItem requestItem = menu.findItem(R.id.request_desktop_site_id); |
| 138 requestItem.setVisible(!isChromeScheme || currentTab.isNativePage())
; |
| 139 requestItem.setChecked(currentTab.getUseDesktopUserAgent()); |
| 140 requestItem.setTitleCondensed(requestItem.isChecked() |
| 141 ? mActivity.getString(R.string.menu_request_desktop_site_on) |
| 142 : mActivity.getString(R.string.menu_request_desktop_site_off
)); |
| 143 |
| 144 PrintingController printingController = |
| 145 mActivity.getChromeApplication().getPrintingController(); |
| 146 disableEnableMenuItem(menu, R.id.print_id, |
| 147 printingController != null && !currentTab.isNativePage(), |
| 148 printingController != null && !printingController.isBusy() |
| 149 && PrefServiceBridge.getInstance().isPrintingEnabled
(), |
| 150 PrefServiceBridge.getInstance().isPrintingManaged()); |
| 151 |
| 152 // Only display reader mode settings menu option if the current page
is in reader mode. |
| 153 menu.findItem(R.id.reader_mode_prefs_id) |
| 154 .setVisible(DomDistillerUrlUtils.isDistilledPage(currentTab.
getUrl())); |
| 155 } |
| 156 |
| 157 if (isOverviewMenu) { |
| 158 if (isIncognito) { |
| 159 // Hide normal close all tabs item. |
| 160 menu.findItem(R.id.close_all_tabs_menu_id).setVisible(false); |
| 161 // Enable close incognito tabs only if there are incognito tabs. |
| 162 menu.findItem(R.id.close_all_incognito_tabs_menu_id).setEnabled(
true); |
| 163 } else { |
| 164 // Hide close incognito tabs item. |
| 165 menu.findItem(R.id.close_all_incognito_tabs_menu_id).setVisible(
false); |
| 166 // Enable close all tabs if there are normal tabs or incognito t
abs. |
| 167 menu.findItem(R.id.close_all_tabs_menu_id).setEnabled( |
| 168 mActivity.getTabModelSelector().getTotalTabCount() > 0); |
| 169 } |
| 170 } |
| 171 |
| 172 // Incognito NTP in Document mode should not show "New incognito tab" me
nu item. |
| 173 boolean incognitoItemVisible = !FeatureUtilities.isDocumentMode(mActivit
y) |
| 174 || (currentTab == null) |
| 175 || !(TextUtils.equals(currentTab.getUrl(), UrlConstants.NTP_URL)
&& isIncognito); |
| 176 |
| 177 // Disable new incognito tab when it is blocked (e.g. by a policy). |
| 178 // findItem(...).setEnabled(...)" is not enough here, because of the inf
lated |
| 179 // main_menu.xml contains multiple items with the same id in different g
roups |
| 180 // e.g.: new_incognito_tab_menu_id. |
| 181 disableEnableMenuItem(menu, R.id.new_incognito_tab_menu_id, |
| 182 incognitoItemVisible, |
| 183 PrefServiceBridge.getInstance().isIncognitoModeEnabled(), |
| 184 PrefServiceBridge.getInstance().isIncognitoModeManaged()); |
| 185 mActivity.prepareMenu(menu); |
| 186 } |
| 187 |
| 188 /** |
| 189 * Notify the delegate that the load state changed. |
| 190 * @param isLoading Whether the page is currently loading. |
| 191 */ |
| 192 public void loadingStateChanged(boolean isLoading) { |
| 193 if (mReloadMenuItem != null) { |
| 194 mReloadMenuItem.getIcon().setLevel(isLoading |
| 195 ? RELOAD_BUTTON_LEVEL_STOP_LOADING : RELOAD_BUTTON_LEVEL_REL
OAD); |
| 196 mReloadMenuItem.setTitle(isLoading |
| 197 ? R.string.accessibility_btn_stop_loading : R.string.accessi
bility_btn_refresh); |
| 198 } |
| 199 } |
| 200 |
| 201 /** |
| 202 * Notify the delegate that menu was dismissed. |
| 203 */ |
| 204 public void onMenuDismissed() { |
| 205 mReloadMenuItem = null; |
| 206 } |
| 207 |
| 208 // Set enabled to be |enable| for all MenuItems with |id| in |menu|. |
| 209 // If |managed| is true then the "Managed By Enterprise" icon is shown next
to the menu. |
| 210 private void disableEnableMenuItem( |
| 211 Menu menu, int id, boolean visible, boolean enabled, boolean managed
) { |
| 212 for (int i = 0; i < menu.size(); ++i) { |
| 213 MenuItem item = menu.getItem(i); |
| 214 if (item.getItemId() == id && item.isVisible()) { |
| 215 item.setVisible(visible); |
| 216 item.setEnabled(enabled); |
| 217 if (managed) { |
| 218 item.setIcon(ManagedPreferencesUtils.getManagedByEnterpriseI
conId()); |
| 219 } else { |
| 220 item.setIcon(null); |
| 221 } |
| 222 } |
| 223 } |
| 224 } |
| 225 } |
OLD | NEW |