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.window;
18 
19 import android.os.IBinder;
20 import android.os.RemoteException;
21 import android.os.SystemClock;
22 import android.util.Singleton;
23 
24 /**
25  * A helper class for who plays transition animation can report its metrics easily.
26  * @hide
27  */
28 public class TransitionMetrics {
29 
30     private final ITransitionMetricsReporter mTransitionMetricsReporter;
31 
TransitionMetrics(ITransitionMetricsReporter reporter)32     private TransitionMetrics(ITransitionMetricsReporter reporter) {
33         mTransitionMetricsReporter = reporter;
34     }
35 
36     /** Reports the current timestamp as when the transition animation starts. */
reportAnimationStart(IBinder transitionToken)37     public void reportAnimationStart(IBinder transitionToken) {
38         try {
39             mTransitionMetricsReporter.reportAnimationStart(transitionToken,
40                     SystemClock.elapsedRealtime());
41         } catch (RemoteException e) {
42             e.rethrowFromSystemServer();
43         }
44     }
45 
46     /** Gets the singleton instance of TransitionMetrics. */
getInstance()47     public static TransitionMetrics getInstance() {
48         return sTransitionMetrics.get();
49     }
50 
51     private static final Singleton<TransitionMetrics> sTransitionMetrics = new Singleton<>() {
52         @Override
53         protected TransitionMetrics create() {
54             return new TransitionMetrics(WindowOrganizer.getTransitionMetricsReporter());
55         }
56     };
57 }
58