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

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;
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
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 canShowHomeScreenMenuItem = ShortcutHelper.isAddToHomeIntent Supported()
166 boolean homescreenItemVisible = ShortcutHelper.isAddToHomeIntentSupp orted()
167 && !isChromeScheme && !isFileScheme && !isContentScheme && ! isIncognito; 174 && !isChromeScheme && !isFileScheme && !isContentScheme && ! isIncognito;
168 if (homescreenItemVisible) { 175 prepareAddToHomescreenMenuItem(menu, currentTab.getUrl(), canShowHom eScreenMenuItem);
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 29 matching lines...) Expand all
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
220 /** 224 /**
225 * Sets the visibility and labels of the "Add to Home screen" and "Open WebA PK" menu items.
226 */
227 protected void prepareAddToHomescreenMenuItem(
228 Menu menu, String url, boolean canShowHomeScreenMenuItem) {
229 MenuItem homescreenItem = menu.findItem(R.id.add_to_homescreen_id);
230 MenuItem openWebApkItem = menu.findItem(R.id.open_webapk_id);
231 if (canShowHomeScreenMenuItem) {
232 Context context = ContextUtils.getApplicationContext();
233 long addToHomeScreenStart = SystemClock.elapsedRealtime();
234 ResolveInfo resolveInfo = WebApkValidator.queryResolveInfo(context, url);
235 RecordHistogram.recordTimesHistogram("Android.PrepareMenu.OpenWebApk VisibilityCheck",
236 SystemClock.elapsedRealtime() - addToHomeScreenStart, TimeUn it.MILLISECONDS);
237
238 boolean openWebApkItemVisible =
239 resolveInfo != null && resolveInfo.activityInfo.packageName != null;
240
241 if (openWebApkItemVisible) {
242 String appName = resolveInfo.loadLabel(context.getPackageManager ()).toString();
243 openWebApkItem.setTitle(context.getString(R.string.menu_open_web apk, appName));
244
245 homescreenItem.setVisible(false);
246 openWebApkItem.setVisible(true);
247 } else {
248 homescreenItem.setTitle(AppBannerManager.getHomescreenLanguageOp tion());
249 homescreenItem.setVisible(true);
250 openWebApkItem.setVisible(false);
251 }
252 } else {
253 homescreenItem.setVisible(false);
254 openWebApkItem.setVisible(false);
255 }
256 }
257
258 /**
221 * Notify the delegate that the load state changed. 259 * Notify the delegate that the load state changed.
222 * @param isLoading Whether the page is currently loading. 260 * @param isLoading Whether the page is currently loading.
223 */ 261 */
224 public void loadingStateChanged(boolean isLoading) { 262 public void loadingStateChanged(boolean isLoading) {
225 if (mReloadMenuItem != null) { 263 if (mReloadMenuItem != null) {
226 mReloadMenuItem.getIcon().setLevel(isLoading 264 mReloadMenuItem.getIcon().setLevel(isLoading
227 ? RELOAD_BUTTON_LEVEL_STOP_LOADING : RELOAD_BUTTON_LEVEL_REL OAD); 265 ? RELOAD_BUTTON_LEVEL_STOP_LOADING : RELOAD_BUTTON_LEVEL_REL OAD);
228 mReloadMenuItem.setTitle(isLoading 266 mReloadMenuItem.setTitle(isLoading
229 ? R.string.accessibility_btn_stop_loading : R.string.accessi bility_btn_refresh); 267 ? R.string.accessibility_btn_stop_loading : R.string.accessi bility_btn_refresh);
230 } 268 }
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
301 String url = currentTab.getUrl(); 339 String url = currentTab.getUrl();
302 boolean isChromeScheme = url.startsWith(UrlConstants.CHROME_URL_PREFIX) 340 boolean isChromeScheme = url.startsWith(UrlConstants.CHROME_URL_PREFIX)
303 || url.startsWith(UrlConstants.CHROME_NATIVE_URL_PREFIX); 341 || url.startsWith(UrlConstants.CHROME_NATIVE_URL_PREFIX);
304 requstMenuItem.setVisible(!isChromeScheme || currentTab.isNativePage()); 342 requstMenuItem.setVisible(!isChromeScheme || currentTab.isNativePage());
305 requstMenuItem.setChecked(currentTab.getUseDesktopUserAgent()); 343 requstMenuItem.setChecked(currentTab.getUseDesktopUserAgent());
306 requstMenuItem.setTitleCondensed(requstMenuItem.isChecked() 344 requstMenuItem.setTitleCondensed(requstMenuItem.isChecked()
307 ? mActivity.getString(R.string.menu_request_desktop_site_on) 345 ? mActivity.getString(R.string.menu_request_desktop_site_on)
308 : mActivity.getString(R.string.menu_request_desktop_site_off)); 346 : mActivity.getString(R.string.menu_request_desktop_site_off));
309 } 347 }
310 } 348 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698