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

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

Report widget events to FrameworkStatsLog

Updates AppWidgetServiceImpl to write widget events to
FrameworkStatsLog.

Bug: 435707129
Test: manual, with Atom Tester
Test: atest CtsAppWidgetHostTestCases:StatsTest
Flag: android.appwidget.flags.engagement_metrics
Change-Id: I14a7eb733a5d63bbb3989061b0a237fddfec0e6e
parent e591d195
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -58,7 +58,7 @@ public abstract class AppWidgetManagerInternal {
            boolean updateFrameworkRes);

    /**
     * Trigger reporting of widget usage events to UsageStatsService.
     * Trigger saving of any pending widget usage events to UsageStatsService and FrameworkStatsLog.
     */
    public abstract void reportWidgetEventsToUsageStats();
    public abstract void saveWidgetEvents();
}
+44 −16
Original line number Diff line number Diff line
@@ -5105,9 +5105,9 @@ class AppWidgetServiceImpl extends IAppWidgetService.Stub implements WidgetBacku
                    }
                    return;
                }
                widget.event.merge(event);
                widget.eventBuilder.merge(event);
                if (mWidgetEventsReportIntervalMs <= 0) {
                    widget.reportWidgetEventIfNeededLocked(mUsageStatsManagerInternal);
                    widget.saveWidgetEventIfNeededLocked(mUsageStatsManagerInternal);
                }
            }
        }
@@ -5649,16 +5649,16 @@ class AppWidgetServiceImpl extends IAppWidgetService.Stub implements WidgetBacku
    }

    /**
     * Reports any pending widget events to UsageStatsManager.
     * Saves any pending widget events to UsageStatsService and FrameworkStatsLog.
     */
    private void reportWidgetEventsToUsageStats() {
    private void saveWidgetEvents() {
        if (DEBUG) {
            Slog.i(TAG, "reportWidgetEventsToUsageStats");
            Slog.i(TAG, "saveWidgetEvents");
        }
        synchronized (mLock) {
            final int widgetCount = mWidgets.size();
            for (int i = 0; i < widgetCount; i++) {
                mWidgets.get(i).reportWidgetEventIfNeededLocked(mUsageStatsManagerInternal);
                mWidgets.get(i).saveWidgetEventIfNeededLocked(mUsageStatsManagerInternal);
            }
        }
    }
@@ -6477,7 +6477,7 @@ class AppWidgetServiceImpl extends IAppWidgetService.Stub implements WidgetBacku
        SparseLongArray updateSequenceNos = new SparseLongArray(2);
        boolean trackingUpdate = false;
        boolean isFirstConfigActivityPending = false;
        final AppWidgetEvent.Builder event = new AppWidgetEvent.Builder();
        final AppWidgetEvent.Builder eventBuilder = new AppWidgetEvent.Builder();

        @Override
        public String toString() {
@@ -6503,23 +6503,51 @@ class AppWidgetServiceImpl extends IAppWidgetService.Stub implements WidgetBacku
        }

        /**
         * Reports a widget event to UsageStatsManager if there is event data to report.
         * If the eventBuilder is not empty, saves the pending widget event to UsageStatsService and
         * FrameworkStatsLog.
         */
        public void reportWidgetEventIfNeededLocked(
        public void saveWidgetEventIfNeededLocked(
                @NonNull UsageStatsManagerInternal usageStatsManager) {
            // Each event must have a non-zero duration.
            if (event.isEmpty()) {
            if (eventBuilder.isEmpty() || provider == null) {
                return;
            }

            AppWidgetEvent event = eventBuilder.build();
            usageStatsManager.reportUserInteractionEvent(
                    provider.id.componentName.getPackageName(),
                    UserHandle.getUserId(provider.id.uid), event.build().toBundle());
                    UserHandle.getUserId(provider.id.uid), event.toBundle());

            int hostUid = host != null ? host.id.uid : -1;
            String providerComponent =
                    provider.info != null ? provider.info.provider.flattenToString() : null;
            int left, top, right, bottom;
            if (event.getPosition() != null) {
                left = event.getPosition().left;
                top = event.getPosition().top;
                right = event.getPosition().right;
                bottom = event.getPosition().bottom;
            } else {
                left = -1;
                top = -1;
                right = -1;
                bottom = -1;
            }
            FrameworkStatsLog.write(FrameworkStatsLog.WIDGET_INTERACTION_EVENT,
                    /* hostUid= */ hostUid,
                    /* provider= */ providerComponent,
                    /* start= */ event.getStart().toEpochMilli(),
                    /* end= */ event.getEnd().toEpochMilli(),
                    /* visibleDuration= */ event.getVisibleDuration().toMillis(),
                    /* rectLeft= */ left,
                    /* rectTop= */ top,
                    /* rectRight= */ right,
                    /* rectBottom= */ bottom);

            if (DEBUG) {
                Slog.i(TAG, "Reported widget interaction usage event: " + event.build());
                Slog.i(TAG, "Reported widget interaction usage event: " + event);
            }

            event.clear();
            eventBuilder.clear();
        }
    }

@@ -7381,8 +7409,8 @@ class AppWidgetServiceImpl extends IAppWidgetService.Stub implements WidgetBacku
        }

        @Override
        public void reportWidgetEventsToUsageStats() {
            AppWidgetServiceImpl.this.reportWidgetEventsToUsageStats();
        public void saveWidgetEvents() {
            AppWidgetServiceImpl.this.saveWidgetEvents();
        }
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -68,7 +68,7 @@ public class ReportWidgetEventsJob extends JobService {
    public boolean onStartJob(JobParameters params) {
        BackgroundThread.getExecutor().execute(() -> {
            LocalServices.getService(AppWidgetManagerInternal.class)
                    .reportWidgetEventsToUsageStats();
                    .saveWidgetEvents();
            jobFinished(params, /* wantsReschedule= */ false);
        });
        // Return true to indicate that job is still running in another thread.