1 /*
2  * Copyright (C) 2021 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 import android.os.PersistableBundle;
22 
23 import com.android.internal.util.AnnotationValidations;
24 
25 import java.util.Objects;
26 
27 /**
28  * An instances of this class represents a session with data stored in a bundle.
29  */
30 public final class BundleSession implements AutoCloseable {
31     private final @NonNull String mId;
32     private final @NonNull MediaMetricsManager mManager;
33     private final @NonNull LogSessionId mLogSessionId;
34 
35     /**
36      * A key describing the statsd atom into which to place this bundle's other contents.
37      * The associated value is an integer.
38      *
39      * @see #reportBundleMetrics
40      */
41 
42     public static final String KEY_STATSD_ATOM = "bundlesession-statsd-atom";
43 
44     /** @hide */
BundleSession(@onNull String id, @NonNull MediaMetricsManager manager)45     public BundleSession(@NonNull String id, @NonNull MediaMetricsManager manager) {
46         mId = id;
47         mManager = manager;
48         AnnotationValidations.validate(NonNull.class, null, mId);
49         AnnotationValidations.validate(NonNull.class, null, mManager);
50         mLogSessionId = new LogSessionId(mId);
51     }
52 
53     /**
54      * Reports metrics via bundle.
55      *
56      * The key {@link #KEY_STATSD_ATOM} references an integer value that
57      * indicates the statsd atom for the data in this bundle. Other keys
58      * and their types are defined on a per-atom basis.
59      *
60      */
reportBundleMetrics(@onNull PersistableBundle metrics)61     public void reportBundleMetrics(@NonNull PersistableBundle metrics) {
62         mManager.reportBundleMetrics(mId, metrics);
63     }
64 
getSessionId()65     public @NonNull LogSessionId getSessionId() {
66         return mLogSessionId;
67     }
68 
69     @Override
equals(@ullable Object o)70     public boolean equals(@Nullable Object o) {
71         if (this == o) return true;
72         if (o == null || getClass() != o.getClass()) return false;
73         BundleSession that = (BundleSession) o;
74         return Objects.equals(mId, that.mId);
75     }
76 
77     @Override
hashCode()78     public int hashCode() {
79         return Objects.hash(mId);
80     }
81 
82     @Override
close()83     public void close() {
84         mManager.releaseSessionId(mLogSessionId.getStringId());
85     }
86 }
87