1/*
2 * Copyright (C) 2016 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
17package com.android.incallui.latencyreport;
18
19import android.os.Bundle;
20import android.os.SystemClock;
21
22/** Tracks latency information for a call. */
23public class LatencyReport {
24
25  public static final long INVALID_TIME = -1;
26  // The following are hidden constants from android.telecom.TelecomManager.
27  private static final String EXTRA_CALL_CREATED_TIME_MILLIS =
28      "android.telecom.extra.CALL_CREATED_TIME_MILLIS";
29  private static final String EXTRA_CALL_TELECOM_ROUTING_START_TIME_MILLIS =
30      "android.telecom.extra.CALL_TELECOM_ROUTING_START_TIME_MILLIS";
31  private static final String EXTRA_CALL_TELECOM_ROUTING_END_TIME_MILLIS =
32      "android.telecom.extra.CALL_TELECOM_ROUTING_END_TIME_MILLIS";
33  private final boolean mWasIncoming;
34
35  // Time elapsed since boot when the call was created by the connection service.
36  private final long mCreatedTimeMillis;
37
38  // Time elapsed since boot when telecom began processing the call.
39  private final long mTelecomRoutingStartTimeMillis;
40
41  // Time elapsed since boot when telecom finished processing the call. This includes things like
42  // looking up contact info and call blocking but before showing any UI.
43  private final long mTelecomRoutingEndTimeMillis;
44
45  // Time elapsed since boot when the call was added to the InCallUi.
46  private final long mCallAddedTimeMillis;
47
48  // Time elapsed since boot when the call was added and call blocking evaluation was completed.
49  private long mCallBlockingTimeMillis = INVALID_TIME;
50
51  // Time elapsed since boot when the call notification was shown.
52  private long mCallNotificationTimeMillis = INVALID_TIME;
53
54  // Time elapsed since boot when the InCallUI was shown.
55  private long mInCallUiShownTimeMillis = INVALID_TIME;
56
57  // Whether the call was shown to the user as a heads up notification instead of a full screen
58  // UI.
59  private boolean mDidDisplayHeadsUpNotification;
60
61  public LatencyReport() {
62    mWasIncoming = false;
63    mCreatedTimeMillis = INVALID_TIME;
64    mTelecomRoutingStartTimeMillis = INVALID_TIME;
65    mTelecomRoutingEndTimeMillis = INVALID_TIME;
66    mCallAddedTimeMillis = SystemClock.elapsedRealtime();
67  }
68
69  public LatencyReport(android.telecom.Call telecomCall) {
70    mWasIncoming = telecomCall.getState() == android.telecom.Call.STATE_RINGING;
71    Bundle extras = telecomCall.getDetails().getIntentExtras();
72    if (extras == null) {
73      mCreatedTimeMillis = INVALID_TIME;
74      mTelecomRoutingStartTimeMillis = INVALID_TIME;
75      mTelecomRoutingEndTimeMillis = INVALID_TIME;
76    } else {
77      mCreatedTimeMillis = extras.getLong(EXTRA_CALL_CREATED_TIME_MILLIS, INVALID_TIME);
78      mTelecomRoutingStartTimeMillis =
79          extras.getLong(EXTRA_CALL_TELECOM_ROUTING_START_TIME_MILLIS, INVALID_TIME);
80      mTelecomRoutingEndTimeMillis =
81          extras.getLong(EXTRA_CALL_TELECOM_ROUTING_END_TIME_MILLIS, INVALID_TIME);
82    }
83    mCallAddedTimeMillis = SystemClock.elapsedRealtime();
84  }
85
86  public boolean getWasIncoming() {
87    return mWasIncoming;
88  }
89
90  public long getCreatedTimeMillis() {
91    return mCreatedTimeMillis;
92  }
93
94  public long getTelecomRoutingStartTimeMillis() {
95    return mTelecomRoutingStartTimeMillis;
96  }
97
98  public long getTelecomRoutingEndTimeMillis() {
99    return mTelecomRoutingEndTimeMillis;
100  }
101
102  public long getCallAddedTimeMillis() {
103    return mCallAddedTimeMillis;
104  }
105
106  public long getCallBlockingTimeMillis() {
107    return mCallBlockingTimeMillis;
108  }
109
110  public void onCallBlockingDone() {
111    if (mCallBlockingTimeMillis == INVALID_TIME) {
112      mCallBlockingTimeMillis = SystemClock.elapsedRealtime();
113    }
114  }
115
116  public long getCallNotificationTimeMillis() {
117    return mCallNotificationTimeMillis;
118  }
119
120  public void onNotificationShown() {
121    if (mCallNotificationTimeMillis == INVALID_TIME) {
122      mCallNotificationTimeMillis = SystemClock.elapsedRealtime();
123    }
124  }
125
126  public long getInCallUiShownTimeMillis() {
127    return mInCallUiShownTimeMillis;
128  }
129
130  public void onInCallUiShown(boolean forFullScreenIntent) {
131    if (mInCallUiShownTimeMillis == INVALID_TIME) {
132      mInCallUiShownTimeMillis = SystemClock.elapsedRealtime();
133      mDidDisplayHeadsUpNotification = mWasIncoming && !forFullScreenIntent;
134    }
135  }
136
137  public boolean getDidDisplayHeadsUpNotification() {
138    return mDidDisplayHeadsUpNotification;
139  }
140}
141