usage.dart 4.81 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

import 'package:usage/src/usage_impl_io.dart';
import 'package:usage/usage.dart';

import 'base/context.dart';
11
import 'base/utils.dart';
12
import 'globals.dart';
13
import 'version.dart';
14 15 16 17 18 19 20 21 22 23

// TODO(devoncarew): We'll need to do some work on the user agent in order to
// correctly track usage by operating system (dart-lang/usage/issues/70).

// TODO(devoncarew): We'll want to find a way to send (sanitized) command parameters.

const String _kFlutterUA = 'UA-67589403-5';

class Usage {
  Usage() {
24 25
    String version = FlutterVersion.getVersionString(whitelistBranchName: true);
    _analytics = new AnalyticsIO(_kFlutterUA, 'flutter', version);
26 27 28 29 30 31 32 33 34 35 36 37 38

    bool runningOnCI = false;

    // Many CI systems don't do a full git checkout.
    if (version.startsWith('unknown/'))
      runningOnCI = true;

    // Check for common CI systems.
    if (isRunningOnBot)
      runningOnCI = true;

    // If we think we're running on a CI system, default to not sending analytics.
    _analytics.analyticsOpt = runningOnCI ? AnalyticsOpt.optIn : AnalyticsOpt.optOut;
39 40 41 42 43 44 45
  }

  /// Returns [Usage] active in the current app context.
  static Usage get instance => context[Usage] ?? (context[Usage] = new Usage());

  Analytics _analytics;

46 47
  bool _printedUsage = false;

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
  bool get isFirstRun => _analytics.firstRun;

  bool get enabled => _analytics.enabled;

  /// Enable or disable reporting analytics.
  set enabled(bool value) {
    _analytics.enabled = value;
  }

  void sendCommand(String command) {
    if (!isFirstRun)
      _analytics.sendScreenView(command);
  }

  void sendEvent(String category, String parameter) {
    if (!isFirstRun)
      _analytics.sendEvent(category, parameter);
  }

  UsageTimer startTimer(String event) {
    if (isFirstRun)
      return new _MockUsageTimer();
    else
71
      return new UsageTimer._(event, _analytics.startTimer(event, category: 'flutter'));
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
  }

  void sendException(dynamic exception, StackTrace trace) {
    if (!isFirstRun)
      _analytics.sendException('${exception.runtimeType}; ${sanitizeStacktrace(trace)}');
  }

  /// Fires whenever analytics data is sent over the network; public for testing.
  Stream<Map<String, dynamic>> get onSend => _analytics.onSend;

  /// Returns when the last analytics event has been sent, or after a fixed
  /// (short) delay, whichever is less.
  Future<Null> ensureAnalyticsSent() {
    // TODO(devoncarew): This may delay tool exit and could cause some analytics
    // events to not be reported. Perhaps we could send the analytics pings
    // out-of-process from flutter_tools?
    return _analytics.waitForLastPing(timeout: new Duration(milliseconds: 250));
  }
90 91 92 93 94 95 96 97

  void printUsage() {
    if (_printedUsage)
      return;
    _printedUsage = true;

    final String versionString = FlutterVersion.getVersionString(whitelistBranchName: true);

98 99 100 101
    String welcomeString = 'Welcome to Flutter! - Flutter version $versionString - https://flutter.io';
    welcomeString = welcomeString.padLeft((welcomeString.length + 100) ~/ 2);
    welcomeString = welcomeString.padRight(100);

102 103 104
    printStatus('');
    printStatus('''
  ╔════════════════════════════════════════════════════════════════════════════════════════════════════╗
105
$welcomeString
106 107 108 109 110 111 112 113 114
  ║                                                                                                    ║
  ║ The Flutter tool anonymously reports feature usage statistics and basic crash reports to Google in ║
  ║ order to help Google contribute improvements to Flutter over time. See Google's privacy policy:    
   https://www.google.com/intl/en/policies/privacy/                                                   ║
                                                                                                      
                   Use "flutter config --no-analytics" to disable analytics reporting                 
  ╚════════════════════════════════════════════════════════════════════════════════════════════════════╝
  ''', emphasis: true);
  }
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
}

class UsageTimer {
  UsageTimer._(this.event, this._timer);

  final String event;
  final AnalyticsTimer _timer;

  void finish() {
    _timer.finish();
  }
}

class _MockUsageTimer implements UsageTimer {
  @override
  String event;
  @override
  AnalyticsTimer _timer;

  @override
  void finish() { }
}