Commit 1343a020 authored by Matt Perry's avatar Matt Perry

Add Flutter GCM package and extend fitness app to use GCM.

parent 24c0645d
......@@ -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"
......
......@@ -8,7 +8,17 @@
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET" />
<application android:icon="@mipmap/ic_launcher" android:label="Fitness" android:name="org.domokit.sky.shell.SkyApplication">
<!-- for GCM -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- Supposedly this permission prevents other apps from receiving our
messages, but it doesn't seem to have any effect. -->
<permission android:name="org.domokit.sky.shell.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="org.domokit.sky.shell.permission.C2D_MESSAGE" />
<!-- end for GCM -->
<application android:icon="@mipmap/ic_launcher" android:label="Fitness" android:name="org.domokit.fitness.FitnessApplication">
<activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize" android:hardwareAccelerated="true" android:launchMode="singleTask" android:name="org.domokit.sky.shell.SkyActivity" android:theme="@android:style/Theme.Black.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
......@@ -19,5 +29,34 @@
android:name="org.domokit.sky.shell.UpdateService"
android:exported="false"
android:process=":remote"/>
<!-- for GCM -->
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="org.domokit.sky.shell" />
</intent-filter>
</receiver>
<service
android:name="org.domokit.gcm.GcmListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<service
android:name="org.domokit.gcm.InstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID"/>
</intent-filter>
</service>
<service
android:name="org.domokit.gcm.RegistrationIntentService"
android:exported="false">
</service>
</application>
</manifest>
// 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);
}
});
}
}
......@@ -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<FitnessApp> {
}
}
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());
}
......@@ -5,3 +5,5 @@ dependencies:
path: ../../packages/flutter
playfair:
path: ../../packages/playfair
gcm:
path: ../../packages/gcm
......@@ -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'
......
.DS_Store
.idea
.packages
.pub/
build/
packages
pubspec.lock
// 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';
// 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<String> 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<String> topics) {
_gcmService.ptr.subscribeTopics(token, topics);
}
void unsubscribeTopics(String token, List<String> topics) {
_gcmService.ptr.unsubscribeTopics(token, topics);
}
name: gcm
description: Bindings for Google Cloud Messaging API
version: 0.0.1
author: Flutter Authors <flutter-dev@googlegroups.com>
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'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment