Add initial playback metrics classes

Test: make;
Bug: 167036690
Change-Id: I78aa08374b45ffc86a3253037de724a2d3750977
diff --git a/media/java/android/media/metrics/IPlaybackMetricsManager.aidl b/media/java/android/media/metrics/IPlaybackMetricsManager.aidl
new file mode 100644
index 0000000..fcb7d60
--- /dev/null
+++ b/media/java/android/media/metrics/IPlaybackMetricsManager.aidl
@@ -0,0 +1,27 @@
+/*
+ * 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.media.metrics.PlaybackMetrics;
+
+/**
+ * Interface to the playback manager service.
+ * @hide
+ */
+interface IPlaybackMetricsManager {
+    void reportPlaybackMetrics(in PlaybackMetrics metrics, int userId);
+}
\ No newline at end of file
diff --git a/media/java/android/media/metrics/OWNERS b/media/java/android/media/metrics/OWNERS
new file mode 100644
index 0000000..f8696ef
--- /dev/null
+++ b/media/java/android/media/metrics/OWNERS
@@ -0,0 +1,3 @@
+essick@google.com
+nchalko@google.com
+shubang@google.com
\ No newline at end of file
diff --git a/media/java/android/media/metrics/PlaybackMetrics.aidl b/media/java/android/media/metrics/PlaybackMetrics.aidl
new file mode 100644
index 0000000..321025b
--- /dev/null
+++ b/media/java/android/media/metrics/PlaybackMetrics.aidl
@@ -0,0 +1,19 @@
+/*
+ * 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 PlaybackMetrics;
diff --git a/media/java/android/media/metrics/PlaybackMetrics.java b/media/java/android/media/metrics/PlaybackMetrics.java
new file mode 100644
index 0000000..82a5803
--- /dev/null
+++ b/media/java/android/media/metrics/PlaybackMetrics.java
@@ -0,0 +1,88 @@
+/*
+ * 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 android.os.Parcel;
+import android.os.Parcelable;
+
+import java.util.Objects;
+
+/**
+ * This class is used to store playback data.
+ * @hide
+ */
+public final class PlaybackMetrics implements Parcelable {
+    private int mStreamSourceType;
+
+    /**
+     * Creates a new PlaybackMetrics.
+     *
+     * @hide
+     */
+    public PlaybackMetrics(int streamSourceType) {
+        this.mStreamSourceType = streamSourceType;
+    }
+
+    public int getStreamSourceType() {
+        return mStreamSourceType;
+    }
+
+    @Override
+    public boolean equals(@Nullable Object o) {
+
+        if (this == o) return true;
+        if (o == null || getClass() != o.getClass()) return false;
+        PlaybackMetrics that = (PlaybackMetrics) o;
+        return mStreamSourceType == that.mStreamSourceType;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(mStreamSourceType);
+    }
+
+    @Override
+    public void writeToParcel(@NonNull Parcel dest, int flags) {
+        dest.writeInt(mStreamSourceType);
+    }
+
+    @Override
+    public int describeContents() {
+        return 0;
+    }
+
+    /** @hide */
+    /* package-private */ PlaybackMetrics(@NonNull Parcel in) {
+        int streamSourceType = in.readInt();
+        this.mStreamSourceType = streamSourceType;
+    }
+
+    public static final @NonNull Parcelable.Creator<PlaybackMetrics> CREATOR =
+            new Parcelable.Creator<PlaybackMetrics>() {
+                @Override
+                public PlaybackMetrics[] newArray(int size) {
+                    return new PlaybackMetrics[size];
+                }
+
+                @Override
+                public PlaybackMetrics createFromParcel(@NonNull Parcel in) {
+                    return new PlaybackMetrics(in);
+                }
+            };
+}
diff --git a/media/java/android/media/metrics/PlaybackMetricsManager.java b/media/java/android/media/metrics/PlaybackMetricsManager.java
new file mode 100644
index 0000000..3606f53
--- /dev/null
+++ b/media/java/android/media/metrics/PlaybackMetricsManager.java
@@ -0,0 +1,49 @@
+/*
+ * 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.os.RemoteException;
+
+/**
+ * @hide
+ */
+public class PlaybackMetricsManager {
+    // TODO: unhide APIs.
+    private static final String TAG = "PlaybackMetricsManager";
+
+    private IPlaybackMetricsManager mService;
+    private int mUserId;
+
+    /**
+     * @hide
+     */
+    public PlaybackMetricsManager(IPlaybackMetricsManager service, int userId) {
+        mService = service;
+        mUserId = userId;
+    }
+
+    /**
+     * Reports playback metrics.
+     */
+    public void reportPlaybackMetrics(PlaybackMetrics metrics) {
+        try {
+            mService.reportPlaybackMetrics(metrics, mUserId);
+        } catch (RemoteException e) {
+            throw e.rethrowFromSystemServer();
+        }
+    }
+}
diff --git a/services/core/java/com/android/server/media/metrics/OWNERS b/services/core/java/com/android/server/media/metrics/OWNERS
new file mode 100644
index 0000000..f8696ef
--- /dev/null
+++ b/services/core/java/com/android/server/media/metrics/OWNERS
@@ -0,0 +1,3 @@
+essick@google.com
+nchalko@google.com
+shubang@google.com
\ No newline at end of file
diff --git a/services/core/java/com/android/server/media/metrics/PlaybackMetricsManagerService.java b/services/core/java/com/android/server/media/metrics/PlaybackMetricsManagerService.java
new file mode 100644
index 0000000..adb9869
--- /dev/null
+++ b/services/core/java/com/android/server/media/metrics/PlaybackMetricsManagerService.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright 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 com.android.server.media.metrics;
+
+import android.content.Context;
+import android.media.metrics.IPlaybackMetricsManager;
+import android.media.metrics.PlaybackMetrics;
+
+import com.android.server.SystemService;
+
+/**
+ * System service manages playback metrics.
+ */
+public final class PlaybackMetricsManagerService extends SystemService {
+
+    /**
+     * Initializes the playback metrics manager service.
+     *
+     * @param context The system server context.
+     */
+    public PlaybackMetricsManagerService(Context context) {
+        super(context);
+    }
+
+    @Override
+    public void onStart() {
+        // TODO: make the service name a constant in Context.java
+        publishBinderService("playback_metrics", new BinderService());
+    }
+
+    private final class BinderService extends IPlaybackMetricsManager.Stub {
+        @Override
+        public void reportPlaybackMetrics(PlaybackMetrics metrics, int userId) {
+            // TODO: log it to statsd
+        }
+    }
+}