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.SystemService;
21 import android.content.Context;
22 import android.os.PersistableBundle;
23 import android.os.RemoteException;
24 
25 /**
26  * This class gives information about, and interacts with media metrics.
27  */
28 @SystemService(Context.MEDIA_METRICS_SERVICE)
29 public final class MediaMetricsManager {
30     public static final long INVALID_TIMESTAMP = -1;
31 
32     private static final String TAG = "MediaMetricsManager";
33 
34     private IMediaMetricsManager mService;
35     private int mUserId;
36 
37     /**
38      * @hide
39      */
MediaMetricsManager(IMediaMetricsManager service, int userId)40     public MediaMetricsManager(IMediaMetricsManager service, int userId) {
41         mService = service;
42         mUserId = userId;
43     }
44 
45     /**
46      * Reports playback metrics.
47      * @hide
48      */
reportPlaybackMetrics(@onNull String sessionId, PlaybackMetrics metrics)49     public void reportPlaybackMetrics(@NonNull String sessionId, PlaybackMetrics metrics) {
50         try {
51             mService.reportPlaybackMetrics(sessionId, metrics, mUserId);
52         } catch (RemoteException e) {
53             throw e.rethrowFromSystemServer();
54         }
55     }
56     /**
57      * Reports bundle metrics.
58      * @hide
59      */
reportBundleMetrics(@onNull String sessionId, PersistableBundle metrics)60     public void reportBundleMetrics(@NonNull String sessionId, PersistableBundle metrics) {
61         try {
62             mService.reportBundleMetrics(sessionId, metrics, mUserId);
63         } catch (RemoteException e) {
64             throw e.rethrowFromSystemServer();
65         }
66     }
67     /**
68      * Reports network event.
69      * @hide
70      */
reportNetworkEvent(@onNull String sessionId, NetworkEvent event)71     public void reportNetworkEvent(@NonNull String sessionId, NetworkEvent event) {
72         try {
73             mService.reportNetworkEvent(sessionId, event, mUserId);
74         } catch (RemoteException e) {
75             throw e.rethrowFromSystemServer();
76         }
77     }
78 
79     /**
80      * Reports playback state event.
81      * @hide
82      */
reportPlaybackStateEvent(@onNull String sessionId, PlaybackStateEvent event)83     public void reportPlaybackStateEvent(@NonNull String sessionId, PlaybackStateEvent event) {
84         try {
85             mService.reportPlaybackStateEvent(sessionId, event, mUserId);
86         } catch (RemoteException e) {
87             throw e.rethrowFromSystemServer();
88         }
89     }
90 
91     /**
92      * Reports track change event.
93      * @hide
94      */
reportTrackChangeEvent(@onNull String sessionId, TrackChangeEvent event)95     public void reportTrackChangeEvent(@NonNull String sessionId, TrackChangeEvent event) {
96         try {
97             mService.reportTrackChangeEvent(sessionId, event, mUserId);
98         } catch (RemoteException e) {
99             throw e.rethrowFromSystemServer();
100         }
101     }
102 
103     /**
104      * Creates a playback session.
105      */
106     @NonNull
createPlaybackSession()107     public PlaybackSession createPlaybackSession() {
108         try {
109             String id = mService.getPlaybackSessionId(mUserId);
110             PlaybackSession session = new PlaybackSession(id, this);
111             return session;
112         } catch (RemoteException e) {
113             throw e.rethrowFromSystemServer();
114         }
115     }
116 
117     /**
118      * Creates a recording session.
119      */
120     @NonNull
createRecordingSession()121     public RecordingSession createRecordingSession() {
122         try {
123             String id = mService.getRecordingSessionId(mUserId);
124             RecordingSession session = new RecordingSession(id, this);
125             return session;
126         } catch (RemoteException e) {
127             throw e.rethrowFromSystemServer();
128         }
129     }
130 
131     /**
132      * Creates a transcoding session.
133      */
134     @NonNull
createTranscodingSession()135     public TranscodingSession createTranscodingSession() {
136         try {
137             String id = mService.getTranscodingSessionId(mUserId);
138             TranscodingSession session = new TranscodingSession(id, this);
139             return session;
140         } catch (RemoteException e) {
141             throw e.rethrowFromSystemServer();
142         }
143     }
144 
145     /**
146      * Creates a editing session.
147      */
148     @NonNull
createEditingSession()149     public EditingSession createEditingSession() {
150         try {
151             String id = mService.getEditingSessionId(mUserId);
152             EditingSession session = new EditingSession(id, this);
153             return session;
154         } catch (RemoteException e) {
155             throw e.rethrowFromSystemServer();
156         }
157     }
158 
159     /**
160      * Creates a generic bundle session.
161      */
162     @NonNull
createBundleSession()163     public BundleSession createBundleSession() {
164         try {
165             String id = mService.getBundleSessionId(mUserId);
166             BundleSession session = new BundleSession(id, this);
167             return session;
168         } catch (RemoteException e) {
169             throw e.rethrowFromSystemServer();
170         }
171     }
172 
173     /**
174      * Creates a generic bundle session.
175      */
176     @NonNull
releaseSessionId(@onNull String sessionId)177     public void releaseSessionId(@NonNull String sessionId) {
178         try {
179             mService.releaseSessionId(sessionId, mUserId);
180         } catch (RemoteException e) {
181             throw e.rethrowFromSystemServer();
182         }
183     }
184 
185     /**
186      * Reports error event.
187      * @hide
188      */
reportPlaybackErrorEvent(@onNull String sessionId, PlaybackErrorEvent event)189     public void reportPlaybackErrorEvent(@NonNull String sessionId, PlaybackErrorEvent event) {
190         try {
191             mService.reportPlaybackErrorEvent(sessionId, event, mUserId);
192         } catch (RemoteException e) {
193             throw e.rethrowFromSystemServer();
194         }
195     }
196 }
197