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; |
29 import org.chromium.components.dom_distiller.core.DomDistillerUrlUtils; | 34 import org.chromium.components.dom_distiller.core.DomDistillerUrlUtils; |
30 import org.chromium.ui.base.DeviceFormFactor; | 35 import org.chromium.ui.base.DeviceFormFactor; |
36 import org.chromium.webapk.lib.client.WebApkValidator; | |
37 | |
38 import java.util.concurrent.TimeUnit; | |
31 | 39 |
32 /** | 40 /** |
33 * App Menu helper that handles hiding and showing menu items based on activity state. | 41 * App Menu helper that handles hiding and showing menu items based on activity state. |
34 */ | 42 */ |
35 public class AppMenuPropertiesDelegate { | 43 public class AppMenuPropertiesDelegate { |
36 // Indices for different levels in drawable.btn_reload_stop. | 44 // Indices for different levels in drawable.btn_reload_stop. |
37 // Used only when preparing menu and refresh reload button in menu when tab | 45 // Used only when preparing menu and refresh reload button in menu when tab |
38 // page load status changes. | 46 // page load status changes. |
39 private static final int RELOAD_BUTTON_LEVEL_RELOAD = 0; | 47 private static final int RELOAD_BUTTON_LEVEL_RELOAD = 0; |
40 private static final int RELOAD_BUTTON_LEVEL_STOP_LOADING = 1; | 48 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: | 163 // Hide 'Add to homescreen' for the following: |
156 // * chrome:// pages - Android doesn't know how to direct those URLs . | 164 // * chrome:// pages - Android doesn't know how to direct those URLs . |
157 // * incognito pages - To avoid problems where users create shortcut s in incognito | 165 // * incognito pages - To avoid problems where users create shortcut s in incognito |
158 // mode and then open the webapp in regular mod e. | 166 // 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 | 167 // * file:// - After API 24, file: URIs are not supported in VIEW in tents and thus |
160 // can not be added to the homescreen. | 168 // can not be added to the homescreen. |
161 // * content:// - Accessing external content URIs requires the calli ng app to grant | 169 // * 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 | 170 // access to the resource via FLAG_GRANT_READ_URI_PER MISSION, and that |
163 // is not persisted when adding to the homescreen. | 171 // is not persisted when adding to the homescreen. |
164 // * If creating shortcuts it not supported by the current home scre en. | 172 // * If creating shortcuts it not supported by the current home scre en. |
165 MenuItem homescreenItem = menu.findItem(R.id.add_to_homescreen_id); | 173 boolean homeScreenVisible = ShortcutHelper.isAddToHomeIntentSupporte d(mActivity) |
166 boolean homescreenItemVisible = ShortcutHelper.isAddToHomeIntentSupp orted(mActivity) | |
167 && !isChromeScheme && !isFileScheme && !isContentScheme && ! isIncognito; | 174 && !isChromeScheme && !isFileScheme && !isContentScheme && ! isIncognito; |
168 if (homescreenItemVisible) { | 175 prepareAddToHomescreenMenuItem(menu, currentTab.getUrl(), homeScreen Visible); |
169 homescreenItem.setTitle(AppBannerManager.getHomescreenLanguageOp tion()); | |
170 } | |
171 homescreenItem.setVisible(homescreenItemVisible); | |
172 | 176 |
173 // Hide request desktop site on all chrome:// pages except for the N TP. Check request | 177 // 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. | 178 // desktop site if it's activated on this page. |
175 MenuItem requestItem = menu.findItem(R.id.request_desktop_site_id); | 179 MenuItem requestItem = menu.findItem(R.id.request_desktop_site_id); |
176 updateRequestDesktopSiteMenuItem(requestItem, currentTab); | 180 updateRequestDesktopSiteMenuItem(requestItem, currentTab); |
177 | 181 |
178 // Only display reader mode settings menu option if the current page is in reader mode. | 182 // Only display reader mode settings menu option if the current page is in reader mode. |
179 menu.findItem(R.id.reader_mode_prefs_id) | 183 menu.findItem(R.id.reader_mode_prefs_id) |
180 .setVisible(DomDistillerUrlUtils.isDistilledPage(currentTab. getUrl())); | 184 .setVisible(DomDistillerUrlUtils.isDistilledPage(currentTab. getUrl())); |
181 | 185 |
(...skipping 27 matching lines...) Expand all Loading... | |
209 // Disable new incognito tab when it is blocked (e.g. by a policy). | 213 // Disable new incognito tab when it is blocked (e.g. by a policy). |
210 // findItem(...).setEnabled(...)" is not enough here, because of the inf lated | 214 // findItem(...).setEnabled(...)" is not enough here, because of the inf lated |
211 // main_menu.xml contains multiple items with the same id in different g roups | 215 // main_menu.xml contains multiple items with the same id in different g roups |
212 // e.g.: new_incognito_tab_menu_id. | 216 // e.g.: new_incognito_tab_menu_id. |
213 disableEnableMenuItem(menu, R.id.new_incognito_tab_menu_id, | 217 disableEnableMenuItem(menu, R.id.new_incognito_tab_menu_id, |
214 true, | 218 true, |
215 PrefServiceBridge.getInstance().isIncognitoModeEnabled(), | 219 PrefServiceBridge.getInstance().isIncognitoModeEnabled(), |
216 PrefServiceBridge.getInstance().isIncognitoModeManaged()); | 220 PrefServiceBridge.getInstance().isIncognitoModeManaged()); |
217 mActivity.prepareMenu(menu); | 221 mActivity.prepareMenu(menu); |
218 } | 222 } |
219 | 223 |
pkotwicz
2017/02/22 20:26:10
Can you add a comment to this function?
How about
gonzalon
2017/02/22 20:45:04
Done.
| |
224 protected void prepareAddToHomescreenMenuItem( | |
225 Menu menu, String url, boolean homeScreenVisible) { | |
pkotwicz
2017/02/22 20:26:10
Perhaps rename |homeScreenVisible| to |homeScreenH
gonzalon
2017/02/22 20:45:04
Well, if I call it homeScreenHidden and it's false
pkotwicz
2017/02/23 20:46:17
Perhaps canShowHomeScreenMenuItem() to be consiste
gonzalon
2017/02/24 16:02:58
Done.
| |
226 MenuItem homescreenItem = menu.findItem(R.id.add_to_homescreen_id); | |
227 MenuItem openWebApkItem = menu.findItem(R.id.open_webapk_id); | |
228 if (homeScreenVisible) { | |
229 Context context = ContextUtils.getApplicationContext(); | |
230 long addToHomeScreenStart = SystemClock.elapsedRealtime(); | |
231 ResolveInfo resolveInfo = WebApkValidator.queryResolveInfo(context, url); | |
232 RecordHistogram.recordTimesHistogram("Android.PrepareMenu.OpenWebApk VisibilityCheck", | |
233 SystemClock.elapsedRealtime() - addToHomeScreenStart, TimeUn it.MILLISECONDS); | |
234 | |
235 boolean openWebApkItemVisible = | |
236 resolveInfo != null && resolveInfo.activityInfo.packageName != null; | |
237 | |
238 if (openWebApkItemVisible) { | |
239 String appName = resolveInfo.loadLabel(context.getPackageManager ()).toString(); | |
240 openWebApkItem.setTitle(context.getString(R.string.menu_open_web apk, appName)); | |
241 | |
242 homescreenItem.setVisible(false); | |
243 openWebApkItem.setVisible(true); | |
244 } else { | |
245 homescreenItem.setTitle(AppBannerManager.getHomescreenLanguageOp tion()); | |
246 homescreenItem.setVisible(true); | |
247 openWebApkItem.setVisible(false); | |
248 } | |
249 } else { | |
250 homescreenItem.setVisible(false); | |
251 openWebApkItem.setVisible(false); | |
252 } | |
253 } | |
254 | |
220 /** | 255 /** |
221 * Notify the delegate that the load state changed. | 256 * Notify the delegate that the load state changed. |
222 * @param isLoading Whether the page is currently loading. | 257 * @param isLoading Whether the page is currently loading. |
223 */ | 258 */ |
224 public void loadingStateChanged(boolean isLoading) { | 259 public void loadingStateChanged(boolean isLoading) { |
225 if (mReloadMenuItem != null) { | 260 if (mReloadMenuItem != null) { |
226 mReloadMenuItem.getIcon().setLevel(isLoading | 261 mReloadMenuItem.getIcon().setLevel(isLoading |
227 ? RELOAD_BUTTON_LEVEL_STOP_LOADING : RELOAD_BUTTON_LEVEL_REL OAD); | 262 ? RELOAD_BUTTON_LEVEL_STOP_LOADING : RELOAD_BUTTON_LEVEL_REL OAD); |
228 mReloadMenuItem.setTitle(isLoading | 263 mReloadMenuItem.setTitle(isLoading |
229 ? R.string.accessibility_btn_stop_loading : R.string.accessi bility_btn_refresh); | 264 ? R.string.accessibility_btn_stop_loading : R.string.accessi bility_btn_refresh); |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
301 String url = currentTab.getUrl(); | 336 String url = currentTab.getUrl(); |
302 boolean isChromeScheme = url.startsWith(UrlConstants.CHROME_URL_PREFIX) | 337 boolean isChromeScheme = url.startsWith(UrlConstants.CHROME_URL_PREFIX) |
303 || url.startsWith(UrlConstants.CHROME_NATIVE_URL_PREFIX); | 338 || url.startsWith(UrlConstants.CHROME_NATIVE_URL_PREFIX); |
304 requstMenuItem.setVisible(!isChromeScheme || currentTab.isNativePage()); | 339 requstMenuItem.setVisible(!isChromeScheme || currentTab.isNativePage()); |
305 requstMenuItem.setChecked(currentTab.getUseDesktopUserAgent()); | 340 requstMenuItem.setChecked(currentTab.getUseDesktopUserAgent()); |
306 requstMenuItem.setTitleCondensed(requstMenuItem.isChecked() | 341 requstMenuItem.setTitleCondensed(requstMenuItem.isChecked() |
307 ? mActivity.getString(R.string.menu_request_desktop_site_on) | 342 ? mActivity.getString(R.string.menu_request_desktop_site_on) |
308 : mActivity.getString(R.string.menu_request_desktop_site_off)); | 343 : mActivity.getString(R.string.menu_request_desktop_site_off)); |
309 } | 344 } |
310 } | 345 } |
OLD | NEW |