summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--media/java/android/media/MediaMetrics.java95
1 files changed, 95 insertions, 0 deletions
diff --git a/media/java/android/media/MediaMetrics.java b/media/java/android/media/MediaMetrics.java
index 88a829546989..540955f3b393 100644
--- a/media/java/android/media/MediaMetrics.java
+++ b/media/java/android/media/MediaMetrics.java
@@ -17,6 +17,7 @@
package android.media;
import android.annotation.NonNull;
+import android.annotation.Nullable;
import android.annotation.TestApi;
import android.os.Bundle;
@@ -24,6 +25,7 @@ import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
+import java.util.Objects;
/**
* MediaMetrics is the Java interface to the MediaMetrics service.
@@ -50,6 +52,77 @@ public class MediaMetrics {
private static final Charset MEDIAMETRICS_CHARSET = StandardCharsets.UTF_8;
/**
+ * Key interface.
+ *
+ * The presence of this {@code Key} interface on an object allows
+ * it to be used to set metrics.
+ *
+ * @param <T> type of value associated with {@code Key}.
+ */
+ public interface Key<T> {
+ /**
+ * Returns the internal name of the key.
+ */
+ @NonNull
+ String getName();
+
+ /**
+ * Returns the class type of the associated value.
+ */
+ @NonNull
+ Class<T> getValueClass();
+ }
+
+ /**
+ * Returns a Key object with the correct interface for MediaMetrics.
+ *
+ * @param name The name of the key.
+ * @param type The class type of the value represented by the key.
+ * @param <T> The type of value.
+ * @return a new key interface.
+ */
+ @NonNull
+ public static <T> Key<T> createKey(@NonNull String name, @NonNull Class<T> type) {
+ // Implementation specific.
+ return new Key<T>() {
+ private final String mName = name;
+ private final Class<T> mType = type;
+
+ @Override
+ @NonNull
+ public String getName() {
+ return mName;
+ }
+
+ @Override
+ @NonNull
+ public Class<T> getValueClass() {
+ return mType;
+ }
+
+ /**
+ * Return true if the name and the type of two objects are the same.
+ */
+ @Override
+ public boolean equals(Object obj) {
+ if (obj == this) {
+ return true;
+ }
+ if (!(obj instanceof Key)) {
+ return false;
+ }
+ Key<?> other = (Key<?>) obj;
+ return mName.equals(other.getName()) && mType.equals(other.getValueClass());
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(mName, mType);
+ }
+ };
+ }
+
+ /**
* Item records properties and delivers to the MediaMetrics service
*
*/
@@ -202,6 +275,28 @@ public class MediaMetrics {
}
/**
+ * Sets a metrics typed key
+ * @param key
+ * @param value
+ * @param <T>
+ * @return
+ */
+ @NonNull
+ public <T> Item set(@NonNull Key<T> key, @Nullable T value) {
+ if (value instanceof Integer) {
+ putInt(key.getName(), (int) value);
+ } else if (value instanceof Long) {
+ putLong(key.getName(), (long) value);
+ } else if (value instanceof Double) {
+ putDouble(key.getName(), (double) value);
+ } else if (value instanceof String) {
+ putString(key.getName(), (String) value);
+ }
+ // if value is null, etc. no error is raised.
+ return this;
+ }
+
+ /**
* Sets the property with key to an integer (32 bit) value.
*
* @param key