Commit 58328102 authored by Michael Thiessen's avatar Michael Thiessen Committed by Chromium LUCI CQ

Implement Homescreen Widget LaunchCauseMetrics

Implements LaunchCauseMetrics for the Search and Bookmarks widgets.

Bug: 1163961
Change-Id: I87427a11be245fbb990377cf02da00b408c8fe2c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2635447Reviewed-by: default avatarTed Choc <tedchoc@chromium.org>
Commit-Queue: Michael Thiessen <mthiesse@chromium.org>
Cr-Commit-Position: refs/heads/master@{#845008}
parent a26ee33a
......@@ -56,7 +56,8 @@ public abstract class LaunchCauseMetrics implements ApplicationStatus.Applicatio
// These values are persisted in histograms. Please do not renumber. Append only.
@IntDef({LaunchCause.OTHER, LaunchCause.CUSTOM_TAB, LaunchCause.TWA, LaunchCause.RECENTS,
LaunchCause.RECENTS_OR_BACK, LaunchCause.FOREGROUND_WHEN_LOCKED,
LaunchCause.MAIN_LAUNCHER_ICON, LaunchCause.MAIN_LAUNCHER_ICON_SHORTCUT})
LaunchCause.MAIN_LAUNCHER_ICON, LaunchCause.MAIN_LAUNCHER_ICON_SHORTCUT,
LaunchCause.HOME_SCREEN_WIDGET})
@Retention(RetentionPolicy.SOURCE)
public @interface LaunchCause {
int OTHER = 0;
......@@ -67,8 +68,9 @@ public abstract class LaunchCauseMetrics implements ApplicationStatus.Applicatio
int FOREGROUND_WHEN_LOCKED = 5;
int MAIN_LAUNCHER_ICON = 6;
int MAIN_LAUNCHER_ICON_SHORTCUT = 7;
int HOME_SCREEN_WIDGET = 8;
int NUM_ENTRIES = 8;
int NUM_ENTRIES = 9;
}
/**
......@@ -225,6 +227,9 @@ public abstract class LaunchCauseMetrics implements ApplicationStatus.Applicatio
case LaunchCause.MAIN_LAUNCHER_ICON_SHORTCUT:
launchCause = "MAIN_LAUNCHER_ICON_SHORTCUT";
break;
case LaunchCause.HOME_SCREEN_WIDGET:
launchCause = "HOME_SCREEN_WIDGET";
break;
}
Log.d(TAG, "Launch Cause: " + launchCause);
}
......
......@@ -9,6 +9,9 @@ import android.content.Intent;
import org.chromium.base.IntentUtils;
import org.chromium.chrome.browser.IntentHandler;
import org.chromium.chrome.browser.ShortcutHelper;
import org.chromium.chrome.browser.searchwidget.SearchWidgetProvider;
import org.chromium.components.webapps.ShortcutSource;
/**
* LaunchCauseMetrics for ChromeTabbedActivity.
......@@ -36,6 +39,17 @@ public class TabbedActivityLaunchCauseMetrics extends LaunchCauseMetrics {
return LaunchCause.MAIN_LAUNCHER_ICON_SHORTCUT;
}
if (ShortcutSource.BOOKMARK_NAVIGATOR_WIDGET
== IntentUtils.safeGetIntExtra(
launchIntent, ShortcutHelper.EXTRA_SOURCE, ShortcutSource.UNKNOWN)) {
return LaunchCause.HOME_SCREEN_WIDGET;
}
if (IntentUtils.safeGetBooleanExtra(
launchIntent, SearchWidgetProvider.EXTRA_FROM_SEARCH_WIDGET, false)) {
return LaunchCause.HOME_SCREEN_WIDGET;
};
// TODO(https://crbug.com/1163961): Implement remaining ChromeTabbedActivity launch cause
// metrics.
......
......@@ -342,6 +342,11 @@ public class SearchActivity extends AsyncInitializationActivity
getIntent(), SearchWidgetProvider.EXTRA_START_VOICE_SEARCH, false);
}
private boolean isFromSearchWidget() {
return IntentUtils.safeGetBooleanExtra(
getIntent(), SearchWidgetProvider.EXTRA_FROM_SEARCH_WIDGET, false);
}
private String getOptionalIntentQuery() {
return IntentUtils.safeGetStringExtra(getIntent(), SearchManager.QUERY);
}
......@@ -420,6 +425,9 @@ public class SearchActivity extends AsyncInitializationActivity
intent.putExtra(IntentHandler.EXTRA_POST_DATA_TYPE, postDataType);
intent.putExtra(IntentHandler.EXTRA_POST_DATA, postData);
}
if (isFromSearchWidget()) {
intent.putExtra(SearchWidgetProvider.EXTRA_FROM_SEARCH_WIDGET, true);
}
IntentHandler.addTrustedIntentExtras(intent);
return intent;
......
......@@ -116,6 +116,8 @@ public class SearchWidgetProvider extends AppWidgetProvider {
public static final String EXTRA_START_VOICE_SEARCH =
"org.chromium.chrome.browser.searchwidget.START_VOICE_SEARCH";
public static final String EXTRA_FROM_SEARCH_WIDGET =
"org.chromium.chrome.browser.searchwidget.FROM_SEARCH_WIDGET";
/** Number of consecutive crashes this widget will absorb before giving up. */
private static final int CRASH_LIMIT = 3;
......@@ -219,6 +221,7 @@ public class SearchWidgetProvider extends AppWidgetProvider {
searchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
searchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
searchIntent.putExtra(EXTRA_START_VOICE_SEARCH, startVoiceSearch);
searchIntent.putExtra(EXTRA_FROM_SEARCH_WIDGET, true);
Bundle optionsBundle =
ActivityOptionsCompat.makeCustomAnimation(context, R.anim.activity_open_enter, 0)
......
......@@ -6,6 +6,8 @@ package org.chromium.chrome.browser.metrics;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.support.test.runner.lifecycle.Stage;
import androidx.test.filters.MediumTest;
......@@ -20,11 +22,15 @@ import org.chromium.base.ApplicationStatus;
import org.chromium.base.ContextUtils;
import org.chromium.base.library_loader.LibraryLoader;
import org.chromium.base.metrics.RecordHistogram;
import org.chromium.base.test.util.ApplicationTestUtils;
import org.chromium.base.test.util.Batch;
import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.base.test.util.Criteria;
import org.chromium.base.test.util.CriteriaHelper;
import org.chromium.base.test.util.ScalableTimeout;
import org.chromium.chrome.browser.ChromeTabbedActivity;
import org.chromium.chrome.browser.LauncherShortcutActivity;
import org.chromium.chrome.browser.bookmarkswidget.BookmarkWidgetProxy;
import org.chromium.chrome.browser.flags.ChromeSwitches;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.chrome.test.ChromeTabbedActivityTestRule;
......@@ -116,4 +122,22 @@ public final class TabbedActivityLaunchCauseMetricsTest {
Matchers.is(count));
});
}
@Test
@MediumTest
public void testBookmarkWidgetMetrics() throws Throwable {
Intent intent = new Intent();
intent.setClass(ContextUtils.getApplicationContext(), BookmarkWidgetProxy.class);
intent.setData(Uri.parse("about:blank"));
final int count =
1 + histogramCountForValue(LaunchCauseMetrics.LaunchCause.HOME_SCREEN_WIDGET);
mActivityTestRule.setActivity(ApplicationTestUtils.waitForActivityWithClass(
ChromeTabbedActivity.class, Stage.RESUMED,
() -> ContextUtils.getApplicationContext().sendBroadcast(intent)));
CriteriaHelper.pollInstrumentationThread(() -> {
Criteria.checkThat(
histogramCountForValue(LaunchCauseMetrics.LaunchCause.HOME_SCREEN_WIDGET),
Matchers.is(count));
}, ScalableTimeout.scaleTimeout(5000L), CriteriaHelper.DEFAULT_POLLING_INTERVAL);
}
}
......@@ -36,6 +36,7 @@ import org.chromium.base.ApplicationStatus;
import org.chromium.base.Callback;
import org.chromium.base.ContentUriUtils;
import org.chromium.base.IntentUtils;
import org.chromium.base.metrics.RecordHistogram;
import org.chromium.base.test.util.CallbackHelper;
import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.base.test.util.Criteria;
......@@ -54,6 +55,7 @@ import org.chromium.chrome.browser.locale.DefaultSearchEngineDialogHelperUtils;
import org.chromium.chrome.browser.locale.DefaultSearchEnginePromoDialog;
import org.chromium.chrome.browser.locale.DefaultSearchEnginePromoDialog.DefaultSearchEnginePromoDialogObserver;
import org.chromium.chrome.browser.locale.LocaleManager;
import org.chromium.chrome.browser.metrics.LaunchCauseMetrics;
import org.chromium.chrome.browser.omnibox.LocationBarCoordinator;
import org.chromium.chrome.browser.omnibox.OmniboxSuggestionType;
import org.chromium.chrome.browser.omnibox.UrlBar;
......@@ -246,6 +248,10 @@ public class SearchActivityTest {
return null;
}
}, url);
Assert.assertEquals(1,
RecordHistogram.getHistogramValueCountForTesting(
LaunchCauseMetrics.LAUNCH_CAUSE_HISTOGRAM,
LaunchCauseMetrics.LaunchCause.HOME_SCREEN_WIDGET));
}
@Test
......@@ -772,6 +778,7 @@ public class SearchActivityTest {
Criteria.checkThat(tab, Matchers.notNullValue());
Criteria.checkThat(tab.getUrlString(), Matchers.is(expectedUrl));
});
mActivityTestRule.setActivity(cta);
}
private void waitForSuggestionType(final SearchActivityLocationBarLayout locationBar,
......
......@@ -286,6 +286,9 @@ public class SearchWidgetProviderTest {
boolean microphoneState = IntentUtils.safeGetBooleanExtra(
intent, SearchWidgetProvider.EXTRA_START_VOICE_SEARCH, false);
Assert.assertEquals(clickTarget == R.id.microphone_icon, microphoneState);
boolean fromWidget = IntentUtils.safeGetBooleanExtra(
intent, SearchWidgetProvider.EXTRA_FROM_SEARCH_WIDGET, false);
Assert.assertTrue(fromWidget);
}
}
......
......@@ -70,7 +70,8 @@ struct ShortcutInfo {
// Used for legacy PWAs added via the banner.
SOURCE_APP_BANNER = 2,
SOURCE_BOOKMARK_NAVIGATOR_WIDGET = 3,
SOURCE_BOOKMARK_SHORTCUT_WIDGET = 4,
// unused
// SOURCE_BOOKMARK_SHORTCUT_WIDGET = 4,
// Used for legacy and WebAPKs launched from a notification.
SOURCE_NOTIFICATION = 5,
......
......@@ -41280,6 +41280,7 @@ Called by update_gpu_driver_bug_workaround_entries.py.-->
<int value="6" label="Main Chrome Launcher Icon"/>
<int value="7"
label="Main Chrome Launcher Icon Shortcuts (New Tab shortcuts)"/>
<int value="8" label="Home screen widget (eg. Search or Bookmarks)"/>
</enum>
<enum name="LauncherRankingItemType">
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