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

Commit ba870c7b authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Add playback state event"

parents 5e18504f f2536f04
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package android.media.metrics;
import android.media.metrics.NetworkEvent;
import android.media.metrics.PlaybackErrorEvent;
import android.media.metrics.PlaybackMetrics;
import android.media.metrics.PlaybackStateEvent;

/**
 * Interface to the playback manager service.
@@ -27,6 +28,7 @@ import android.media.metrics.PlaybackMetrics;
interface IPlaybackMetricsManager {
    void reportPlaybackMetrics(in String sessionId, in PlaybackMetrics metrics, int userId);
    String getSessionId(int userId);
    void reportPlaybackErrorEvent(in String sessionId, in PlaybackErrorEvent event, int userId);
    void reportNetworkEvent(in String sessionId, in NetworkEvent event, int userId);
    void reportPlaybackErrorEvent(in String sessionId, in PlaybackErrorEvent event, int userId);
    void reportPlaybackStateEvent(in String sessionId, in PlaybackStateEvent event, int userId);
}
 No newline at end of file
+12 −0
Original line number Diff line number Diff line
@@ -60,6 +60,18 @@ public class PlaybackMetricsManager {
        }
    }

    /**
     * Reports playback state event.
     * @hide
     */
    public void reportPlaybackStateEvent(@NonNull String sessionId, PlaybackStateEvent event) {
        try {
            mService.reportPlaybackStateEvent(sessionId, event, mUserId);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

    /**
     * Creates a playback session.
     */
+7 −0
Original line number Diff line number Diff line
@@ -64,6 +64,13 @@ public final class PlaybackSession implements AutoCloseable {
        mManager.reportNetworkEvent(mId, event);
    }

    /**
     * Reports playback state event.
     */
    public void reportPlaybackStateEvent(PlaybackStateEvent event) {
        mManager.reportPlaybackStateEvent(mId, event);
    }

    public @NonNull String getId() {
        return mId;
    }
+19 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.media.metrics;

parcelable PlaybackStateEvent;
+153 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.media.metrics;

import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.os.Parcel;
import android.os.Parcelable;

import java.lang.annotation.Retention;
import java.util.Objects;

/**
 * Playback state event.
 * @hide
 */
public final class PlaybackStateEvent implements Parcelable {
    // TODO: more states
    /** Playback has not started (initial state) */
    public static final int STATE_NOT_STARTED = 0;
    /** Playback is buffering in the background for initial playback start */
    public static final int STATE_JOINING_BACKGROUND = 1;
    /** Playback is buffering in the foreground for initial playback start */
    public static final int STATE_JOINING_FOREGROUND = 2;
    /** Playback is actively playing */
    public static final int STATE_PLAYING = 3;
    /** Playback is paused but ready to play */
    public static final int STATE_PAUSED = 4;

    private int mState;
    private long mTimeSincePlaybackCreatedMillis;

    // These track ExoPlayer states. See the ExoPlayer documentation for the state transitions.
    @IntDef(prefix = "STATE_", value = {
        STATE_NOT_STARTED,
        STATE_JOINING_BACKGROUND,
        STATE_JOINING_FOREGROUND,
        STATE_PLAYING,
        STATE_PAUSED
    })
    @Retention(java.lang.annotation.RetentionPolicy.SOURCE)
    public @interface State {}

    /**
     * Converts playback state to string.
     */
    public static String stateToString(@State int value) {
        switch (value) {
            case STATE_NOT_STARTED:
                return "STATE_NOT_STARTED";
            case STATE_JOINING_BACKGROUND:
                return "STATE_JOINING_BACKGROUND";
            case STATE_JOINING_FOREGROUND:
                return "STATE_JOINING_FOREGROUND";
            case STATE_PLAYING:
                return "STATE_PLAYING";
            case STATE_PAUSED:
                return "STATE_PAUSED";
            default:
                return Integer.toHexString(value);
        }
    }

    /**
     * Creates a new PlaybackStateEvent.
     *
     * @hide
     */
    public PlaybackStateEvent(
            int state,
            long timeSincePlaybackCreatedMillis) {
        this.mState = state;
        this.mTimeSincePlaybackCreatedMillis = timeSincePlaybackCreatedMillis;
    }

    /**
     * Gets playback state.
     * @return
     */
    public int getState() {
        return mState;
    }

    /**
     * Gets time since the corresponding playback is created in millisecond.
     */
    public long getTimeSincePlaybackCreatedMillis() {
        return mTimeSincePlaybackCreatedMillis;
    }

    @Override
    public boolean equals(@Nullable Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        PlaybackStateEvent that = (PlaybackStateEvent) o;
        return mState == that.mState
                && mTimeSincePlaybackCreatedMillis == that.mTimeSincePlaybackCreatedMillis;
    }

    @Override
    public int hashCode() {
        return Objects.hash(mState, mTimeSincePlaybackCreatedMillis);
    }

    @Override
    public void writeToParcel(@NonNull Parcel dest, int flags) {
        dest.writeInt(mState);
        dest.writeLong(mTimeSincePlaybackCreatedMillis);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    /** @hide */
    /* package-private */ PlaybackStateEvent(@NonNull Parcel in) {
        int state = in.readInt();
        long timeSincePlaybackCreatedMillis = in.readLong();

        this.mState = state;
        this.mTimeSincePlaybackCreatedMillis = timeSincePlaybackCreatedMillis;
    }

    public static final @NonNull Parcelable.Creator<PlaybackStateEvent> CREATOR =
            new Parcelable.Creator<PlaybackStateEvent>() {
        @Override
        public PlaybackStateEvent[] newArray(int size) {
            return new PlaybackStateEvent[size];
        }

        @Override
        public PlaybackStateEvent createFromParcel(@NonNull Parcel in) {
            return new PlaybackStateEvent(in);
        }
    };

}
Loading