Merge "Add dumpsys to Telecom analytics output" into nyc-mr1-dev
diff --git a/Android.mk b/Android.mk
index 4785b24..79ef194 100644
--- a/Android.mk
+++ b/Android.mk
@@ -5,9 +5,13 @@
 
 LOCAL_JAVA_LIBRARIES := telephony-common
 
-LOCAL_SRC_FILES := $(call all-java-files-under, src)
+LOCAL_SRC_FILES := $(call all-java-files-under, src) $(call all-proto-files-under, proto)
 LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/res
 
+LOCAL_PROTOC_OPTIMIZE_TYPE := nano
+LOCAL_PROTOC_FLAGS := --proto_path=$(LOCAL_PATH)/proto/
+LOCAL_PROTO_JAVA_OUTPUT_PARAMS := optional_field_style=accessors
+
 LOCAL_PACKAGE_NAME := Telecom
 
 LOCAL_CERTIFICATE := platform
diff --git a/proto/telecom.proto b/proto/telecom.proto
new file mode 100644
index 0000000..f0b3d02
--- /dev/null
+++ b/proto/telecom.proto
@@ -0,0 +1,256 @@
+syntax = "proto2";
+
+package com.android.server.telecom;
+
+option java_package = "com.android.server.telecom";
+option java_outer_classname = "TelecomLogClass";
+
+// The information about the telecom events.
+message TelecomLog {
+
+  // Information about each call.
+  repeated CallLog call_logs = 1;
+
+  // Timing information for the logging sessions
+  repeated LogSessionTiming session_timings = 2;
+}
+
+message LogSessionTiming {
+  enum SessionEntryPoint {
+    ICA_ANSWER_CALL = 1;
+    ICA_REJECT_CALL = 2;
+    ICA_DISCONNECT_CALL = 3;
+    ICA_HOLD_CALL = 4;
+    ICA_UNHOLD_CALL = 5;
+    ICA_MUTE = 6;
+    ICA_SET_AUDIO_ROUTE = 7;
+    ICA_CONFERENCE = 8;
+
+    CSW_HANDLE_CREATE_CONNECTION_COMPLETE = 100;
+    CSW_SET_ACTIVE = 101;
+    CSW_SET_RINGING = 102;
+    CSW_SET_DIALING = 103;
+    CSW_SET_DISCONNECTED = 104;
+    CSW_SET_ON_HOLD = 105;
+    CSW_REMOVE_CALL = 106;
+    CSW_SET_IS_CONFERENCED = 107;
+    CSW_ADD_CONFERENCE_CALL = 108;
+  }
+
+  // The entry point into Telecom code that this session tracks.
+  optional SessionEntryPoint sessionEntryPoint = 1;
+  // The time it took for this session to finish.
+  optional int64 time_millis = 2;
+}
+
+message Event {
+  // From android.telecom.ParcelableAnalytics
+  enum EventName {
+    SET_SELECT_PHONE_ACCOUNT = 0;
+    SET_ACTIVE = 1;
+    SET_DISCONNECTED = 2;
+    START_CONNECTION = 3;
+    SET_DIALING = 4;
+    BIND_CS = 5;
+    CS_BOUND = 6;
+    REQUEST_ACCEPT = 7;
+    REQUEST_REJECT = 8;
+
+    SCREENING_SENT = 100;
+    SCREENING_COMPLETED = 101;
+    DIRECT_TO_VM_INITIATED = 102;
+    DIRECT_TO_VM_FINISHED = 103;
+    BLOCK_CHECK_INITIATED = 104;
+    BLOCK_CHECK_FINISHED = 105;
+    FILTERING_INITIATED = 106;
+    FILTERING_COMPLETED = 107;
+    FILTERING_TIMED_OUT = 108;
+
+    SKIP_RINGING = 200;
+    SILENCE = 201;
+    MUTE = 202;
+    UNMUTE = 203;
+    AUDIO_ROUTE_BT = 204;
+    AUDIO_ROUTE_EARPIECE = 205;
+    AUDIO_ROUTE_HEADSET = 206;
+    AUDIO_ROUTE_SPEAKER = 207;
+
+    CONFERENCE_WITH = 300;
+    SPLIT_CONFERENCE = 301;
+    SET_PARENT = 302;
+
+    REQUEST_HOLD = 400;
+    REQUEST_UNHOLD = 401;
+    REMOTELY_HELD = 402;
+    REMOTELY_UNHELD = 403;
+    SET_HOLD = 404;
+    SWAP = 405;
+
+    REQUEST_PULL = 500;
+  }
+
+  // The ID of the event.
+  optional EventName event_name = 1;
+
+  // The elapsed time since the last event, rounded to one significant digit.
+  // If the event is the first, this will be negative.
+  optional int64 time_since_last_event_millis = 2;
+}
+
+message VideoEvent {
+  // From android.telecom.ParcelableCallAnalytics
+  enum VideoEventName {
+    SEND_LOCAL_SESSION_MODIFY_REQUEST = 0;
+    SEND_LOCAL_SESSION_MODIFY_RESPONSE = 1;
+    RECEIVE_REMOTE_SESSION_MODIFY_REQUEST = 2;
+    RECEIVE_REMOTE_SESSION_MODIFY_RESPONSE = 3;
+  }
+
+  // From android.telecom.VideoProfile
+  enum VideoState {
+     STATE_AUDIO_ONLY = 0;
+     STATE_TX_ENABLED = 1;
+     STATE_RX_ENABLED = 2;
+     STATE_BIDIRECTIONAL = 3;
+     STATE_PAUSED = 4;
+  }
+
+  // The ID of the event.
+  optional VideoEventName event_name = 1;
+
+  // The elapsed time since the last event, rounded to one significant digit.
+  // If the event is the first, this will be negative.
+  optional int64 time_since_last_event_millis = 2;
+
+  // The video state
+  optional int32 video_state = 3;
+}
+
+message EventTimingEntry {
+  enum EventTimingName {
+    ACCEPT_TIMING = 0;
+    REJECT_TIMING = 1;
+    DISCONNECT_TIMING = 2;
+    HOLD_TIMING = 3;
+    UNHOLD_TIMING = 4;
+    OUTGOING_TIME_TO_DIALING_TIMING = 5;
+    BIND_CS_TIMING = 6;
+    SCREENING_COMPLETED_TIMING = 7;
+    DIRECT_TO_VM_FINISHED_TIMING = 8;
+    BLOCK_CHECK_FINISHED_TIMING = 9;
+    FILTERING_COMPLETED_TIMING = 10;
+    FILTERING_TIMED_OUT_TIMING = 11;
+  }
+
+  // The name of the event timing.
+  optional EventTimingName timing_name = 1;
+
+  // The number of milliseconds that this event pair took.
+  optional int64 time_millis = 2;
+}
+
+// Information about each call.
+message CallLog {
+
+  // Information on call-types.
+  enum CallType {
+
+    // Call type is not known.
+    CALLTYPE_UNKNOWN = 0;
+
+    // Incoming call.
+    CALLTYPE_INCOMING = 1;
+
+    // Outgoing call.
+    CALLTYPE_OUTGOING = 2;
+  }
+
+  // Termination code.
+  enum CallTerminationCode {
+
+    // Disconnected because of an unknown or unspecified reason.
+    CALL_TERMINATION_CODE_UNKNOWN = 0;
+
+    // Disconnected because there was an error, such as a problem
+    // with the network.
+    CALL_TERMINATION_CODE_ERROR = 1;
+
+    // Disconnected because of a local user-initiated action,
+    // such as hanging up.
+    CALL_TERMINATION_CODE_LOCAL = 2;
+
+    // Disconnected because of a remote user-initiated action,
+    // such as the other party hanging up.
+    CALL_TERMINATION_CODE_REMOTE = 3;
+
+    // Disconnected because it has been canceled.
+    CALL_TERMINATION_CODE_CANCELED = 4;
+
+    // Disconnected because there was no response to an incoming call.
+    CALL_TERMINATION_CODE_MISSED = 5;
+
+    // Disconnected because the user rejected an incoming call.
+    CALL_TERMINATION_CODE_REJECTED = 6;
+
+    // Disconnected because the other party was busy.
+    CALL_TERMINATION_CODE_BUSY = 7;
+
+    // Disconnected because of a restriction on placing the call,
+    // such as dialing in airplane mode.
+    CALL_TERMINATION_CODE_RESTRICTED = 8;
+
+    // Disconnected for reason not described by other disconnect codes.
+    CALL_TERMINATION_CODE_OTHER = 9;
+
+    // Disconnected because the connection manager did not support the call.
+    // The call will be tried again without a connection manager.
+    CONNECTION_MANAGER_NOT_SUPPORTED = 10;
+  }
+
+  // Start time of the connection.
+  // Rounded to the nearest 5 minute interval.
+  optional int64 start_time_5min = 1;
+
+  // Duration in millis.
+  optional int64 call_duration_millis = 2;
+
+  // Call type.
+  optional CallType type  = 3;
+
+  // True if the call interrupted an in-progress call, whether it was the
+  // user dialing out during a call or an incoming call during another call.
+  optional bool is_additional_call = 4 [default = false];
+
+  // True if the call was interrupted by another call.
+  optional bool is_interrupted = 5 [default = false];
+
+  // A bitmask with bits corresponding to call technologies that were used
+  // during the call. The ones that we will record are CDMA, GSM, IMS, SIP,
+  // and third-party.
+  // https://googleplex-android-review.git.corp.google.com/#/c/816516/6/src/com/android/server/telecom/Analytics.java
+  optional int32 call_technologies = 6;
+
+  // Indicates the call termination code.
+  optional CallTerminationCode call_termination_code = 7;
+
+  // A list of the package names of connection services used.
+  repeated string connection_service = 9;
+
+  // Set to true if the call was created from createCallForExistingConnection.
+  optional bool is_created_from_existing_connection = 10 [default = false];
+
+  // Set to true if its an emergency call.
+  optional bool is_emergency_call = 11 [default = false];
+
+  // A list of the events that occur during the call.
+  repeated Event call_events = 12;
+
+  // A map from the names of latency timings to the timings.
+  repeated EventTimingEntry call_timings = 13;
+
+  // Whether this call has ever been a video call
+  optional bool is_video_call = 14 [default = false];
+
+  // A list of the video events during the call.
+  repeated VideoEvent video_events = 15;
+}
diff --git a/src/com/android/server/telecom/Analytics.java b/src/com/android/server/telecom/Analytics.java
index 50ff14d..0ad130b 100644
--- a/src/com/android/server/telecom/Analytics.java
+++ b/src/com/android/server/telecom/Analytics.java
@@ -19,11 +19,14 @@
 import android.telecom.DisconnectCause;
 import android.telecom.ParcelableCallAnalytics;
 import android.telecom.TelecomAnalytics;
+import android.util.Base64;
 
 import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.util.IndentingPrintWriter;
 
+import java.io.PrintWriter;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.LinkedList;
@@ -39,6 +42,9 @@
  * aggregate these into useful statistics.
  */
 public class Analytics {
+    public static final String ANALYTICS_DUMPSYS_ARG = "analytics";
+    private static final String CLEAR_ANALYTICS_ARG = "clear";
+
     public static final Map<String, Integer> sLogEventToAnalyticsEvent =
             new HashMap<String, Integer>() {{
                 put(Log.Events.SET_SELECT_PHONE_ACCOUNT, AnalyticsEvent.SET_SELECT_PHONE_ACCOUNT);
@@ -200,7 +206,7 @@
         public Log.CallEventRecord callEvents;
 
         public boolean isVideo = false;
-        public List<ParcelableCallAnalytics.VideoEvent> videoEvents;
+        public List<TelecomLogClass.VideoEvent> videoEvents;
         private long mTimeOfLastVideoEvent = -1;
 
         CallInfoImpl(String callId, int callDirection) {
@@ -311,8 +317,10 @@
             }
             mTimeOfLastVideoEvent = currentTime;
 
-            videoEvents.add(new ParcelableCallAnalytics.VideoEvent(
-                    eventId, timeSinceLastEvent, videoState));
+            videoEvents.add(new TelecomLogClass.VideoEvent()
+                    .setEventName(eventId)
+                    .setTimeSinceLastEventMillis(timeSinceLastEvent)
+                    .setVideoState(videoState));
         }
 
         @Override
@@ -331,40 +339,80 @@
         }
 
         public ParcelableCallAnalytics toParcelableAnalytics() {
+            TelecomLogClass.CallLog analyticsProto = toProto();
+            List<ParcelableCallAnalytics.AnalyticsEvent> events =
+                    Arrays.stream(analyticsProto.callEvents)
+                    .map(callEventProto -> new ParcelableCallAnalytics.AnalyticsEvent(
+                                callEventProto.getEventName(),
+                                callEventProto.getTimeSinceLastEventMillis())
+                    ).collect(Collectors.toList());
+
+            List<ParcelableCallAnalytics.EventTiming> timings =
+                    Arrays.stream(analyticsProto.callTimings)
+                    .map(callTimingProto -> new ParcelableCallAnalytics.EventTiming(
+                            callTimingProto.getTimingName(),
+                            callTimingProto.getTimeMillis())
+                    ).collect(Collectors.toList());
+
+            ParcelableCallAnalytics result = new ParcelableCallAnalytics(
+                    // rounds down to nearest 5 minute mark
+                    analyticsProto.getStartTime5Min(),
+                    analyticsProto.getCallDurationMillis(),
+                    analyticsProto.getType(),
+                    analyticsProto.getIsAdditionalCall(),
+                    analyticsProto.getIsInterrupted(),
+                    analyticsProto.getCallTechnologies(),
+                    analyticsProto.getCallTerminationCode(),
+                    analyticsProto.getIsEmergencyCall(),
+                    analyticsProto.connectionService[0],
+                    analyticsProto.getIsCreatedFromExistingConnection(),
+                    events,
+                    timings);
+
+            result.setIsVideoCall(analyticsProto.getIsVideoCall());
+            result.setVideoEvents(Arrays.stream(analyticsProto.videoEvents)
+                    .map(videoEventProto -> new ParcelableCallAnalytics.VideoEvent(
+                            videoEventProto.getEventName(),
+                            videoEventProto.getTimeSinceLastEventMillis(),
+                            videoEventProto.getVideoState())
+                    ).collect(Collectors.toList()));
+
+            return result;
+        }
+
+        public TelecomLogClass.CallLog toProto() {
+            TelecomLogClass.CallLog result = new TelecomLogClass.CallLog();
+            result.setStartTime5Min(
+                    startTime - startTime % ParcelableCallAnalytics.MILLIS_IN_5_MINUTES);
+
             // Rounds up to the nearest second.
             long callDuration = (endTime == 0 || startTime == 0) ? 0 : endTime - startTime;
             callDuration += (callDuration % MILLIS_IN_1_SECOND == 0) ?
                     0 : (MILLIS_IN_1_SECOND - callDuration % MILLIS_IN_1_SECOND);
+            result.setCallDurationMillis(callDuration);
 
-            List<AnalyticsEvent> events;
-            List<ParcelableCallAnalytics.EventTiming> timings;
+            result.setType(callDirection)
+                    .setIsAdditionalCall(isAdditionalCall)
+                    .setIsInterrupted(isInterrupted)
+                    .setCallTechnologies(callTechnologies)
+                    .setCallTerminationCode(
+                            callTerminationReason == null ?
+                                    ParcelableCallAnalytics.STILL_CONNECTED :
+                                    callTerminationReason.getCode())
+                    .setIsEmergencyCall(isEmergency)
+                    .setIsCreatedFromExistingConnection(createdFromExistingConnection)
+                    .setIsEmergencyCall(isEmergency)
+                    .setIsVideoCall(isVideo);
+
+            result.connectionService = new String[] {connectionService};
             if (callEvents != null) {
-                events = convertLogEventsToAnalyticsEvents(callEvents.getEvents());
-                timings = callEvents.extractEventTimings().stream()
-                        .map(Analytics::logEventTimingToAnalyticsEventTiming)
-                        .collect(Collectors.toList());
-            } else {
-                events = Collections.emptyList();
-                timings = Collections.emptyList();
+                result.callEvents = convertLogEventsToProtoEvents(callEvents.getEvents());
+                result.callTimings = callEvents.extractEventTimings().stream()
+                        .map(Analytics::logEventTimingToProtoEventTiming)
+                        .toArray(TelecomLogClass.EventTimingEntry[]::new);
             }
-            ParcelableCallAnalytics result = new ParcelableCallAnalytics(
-                    // rounds down to nearest 5 minute mark
-                    startTime - startTime % ParcelableCallAnalytics.MILLIS_IN_5_MINUTES,
-                    callDuration,
-                    callDirection,
-                    isAdditionalCall,
-                    isInterrupted,
-                    callTechnologies,
-                    callTerminationReason == null ?
-                            ParcelableCallAnalytics.STILL_CONNECTED :
-                            callTerminationReason.getCode(),
-                    isEmergency,
-                    connectionService,
-                    createdFromExistingConnection,
-                    events,
-                    timings);
-            result.setIsVideoCall(isVideo);
-            result.setVideoEvents(videoEvents);
+            result.videoEvents =
+                    videoEvents.toArray(new TelecomLogClass.VideoEvent[videoEvents.size()]);
             return result;
         }
 
@@ -463,6 +511,28 @@
         return new TelecomAnalytics(sessionTimings, calls);
     }
 
+    public static void dumpToEncodedProto(PrintWriter pw, String[] args) {
+        TelecomLogClass.TelecomLog result = new TelecomLogClass.TelecomLog();
+
+        synchronized (sLock) {
+            result.callLogs = sCallIdToInfo.values().stream()
+                    .map(CallInfoImpl::toProto)
+                    .toArray(TelecomLogClass.CallLog[]::new);
+            result.sessionTimings = sSessionTimings.stream()
+                    .map(timing -> new TelecomLogClass.LogSessionTiming()
+                            .setSessionEntryPoint(timing.getKey())
+                            .setTimeMillis(timing.getTime()))
+                    .toArray(TelecomLogClass.LogSessionTiming[]::new);
+            if (args.length > 1 && CLEAR_ANALYTICS_ARG.equals(args[1])) {
+                sCallIdToInfo.clear();
+                sSessionTimings.clear();
+            }
+        }
+        String encodedProto = Base64.encodeToString(
+                TelecomLogClass.TelecomLog.toByteArray(result), Base64.DEFAULT);
+        pw.write(encodedProto);
+    }
+
     public static void dump(IndentingPrintWriter writer) {
         synchronized (sLock) {
             int prefixLength = CallsManager.TELECOM_CALL_ID_PREFIX.length();
@@ -521,33 +591,32 @@
         }
     }
 
-    private static List<AnalyticsEvent> convertLogEventsToAnalyticsEvents(
+    private static TelecomLogClass.Event[] convertLogEventsToProtoEvents(
             List<Log.CallEvent> logEvents) {
         long timeOfLastEvent = -1;
-        ArrayList<AnalyticsEvent> events = new ArrayList<>(logEvents.size());
+        ArrayList<TelecomLogClass.Event> events = new ArrayList<>(logEvents.size());
         for (Log.CallEvent logEvent : logEvents) {
             if (sLogEventToAnalyticsEvent.containsKey(logEvent.eventId)) {
-                int analyticsEventId = sLogEventToAnalyticsEvent.get(logEvent.eventId);
-                long timeSinceLastEvent =
-                        timeOfLastEvent < 0 ? -1 : logEvent.time - timeOfLastEvent;
-                events.add(new AnalyticsEvent(
-                        analyticsEventId,
-                        roundToOneSigFig(timeSinceLastEvent)
-                ));
+                TelecomLogClass.Event event = new TelecomLogClass.Event();
+                event.setEventName(sLogEventToAnalyticsEvent.get(logEvent.eventId));
+                event.setTimeSinceLastEventMillis(roundToOneSigFig(
+                        timeOfLastEvent < 0 ? -1 : logEvent.time - timeOfLastEvent));
+                events.add(event);
                 timeOfLastEvent = logEvent.time;
             }
         }
-        return events;
+        return events.toArray(new TelecomLogClass.Event[events.size()]);
     }
 
-    private static ParcelableCallAnalytics.EventTiming logEventTimingToAnalyticsEventTiming(
+    private static TelecomLogClass.EventTimingEntry logEventTimingToProtoEventTiming(
             Log.CallEventRecord.EventTiming logEventTiming) {
         int analyticsEventTimingName =
                 sLogEventTimingToAnalyticsEventTiming.containsKey(logEventTiming.name) ?
                         sLogEventTimingToAnalyticsEventTiming.get(logEventTiming.name) :
                         ParcelableCallAnalytics.EventTiming.INVALID;
-        return new ParcelableCallAnalytics.EventTiming(analyticsEventTimingName,
-                (long) logEventTiming.time);
+        return new TelecomLogClass.EventTimingEntry()
+                .setTimingName(analyticsEventTimingName)
+                .setTimeMillis(logEventTiming.time);
     }
 
     @VisibleForTesting
diff --git a/src/com/android/server/telecom/TelecomServiceImpl.java b/src/com/android/server/telecom/TelecomServiceImpl.java
index 7dc0a79..c93a752 100644
--- a/src/com/android/server/telecom/TelecomServiceImpl.java
+++ b/src/com/android/server/telecom/TelecomServiceImpl.java
@@ -39,7 +39,6 @@
 import android.os.UserHandle;
 import android.os.UserManager;
 import android.telecom.DefaultDialerManager;
-import android.telecom.ParcelableCallAnalytics;
 import android.telecom.PhoneAccount;
 import android.telecom.PhoneAccountHandle;
 import android.telecom.TelecomAnalytics;
@@ -57,8 +56,6 @@
 
 import java.io.FileDescriptor;
 import java.io.PrintWriter;
-import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
 
@@ -1174,6 +1171,11 @@
                 return;
             }
 
+            if (args.length > 0 && Analytics.ANALYTICS_DUMPSYS_ARG.equals(args[0])) {
+                Analytics.dumpToEncodedProto(writer, args);
+                return;
+            }
+
             final IndentingPrintWriter pw = new IndentingPrintWriter(writer, "  ");
             if (mCallsManager != null) {
                 pw.println("CallsManager: ");
diff --git a/src/com/android/server/telecom/VideoProviderProxy.java b/src/com/android/server/telecom/VideoProviderProxy.java
index a4dfce2..6b63255 100644
--- a/src/com/android/server/telecom/VideoProviderProxy.java
+++ b/src/com/android/server/telecom/VideoProviderProxy.java
@@ -187,7 +187,9 @@
                 if (status == Connection.VideoProvider.SESSION_MODIFY_REQUEST_SUCCESS) {
                     mCall.getAnalytics().addVideoEvent(
                             Analytics.RECEIVE_REMOTE_SESSION_MODIFY_RESPONSE,
-                            requestProfile.getVideoState());
+                            responseProfile == null ?
+                                    VideoProfile.STATE_AUDIO_ONLY :
+                                    responseProfile.getVideoState());
                 }
                 VideoProviderProxy.this.receiveSessionModifyResponse(status, requestProfile,
                         responseProfile);
diff --git a/tests/Android.mk b/tests/Android.mk
index f7c1b5a..1065ad1 100644
--- a/tests/Android.mk
+++ b/tests/Android.mk
@@ -25,7 +25,12 @@
 
 LOCAL_SRC_FILES := \
         $(call all-java-files-under, src) \
-        $(call all-java-files-under, ../src)
+        $(call all-java-files-under, ../src) \
+        $(call all-proto-files-under, ../proto)
+
+LOCAL_PROTOC_OPTIMIZE_TYPE := nano
+LOCAL_PROTOC_FLAGS := --proto_path=$(LOCAL_PATH)/../proto/
+LOCAL_PROTO_JAVA_OUTPUT_PARAMS := optional_field_style=accessors
 
 LOCAL_RESOURCE_DIR := \
     $(LOCAL_PATH)/res \
diff --git a/tests/src/com/android/server/telecom/tests/AnalyticsTests.java b/tests/src/com/android/server/telecom/tests/AnalyticsTests.java
index 241f66d..d8e152a 100644
--- a/tests/src/com/android/server/telecom/tests/AnalyticsTests.java
+++ b/tests/src/com/android/server/telecom/tests/AnalyticsTests.java
@@ -18,21 +18,34 @@
 
 import android.content.Context;
 import android.telecom.DisconnectCause;
+import android.telecom.InCallService;
 import android.telecom.ParcelableCallAnalytics;
 import android.telecom.TelecomAnalytics;
 import android.telecom.TelecomManager;
+import android.telecom.VideoCallImpl;
+import android.telecom.VideoProfile;
 import android.test.suitebuilder.annotation.MediumTest;
 import android.test.suitebuilder.annotation.SmallTest;
+import android.util.Base64;
 
 import com.android.internal.util.IndentingPrintWriter;
 import com.android.server.telecom.Analytics;
 import com.android.server.telecom.Log;
+import com.android.server.telecom.TelecomLogClass;
 
+import java.io.PrintWriter;
 import java.io.StringWriter;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyInt;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
 
 public class AnalyticsTests extends TelecomSystemTest {
     @MediumTest
@@ -110,6 +123,7 @@
         Set<Integer> capturedEvents = new HashSet<>();
         for (ParcelableCallAnalytics.AnalyticsEvent e : analyticsEvents) {
             capturedEvents.add(e.getEventName());
+            assertIsRoundedToOneSigFig(e.getTimeSinceLastEvent());
         }
         assertTrue(capturedEvents.contains(ParcelableCallAnalytics.AnalyticsEvent.SET_ACTIVE));
         assertTrue(capturedEvents.contains(
@@ -168,6 +182,54 @@
         assertEquals(DisconnectCause.REMOTE, callAnalytics2.callTerminationReason.getCode());
     }
 
+    @MediumTest
+    public void testAnalyticsVideo() throws Exception {
+        Analytics.reset();
+        IdPair callIds = startAndMakeActiveOutgoingCall(
+                "650-555-1212",
+                mPhoneAccountA0.getAccountHandle(),
+                mConnectionServiceFixtureA);
+
+        CountDownLatch counter = new CountDownLatch(1);
+        InCallService.VideoCall.Callback callback = mock(InCallService.VideoCall.Callback.class);
+
+        doAnswer(invocation -> {
+            counter.countDown();
+            return null;
+        }).when(callback)
+                .onSessionModifyResponseReceived(anyInt(), any(VideoProfile.class),
+                        any(VideoProfile.class));
+
+        mConnectionServiceFixtureA.sendSetVideoProvider(
+                mConnectionServiceFixtureA.mLatestConnectionId);
+        InCallService.VideoCall videoCall =
+                mInCallServiceFixtureX.getCall(callIds.mCallId).getVideoCallImpl();
+        videoCall.registerCallback(callback);
+        ((VideoCallImpl) videoCall).setVideoState(VideoProfile.STATE_BIDIRECTIONAL);
+
+        videoCall.sendSessionModifyRequest(new VideoProfile(VideoProfile.STATE_RX_ENABLED));
+        counter.await(10000, TimeUnit.MILLISECONDS);
+
+        StringWriter sw = new StringWriter();
+        PrintWriter pw = new PrintWriter(sw);
+        Analytics.dumpToEncodedProto(pw, new String[]{});
+        TelecomLogClass.TelecomLog analyticsProto =
+                TelecomLogClass.TelecomLog.parseFrom(Base64.decode(sw.toString(), Base64.DEFAULT));
+
+        assertEquals(1, analyticsProto.callLogs.length);
+        TelecomLogClass.VideoEvent[] videoEvents = analyticsProto.callLogs[0].videoEvents;
+        assertEquals(2, videoEvents.length);
+
+        assertEquals(Analytics.SEND_LOCAL_SESSION_MODIFY_REQUEST, videoEvents[0].getEventName());
+        assertEquals(VideoProfile.STATE_RX_ENABLED, videoEvents[0].getVideoState());
+        assertEquals(-1, videoEvents[0].getTimeSinceLastEventMillis());
+
+        assertEquals(Analytics.RECEIVE_REMOTE_SESSION_MODIFY_RESPONSE,
+                videoEvents[1].getEventName());
+        assertEquals(VideoProfile.STATE_RX_ENABLED, videoEvents[1].getVideoState());
+        assertIsRoundedToOneSigFig(videoEvents[1].getTimeSinceLastEventMillis());
+    }
+
     @SmallTest
     public void testAnalyticsRounding() {
         long[] testVals = {0, -1, -10, -100, -57836, 1, 10, 100, 1000, 458457};
@@ -190,4 +252,55 @@
                         Analytics.sSessionIdToLogSession.get(s.getKey())))
                 .forEach(s -> assertTrue(s.getTime() > minTime));
     }
+
+    @MediumTest
+    public void testAnalyticsDumpToProto() throws Exception {
+        Analytics.reset();
+        IdPair testCall = startAndMakeActiveIncomingCall(
+                "650-555-1212",
+                mPhoneAccountA0.getAccountHandle(),
+                mConnectionServiceFixtureA);
+
+        mConnectionServiceFixtureA.
+                sendSetDisconnected(testCall.mConnectionId, DisconnectCause.ERROR);
+        Analytics.CallInfoImpl expectedAnalytics = Analytics.cloneData().get(testCall.mCallId);
+
+        StringWriter sw = new StringWriter();
+        PrintWriter pw = new PrintWriter(sw);
+        Analytics.dumpToEncodedProto(pw, new String[]{});
+        TelecomLogClass.TelecomLog analyticsProto =
+                TelecomLogClass.TelecomLog.parseFrom(Base64.decode(sw.toString(), Base64.DEFAULT));
+
+        assertEquals(1, analyticsProto.callLogs.length);
+        TelecomLogClass.CallLog callLog = analyticsProto.callLogs[0];
+
+        assertTrue(Math.abs(expectedAnalytics.startTime - callLog.getStartTime5Min()) <
+                ParcelableCallAnalytics.MILLIS_IN_5_MINUTES);
+        assertEquals(0, callLog.getStartTime5Min() % ParcelableCallAnalytics.MILLIS_IN_5_MINUTES);
+        assertTrue(Math.abs((expectedAnalytics.endTime - expectedAnalytics.startTime) -
+                callLog.getCallDurationMillis()) < ParcelableCallAnalytics.MILLIS_IN_1_SECOND);
+        assertEquals(0,
+                callLog.getCallDurationMillis() % ParcelableCallAnalytics.MILLIS_IN_1_SECOND);
+
+        assertEquals(expectedAnalytics.callDirection, callLog.getType());
+        assertEquals(expectedAnalytics.isAdditionalCall, callLog.getIsAdditionalCall());
+        assertEquals(expectedAnalytics.isInterrupted, callLog.getIsInterrupted());
+        assertEquals(expectedAnalytics.callTechnologies, callLog.getCallTechnologies());
+        assertEquals(expectedAnalytics.callTerminationReason.getCode(),
+                callLog.getCallTerminationCode());
+        assertEquals(expectedAnalytics.connectionService, callLog.connectionService[0]);
+        TelecomLogClass.Event[] analyticsEvents = callLog.callEvents;
+        Set<Integer> capturedEvents = new HashSet<>();
+        for (TelecomLogClass.Event e : analyticsEvents) {
+            capturedEvents.add(e.getEventName());
+            assertIsRoundedToOneSigFig(e.getTimeSinceLastEventMillis());
+        }
+        assertTrue(capturedEvents.contains(ParcelableCallAnalytics.AnalyticsEvent.SET_ACTIVE));
+        assertTrue(capturedEvents.contains(
+                ParcelableCallAnalytics.AnalyticsEvent.FILTERING_INITIATED));
+    }
+
+    private void assertIsRoundedToOneSigFig(long x) {
+        assertEquals(x, Analytics.roundToOneSigFig(x));
+    }
 }