Add playback session class

Test: make;
Bug: 167036690
Change-Id: Iea7c1f76fcabeac9a48082d22d0d06cddd60a5de
This commit is contained in:
shubang
2020-11-25 13:28:56 -08:00
parent 26ca287da7
commit d0e06f4d07
4 changed files with 107 additions and 4 deletions

View File

@@ -23,5 +23,6 @@ import android.media.metrics.PlaybackMetrics;
* @hide
*/
interface IPlaybackMetricsManager {
void reportPlaybackMetrics(in PlaybackMetrics metrics, int userId);
void reportPlaybackMetrics(in String sessionId, in PlaybackMetrics metrics, int userId);
String getSessionId(int userId);
}

View File

@@ -16,6 +16,7 @@
package android.media.metrics;
import android.annotation.NonNull;
import android.os.RemoteException;
/**
@@ -38,10 +39,24 @@ public class PlaybackMetricsManager {
/**
* Reports playback metrics.
* @hide
*/
public void reportPlaybackMetrics(PlaybackMetrics metrics) {
public void reportPlaybackMetrics(@NonNull String sessionId, PlaybackMetrics metrics) {
try {
mService.reportPlaybackMetrics(metrics, mUserId);
mService.reportPlaybackMetrics(sessionId, metrics, mUserId);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Creates a playback session.
*/
public PlaybackSession createSession() {
try {
String id = mService.getSessionId(mUserId);
PlaybackSession session = new PlaybackSession(id, this);
return session;
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}

View File

@@ -0,0 +1,74 @@
/*
* 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.NonNull;
import android.annotation.Nullable;
import com.android.internal.util.AnnotationValidations;
import java.util.Objects;
/**
* @hide
*/
public final class PlaybackSession implements AutoCloseable {
private final @NonNull String mId;
private final @NonNull PlaybackMetricsManager mManager;
private boolean mClosed = false;
/**
* Creates a new PlaybackSession.
*
* @hide
*/
public PlaybackSession(@NonNull String id, @NonNull PlaybackMetricsManager manager) {
mId = id;
mManager = manager;
AnnotationValidations.validate(NonNull.class, null, mId);
AnnotationValidations.validate(NonNull.class, null, mManager);
}
/**
* Reports playback metrics.
*/
public void reportPlaybackMetrics(@NonNull PlaybackMetrics metrics) {
mManager.reportPlaybackMetrics(mId, metrics);
}
public @NonNull String getId() {
return mId;
}
@Override
public boolean equals(@Nullable Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PlaybackSession that = (PlaybackSession) o;
return Objects.equals(mId, that.mId);
}
@Override
public int hashCode() {
return Objects.hash(mId);
}
@Override
public void close() throws Exception {
mClosed = true;
}
}

View File

@@ -19,13 +19,17 @@ package com.android.server.media.metrics;
import android.content.Context;
import android.media.metrics.IPlaybackMetricsManager;
import android.media.metrics.PlaybackMetrics;
import android.util.Base64;
import com.android.server.SystemService;
import java.security.SecureRandom;
/**
* System service manages playback metrics.
*/
public final class PlaybackMetricsManagerService extends SystemService {
private final SecureRandom mSecureRandom;
/**
* Initializes the playback metrics manager service.
@@ -34,6 +38,7 @@ public final class PlaybackMetricsManagerService extends SystemService {
*/
public PlaybackMetricsManagerService(Context context) {
super(context);
mSecureRandom = new SecureRandom();
}
@Override
@@ -44,8 +49,16 @@ public final class PlaybackMetricsManagerService extends SystemService {
private final class BinderService extends IPlaybackMetricsManager.Stub {
@Override
public void reportPlaybackMetrics(PlaybackMetrics metrics, int userId) {
public void reportPlaybackMetrics(String sessionId, PlaybackMetrics metrics, int userId) {
// TODO: log it to statsd
}
@Override
public String getSessionId(int userId) {
byte[] byteId = new byte[16]; // 128 bits
mSecureRandom.nextBytes(byteId);
String id = Base64.encodeToString(byteId, Base64.DEFAULT);
return id;
}
}
}