Commit 2c8911bf authored by Pavol Drotar's avatar Pavol Drotar Committed by Commit Bot

Check category before adding site settings shortcut

We require the ability to tell whether the browser supports
site settings shortcuts for WebApks. This change will allow us to
turn site settings shortcut on/off by adding/removing a category
from the manifest.

Bug: 1128985
Change-Id: If1f1d672bbee162287db401ebfc947cf4ce567fe
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2428965
Commit-Queue: Pavol Drotár <pavoldrotar@google.com>
Reviewed-by: default avatarPeter Kotwicz <pkotwicz@chromium.org>
Reviewed-by: default avatarPeter Conn <peconn@chromium.org>
Cr-Commit-Position: refs/heads/master@{#810692}
parent 9770d88f
...@@ -12,4 +12,4 @@ ...@@ -12,4 +12,4 @@
# //chrome/android/webapk/shell_apk:webapk is changed. This includes # //chrome/android/webapk/shell_apk:webapk is changed. This includes
# Java files, Android resource files and AndroidManifest.xml. Does not affect # Java files, Android resource files and AndroidManifest.xml. Does not affect
# Chrome.apk # Chrome.apk
current_shell_apk_version = 132 current_shell_apk_version = 133
...@@ -7,7 +7,6 @@ package org.chromium.webapk.shell_apk.h2o; ...@@ -7,7 +7,6 @@ package org.chromium.webapk.shell_apk.h2o;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.chromium.webapk.shell_apk.ManageDataLauncherActivity.CHROMIUM_VERSION_SUPPORTS_WEBAPK_MANAGE_SPACE;
import static org.chromium.webapk.shell_apk.ManageDataLauncherActivity.SITE_SETTINGS_SHORTCUT_ID; import static org.chromium.webapk.shell_apk.ManageDataLauncherActivity.SITE_SETTINGS_SHORTCUT_ID;
import android.app.Activity; import android.app.Activity;
...@@ -17,6 +16,7 @@ import android.content.Context; ...@@ -17,6 +16,7 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.pm.ShortcutInfo; import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager; import android.content.pm.ShortcutManager;
import android.net.Uri; import android.net.Uri;
...@@ -58,9 +58,12 @@ public final class LaunchTest { ...@@ -58,9 +58,12 @@ public final class LaunchTest {
/** Values based on manifest specified in GN file. */ /** Values based on manifest specified in GN file. */
private static final String BROWSER_PACKAGE_NAME = "com.google.android.apps.chrome"; private static final String BROWSER_PACKAGE_NAME = "com.google.android.apps.chrome";
private static final String DEFAULT_START_URL = "https://pwa.rocks/"; private static final String DEFAULT_START_URL = "https://pwa.rocks/";
private static final String CATEGORY_LAUNCH_WEBAPK_SITE_SETTINGS =
"androidx.browser.trusted.category.LaunchWebApkSiteSettings";
/** Chromium version which does not support showing the splash screen within WebAPK. */ /** Chromium version which does not support showing the splash screen within WebAPK. */
private static final int BROWSER_H2O_INCOMPATIBLE_VERSION = 57; private static final int BROWSER_H2O_INCOMPATIBLE_VERSION = 57;
public static final int SITE_SETTINGS_COMPATIBLE_BROWSER_VERSION = 87;
private static String sWebApkPackageName; private static String sWebApkPackageName;
...@@ -492,12 +495,13 @@ public final class LaunchTest { ...@@ -492,12 +495,13 @@ public final class LaunchTest {
@Config(sdk = Build.VERSION_CODES.N_MR1) @Config(sdk = Build.VERSION_CODES.N_MR1)
public void testAddsSiteSettings() { public void testAddsSiteSettings() {
registerWebApk(true /* isNewStyleWebApk */); registerWebApk(true /* isNewStyleWebApk */);
registerSiteSettingsCategory();
Intent launchIntent = new Intent(Intent.ACTION_MAIN); Intent launchIntent = new Intent(Intent.ACTION_MAIN);
launchIntent.setPackage(sWebApkPackageName); launchIntent.setPackage(sWebApkPackageName);
launchAndCheckBrowserLaunched(false /* opaqueMainActivityInitiallyEnabled */, launchIntent, launchAndCheckBrowserLaunched(false /* opaqueMainActivityInitiallyEnabled */, launchIntent,
H2OMainActivity.class, CHROMIUM_VERSION_SUPPORTS_WEBAPK_MANAGE_SPACE); H2OMainActivity.class, SITE_SETTINGS_COMPATIBLE_BROWSER_VERSION);
ShortcutManager shortcutManager = mAppContext.getSystemService(ShortcutManager.class); ShortcutManager shortcutManager = mAppContext.getSystemService(ShortcutManager.class);
assertTrue(containsSiteSettingsDynamicShortcut(shortcutManager)); assertTrue(containsSiteSettingsDynamicShortcut(shortcutManager));
...@@ -505,7 +509,7 @@ public final class LaunchTest { ...@@ -505,7 +509,7 @@ public final class LaunchTest {
shortcutManager.removeAllDynamicShortcuts(); shortcutManager.removeAllDynamicShortcuts();
launchAndCheckBrowserLaunched(true /* opaqueMainActivityInitiallyEnabled */, launchIntent, launchAndCheckBrowserLaunched(true /* opaqueMainActivityInitiallyEnabled */, launchIntent,
H2OOpaqueMainActivity.class, CHROMIUM_VERSION_SUPPORTS_WEBAPK_MANAGE_SPACE); H2OOpaqueMainActivity.class, SITE_SETTINGS_COMPATIBLE_BROWSER_VERSION);
assertTrue(containsSiteSettingsDynamicShortcut(shortcutManager)); assertTrue(containsSiteSettingsDynamicShortcut(shortcutManager));
} }
...@@ -515,20 +519,16 @@ public final class LaunchTest { ...@@ -515,20 +519,16 @@ public final class LaunchTest {
*/ */
@Test @Test
@Config(sdk = Build.VERSION_CODES.N_MR1) @Config(sdk = Build.VERSION_CODES.N_MR1)
public void testRemovesSiteSettingsIfChromeVersionLow() { public void testDoesNotAddSiteSettingsIfCategoryMissing() {
registerWebApk(true /* isNewStyleWebApk */); registerWebApk(true /* isNewStyleWebApk */);
Intent launchIntent = new Intent(Intent.ACTION_MAIN); Intent launchIntent = new Intent(Intent.ACTION_MAIN);
launchIntent.setPackage(sWebApkPackageName); launchIntent.setPackage(sWebApkPackageName);
launchAndCheckBrowserLaunched(false /* opaqueMainActivityInitiallyEnabled */, launchIntent, launchAndCheckBrowserLaunched(false /* opaqueMainActivityInitiallyEnabled */, launchIntent,
H2OMainActivity.class, CHROMIUM_VERSION_SUPPORTS_WEBAPK_MANAGE_SPACE); H2OMainActivity.class, SITE_SETTINGS_COMPATIBLE_BROWSER_VERSION);
ShortcutManager shortcutManager = mAppContext.getSystemService(ShortcutManager.class); ShortcutManager shortcutManager = mAppContext.getSystemService(ShortcutManager.class);
assertTrue(containsSiteSettingsDynamicShortcut(shortcutManager));
launchAndCheckBrowserLaunched(false /* opaqueMainActivityInitiallyEnabled */, launchIntent,
H2OMainActivity.class, CHROMIUM_VERSION_SUPPORTS_WEBAPK_MANAGE_SPACE - 1);
assertFalse(containsSiteSettingsDynamicShortcut(shortcutManager)); assertFalse(containsSiteSettingsDynamicShortcut(shortcutManager));
} }
...@@ -537,12 +537,13 @@ public final class LaunchTest { ...@@ -537,12 +537,13 @@ public final class LaunchTest {
@Config(sdk = Build.VERSION_CODES.LOLLIPOP) @Config(sdk = Build.VERSION_CODES.LOLLIPOP)
public void testDoesNotAddSiteSettingsWhenSdkLow() { public void testDoesNotAddSiteSettingsWhenSdkLow() {
registerWebApk(true /* isNewStyleWebApk */); registerWebApk(true /* isNewStyleWebApk */);
registerSiteSettingsCategory();
Intent launchIntent = new Intent(Intent.ACTION_MAIN); Intent launchIntent = new Intent(Intent.ACTION_MAIN);
launchIntent.setPackage(sWebApkPackageName); launchIntent.setPackage(sWebApkPackageName);
launchAndCheckBrowserLaunched(false /* opaqueMainActivityInitiallyEnabled */, launchIntent, launchAndCheckBrowserLaunched(false /* opaqueMainActivityInitiallyEnabled */, launchIntent,
H2OMainActivity.class, CHROMIUM_VERSION_SUPPORTS_WEBAPK_MANAGE_SPACE); H2OMainActivity.class, SITE_SETTINGS_COMPATIBLE_BROWSER_VERSION);
// There is no shortcut manager in Android M. Therefore if // There is no shortcut manager in Android M. Therefore if
// this test passes, then we did not attempt to add the shortcut. // this test passes, then we did not attempt to add the shortcut.
...@@ -581,6 +582,14 @@ public final class LaunchTest { ...@@ -581,6 +582,14 @@ public final class LaunchTest {
WebApkTestHelper.registerWebApkWithMetaData(sWebApkPackageName, metadata, null); WebApkTestHelper.registerWebApkWithMetaData(sWebApkPackageName, metadata, null);
} }
private void registerSiteSettingsCategory() {
Intent intent =
new Intent().setAction("android.support.customtabs.action.CustomTabsService");
intent.setPackage(BROWSER_PACKAGE_NAME);
intent.addCategory(CATEGORY_LAUNCH_WEBAPK_SITE_SETTINGS);
mShadowPackageManager.addResolveInfoForIntent(intent, new ResolveInfo());
}
/** /**
* Launches WebAPK with the given intent and configuration. Tests that the host * Launches WebAPK with the given intent and configuration. Tests that the host
* browser is launched and which activities are enabled after the browser launch. * browser is launched and which activities are enabled after the browser launch.
......
...@@ -14,6 +14,7 @@ import android.content.Context; ...@@ -14,6 +14,7 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.ApplicationInfo; import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.pm.ShortcutInfo; import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager; import android.content.pm.ShortcutManager;
import android.graphics.drawable.Icon; import android.graphics.drawable.Icon;
...@@ -30,12 +31,14 @@ import androidx.annotation.NonNull; ...@@ -30,12 +31,14 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.browser.customtabs.CustomTabsClient; import androidx.browser.customtabs.CustomTabsClient;
import androidx.browser.customtabs.CustomTabsIntent; import androidx.browser.customtabs.CustomTabsIntent;
import androidx.browser.customtabs.CustomTabsService;
import androidx.browser.customtabs.CustomTabsServiceConnection; import androidx.browser.customtabs.CustomTabsServiceConnection;
import androidx.browser.customtabs.CustomTabsSession; import androidx.browser.customtabs.CustomTabsSession;
import org.chromium.webapk.lib.common.WebApkConstants; import org.chromium.webapk.lib.common.WebApkConstants;
import java.util.Collections; import java.util.Collections;
import java.util.List;
/** /**
* A convenience class for adding site setting shortcuts into WebApks. * A convenience class for adding site setting shortcuts into WebApks.
...@@ -54,7 +57,8 @@ public class ManageDataLauncherActivity extends Activity { ...@@ -54,7 +57,8 @@ public class ManageDataLauncherActivity extends Activity {
private static final String EXTRA_SITE_SETTINGS_URL = "SITE_SETTINGS_URL"; private static final String EXTRA_SITE_SETTINGS_URL = "SITE_SETTINGS_URL";
private static final String EXTRA_PROVIDER_PACKAGE = "PROVIDER_PACKAGE"; private static final String EXTRA_PROVIDER_PACKAGE = "PROVIDER_PACKAGE";
public static final int CHROMIUM_VERSION_SUPPORTS_WEBAPK_MANAGE_SPACE = 87; private static final String CATEGORY_LAUNCH_WEBAPK_SITE_SETTINGS =
"androidx.browser.trusted.category.LaunchWebApkSiteSettings";
@Nullable @Nullable
private String mProviderPackage; private String mProviderPackage;
...@@ -71,7 +75,7 @@ public class ManageDataLauncherActivity extends Activity { ...@@ -71,7 +75,7 @@ public class ManageDataLauncherActivity extends Activity {
mProviderPackage = getIntent().getStringExtra(EXTRA_PROVIDER_PACKAGE); mProviderPackage = getIntent().getStringExtra(EXTRA_PROVIDER_PACKAGE);
mUrl = Uri.parse(getIntent().getStringExtra(EXTRA_SITE_SETTINGS_URL)); mUrl = Uri.parse(getIntent().getStringExtra(EXTRA_SITE_SETTINGS_URL));
if (!supportsManageSpace(this, mProviderPackage)) { if (!supportsWebApkManageSpace(this, mProviderPackage)) {
handleNoSupportForManageSpace(); handleNoSupportForManageSpace();
return; return;
} }
...@@ -169,9 +173,13 @@ public class ManageDataLauncherActivity extends Activity { ...@@ -169,9 +173,13 @@ public class ManageDataLauncherActivity extends Activity {
} }
} }
private static boolean supportsManageSpace(Context context, String providerPackage) { private static boolean supportsWebApkManageSpace(Context context, String providerPackage) {
return HostBrowserUtils.queryHostBrowserMajorChromiumVersion(context, providerPackage) Intent customTabsIntent = new Intent(CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION);
>= CHROMIUM_VERSION_SUPPORTS_WEBAPK_MANAGE_SPACE; customTabsIntent.addCategory(CATEGORY_LAUNCH_WEBAPK_SITE_SETTINGS);
customTabsIntent.setPackage(providerPackage);
List<ResolveInfo> services = context.getPackageManager().queryIntentServices(
customTabsIntent, PackageManager.GET_RESOLVED_FILTER);
return services.size() > 0;
} }
/** /**
...@@ -217,7 +225,7 @@ public class ManageDataLauncherActivity extends Activity { ...@@ -217,7 +225,7 @@ public class ManageDataLauncherActivity extends Activity {
ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class); ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);
// Remove potentially existing shortcut if package does not support shortcuts. // Remove potentially existing shortcut if package does not support shortcuts.
if (!supportsManageSpace(context, params.getHostBrowserPackageName())) { if (!supportsWebApkManageSpace(context, params.getHostBrowserPackageName())) {
shortcutManager.removeDynamicShortcuts(Collections.singletonList( shortcutManager.removeDynamicShortcuts(Collections.singletonList(
ManageDataLauncherActivity.SITE_SETTINGS_SHORTCUT_ID)); ManageDataLauncherActivity.SITE_SETTINGS_SHORTCUT_ID));
return; return;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment