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

Commit e41e441e authored by Chris Göllner's avatar Chris Göllner
Browse files

[Media Projection] Fix LAST_ACTIVE_SESSION having the wrong timestamp

It can happen that stop is called before the session was initiated,
when apps wrongly reuse the token.

The fix here is to only register the last active session, when there
actually was an active session ongoing.

Test: atest FrameworksServicesTests:MediaProjectionMetricsLoggerTest
Fixes: 307223500
Change-Id: I1dc5a08fd6d7e0d9cc42c7c7ce7b33284309b7c5
parent 794b6253
Loading
Loading
Loading
Loading
+15 −1
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import static com.android.internal.util.FrameworkStatsLog.MEDIA_PROJECTION_STATE
import static com.android.internal.util.FrameworkStatsLog.MEDIA_PROJECTION_STATE_CHANGED__STATE__MEDIA_PROJECTION_STATE_STOPPED;

import android.content.Context;
import android.util.Log;

import com.android.internal.util.FrameworkStatsLog;

@@ -31,6 +32,8 @@ import java.time.Duration;

/** Class for emitting logs describing a MediaProjection session. */
public class MediaProjectionMetricsLogger {
    private static final String TAG = "MediaProjectionMetricsLogger";

    private static final int TARGET_UID_UNKNOWN = -2;
    private static final int TIME_SINCE_LAST_ACTIVE_UNKNOWN = -1;

@@ -82,6 +85,7 @@ public class MediaProjectionMetricsLogger {
     *     </ul>
     */
    public void logInitiated(int hostUid, int sessionCreationSource) {
        Log.d(TAG, "logInitiated");
        Duration durationSinceLastActiveSession = mTimestampStore.timeSinceLastActiveSession();
        int timeSinceLastActiveInSeconds =
                durationSinceLastActiveSession == null
@@ -103,6 +107,7 @@ public class MediaProjectionMetricsLogger {
     * @param hostUid UID of the package that initiates MediaProjection.
     */
    public void logPermissionRequestDisplayed(int hostUid) {
        Log.d(TAG, "logPermissionRequestDisplayed");
        write(
                mSessionIdGenerator.getCurrentSessionId(),
                MEDIA_PROJECTION_STATE_CHANGED__STATE__MEDIA_PROJECTION_STATE_PERMISSION_REQUEST_DISPLAYED,
@@ -118,6 +123,7 @@ public class MediaProjectionMetricsLogger {
     * @param hostUid UID of the package that initiates MediaProjection.
     */
    public void logAppSelectorDisplayed(int hostUid) {
        Log.d(TAG, "logAppSelectorDisplayed");
        write(
                mSessionIdGenerator.getCurrentSessionId(),
                MEDIA_PROJECTION_STATE_CHANGED__STATE__MEDIA_PROJECTION_STATE_APP_SELECTOR_DISPLAYED,
@@ -134,6 +140,7 @@ public class MediaProjectionMetricsLogger {
     * @param targetUid UID of the package that is captured if selected.
     */
    public void logInProgress(int hostUid, int targetUid) {
        Log.d(TAG, "logInProgress");
        write(
                mSessionIdGenerator.getCurrentSessionId(),
                MEDIA_PROJECTION_STATE_CHANGED__STATE__MEDIA_PROJECTION_STATE_CAPTURING_IN_PROGRESS,
@@ -150,6 +157,10 @@ public class MediaProjectionMetricsLogger {
     * @param targetUid UID of the package that is captured if selected.
     */
    public void logStopped(int hostUid, int targetUid) {
        boolean wasCaptureInProgress =
                mPreviousState
                        == MEDIA_PROJECTION_STATE_CHANGED__STATE__MEDIA_PROJECTION_STATE_CAPTURING_IN_PROGRESS;
        Log.d(TAG, "logStopped: wasCaptureInProgress=" + wasCaptureInProgress);
        write(
                mSessionIdGenerator.getCurrentSessionId(),
                MEDIA_PROJECTION_STATE_CHANGED__STATE__MEDIA_PROJECTION_STATE_STOPPED,
@@ -157,8 +168,11 @@ public class MediaProjectionMetricsLogger {
                targetUid,
                TIME_SINCE_LAST_ACTIVE_UNKNOWN,
                MEDIA_PROJECTION_STATE_CHANGED__CREATION_SOURCE__CREATION_SOURCE_UNKNOWN);

        if (wasCaptureInProgress) {
            mTimestampStore.registerActiveSessionEnded();
        }
    }

    public void notifyProjectionStateChange(int hostUid, int state, int sessionCreationSource) {
        write(hostUid, state, sessionCreationSource);
+12 −15
Original line number Diff line number Diff line
@@ -26,7 +26,7 @@ import static com.android.internal.util.FrameworkStatsLog.MEDIA_PROJECTION_STATE

import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@@ -40,7 +40,6 @@ import com.android.internal.util.FrameworkStatsLog;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

@@ -219,21 +218,19 @@ public class MediaProjectionMetricsLoggerTest {
    }

    @Test
    public void logStopped_registersActiveSessionEnded_afterLogging() {
    public void logStopped_capturingWasInProgress_registersActiveSessionEnded() {
        mLogger.logInProgress(TEST_HOST_UID, TEST_TARGET_UID);

        mLogger.logStopped(TEST_HOST_UID, TEST_TARGET_UID);

        InOrder inOrder = inOrder(mFrameworkStatsLogWrapper, mTimestampStore);
        inOrder.verify(mFrameworkStatsLogWrapper)
                .write(
                        /* code= */ anyInt(),
                        /* sessionId= */ anyInt(),
                        /* state= */ anyInt(),
                        /* previousState= */ anyInt(),
                        /* hostUid= */ anyInt(),
                        /* targetUid= */ anyInt(),
                        /* timeSinceLastActive= */ anyInt(),
                        /* creationSource= */ anyInt());
        inOrder.verify(mTimestampStore).registerActiveSessionEnded();
        verify(mTimestampStore).registerActiveSessionEnded();
    }

    @Test
    public void logStopped_capturingWasNotInProgress_doesNotRegistersActiveSessionEnded() {
        mLogger.logStopped(TEST_HOST_UID, TEST_TARGET_UID);

        verify(mTimestampStore, never()).registerActiveSessionEnded();
    }

    @Test