Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c301c834 authored by Willie Koomson's avatar Willie Koomson
Browse files

Updates to widget impressions for review feedback

- AppWidgetEvent.Builder uses longs and System.currentTimeMillis to
  avoid allocating temporary objects
- AppWidgetServiceImpl uses JobScheduler instead of AlarmManager for
  periodic event reporting jobs
- Remove @FlaggedApi annotation from hidden APIs

Bug: 425685745
Bug: 375515125
Test: FrameworksCoreTests:AppWidgetEventsTest CtsAppWidgetTestCases:WidgetEventsTest
Flag: android.appwidget.flags.engagement_metrics
Change-Id: I87ad16f5f7eadba3bc83a4df47f38ccbe68b3a84
parent 65e48d71
Loading
Loading
Loading
Loading
+27 −31
Original line number Diff line number Diff line
@@ -126,13 +126,12 @@ public final class AppWidgetEvent implements Parcelable {
        return mScrolledIds;
    }

    private AppWidgetEvent(int appWidgetId, @NonNull Duration visibleDuration,
            @NonNull Instant start, @NonNull Instant end, @Nullable Rect position,
            @Nullable int[] clickedIds, @Nullable int[] scrolledIds) {
    private AppWidgetEvent(int appWidgetId, long visibleDurationMillis, long start, long end,
            @Nullable Rect position, @Nullable int[] clickedIds, @Nullable int[] scrolledIds) {
        mAppWidgetId = appWidgetId;
        mVisibleDuration = visibleDuration;
        mStart = start;
        mEnd = end;
        mVisibleDuration = Duration.ofMillis(visibleDurationMillis);
        mStart = Instant.ofEpochMilli(start);
        mEnd = Instant.ofEpochMilli(end);
        mPosition = position;
        mClickedIds = clickedIds;
        mScrolledIds = scrolledIds;
@@ -218,11 +217,9 @@ public final class AppWidgetEvent implements Parcelable {
        PersistableBundle extras = usageEvent.getExtras();
        int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);
        Duration duration = Duration.ofMillis(extras.getLong(
                AppWidgetManager.EXTRA_EVENT_DURATION_MS, 0L));
        Instant start = Instant.ofEpochMilli(extras.getLong(AppWidgetManager.EXTRA_EVENT_START,
                0L));
        Instant end = Instant.ofEpochMilli(extras.getLong(AppWidgetManager.EXTRA_EVENT_END, 0L));
        long durationMillis = extras.getLong(AppWidgetManager.EXTRA_EVENT_DURATION_MS, 0L);
        long start = extras.getLong(AppWidgetManager.EXTRA_EVENT_START, 0L);
        long end = extras.getLong(AppWidgetManager.EXTRA_EVENT_END, 0L);
        Rect position = null;
        int[] clickedIds = null;
        int[] scrolledIds = null;
@@ -239,7 +236,7 @@ public final class AppWidgetEvent implements Parcelable {
        if (extras.containsKey(AppWidgetManager.EXTRA_EVENT_SCROLLED_VIEWS)) {
            scrolledIds = extras.getIntArray(AppWidgetManager.EXTRA_EVENT_SCROLLED_VIEWS);
        }
        return new AppWidgetEvent(appWidgetId, duration, start, end, position, clickedIds,
        return new AppWidgetEvent(appWidgetId, durationMillis, start, end, position, clickedIds,
            scrolledIds);
    }

@@ -272,9 +269,9 @@ public final class AppWidgetEvent implements Parcelable {
        @NonNull
        private final ArraySet<Integer> mScrolledIds = new ArraySet<>(MAX_NUM_ITEMS);
        private int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
        private Instant mStart = Instant.MAX;
        private Instant mEnd = Instant.MIN;
        private Duration mDuration = Duration.ZERO;
        private long mStart = Long.MAX_VALUE;
        private long mEnd = Long.MIN_VALUE;
        private long mDurationMillis = 0L;
        private long mLastVisibilityChangeMillis = 0L;
        @Nullable
        private Rect mPosition = null;
@@ -291,8 +288,8 @@ public final class AppWidgetEvent implements Parcelable {
         * Start a new visibility duration for this event.
         */
        public Builder startVisibility() {
            Instant now = Instant.now();
            if (now.isBefore(mStart)) {
            long now = System.currentTimeMillis();
            if (now < mStart) {
                mStart = now;
            }
            mLastVisibilityChangeMillis = SystemClock.uptimeMillis();
@@ -303,12 +300,11 @@ public final class AppWidgetEvent implements Parcelable {
         * End the visibility duration, and add the duration to this event's total duration.
         */
        public Builder endVisibility() {
            Instant now = Instant.now();
            if (now.isAfter(mEnd)) {
            long now = System.currentTimeMillis();
            if (now > mEnd) {
                mEnd = now;
            }
            mDuration = mDuration.plusMillis(
                SystemClock.uptimeMillis() - mLastVisibilityChangeMillis);
            mDurationMillis += SystemClock.uptimeMillis() - mLastVisibilityChangeMillis;
            return this;
        }

@@ -345,13 +341,13 @@ public final class AppWidgetEvent implements Parcelable {
                throw new IllegalArgumentException("Trying to merge events with different app "
                    + "widget IDs: " + mAppWidgetId + " != " + event.getAppWidgetId());
            }
            if (event.getStart().isBefore(mStart)) {
                mStart = event.getStart();
            if (event.getStart().toEpochMilli() < mStart) {
                mStart = event.getStart().toEpochMilli();
            }
            if (event.getEnd().isAfter(mEnd)) {
                mEnd = event.getEnd();
            if (event.getEnd().toEpochMilli() > mEnd) {
                mEnd = event.getEnd().toEpochMilli();
            }
            mDuration = mDuration.plus(event.getVisibleDuration());
            mDurationMillis += event.getVisibleDuration().toMillis();
            setPosition(event.getPosition());
            addAllUntilMax(mClickedIds, event.getClickedIds());
            addAllUntilMax(mScrolledIds, event.getScrolledIds());
@@ -362,23 +358,23 @@ public final class AppWidgetEvent implements Parcelable {
         * event yet.
         */
        public boolean isEmpty() {
            return mAppWidgetId <= 0 || mDuration.isZero();
            return mAppWidgetId <= 0 || mDurationMillis == 0;
        }

        /**
         * Resets the event data fields.
         */
        public void clear() {
            mDuration = Duration.ZERO;
            mStart = Instant.MAX;
            mEnd = Instant.MIN;
            mDurationMillis = 0L;
            mStart = Long.MAX_VALUE;
            mEnd = Long.MIN_VALUE;
            mPosition = null;
            mClickedIds.clear();
            mScrolledIds.clear();
        }

        public AppWidgetEvent build() {
            return new AppWidgetEvent(mAppWidgetId, mDuration, mStart, mEnd, mPosition,
            return new AppWidgetEvent(mAppWidgetId, mDurationMillis, mStart, mEnd, mPosition,
                    toIntArray(mClickedIds), toIntArray(mScrolledIds));
        }

+2 −9
Original line number Diff line number Diff line
@@ -501,7 +501,6 @@ public class AppWidgetManager {
     * impression of the widget.
     * @hide
     */
    @FlaggedApi(Flags.FLAG_ENGAGEMENT_METRICS)
    static final String EVENT_TYPE_WIDGET_INTERACTION = "widget_interaction";

    /**
@@ -509,7 +508,6 @@ public class AppWidgetManager {
     * widget user interaction events.
     * @hide
     */
    @FlaggedApi(Flags.FLAG_ENGAGEMENT_METRICS)
    public static final String EVENT_CATEGORY_APPWIDGET = "android.appwidget";

    /**
@@ -523,7 +521,6 @@ public class AppWidgetManager {
     * @see android.widget.RemoteViews#setAppWidgetEventTag
     * @hide
     */
    @FlaggedApi(Flags.FLAG_ENGAGEMENT_METRICS)
    static final String EXTRA_EVENT_CLICKED_VIEWS =
            "android.appwidget.extra.EVENT_CLICKED_VIEWS";

@@ -538,7 +535,6 @@ public class AppWidgetManager {
     * @see android.widget.RemoteViews#setAppWidgetEventTag
     * @hide
     */
    @FlaggedApi(Flags.FLAG_ENGAGEMENT_METRICS)
    static final String EXTRA_EVENT_SCROLLED_VIEWS =
            "android.appwidget.extra.EVENT_SCROLLED_VIEWS";

@@ -547,18 +543,17 @@ public class AppWidgetManager {
     * during which the widget was visible.
     * @hide
     */
    @FlaggedApi(Flags.FLAG_ENGAGEMENT_METRICS)
    static final String EXTRA_EVENT_DURATION_MS =
            "android.appwidget.extra.EVENT_DURATION_MS";

    /**
     * This bundle extra contains an integer array with 4 elements that describe the left, top,
     * right, and bottom coordinates of the widget at the end of the interaction event.
     * right, and bottom coordinates (in that order) of the widget at the end of the interaction
     * event.
     *
     * This Rect indicates the current position and size of the widget.
     * @hide
     */
    @FlaggedApi(Flags.FLAG_ENGAGEMENT_METRICS)
    static final String EXTRA_EVENT_POSITION_RECT =
            "android.appwidget.extra.EVENT_POSITION_RECT";

@@ -567,7 +562,6 @@ public class AppWidgetManager {
     * time range that this event represents.
     * @hide
     */
    @FlaggedApi(Flags.FLAG_ENGAGEMENT_METRICS)
    static final String EXTRA_EVENT_START = "android.appwidget.extra.EVENT_START";

    /**
@@ -575,7 +569,6 @@ public class AppWidgetManager {
     * range that this event represents.
     * @hide
     */
    @FlaggedApi(Flags.FLAG_ENGAGEMENT_METRICS)
    static final String EXTRA_EVENT_END = "android.appwidget.extra.EVENT_END";

    private static final String TAG = "AppWidgetManager";
+5 −0
Original line number Diff line number Diff line
@@ -56,4 +56,9 @@ public abstract class AppWidgetManagerInternal {
     */
    public abstract void applyResourceOverlaysToWidgets(Set<String> packageNames, int userId,
            boolean updateFrameworkRes);

    /**
     * Trigger reporting of widget usage events to UsageStatsService.
     */
    public abstract void reportWidgetEventsToUsageStats();
}
+3 −0
Original line number Diff line number Diff line
@@ -9918,6 +9918,9 @@
            </intent-filter>
        </service>
        <service android:name="com.android.server.appwidget.ReportWidgetEventsJob"
            android:permission="android.permission.BIND_JOB_SERVICE" />
        <provider
            android:name="com.android.server.textclassifier.IconsContentProvider"
            android:authorities="com.android.textclassifier.icons"
+2 −2
Original line number Diff line number Diff line
@@ -204,8 +204,8 @@ class AppWidgetEventsTest {
        val remoteViews = RemoteViews(context.packageName, R.layout.remote_views_test)
        hostView.updateAppWidget(remoteViews)
        assertThat(hostView.interactionLogger.event.visibleDuration.toMillis()).isEqualTo(0)
        assertThat(hostView.interactionLogger.event.start).isEqualTo(Instant.MAX)
        assertThat(hostView.interactionLogger.event.end).isEqualTo(Instant.MIN)
        assertThat(hostView.interactionLogger.event.start).isEqualTo(Instant.ofEpochMilli(Long.MAX_VALUE))
        assertThat(hostView.interactionLogger.event.end).isEqualTo(Instant.ofEpochMilli(Long.MIN_VALUE))

        ActivityScenario<Activity>.launch(EmptyActivity::class.java).use { scenario ->
            val start = Instant.now()
Loading