Adds Assistant invocation logging to statsd

This is the first step in migrating the logging from the old framework.
Once the data has been verified, the old logging code will be removed.

Test: Tested locally
BUG: 147508374
BUG: 155745843
Change-Id: I738278c0a3e1ab42b53300792bfd80ae778d6be1
Merged-In: I738278c0a3e1ab42b53300792bfd80ae778d6be1
diff --git a/cmds/statsd/src/atoms.proto b/cmds/statsd/src/atoms.proto
index 81d059e..d3ead4e 100644
--- a/cmds/statsd/src/atoms.proto
+++ b/cmds/statsd/src/atoms.proto
@@ -449,6 +449,7 @@
         TvTunerDvrStatus tv_tuner_dvr_status = 279 [(module) = "framework"];
         TvCasSessionOpenStatus tv_cas_session_open_status =
             280 [(module) = "framework"];
+        AssistantInvocationReported assistant_invocation_reported = 281 [(module) = "framework"];
 
         // StatsdStats tracks platform atoms with ids upto 500.
         // Update StatsdStats::kMaxPushedAtomId when atom ids here approach that value.
@@ -9995,3 +9996,42 @@
     // The amount of applied devices within a remote dynamic group after a switching is done.
     optional int32 applied_device_count_within_remote_group = 9;
 }
+
+/**
+ * Logs when the Assistant is invoked.
+ *
+ * Logged from:
+ *   frameworks/base/packages/SystemUI/src/com/android/systemui/assist/AssistManager.java
+ */
+message AssistantInvocationReported {
+
+    // The event_id (as for UiEventReported).
+    optional int32 event_id = 1;
+
+    // The registered Assistant's uid and package (as for UiEventReported).
+    optional int32 uid = 2 [(is_uid) = true];
+    optional string package_name = 3;
+
+    // An identifier used to disambiguate which logs refer to a particular invocation of the
+    // Assistant  (as for UiEventReported).
+    optional int32 instance_id = 4;
+
+    // The state of the device at the time of invocation.
+    enum DeviceState {
+        UNKNOWN_DEVICE_STATE = 0;
+        AOD1 = 1;
+        AOD2 = 2;
+        BOUNCER = 3;
+        UNLOCKED_LOCKSCREEN = 4;
+        LAUNCHER_HOME = 5;
+        LAUNCHER_OVERVIEW = 6;
+        LAUNCHER_ALL_APPS = 7;
+        APP_DEFAULT = 8;
+        APP_IMMERSIVE = 9;
+        APP_FULLSCREEN = 10;
+    }
+    optional DeviceState device_state = 5;
+
+    // Whether the Assistant handles were showing at the time of invocation.
+    optional bool assistant_handles_showing = 6;
+}
diff --git a/packages/SystemUI/src/com/android/systemui/assist/AssistManager.java b/packages/SystemUI/src/com/android/systemui/assist/AssistManager.java
index a876dee..a7533ad 100644
--- a/packages/SystemUI/src/com/android/systemui/assist/AssistManager.java
+++ b/packages/SystemUI/src/com/android/systemui/assist/AssistManager.java
@@ -40,8 +40,11 @@
 import com.android.internal.app.AssistUtils;
 import com.android.internal.app.IVoiceInteractionSessionListener;
 import com.android.internal.app.IVoiceInteractionSessionShowCallback;
+import com.android.internal.logging.InstanceId;
+import com.android.internal.logging.InstanceIdSequence;
 import com.android.internal.logging.MetricsLogger;
 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
+import com.android.internal.util.FrameworkStatsLog;
 import com.android.keyguard.KeyguardUpdateMonitor;
 import com.android.settingslib.applications.InterestingConfigChanges;
 import com.android.systemui.R;
@@ -121,6 +124,7 @@
 
     private static final long TIMEOUT_SERVICE = 2500;
     private static final long TIMEOUT_ACTIVITY = 1000;
+    private static final int INSTANCE_ID_MAX = 1 << 20;
 
     protected final Context mContext;
     private final WindowManager mWindowManager;
@@ -130,6 +134,8 @@
     private final AssistHandleBehaviorController mHandleController;
     private final UiController mUiController;
     protected final Lazy<SysUiState> mSysUiState;
+    protected final InstanceIdSequence mInstanceIdSequence =
+            new InstanceIdSequence(INSTANCE_ID_MAX);
 
     private AssistOrbContainer mView;
     private final DeviceProvisionedController mDeviceProvisionedController;
@@ -299,7 +305,8 @@
         int phoneState = mPhoneStateMonitor.getPhoneState();
         args.putInt(INVOCATION_PHONE_STATE_KEY, phoneState);
         args.putLong(INVOCATION_TIME_MS_KEY, SystemClock.elapsedRealtime());
-        logStartAssist(invocationType, phoneState);
+        logStartAssist(/* instanceId = */ null, invocationType, phoneState);
+        logStartAssistLegacy(invocationType, phoneState);
         startAssistInternal(args, assistComponent, isService);
     }
 
@@ -499,7 +506,35 @@
         return toLoggingSubType(invocationType, mPhoneStateMonitor.getPhoneState());
     }
 
-    protected void logStartAssist(int invocationType, int phoneState) {
+    protected void logStartAssist(
+            @Nullable InstanceId instanceId, int invocationType, int deviceState) {
+        InstanceId currentInstanceId =
+                instanceId == null ? mInstanceIdSequence.newInstanceId() : instanceId;
+        ComponentName assistantComponent =
+                mAssistUtils.getAssistComponentForUser(UserHandle.USER_CURRENT);
+        int assistantUid = 0;
+        try {
+            assistantUid =
+                    mContext.getPackageManager()
+                            .getApplicationInfo(
+                                    assistantComponent.getPackageName(),
+                                    /* flags = */ 0)
+                            .uid;
+        } catch (PackageManager.NameNotFoundException e) {
+            Log.e(TAG, "Unable to find Assistant UID", e);
+        }
+
+        FrameworkStatsLog.write(
+                FrameworkStatsLog.ASSISTANT_INVOCATION_REPORTED,
+                AssistantInvocationEvent.Companion.eventIdFromLegacyInvocationType(invocationType),
+                assistantUid,
+                assistantComponent.flattenToString(),
+                currentInstanceId.getId(),
+                AssistantInvocationEvent.Companion.deviceStateFromLegacyDeviceState(deviceState),
+                mHandleController.areHandlesShowing());
+    }
+
+    protected void logStartAssistLegacy(int invocationType, int phoneState) {
         MetricsLogger.action(
                 new LogMaker(MetricsEvent.ASSISTANT)
                         .setType(MetricsEvent.TYPE_OPEN)
diff --git a/packages/SystemUI/src/com/android/systemui/assist/AssistantInvocationEvent.kt b/packages/SystemUI/src/com/android/systemui/assist/AssistantInvocationEvent.kt
new file mode 100644
index 0000000..1de7b84
--- /dev/null
+++ b/packages/SystemUI/src/com/android/systemui/assist/AssistantInvocationEvent.kt
@@ -0,0 +1,119 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.systemui.assist
+
+import com.android.internal.logging.UiEvent
+import com.android.internal.logging.UiEventLogger
+import com.android.internal.util.FrameworkStatsLog.ASSISTANT_INVOCATION_REPORTED__DEVICE_STATE__AOD1
+import com.android.internal.util.FrameworkStatsLog.ASSISTANT_INVOCATION_REPORTED__DEVICE_STATE__AOD2
+import com.android.internal.util.FrameworkStatsLog.ASSISTANT_INVOCATION_REPORTED__DEVICE_STATE__APP_DEFAULT
+import com.android.internal.util.FrameworkStatsLog.ASSISTANT_INVOCATION_REPORTED__DEVICE_STATE__APP_FULLSCREEN
+import com.android.internal.util.FrameworkStatsLog.ASSISTANT_INVOCATION_REPORTED__DEVICE_STATE__APP_IMMERSIVE
+import com.android.internal.util.FrameworkStatsLog.ASSISTANT_INVOCATION_REPORTED__DEVICE_STATE__BOUNCER
+import com.android.internal.util.FrameworkStatsLog.ASSISTANT_INVOCATION_REPORTED__DEVICE_STATE__LAUNCHER_ALL_APPS
+import com.android.internal.util.FrameworkStatsLog.ASSISTANT_INVOCATION_REPORTED__DEVICE_STATE__LAUNCHER_HOME
+import com.android.internal.util.FrameworkStatsLog.ASSISTANT_INVOCATION_REPORTED__DEVICE_STATE__LAUNCHER_OVERVIEW
+import com.android.internal.util.FrameworkStatsLog.ASSISTANT_INVOCATION_REPORTED__DEVICE_STATE__UNKNOWN_DEVICE_STATE
+import com.android.internal.util.FrameworkStatsLog.ASSISTANT_INVOCATION_REPORTED__DEVICE_STATE__UNLOCKED_LOCKSCREEN
+
+enum class AssistantInvocationEvent(private val id: Int) : UiEventLogger.UiEventEnum {
+    @UiEvent(doc = "Assistant invoked by unknown method")
+    ASSISTANT_INVOCATION_UNKNOWN(442),
+
+    @UiEvent(doc = "Assistant invoked by touch gesture")
+    ASSISTANT_INVOCATION_TOUCH_GESTURE(443),
+
+    @UiEvent(doc = "Assistant invoked by alternate touch gesture")
+    ASSISTANT_INVOCATION_TOUCH_GESTURE_ALT(444),
+
+    @UiEvent(doc = "Assistant invoked by hotword")
+    ASSISTANT_INVOCATION_HOTWORD(445),
+
+    @UiEvent(doc = "Assistant invoked by tapping quick search bar icon")
+    ASSISTANT_INVOCATION_QUICK_SEARCH_BAR(446),
+
+    @UiEvent(doc = "Assistant invoked by home button long press")
+    ASSISTANT_INVOCATION_HOME_LONG_PRESS(447),
+
+    @UiEvent(doc = "Assistant invoked by physical gesture")
+    ASSISTANT_INVOCATION_PHYSICAL_GESTURE(448);
+
+    override fun getId(): Int {
+        return id
+    }
+
+    companion object {
+        fun eventIdFromLegacyInvocationType(legacyInvocationType: Int): Int {
+            return when (legacyInvocationType) {
+                AssistManager.INVOCATION_TYPE_GESTURE ->
+                    ASSISTANT_INVOCATION_TOUCH_GESTURE
+
+                AssistManager.INVOCATION_TYPE_OTHER ->
+                    ASSISTANT_INVOCATION_PHYSICAL_GESTURE
+
+                AssistManager.INVOCATION_TYPE_VOICE ->
+                    ASSISTANT_INVOCATION_HOTWORD
+
+                AssistManager.INVOCATION_TYPE_QUICK_SEARCH_BAR ->
+                    ASSISTANT_INVOCATION_QUICK_SEARCH_BAR
+
+                AssistManager.INVOCATION_HOME_BUTTON_LONG_PRESS ->
+                    ASSISTANT_INVOCATION_HOME_LONG_PRESS
+
+                else ->
+                    ASSISTANT_INVOCATION_UNKNOWN
+            }.id
+        }
+
+        fun deviceStateFromLegacyDeviceState(legacyDeviceState: Int): Int {
+            return when (legacyDeviceState) {
+                PhoneStateMonitor.PHONE_STATE_AOD1 ->
+                    ASSISTANT_INVOCATION_REPORTED__DEVICE_STATE__AOD1
+
+                PhoneStateMonitor.PHONE_STATE_AOD2 ->
+                    ASSISTANT_INVOCATION_REPORTED__DEVICE_STATE__AOD2
+
+                PhoneStateMonitor.PHONE_STATE_BOUNCER ->
+                    ASSISTANT_INVOCATION_REPORTED__DEVICE_STATE__BOUNCER
+
+                PhoneStateMonitor.PHONE_STATE_UNLOCKED_LOCKSCREEN ->
+                    ASSISTANT_INVOCATION_REPORTED__DEVICE_STATE__UNLOCKED_LOCKSCREEN
+
+                PhoneStateMonitor.PHONE_STATE_HOME ->
+                    ASSISTANT_INVOCATION_REPORTED__DEVICE_STATE__LAUNCHER_HOME
+
+                PhoneStateMonitor.PHONE_STATE_OVERVIEW ->
+                    ASSISTANT_INVOCATION_REPORTED__DEVICE_STATE__LAUNCHER_OVERVIEW
+
+                PhoneStateMonitor.PHONE_STATE_ALL_APPS ->
+                    ASSISTANT_INVOCATION_REPORTED__DEVICE_STATE__LAUNCHER_ALL_APPS
+
+                PhoneStateMonitor.PHONE_STATE_APP_DEFAULT ->
+                    ASSISTANT_INVOCATION_REPORTED__DEVICE_STATE__APP_DEFAULT
+
+                PhoneStateMonitor.PHONE_STATE_APP_IMMERSIVE ->
+                    ASSISTANT_INVOCATION_REPORTED__DEVICE_STATE__APP_IMMERSIVE
+
+                PhoneStateMonitor.PHONE_STATE_APP_FULLSCREEN ->
+                    ASSISTANT_INVOCATION_REPORTED__DEVICE_STATE__APP_FULLSCREEN
+
+                else ->
+                    ASSISTANT_INVOCATION_REPORTED__DEVICE_STATE__UNKNOWN_DEVICE_STATE
+            }
+        }
+    }
+}
\ No newline at end of file
diff --git a/packages/SystemUI/src/com/android/systemui/assist/PhoneStateMonitor.java b/packages/SystemUI/src/com/android/systemui/assist/PhoneStateMonitor.java
index 9de6854..652ce6f 100644
--- a/packages/SystemUI/src/com/android/systemui/assist/PhoneStateMonitor.java
+++ b/packages/SystemUI/src/com/android/systemui/assist/PhoneStateMonitor.java
@@ -50,16 +50,16 @@
 @Singleton
 public final class PhoneStateMonitor {
 
-    private static final int PHONE_STATE_AOD1 = 1;
-    private static final int PHONE_STATE_AOD2 = 2;
-    private static final int PHONE_STATE_BOUNCER = 3;
-    private static final int PHONE_STATE_UNLOCKED_LOCKSCREEN = 4;
-    private static final int PHONE_STATE_HOME = 5;
-    private static final int PHONE_STATE_OVERVIEW = 6;
-    private static final int PHONE_STATE_ALL_APPS = 7;
-    private static final int PHONE_STATE_APP_DEFAULT = 8;
-    private static final int PHONE_STATE_APP_IMMERSIVE = 9;
-    private static final int PHONE_STATE_APP_FULLSCREEN = 10;
+    public static final int PHONE_STATE_AOD1 = 1;
+    public static final int PHONE_STATE_AOD2 = 2;
+    public static final int PHONE_STATE_BOUNCER = 3;
+    public static final int PHONE_STATE_UNLOCKED_LOCKSCREEN = 4;
+    public static final int PHONE_STATE_HOME = 5;
+    public static final int PHONE_STATE_OVERVIEW = 6;
+    public static final int PHONE_STATE_ALL_APPS = 7;
+    public static final int PHONE_STATE_APP_DEFAULT = 8;
+    public static final int PHONE_STATE_APP_IMMERSIVE = 9;
+    public static final int PHONE_STATE_APP_FULLSCREEN = 10;
 
     private static final String[] DEFAULT_HOME_CHANGE_ACTIONS = new String[] {
             PackageManagerWrapper.ACTION_PREFERRED_ACTIVITY_CHANGED,