|
| 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) = reporter.report(data) |
| 18 | +} |
| 19 | + |
| 20 | +abstract class AnalyticsReporter { |
| 21 | + |
| 22 | + fun report(data: AnalyticsData) = data.reportTo(this) |
| 23 | + |
| 24 | + internal abstract fun process(data: AnalyticsData) |
| 25 | +} |
| 26 | + |
| 27 | +internal object PrintingReporter : AnalyticsReporter() { |
| 28 | + override fun process(data: AnalyticsData) = println(data.data) |
| 29 | + |
| 30 | +} |
| 31 | + |
| 32 | +internal object NoOpReporter : AnalyticsReporter() { |
| 33 | + override fun process(data: AnalyticsData) = Unit |
| 34 | +} |
| 35 | + |
| 36 | +abstract class AnalyticsData(type: String) { |
| 37 | + val data = mutableMapOf<String, Any>() |
| 38 | + |
| 39 | + init { |
| 40 | + add("type", type) |
| 41 | + } |
| 42 | + |
| 43 | + companion object { |
| 44 | + @JvmStatic |
| 45 | + fun forAction(action: AnAction, event: AnActionEvent): ActionData = ActionData( |
| 46 | + event.actionManager.getId(action) |
| 47 | + // `FlutterAppAction`s aren't registered so ask them directly. |
| 48 | + ?: (action as? FlutterAppAction)?.id, |
| 49 | + event.place |
| 50 | + ) |
| 51 | + } |
| 52 | + |
| 53 | + fun add(key: String, value: Boolean) { |
| 54 | + data[key] = value |
| 55 | + } |
| 56 | + |
| 57 | + fun add(key: String, value: Int) { |
| 58 | + data[key] = value |
| 59 | + } |
| 60 | + |
| 61 | + fun add(key: String, value: String) { |
| 62 | + data[key] = value |
| 63 | + } |
| 64 | + |
| 65 | + open fun reportTo(reporter: AnalyticsReporter) = reporter.process(this) |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * Data describing an IntelliJ [com.intellij.openapi.actionSystem.AnAction] for analytics reporting. |
| 70 | + * |
| 71 | + * @param id The unique identifier of the action, typically defined in `plugin.xml`. |
| 72 | + * @param place The UI location where the action was invoked (e.g., "MainMenu", "Toolbar"). |
| 73 | + * @see <a href="https://plugins.jetbrains.com/docs/intellij/basic-action-system.html">IntelliJ Action System</a> |
| 74 | + */ |
| 75 | +class ActionData(private val id: String?, private val place: String) : AnalyticsData("action") { |
| 76 | + |
| 77 | + init { |
| 78 | + id?.let { add("id", it) } |
| 79 | + add("place", place) |
| 80 | + } |
| 81 | + |
| 82 | + override fun reportTo(reporter: AnalyticsReporter) { |
| 83 | + // We only report if we have an id for the event. |
| 84 | + if (id == null) return |
| 85 | + super.reportTo(reporter) |
| 86 | + } |
| 87 | +} |
0 commit comments