Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(453)

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/appmenu/AppMenuPropertiesDelegate.java

Issue 2707993003: [Android]: Hide add-to-homescreen app menu item when WebAPK is installed (Closed)
Patch Set: Add new state for 'Add to Homescreen' Menu item Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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 =
245 resolveInfo != null && resolveInfo.activityInfo.packageN ame != null;
nyquist 2017/02/28 07:04:19 The null-check of packageName seems unnecessary he
gonzalon 2017/02/28 16:12:23 Great catch. Thanks!
246 }
247 if (openWebApkItemVisible) {
248 String appName = resolveInfo.loadLabel(context.getPackageManager ()).toString();
249 openWebApkItem.setTitle(context.getString(R.string.menu_open_web apk, appName));
250
251 homescreenItem.setVisible(false);
252 openWebApkItem.setVisible(true);
253 } else {
254 homescreenItem.setTitle(AppBannerManager.getHomescreenLanguageOp tion());
255 homescreenItem.setVisible(true);
256 openWebApkItem.setVisible(false);
257 }
258 } else {
259 homescreenItem.setVisible(false);
260 openWebApkItem.setVisible(false);
261 }
262 }
263
264 /**
221 * Notify the delegate that the load state changed. 265 * Notify the delegate that the load state changed.
222 * @param isLoading Whether the page is currently loading. 266 * @param isLoading Whether the page is currently loading.
223 */ 267 */
224 public void loadingStateChanged(boolean isLoading) { 268 public void loadingStateChanged(boolean isLoading) {
225 if (mReloadMenuItem != null) { 269 if (mReloadMenuItem != null) {
226 mReloadMenuItem.getIcon().setLevel(isLoading 270 mReloadMenuItem.getIcon().setLevel(isLoading
227 ? RELOAD_BUTTON_LEVEL_STOP_LOADING : RELOAD_BUTTON_LEVEL_REL OAD); 271 ? RELOAD_BUTTON_LEVEL_STOP_LOADING : RELOAD_BUTTON_LEVEL_REL OAD);
228 mReloadMenuItem.setTitle(isLoading 272 mReloadMenuItem.setTitle(isLoading
229 ? R.string.accessibility_btn_stop_loading : R.string.accessi bility_btn_refresh); 273 ? R.string.accessibility_btn_stop_loading : R.string.accessi bility_btn_refresh);
230 } 274 }
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
301 String url = currentTab.getUrl(); 345 String url = currentTab.getUrl();
302 boolean isChromeScheme = url.startsWith(UrlConstants.CHROME_URL_PREFIX) 346 boolean isChromeScheme = url.startsWith(UrlConstants.CHROME_URL_PREFIX)
303 || url.startsWith(UrlConstants.CHROME_NATIVE_URL_PREFIX); 347 || url.startsWith(UrlConstants.CHROME_NATIVE_URL_PREFIX);
304 requstMenuItem.setVisible(!isChromeScheme || currentTab.isNativePage()); 348 requstMenuItem.setVisible(!isChromeScheme || currentTab.isNativePage());
305 requstMenuItem.setChecked(currentTab.getUseDesktopUserAgent()); 349 requstMenuItem.setChecked(currentTab.getUseDesktopUserAgent());
306 requstMenuItem.setTitleCondensed(requstMenuItem.isChecked() 350 requstMenuItem.setTitleCondensed(requstMenuItem.isChecked()
307 ? mActivity.getString(R.string.menu_request_desktop_site_on) 351 ? mActivity.getString(R.string.menu_request_desktop_site_on)
308 : mActivity.getString(R.string.menu_request_desktop_site_off)); 352 : mActivity.getString(R.string.menu_request_desktop_site_off));
309 } 353 }
310 } 354 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698