1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.media.metrics; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 22 import com.android.internal.util.AnnotationValidations; 23 24 import java.util.Objects; 25 26 /** 27 * An instances of this class represents a session of media playback. 28 */ 29 public final class PlaybackSession implements AutoCloseable { 30 private final @NonNull String mId; 31 private final @NonNull MediaMetricsManager mManager; 32 private final @NonNull LogSessionId mLogSessionId; 33 private boolean mClosed = false; 34 35 /** 36 * Creates a new PlaybackSession. 37 * 38 * @hide 39 */ PlaybackSession(@onNull String id, @NonNull MediaMetricsManager manager)40 public PlaybackSession(@NonNull String id, @NonNull MediaMetricsManager manager) { 41 mId = id; 42 mManager = manager; 43 AnnotationValidations.validate(NonNull.class, null, mId); 44 AnnotationValidations.validate(NonNull.class, null, mManager); 45 mLogSessionId = new LogSessionId(mId); 46 } 47 48 /** 49 * Reports playback metrics. 50 */ reportPlaybackMetrics(@onNull PlaybackMetrics metrics)51 public void reportPlaybackMetrics(@NonNull PlaybackMetrics metrics) { 52 mManager.reportPlaybackMetrics(mId, metrics); 53 } 54 55 /** 56 * Reports error event. 57 */ reportPlaybackErrorEvent(@onNull PlaybackErrorEvent event)58 public void reportPlaybackErrorEvent(@NonNull PlaybackErrorEvent event) { 59 mManager.reportPlaybackErrorEvent(mId, event); 60 } 61 62 /** 63 * Reports network event. 64 */ reportNetworkEvent(@onNull NetworkEvent event)65 public void reportNetworkEvent(@NonNull NetworkEvent event) { 66 mManager.reportNetworkEvent(mId, event); 67 } 68 69 /** 70 * Reports playback state event. 71 */ reportPlaybackStateEvent(@onNull PlaybackStateEvent event)72 public void reportPlaybackStateEvent(@NonNull PlaybackStateEvent event) { 73 mManager.reportPlaybackStateEvent(mId, event); 74 } 75 76 /** 77 * Reports track change event. 78 */ reportTrackChangeEvent(@onNull TrackChangeEvent event)79 public void reportTrackChangeEvent(@NonNull TrackChangeEvent event) { 80 mManager.reportTrackChangeEvent(mId, event); 81 } 82 getSessionId()83 public @NonNull LogSessionId getSessionId() { 84 return mLogSessionId; 85 } 86 87 @Override equals(@ullable Object o)88 public boolean equals(@Nullable Object o) { 89 if (this == o) return true; 90 if (o == null || getClass() != o.getClass()) return false; 91 PlaybackSession that = (PlaybackSession) o; 92 return Objects.equals(mId, that.mId); 93 } 94 95 @Override hashCode()96 public int hashCode() { 97 return Objects.hash(mId); 98 } 99 100 @Override close()101 public void close() { 102 mClosed = true; 103 mManager.releaseSessionId(mLogSessionId.getStringId()); 104 } 105 } 106