From 1343a02032a0a1c6e0b055dd29c44585c1961d65 Mon Sep 17 00:00:00 2001 From: Matt Perry Date: Mon, 11 Jan 2016 13:45:32 -0500 Subject: [PATCH] Add Flutter GCM package and extend fitness app to use GCM. --- examples/fitness/BUILD.gn | 19 ++++++++ examples/fitness/apk/AndroidManifest.xml | 41 +++++++++++++++++- .../domokit/fitness/FitnessApplication.java | 36 ++++++++++++++++ examples/fitness/lib/main.dart | 15 ++++++- examples/fitness/pubspec.yaml | 2 + packages/flutter/pubspec.yaml | 4 +- packages/gcm/.gitignore | 7 +++ packages/gcm/lib/gcm.dart | 10 +++++ packages/gcm/lib/src/gcm.dart | 43 +++++++++++++++++++ packages/gcm/pubspec.yaml | 15 +++++++ 10 files changed, 188 insertions(+), 4 deletions(-) create mode 100644 examples/fitness/apk/src/org/domokit/fitness/FitnessApplication.java create mode 100644 packages/gcm/.gitignore create mode 100644 packages/gcm/lib/gcm.dart create mode 100644 packages/gcm/lib/src/gcm.dart create mode 100644 packages/gcm/pubspec.yaml diff --git a/examples/fitness/BUILD.gn b/examples/fitness/BUILD.gn index c24ac93a85..b576d4ec1c 100644 --- a/examples/fitness/BUILD.gn +++ b/examples/fitness/BUILD.gn @@ -9,10 +9,29 @@ sky_app("fitness") { manifest = "flutter.yaml" if (is_android) { + import("//build/config/android/config.gni") + import("//build/config/android/rules.gni") + apk_name = "Fitness" + android_library("java") { + java_files = [ + "apk/src/org/domokit/fitness/FitnessApplication.java", + ] + + deps = [ + "//base:base_java", + "//mojo/public/java:bindings", + "//mojo/public/java:system", + "//sky/services/gcm:gcm_lib", + "//sky/services/gcm:interfaces_java", + "//sky/shell:java", + ] + } + deps = [ "//examples/fitness/apk:resources", + ":java", ] } else if (is_mac) { info_plist = "mac/Info.plist" diff --git a/examples/fitness/apk/AndroidManifest.xml b/examples/fitness/apk/AndroidManifest.xml index 83967b2cfe..2d36384715 100644 --- a/examples/fitness/apk/AndroidManifest.xml +++ b/examples/fitness/apk/AndroidManifest.xml @@ -8,7 +8,17 @@ - + + + + + + + + + @@ -19,5 +29,34 @@ android:name="org.domokit.sky.shell.UpdateService" android:exported="false" android:process=":remote"/> + + + + + + + + + + + + + + + + + + + + diff --git a/examples/fitness/apk/src/org/domokit/fitness/FitnessApplication.java b/examples/fitness/apk/src/org/domokit/fitness/FitnessApplication.java new file mode 100644 index 0000000000..ab3edb3228 --- /dev/null +++ b/examples/fitness/apk/src/org/domokit/fitness/FitnessApplication.java @@ -0,0 +1,36 @@ +// Copyright 2015 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. + +package org.domokit.fitness; + +import android.content.Context; + +import org.chromium.mojo.system.Core; +import org.chromium.mojo.system.MessagePipeHandle; +import org.chromium.mojom.gcm.GcmService; +import org.domokit.gcm.RegistrationIntentService; +import org.domokit.sky.shell.ServiceFactory; +import org.domokit.sky.shell.ServiceRegistry; +import org.domokit.sky.shell.SkyApplication; + +/** + * Sky implementation of {@link android.app.Application}, managing application-level global + * state and initializations. + */ +public class FitnessApplication extends SkyApplication { + /** + * Override this function to register more services. + */ + protected void onServiceRegistryAvailable(ServiceRegistry registry) { + super.onServiceRegistryAvailable(registry); + + registry.register(GcmService.MANAGER.getName(), new ServiceFactory() { + @Override + public void connectToService(Context context, Core core, MessagePipeHandle pipe) { + GcmService.MANAGER.bind( + new RegistrationIntentService.MojoService(context), pipe); + } + }); + } +} diff --git a/examples/fitness/lib/main.dart b/examples/fitness/lib/main.dart index 029b877acd..311bfc7139 100644 --- a/examples/fitness/lib/main.dart +++ b/examples/fitness/lib/main.dart @@ -7,6 +7,7 @@ library fitness; import 'package:playfair/playfair.dart' as playfair; import 'package:flutter/material.dart'; import 'package:flutter/painting.dart'; +import 'package:gcm/gcm.dart' as gcm; import 'user_data.dart'; import 'date_utils.dart'; @@ -161,6 +162,18 @@ class FitnessAppState extends State { } } -void main() { +initGcm() async { + // Register for GCM messages using the senderId provided in the + // google-services.json we received when registering our app. + String token; + token = await gcm.registerGcmService( + "858790231562", (String from, String message) { + print("onMessageReceived: $from; $message"); + gcm.unsubscribeTopics(token, ["global"]); + }); + gcm.subscribeTopics(token, ["global"]); +} + +main() { runApp(new FitnessApp()); } diff --git a/examples/fitness/pubspec.yaml b/examples/fitness/pubspec.yaml index 75c66e9415..6cf122b61f 100644 --- a/examples/fitness/pubspec.yaml +++ b/examples/fitness/pubspec.yaml @@ -5,3 +5,5 @@ dependencies: path: ../../packages/flutter playfair: path: ../../packages/playfair + gcm: + path: ../../packages/gcm diff --git a/packages/flutter/pubspec.yaml b/packages/flutter/pubspec.yaml index a6ff61e219..b4992b5ca3 100644 --- a/packages/flutter/pubspec.yaml +++ b/packages/flutter/pubspec.yaml @@ -8,8 +8,8 @@ dependencies: collection: '>=1.1.3 <2.0.0' intl: '>=0.12.4+2 <0.13.0' material_design_icons: '>=0.0.3 <0.1.0' - sky_engine: 0.0.75 - sky_services: 0.0.75 + sky_engine: 0.0.77 + sky_services: 0.0.77 vector_math: '>=1.4.3 <2.0.0' quiver: '>=0.21.4 <0.22.0' diff --git a/packages/gcm/.gitignore b/packages/gcm/.gitignore new file mode 100644 index 0000000000..1687d396df --- /dev/null +++ b/packages/gcm/.gitignore @@ -0,0 +1,7 @@ +.DS_Store +.idea +.packages +.pub/ +build/ +packages +pubspec.lock diff --git a/packages/gcm/lib/gcm.dart b/packages/gcm/lib/gcm.dart new file mode 100644 index 0000000000..51171f240c --- /dev/null +++ b/packages/gcm/lib/gcm.dart @@ -0,0 +1,10 @@ +// Copyright 2015 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. + +/// Service exposed to Flutter apps that implements a subset of the GCM API. +/// +/// This library will probably be moved into a separate package eventually. +library gcm; + +export 'src/gcm.dart'; diff --git a/packages/gcm/lib/src/gcm.dart b/packages/gcm/lib/src/gcm.dart new file mode 100644 index 0000000000..23ccd82621 --- /dev/null +++ b/packages/gcm/lib/src/gcm.dart @@ -0,0 +1,43 @@ +// Copyright 2015, the Flutter 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. + +import 'dart:async'; + +import 'package:flutter/services.dart'; +import 'package:sky_services/gcm/gcm.mojom.dart'; + +GcmServiceProxy _initGcmService() { + GcmServiceProxy gcmService = new GcmServiceProxy.unbound(); + shell.connectToService(null, gcmService); + return gcmService; +} + +final GcmServiceProxy _gcmService = _initGcmService(); + +typedef void GcmListenerCallback(String from, String message); +class _GcmListenerImpl implements GcmListener { + _GcmListenerImpl(this.callback); + + GcmListenerCallback callback; + + void onMessageReceived(String from, String message) { + callback(from, message); + } +} + +Future registerGcmService(String senderId, GcmListenerCallback listenerCallback) async { + GcmListenerStub listener = new GcmListenerStub.unbound() + ..impl = new _GcmListenerImpl(listenerCallback); + GcmServiceRegisterResponseParams result = + await _gcmService.ptr.register(senderId, listener); + return result.token; +} + +void subscribeTopics(String token, List topics) { + _gcmService.ptr.subscribeTopics(token, topics); +} + +void unsubscribeTopics(String token, List topics) { + _gcmService.ptr.unsubscribeTopics(token, topics); +} diff --git a/packages/gcm/pubspec.yaml b/packages/gcm/pubspec.yaml new file mode 100644 index 0000000000..242cf036da --- /dev/null +++ b/packages/gcm/pubspec.yaml @@ -0,0 +1,15 @@ +name: gcm +description: Bindings for Google Cloud Messaging API +version: 0.0.1 +author: Flutter Authors +homepage: https://github.com/flutter/flutter/tree/master/packages/playfair + +dependencies: + flutter: + path: ../flutter + +dev_dependencies: + test: 0.12.6+1 + +environment: + sdk: '>=1.12.0 <2.0.0'