|
| 1 | +/* |
| 2 | + * Copyright 2025 The Chromium Authors. All rights reserved. |
| 3 | + * Use of this source code is governed by a BSD-style license that can be |
| 4 | + * found in the LICENSE file. |
| 5 | + */ |
| 6 | + |
| 7 | +package io.flutter.analytics |
| 8 | + |
| 9 | +import com.intellij.openapi.actionSystem.AnAction |
| 10 | +import com.intellij.openapi.actionSystem.AnActionEvent |
| 11 | +import io.flutter.actions.FlutterAppAction |
| 12 | + |
| 13 | +object Analytics { |
| 14 | + private val reporter = NoOpReporter |
| 15 | + |
| 16 | + @JvmStatic |
| 17 | + fun report(data: AnalyticsData) = data.reportTo(reporter) |
| 18 | +} |
| 19 | + |
| 20 | +abstract class AnalyticsReporter { |
| 21 | + internal abstract fun process(data: AnalyticsData) |
| 22 | +} |
| 23 | + |
| 24 | +internal object PrintingReporter : AnalyticsReporter() { |
| 25 | + override fun process(data: AnalyticsData) = println(data.data) |
| 26 | +} |
| 27 | + |
| 28 | +internal object NoOpReporter : AnalyticsReporter() { |
| 29 | + override fun process(data: AnalyticsData) = Unit |
| 30 | +} |
| 31 | + |
| 32 | +abstract class AnalyticsData(type: String) { |
| 33 | + val data = mutableMapOf<String, Any>() |
| 34 | + |
| 35 | + init { |
| 36 | + add(AnalyticsConstants.TYPE, type) |
| 37 | + } |
| 38 | + |
| 39 | + companion object { |
| 40 | + @JvmStatic |
| 41 | + fun forAction(action: AnAction, event: AnActionEvent): ActionData = ActionData( |
| 42 | + event.actionManager.getId(action) |
| 43 | + // `FlutterAppAction`s aren't registered so ask them directly. |
| 44 | + ?: (action as? FlutterAppAction)?.id, |
| 45 | + event.place |
| 46 | + ) |
| 47 | + } |
| 48 | + |
| 49 | + fun <T> add(key: DataValue<T>, value: T) = key.addTo(this, value) |
| 50 | + |
| 51 | + internal operator fun set(key: String, value: Boolean) { |
| 52 | + data[key] = value |
| 53 | + } |
| 54 | + |
| 55 | + internal operator fun set(key: String, value: Int) { |
| 56 | + data[key] = value |
| 57 | + } |
| 58 | + |
| 59 | + internal operator fun set(key: String, value: String) { |
| 60 | + data[key] = value |
| 61 | + } |
| 62 | + |
| 63 | + open fun reportTo(reporter: AnalyticsReporter) = reporter.process(this) |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Data describing an IntelliJ [com.intellij.openapi.actionSystem.AnAction] for analytics reporting. |
| 68 | + * |
| 69 | + * @param id The unique identifier of the action, typically defined in `plugin.xml`. |
| 70 | + * @param place The UI location where the action was invoked (e.g., "MainMenu", "Toolbar"). |
| 71 | + * @see <a href="https://plugins.jetbrains.com/docs/intellij/basic-action-system.html">IntelliJ Action System</a> |
| 72 | + */ |
| 73 | +class ActionData(private val id: String?, private val place: String) : AnalyticsData("action") { |
| 74 | + |
| 75 | + init { |
| 76 | + id?.let { add(AnalyticsConstants.ID, it) } |
| 77 | + add(AnalyticsConstants.PLACE, place) |
| 78 | + } |
| 79 | + |
| 80 | + override fun reportTo(reporter: AnalyticsReporter) { |
| 81 | + // We only report if we have an id for the event. |
| 82 | + if (id == null) return |
| 83 | + super.reportTo(reporter) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * Defines standard keys for analytics data properties. |
| 89 | + * |
| 90 | + * The properties are exposed as `@JvmField`s to be easily accessible as static |
| 91 | + * fields from Java. |
| 92 | + */ |
| 93 | +object AnalyticsConstants { |
| 94 | + /** |
| 95 | + * Indicates if the project is a Google3 project. |
| 96 | + */ |
| 97 | + @JvmField |
| 98 | + val GOOGLE3 = BooleanValue("google3") |
| 99 | + |
| 100 | + /** |
| 101 | + * The unique identifier for an action or event. |
| 102 | + */ |
| 103 | + @JvmField |
| 104 | + val ID = StringValue("id") |
| 105 | + |
| 106 | + /** |
| 107 | + * Indicates if the project is in a Bazel context. |
| 108 | + */ |
| 109 | + @JvmField |
| 110 | + val IN_BAZEL_CONTEXT = BooleanValue("inBazelContext") |
| 111 | + |
| 112 | + /** |
| 113 | + * Indicates if the Flutter SDK is missing. |
| 114 | + */ |
| 115 | + @JvmField |
| 116 | + val MISSING_SDK = BooleanValue("missingSdk") |
| 117 | + |
| 118 | + /** |
| 119 | + * The UI location where an action was invoked, as provided by |
| 120 | + * [com.intellij.openapi.actionSystem.PlaceProvider.getPlace] (for example, "MainMenu", |
| 121 | + * "MainToolbar", "EditorPopup", "GoToAction", etc). |
| 122 | + */ |
| 123 | + @JvmField |
| 124 | + val PLACE = StringValue("place") |
| 125 | + |
| 126 | + /** |
| 127 | + * Indicates if a restart is required for a hot reload request. |
| 128 | + */ |
| 129 | + @JvmField |
| 130 | + val REQUIRES_RESTART = BooleanValue("requiresRestart") |
| 131 | + |
| 132 | + /** |
| 133 | + * The type of the analytics event (e.g., "action", ...). |
| 134 | + */ |
| 135 | + @JvmField |
| 136 | + val TYPE = StringValue("type") |
| 137 | +} |
| 138 | + |
| 139 | + |
| 140 | +sealed class DataValue<T>(val name: String) { |
| 141 | + abstract fun addTo(data: AnalyticsData, value: T); |
| 142 | +} |
| 143 | + |
| 144 | +class StringValue(name: String) : DataValue<String>(name) { |
| 145 | + override fun addTo(data: AnalyticsData, value: String) { |
| 146 | + data[name] = value |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +class IntValue(name: String) : DataValue<Int>(name) { |
| 151 | + override fun addTo(data: AnalyticsData, value: Int) { |
| 152 | + data[name] = value |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +class BooleanValue(name: String) : DataValue<Boolean>(name) { |
| 157 | + override fun addTo(data: AnalyticsData, value: Boolean) { |
| 158 | + data[name] = value |
| 159 | + } |
| 160 | +} |
0 commit comments