OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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.appmenu; | 5 package org.chromium.chrome.browser.appmenu; |
6 | 6 |
| 7 import android.content.Context; |
| 8 import android.content.pm.ResolveInfo; |
7 import android.graphics.PorterDuff; | 9 import android.graphics.PorterDuff; |
8 import android.graphics.drawable.Drawable; | 10 import android.graphics.drawable.Drawable; |
| 11 import android.os.SystemClock; |
9 import android.view.Menu; | 12 import android.view.Menu; |
10 import android.view.MenuItem; | 13 import android.view.MenuItem; |
11 | 14 |
12 import org.chromium.base.ApiCompatibilityUtils; | 15 import org.chromium.base.ApiCompatibilityUtils; |
13 import org.chromium.base.CommandLine; | 16 import org.chromium.base.CommandLine; |
| 17 import org.chromium.base.ContextUtils; |
| 18 import org.chromium.base.metrics.RecordHistogram; |
14 import org.chromium.chrome.R; | 19 import org.chromium.chrome.R; |
15 import org.chromium.chrome.browser.ChromeActivity; | 20 import org.chromium.chrome.browser.ChromeActivity; |
16 import org.chromium.chrome.browser.ChromeFeatureList; | 21 import org.chromium.chrome.browser.ChromeFeatureList; |
17 import org.chromium.chrome.browser.ChromeSwitches; | 22 import org.chromium.chrome.browser.ChromeSwitches; |
18 import org.chromium.chrome.browser.ShortcutHelper; | 23 import org.chromium.chrome.browser.ShortcutHelper; |
19 import org.chromium.chrome.browser.UrlConstants; | 24 import org.chromium.chrome.browser.UrlConstants; |
20 import org.chromium.chrome.browser.banners.AppBannerManager; | 25 import org.chromium.chrome.browser.banners.AppBannerManager; |
21 import org.chromium.chrome.browser.bookmarks.BookmarkBridge; | 26 import org.chromium.chrome.browser.bookmarks.BookmarkBridge; |
22 import org.chromium.chrome.browser.download.DownloadUtils; | 27 import org.chromium.chrome.browser.download.DownloadUtils; |
23 import org.chromium.chrome.browser.multiwindow.MultiWindowUtils; | 28 import org.chromium.chrome.browser.multiwindow.MultiWindowUtils; |
24 import org.chromium.chrome.browser.omaha.UpdateMenuItemHelper; | 29 import org.chromium.chrome.browser.omaha.UpdateMenuItemHelper; |
25 import org.chromium.chrome.browser.preferences.ManagedPreferencesUtils; | 30 import org.chromium.chrome.browser.preferences.ManagedPreferencesUtils; |
26 import org.chromium.chrome.browser.preferences.PrefServiceBridge; | 31 import org.chromium.chrome.browser.preferences.PrefServiceBridge; |
27 import org.chromium.chrome.browser.share.ShareHelper; | 32 import org.chromium.chrome.browser.share.ShareHelper; |
28 import org.chromium.chrome.browser.tab.Tab; | 33 import org.chromium.chrome.browser.tab.Tab; |
| 34 import org.chromium.chrome.browser.webapps.ChromeWebApkHost; |
29 import org.chromium.components.dom_distiller.core.DomDistillerUrlUtils; | 35 import org.chromium.components.dom_distiller.core.DomDistillerUrlUtils; |
30 import org.chromium.ui.base.DeviceFormFactor; | 36 import org.chromium.ui.base.DeviceFormFactor; |
| 37 import org.chromium.webapk.lib.client.WebApkValidator; |
| 38 |
| 39 import java.util.concurrent.TimeUnit; |
31 | 40 |
32 /** | 41 /** |
33 * App Menu helper that handles hiding and showing menu items based on activity
state. | 42 * App Menu helper that handles hiding and showing menu items based on activity
state. |
34 */ | 43 */ |
35 public class AppMenuPropertiesDelegate { | 44 public class AppMenuPropertiesDelegate { |
36 // Indices for different levels in drawable.btn_reload_stop. | 45 // Indices for different levels in drawable.btn_reload_stop. |
37 // Used only when preparing menu and refresh reload button in menu when tab | 46 // Used only when preparing menu and refresh reload button in menu when tab |
38 // page load status changes. | 47 // page load status changes. |
39 private static final int RELOAD_BUTTON_LEVEL_RELOAD = 0; | 48 private static final int RELOAD_BUTTON_LEVEL_RELOAD = 0; |
40 private static final int RELOAD_BUTTON_LEVEL_STOP_LOADING = 1; | 49 private static final int RELOAD_BUTTON_LEVEL_STOP_LOADING = 1; |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 // Hide 'Add to homescreen' for the following: | 164 // Hide 'Add to homescreen' for the following: |
156 // * chrome:// pages - Android doesn't know how to direct those URLs
. | 165 // * chrome:// pages - Android doesn't know how to direct those URLs
. |
157 // * incognito pages - To avoid problems where users create shortcut
s in incognito | 166 // * incognito pages - To avoid problems where users create shortcut
s in incognito |
158 // mode and then open the webapp in regular mod
e. | 167 // mode and then open the webapp in regular mod
e. |
159 // * file:// - After API 24, file: URIs are not supported in VIEW in
tents and thus | 168 // * file:// - After API 24, file: URIs are not supported in VIEW in
tents and thus |
160 // can not be added to the homescreen. | 169 // can not be added to the homescreen. |
161 // * content:// - Accessing external content URIs requires the calli
ng app to grant | 170 // * content:// - Accessing external content URIs requires the calli
ng app to grant |
162 // access to the resource via FLAG_GRANT_READ_URI_PER
MISSION, and that | 171 // access to the resource via FLAG_GRANT_READ_URI_PER
MISSION, and that |
163 // is not persisted when adding to the homescreen. | 172 // is not persisted when adding to the homescreen. |
164 // * If creating shortcuts it not supported by the current home scre
en. | 173 // * If creating shortcuts it not supported by the current home scre
en. |
165 MenuItem homescreenItem = menu.findItem(R.id.add_to_homescreen_id); | 174 boolean canShowHomeScreenMenuItem = ShortcutHelper.isAddToHomeIntent
Supported() |
166 boolean homescreenItemVisible = ShortcutHelper.isAddToHomeIntentSupp
orted() | |
167 && !isChromeScheme && !isFileScheme && !isContentScheme && !
isIncognito; | 175 && !isChromeScheme && !isFileScheme && !isContentScheme && !
isIncognito; |
168 if (homescreenItemVisible) { | 176 prepareAddToHomescreenMenuItem(menu, currentTab.getUrl(), canShowHom
eScreenMenuItem); |
169 homescreenItem.setTitle(AppBannerManager.getHomescreenLanguageOp
tion()); | |
170 } | |
171 homescreenItem.setVisible(homescreenItemVisible); | |
172 | 177 |
173 // Hide request desktop site on all chrome:// pages except for the N
TP. Check request | 178 // Hide request desktop site on all chrome:// pages except for the N
TP. Check request |
174 // desktop site if it's activated on this page. | 179 // desktop site if it's activated on this page. |
175 MenuItem requestItem = menu.findItem(R.id.request_desktop_site_id); | 180 MenuItem requestItem = menu.findItem(R.id.request_desktop_site_id); |
176 updateRequestDesktopSiteMenuItem(requestItem, currentTab); | 181 updateRequestDesktopSiteMenuItem(requestItem, currentTab); |
177 | 182 |
178 // Only display reader mode settings menu option if the current page
is in reader mode. | 183 // Only display reader mode settings menu option if the current page
is in reader mode. |
179 menu.findItem(R.id.reader_mode_prefs_id) | 184 menu.findItem(R.id.reader_mode_prefs_id) |
180 .setVisible(DomDistillerUrlUtils.isDistilledPage(currentTab.
getUrl())); | 185 .setVisible(DomDistillerUrlUtils.isDistilledPage(currentTab.
getUrl())); |
181 | 186 |
(...skipping 29 matching lines...) Expand all Loading... |
211 // main_menu.xml contains multiple items with the same id in different g
roups | 216 // main_menu.xml contains multiple items with the same id in different g
roups |
212 // e.g.: new_incognito_tab_menu_id. | 217 // e.g.: new_incognito_tab_menu_id. |
213 disableEnableMenuItem(menu, R.id.new_incognito_tab_menu_id, | 218 disableEnableMenuItem(menu, R.id.new_incognito_tab_menu_id, |
214 true, | 219 true, |
215 PrefServiceBridge.getInstance().isIncognitoModeEnabled(), | 220 PrefServiceBridge.getInstance().isIncognitoModeEnabled(), |
216 PrefServiceBridge.getInstance().isIncognitoModeManaged()); | 221 PrefServiceBridge.getInstance().isIncognitoModeManaged()); |
217 mActivity.prepareMenu(menu); | 222 mActivity.prepareMenu(menu); |
218 } | 223 } |
219 | 224 |
220 /** | 225 /** |
| 226 * Sets the visibility and labels of the "Add to Home screen" and "Open WebA
PK" menu items. |
| 227 */ |
| 228 protected void prepareAddToHomescreenMenuItem( |
| 229 Menu menu, String url, boolean canShowHomeScreenMenuItem) { |
| 230 MenuItem homescreenItem = menu.findItem(R.id.add_to_homescreen_id); |
| 231 MenuItem openWebApkItem = menu.findItem(R.id.open_webapk_id); |
| 232 if (canShowHomeScreenMenuItem) { |
| 233 Context context = ContextUtils.getApplicationContext(); |
| 234 ResolveInfo resolveInfo = null; |
| 235 boolean openWebApkItemVisible = false; |
| 236 if (ChromeWebApkHost.isEnabled()) { |
| 237 long addToHomeScreenStart = SystemClock.elapsedRealtime(); |
| 238 resolveInfo = WebApkValidator.queryResolveInfo(context, url); |
| 239 RecordHistogram.recordTimesHistogram( |
| 240 "Android.PrepareMenu.OpenWebApkVisibilityCheck", |
| 241 SystemClock.elapsedRealtime() - addToHomeScreenStart, |
| 242 TimeUnit.MILLISECONDS); |
| 243 |
| 244 openWebApkItemVisible = resolveInfo != null; |
| 245 } |
| 246 if (openWebApkItemVisible) { |
| 247 String appName = resolveInfo.loadLabel(context.getPackageManager
()).toString(); |
| 248 openWebApkItem.setTitle(context.getString(R.string.menu_open_web
apk, appName)); |
| 249 |
| 250 homescreenItem.setVisible(false); |
| 251 openWebApkItem.setVisible(true); |
| 252 } else { |
| 253 homescreenItem.setTitle(AppBannerManager.getHomescreenLanguageOp
tion()); |
| 254 homescreenItem.setVisible(true); |
| 255 openWebApkItem.setVisible(false); |
| 256 } |
| 257 } else { |
| 258 homescreenItem.setVisible(false); |
| 259 openWebApkItem.setVisible(false); |
| 260 } |
| 261 } |
| 262 |
| 263 /** |
221 * Notify the delegate that the load state changed. | 264 * Notify the delegate that the load state changed. |
222 * @param isLoading Whether the page is currently loading. | 265 * @param isLoading Whether the page is currently loading. |
223 */ | 266 */ |
224 public void loadingStateChanged(boolean isLoading) { | 267 public void loadingStateChanged(boolean isLoading) { |
225 if (mReloadMenuItem != null) { | 268 if (mReloadMenuItem != null) { |
226 mReloadMenuItem.getIcon().setLevel(isLoading | 269 mReloadMenuItem.getIcon().setLevel(isLoading |
227 ? RELOAD_BUTTON_LEVEL_STOP_LOADING : RELOAD_BUTTON_LEVEL_REL
OAD); | 270 ? RELOAD_BUTTON_LEVEL_STOP_LOADING : RELOAD_BUTTON_LEVEL_REL
OAD); |
228 mReloadMenuItem.setTitle(isLoading | 271 mReloadMenuItem.setTitle(isLoading |
229 ? R.string.accessibility_btn_stop_loading : R.string.accessi
bility_btn_refresh); | 272 ? R.string.accessibility_btn_stop_loading : R.string.accessi
bility_btn_refresh); |
230 } | 273 } |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
301 String url = currentTab.getUrl(); | 344 String url = currentTab.getUrl(); |
302 boolean isChromeScheme = url.startsWith(UrlConstants.CHROME_URL_PREFIX) | 345 boolean isChromeScheme = url.startsWith(UrlConstants.CHROME_URL_PREFIX) |
303 || url.startsWith(UrlConstants.CHROME_NATIVE_URL_PREFIX); | 346 || url.startsWith(UrlConstants.CHROME_NATIVE_URL_PREFIX); |
304 requstMenuItem.setVisible(!isChromeScheme || currentTab.isNativePage()); | 347 requstMenuItem.setVisible(!isChromeScheme || currentTab.isNativePage()); |
305 requstMenuItem.setChecked(currentTab.getUseDesktopUserAgent()); | 348 requstMenuItem.setChecked(currentTab.getUseDesktopUserAgent()); |
306 requstMenuItem.setTitleCondensed(requstMenuItem.isChecked() | 349 requstMenuItem.setTitleCondensed(requstMenuItem.isChecked() |
307 ? mActivity.getString(R.string.menu_request_desktop_site_on) | 350 ? mActivity.getString(R.string.menu_request_desktop_site_on) |
308 : mActivity.getString(R.string.menu_request_desktop_site_off)); | 351 : mActivity.getString(R.string.menu_request_desktop_site_off)); |
309 } | 352 } |
310 } | 353 } |
OLD | NEW |