Merge pull request #67 from dart-lang/refactor_libraries

refactor libraries based on conditional directive changes
diff --git a/changelog.md b/changelog.md
index 858df79..6b33658 100644
--- a/changelog.md
+++ b/changelog.md
@@ -1,10 +1,16 @@
 # Changelog
 
+## unreleased
+- added a `usage` implementation for Flutter (uses conditional directives)
+- removed `lib/usage_html.dart`; use the new Analytics.create() static method
+- removed `lib/usage_io.dart`; use the new Analytics.create() static method
+- bumped to `2.0.0` for API changes and library refactorings
+
 ## 1.2.0
 - added an optional `analyticsUrl` parameter to the usage constructors
 
 ## 1.1.0
-- fix two strong mode analysis issues (overrridding a field declaration with a
+- fix two strong mode analysis issues (overriding a field declaration with a
   setter/getter pair)
 
 ## 1.0.1
diff --git a/example/example.dart b/example/example.dart
index 9d9342c..5ddb51a 100644
--- a/example/example.dart
+++ b/example/example.dart
@@ -2,31 +2,30 @@
 // for details. All rights reserved. Use of this source code is governed by a
 // BSD-style license that can be found in the LICENSE file.
 
-/**
- * A simple web app to hand-test the usage library.
- */
+/// A simple web app to hand-test the usage library.
 library usage_example;
 
+import 'dart:async';
 import 'dart:html';
 
-import 'package:usage/usage_html.dart';
+import 'package:usage/usage.dart';
 
 Analytics _analytics;
 String _lastUa;
 int _count = 0;
 
 void main() {
-  querySelector('#foo').onClick.listen((_) => _handleFoo(getAnalytics()));
-  querySelector('#bar').onClick.listen((_) => _handleBar(getAnalytics()));
-  querySelector('#page').onClick.listen((_) => _changePage(getAnalytics()));
+  querySelector('#foo').onClick.listen((_) => _handleFoo());
+  querySelector('#bar').onClick.listen((_) => _handleBar());
+  querySelector('#page').onClick.listen((_) => _changePage());
 }
 
 String _ua() => (querySelector('#ua') as InputElement).value.trim();
 
-Analytics getAnalytics() {
+Future<Analytics> getAnalytics() async {
   if (_analytics == null || _lastUa != _ua()) {
     _lastUa = _ua();
-    _analytics = new AnalyticsHtml(_lastUa, 'Test app', '1.0');
+    _analytics = await Analytics.create(_lastUa, 'Test app', '1.0');
     _analytics.optIn = true;
     _analytics.sendScreenView(window.location.pathname);
   }
@@ -34,15 +33,18 @@
   return _analytics;
 }
 
-void _handleFoo(Analytics analytics) {
+Future _handleFoo() async {
+  Analytics analytics = await getAnalytics();
   analytics.sendEvent('main', 'foo');
 }
 
-void _handleBar(Analytics analytics) {
+Future _handleBar() async {
+  Analytics analytics = await getAnalytics();
   analytics.sendEvent('main', 'bar');
 }
 
-void _changePage(Analytics analytics) {
+Future _changePage() async {
+  Analytics analytics = await getAnalytics();
   window.history.pushState(null, 'new page', '${++_count}.html');
   analytics.sendScreenView(window.location.pathname);
 }
diff --git a/example/flutter_example/pubspec.yaml b/example/flutter_example/pubspec.yaml
index a8e66a9..e95d585 100644
--- a/example/flutter_example/pubspec.yaml
+++ b/example/flutter_example/pubspec.yaml
@@ -4,4 +4,4 @@
   usage:
     path: ../..
   flutter:
-    path: ../../../../../Users/jackson/git/flutter/packages/flutter
+    path: ../../../flutter/packages/flutter
diff --git a/example/ga.dart b/example/ga.dart
index dcd9110..60e1f07 100644
--- a/example/ga.dart
+++ b/example/ga.dart
@@ -2,14 +2,14 @@
 // for details. All rights reserved. Use of this source code is governed by a
 // BSD-style license that can be found in the LICENSE file.
 
-/**
- * A simple command-line app to hand-test the usage library.
- */
+/// A simple command-line app to hand-test the usage library.
 library usage_ga;
 
-import 'package:usage/usage_io.dart';
+import 'dart:async';
 
-void main(List args) {
+import 'package:usage/usage.dart';
+
+Future main(List args) async {
   final String DEFAULT_UA = 'UA-55029513-1';
 
   if (args.isEmpty) {
@@ -21,7 +21,7 @@
 
   String ua = args.isEmpty ? DEFAULT_UA : args.first;
 
-  Analytics ga = new AnalyticsIO(ua, 'ga_test', '1.0');
+  Analytics ga = await Analytics.create(ua, 'ga_test', '1.0');
   ga.optIn = true;
 
   ga.sendScreenView('home').then((_) {
diff --git a/lib/src/usage_impl.dart b/lib/src/usage_impl.dart
index 5148764..f726195 100644
--- a/lib/src/usage_impl.dart
+++ b/lib/src/usage_impl.dart
@@ -7,8 +7,8 @@
 import 'dart:async';
 import 'dart:math' as math;
 
-import 'uuid.dart';
 import '../usage.dart';
+import 'uuid.dart';
 
 final int _MAX_EXCEPTION_LENGTH = 100;
 
diff --git a/lib/src/usage_impl_default.dart b/lib/src/usage_impl_default.dart
index 6c17178..37d9300 100644
--- a/lib/src/usage_impl_default.dart
+++ b/lib/src/usage_impl_default.dart
@@ -1,4 +1,4 @@
-// Copyright (c) 2014, the Dart project authors.  Please see the AUTHORS file
+// Copyright (c) 2016, the Dart project authors.  Please see the AUTHORS file
 // for details. All rights reserved. Use of this source code is governed by a
 // BSD-style license that can be found in the LICENSE file.
 
diff --git a/lib/src/usage_impl_flutter.dart b/lib/src/usage_impl_flutter.dart
index 9130555..39067d9 100644
--- a/lib/src/usage_impl_flutter.dart
+++ b/lib/src/usage_impl_flutter.dart
@@ -1,4 +1,4 @@
-// Copyright (c) 2014, the Dart project authors.  Please see the AUTHORS file
+// Copyright (c) 2016, the Dart project authors.  Please see the AUTHORS file
 // for details. All rights reserved. Use of this source code is governed by a
 // BSD-style license that can be found in the LICENSE file.
 
@@ -12,8 +12,8 @@
 import 'package:flutter/services.dart';
 import 'package:path/path.dart' as path;
 
-import 'usage_impl.dart';
 import '../usage.dart';
+import 'usage_impl.dart';
 
 Future<Analytics> createAnalytics(
   String trackingId,
diff --git a/lib/src/usage_impl_html.dart b/lib/src/usage_impl_html.dart
index 8ad7a7f..fe9e9a3 100644
--- a/lib/src/usage_impl_html.dart
+++ b/lib/src/usage_impl_html.dart
@@ -8,8 +8,8 @@
 import 'dart:convert' show JSON;
 import 'dart:html';
 
+import '../usage.dart';
 import 'usage_impl.dart';
-import '../usage_html.dart';
 
 Future<Analytics> createAnalytics(
   String trackingId,
@@ -25,6 +25,26 @@
   ));
 }
 
+class AnalyticsHtml extends AnalyticsImpl {
+  AnalyticsHtml(String trackingId, String applicationName, String applicationVersion, {
+    String analyticsUrl
+  }) : super(
+      trackingId,
+      new HtmlPersistentProperties(applicationName),
+      new HtmlPostHandler(),
+      applicationName: applicationName,
+      applicationVersion: applicationVersion,
+      analyticsUrl: analyticsUrl
+  ) {
+    int screenWidth = window.screen.width;
+    int screenHeight = window.screen.height;
+
+    setSessionValue('sr', '${screenWidth}x$screenHeight');
+    setSessionValue('sd', '${window.screen.pixelDepth}-bits');
+    setSessionValue('ul', window.navigator.language);
+  }
+}
+
 class HtmlPostHandler extends PostHandler {
   final Function mockRequestor;
 
diff --git a/lib/src/usage_impl_io.dart b/lib/src/usage_impl_io.dart
index 9e45376..a43862b 100644
--- a/lib/src/usage_impl_io.dart
+++ b/lib/src/usage_impl_io.dart
@@ -10,8 +10,8 @@
 
 import 'package:path/path.dart' as path;
 
+import '../usage.dart';
 import 'usage_impl.dart';
-import '../usage_io.dart';
 
 Future<Analytics> createAnalytics(
   String trackingId,
@@ -27,6 +27,19 @@
   ));
 }
 
+class AnalyticsIO extends AnalyticsImpl {
+  AnalyticsIO(String trackingId, String applicationName, String applicationVersion, {
+    String analyticsUrl
+  }) : super(
+    trackingId,
+    new IOPersistentProperties(applicationName),
+    new IOPostHandler(),
+    applicationName: applicationName,
+    applicationVersion: applicationVersion,
+    analyticsUrl: analyticsUrl
+  );
+}
+
 String _createUserAgent() {
   // Mozilla/5.0 (iPhone; U; CPU iPhone OS 5_1_1 like Mac OS X; en)
   // Dart/1.8.0-edge.41170 (macos; macos; macos; null)
diff --git a/lib/src/uuid.dart b/lib/src/uuid.dart
index 66e99ac..91b9bbb 100644
--- a/lib/src/uuid.dart
+++ b/lib/src/uuid.dart
@@ -2,9 +2,7 @@
 // for details. All rights reserved. Use of this source code is governed by a
 // BSD-style license that can be found in the LICENSE file.
 
-/**
- * A UUID generator library.
- */
+/// A UUID generator library.
 library usage.uuid;
 
 import 'dart:math' show Random;
diff --git a/lib/usage.dart b/lib/usage.dart
index 5891a38..dd5c32b 100644
--- a/lib/usage.dart
+++ b/lib/usage.dart
@@ -3,17 +3,15 @@
 // BSD-style license that can be found in the LICENSE file.
 
 /**
- * `usage` is a wrapper around Google Analytics for both command-line apps
- * and web apps.
+ * `usage` is a wrapper around Google Analytics for both command-line, web, and
+ * Flutter apps.
  *
- * In order to use this library as a web app, import the `analytics_html.dart`
- * library and instantiate the [AnalyticsHtml] class.
+ * In order to use this library, call the [Analytics.create] static method.
+ * You'll get either the command-line, web, or Flutter implementation based on
+ * the current platform.
  *
- * In order to use this library as a command-line app, import the
- * `analytics_io.dart` library and instantiate the [AnalyticsIO] class.
- *
- * For both classes, you need to provide a Google Analytics tracking ID, the
- * application name, and the application version.
+ * When creating a new Analytics instance, you need to provide a Google
+ * Analytics tracking ID, the application name, and the application version.
  *
  * Your application should provide an opt-in option for the user. If they
  * opt-in, set the [optIn] field to `true`. This setting will persist across
@@ -36,14 +34,14 @@
 final RegExp _pathRegex = new RegExp(r'file:/\S+/(\S+\.dart)');
 
 /**
- * An interface to a Google Analytics session. You'll get the correct one
- * for your platform by calling the [Analytics.create] static method.
+ * An interface to a Google Analytics session. You'll get the correct one for
+ * your platform by calling the [Analytics.create] static method.
  * [AnalyticsMock] can be used for testing or for some variants of an opt-in
  * workflow.
  *
- * The analytics information is sent on a best-effort basis. So, failures to
- * send the GA information will not result in errors from the asynchronous
- * `send` methods.
+ * The analytics information is sent on a best-effort basis. Failures to send
+ * the GA information will not result in errors from the asynchronous `send`
+ * methods.
  */
 abstract class Analytics {
   static Future<Analytics> create(
diff --git a/lib/usage_html.dart b/lib/usage_html.dart
deleted file mode 100644
index f3dd555..0000000
--- a/lib/usage_html.dart
+++ /dev/null
@@ -1,45 +0,0 @@
-// Copyright (c) 2014, the Dart project authors.  Please see the AUTHORS file
-// for details. All rights reserved. Use of this source code is governed by a
-// BSD-style license that can be found in the LICENSE file.
-
-/**
- * In order to use this library import the `usage_html.dart` file and
- * instantiate the [AnalyticsHtml] class.
- *
- * You'll need to provide a Google Analytics tracking ID, the application name,
- * and the application version.
- */
-library usage_html;
-
-import 'dart:html';
-
-import 'src/usage_impl.dart';
-import 'src/usage_impl_html.dart';
-
-export 'usage.dart';
-
-/**
- * An interface to a Google Analytics session, suitable for use in web apps.
- *
- * [analyticsUrl] is an optional replacement for the default Google Analytics
- * URL (`https://www.google-analytics.com/collect`).
- */
-class AnalyticsHtml extends AnalyticsImpl {
-  AnalyticsHtml(String trackingId, String applicationName, String applicationVersion, {
-    String analyticsUrl
-  }) : super(
-      trackingId,
-      new HtmlPersistentProperties(applicationName),
-      new HtmlPostHandler(),
-      applicationName: applicationName,
-      applicationVersion: applicationVersion,
-      analyticsUrl: analyticsUrl
-  ) {
-    int screenWidth = window.screen.width;
-    int screenHeight = window.screen.height;
-
-    setSessionValue('sr', '${screenWidth}x$screenHeight');
-    setSessionValue('sd', '${window.screen.pixelDepth}-bits');
-    setSessionValue('ul', window.navigator.language);
-  }
-}
diff --git a/lib/usage_io.dart b/lib/usage_io.dart
deleted file mode 100644
index 61accf7..0000000
--- a/lib/usage_io.dart
+++ /dev/null
@@ -1,37 +0,0 @@
-// Copyright (c) 2014, the Dart project authors.  Please see the AUTHORS file
-// for details. All rights reserved. Use of this source code is governed by a
-// BSD-style license that can be found in the LICENSE file.
-
-/**
- * In order to use this library import the `usage_io.dart` file and
- * instantiate the [AnalyticsIO] class.
- *
- * You'll need to provide a Google Analytics tracking ID, the application name,
- * and the application version.
- */
-library usage_io;
-
-import 'src/usage_impl.dart';
-import 'src/usage_impl_io.dart';
-
-export 'usage.dart';
-
-/**
- * An interface to a Google Analytics session, suitable for use in command-line
- * applications.
- *
- * [analyticsUrl] is an optional replacement for the default Google Analytics
- * URL (`https://www.google-analytics.com/collect`).
- */
-class AnalyticsIO extends AnalyticsImpl {
-  AnalyticsIO(String trackingId, String applicationName, String applicationVersion, {
-    String analyticsUrl
-  }) : super(
-    trackingId,
-    new IOPersistentProperties(applicationName),
-    new IOPostHandler(),
-    applicationName: applicationName,
-    applicationVersion: applicationVersion,
-    analyticsUrl: analyticsUrl
-  );
-}
diff --git a/pubspec.yaml b/pubspec.yaml
index 2c0a6c6..7a02fad 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -4,7 +4,7 @@
 
 name: usage
 version: 2.0.0-dev
-description: A Google Analytics wrapper for both command-line and web apps.
+description: A Google Analytics wrapper for both command-line, web, and Flutter apps.
 homepage: https://github.com/dart-lang/usage
 author: Dart Team <misc@dartlang.org>
 
diff --git a/readme.md b/readme.md
index 5a86ed7..e1c1450 100644
--- a/readme.md
+++ b/readme.md
@@ -1,24 +1,22 @@
 # usage
 
-`usage` is a wrapper around Google Analytics for both command-line apps and web
-apps.
+`usage` is a wrapper around Google Analytics for both command-line, web, and
+Flutter apps.
 
 [![Build Status](https://travis-ci.org/dart-lang/usage.svg)](https://travis-ci.org/dart-lang/usage)
 [![Coverage Status](https://img.shields.io/coveralls/dart-lang/usage.svg)](https://coveralls.io/r/dart-lang/usage?branch=master)
 
-## For web apps
+## Using this library
 
-To use this library as a web app, import the `usage_html.dart` library and
-instantiate the `AnalyticsHtml` class.
+In order to use this library, call the [Analytics.create] static method.
+You'll get either the command-line, web, or Flutter implementation based on
+the current platform.
 
 When you are creating a new property at [google analytics](https://www.google.com/analytics/)
 make sure to select not the website option, but the **mobile app** option.
 
 ## For command-line apps
 
-To use this library as a command-line app, import the `usage_io.dart` library
-and instantiate the `AnalyticsIO` class.
-
 Note, for CLI apps, the usage library will send analytics pings asynchronously.
 This is useful it that it doesn't block the app generally. It does have one
 side-effect, in that outstanding asynchronous requests will block termination
@@ -40,10 +38,10 @@
 
 ## Using the API
 
-Import the package (in this example we use the `dart:io` version):
+Import the package:
 
 ```dart
-import 'package:usage/usage_io.dart';
+import 'package:usage/usage.dart';
 ```
 
 And call some analytics code:
@@ -51,7 +49,7 @@
 ```dart
 final String UA = ...;
 
-Analytics ga = new AnalyticsIO(UA, 'ga_test', '1.0');
+Analytics ga = await Analytics.create(UA, 'ga_test', '1.0');
 ga.optIn = true;
 
 ga.sendScreenView('home');
diff --git a/tool/travis.sh b/tool/travis.sh
index fe23fcc..e6f3aca 100755
--- a/tool/travis.sh
+++ b/tool/travis.sh
@@ -9,9 +9,9 @@
 
 # Verify that the libraries are error free.
 dartanalyzer --fatal-warnings \
+  example/example.dart \
+  example/ga.dart \
   lib/usage.dart \
-  lib/usage_html.dart \
-  lib/usage_io.dart \
   test/all.dart
 
 # Run the tests.