diff --git a/examples/hello_android/app/src/flutter/lib/main.dart b/examples/hello_android/app/src/flutter/lib/main.dart index 151b1da2b8..9e75dba943 100644 --- a/examples/hello_android/app/src/flutter/lib/main.dart +++ b/examples/hello_android/app/src/flutter/lib/main.dart @@ -2,6 +2,71 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -import 'package:flutter/widgets.dart'; +import 'dart:async'; +import 'dart:convert'; +import 'dart:math'; -void main() => runApp(new Center(child: new Text('Hello from Flutter!'))); +import 'package:flutter/material.dart'; +import 'package:flutter/widgets.dart'; +import 'package:flutter/services.dart'; + +final Random random = new Random(); + +Future handleGetRandom(String json) async { + Map message = JSON.decode(json); + double min = message['min'].toDouble(); + double max = message['max'].toDouble(); + + double value = (random.nextDouble() * (max - min)) + min; + + Map reply = {'value': value}; + return JSON.encode(reply); +} + +class HelloAndroid extends StatefulWidget { + @override + _HelloAndroidState createState() => new _HelloAndroidState(); +} + +class _HelloAndroidState extends State { + double _latitude; + double _longitude; + + @override + Widget build(BuildContext context) { + return new Material( + child: new Center( + child: new Column( + children: [ + new Text('Hello from Flutter!'), + new RaisedButton( + child: new Text('Get Location'), + onPressed: _getLocation + ), + new Text('Latitude: ${_latitude}, Longitude: ${_longitude}'), + ] + ) + ) + ); + } + + void _getLocation() { + Map message = {'provider': 'network'}; + HostMessages.sendToHost('getLocation', JSON.encode(message)) + .then(_onReceivedLocation); + } + + void _onReceivedLocation(String json) { + Map reply = JSON.decode(json); + setState(() { + _latitude = reply['latitude']; + _longitude = reply['longitude']; + }); + } +} + +void main() { + runApp(new HelloAndroid()); + + HostMessages.addMessageHandler('getRandom', handleGetRandom); +} diff --git a/examples/hello_android/app/src/main/AndroidManifest.xml b/examples/hello_android/app/src/main/AndroidManifest.xml index a272e1429a..c36839439d 100644 --- a/examples/hello_android/app/src/main/AndroidManifest.xml +++ b/examples/hello_android/app/src/main/AndroidManifest.xml @@ -6,14 +6,15 @@ + diff --git a/examples/hello_android/app/src/main/java/com/example/flutter/ExampleActivity.java b/examples/hello_android/app/src/main/java/com/example/flutter/ExampleActivity.java new file mode 100644 index 0000000000..e0ef7ca575 --- /dev/null +++ b/examples/hello_android/app/src/main/java/com/example/flutter/ExampleActivity.java @@ -0,0 +1,146 @@ +// 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. + +package com.example.flutter; + +import android.app.Activity; +import android.content.Context; +import android.location.Location; +import android.location.LocationManager; +import android.os.Bundle; +import android.util.Log; +import android.view.View; +import android.widget.Button; +import android.widget.TextView; + +import org.chromium.base.PathUtils; +import org.domokit.activity.ActivityImpl; +import org.domokit.sky.shell.SkyMain; +import org.domokit.sky.shell.PlatformViewAndroid; + +import java.io.File; +import org.json.JSONException; +import org.json.JSONObject; + +public class ExampleActivity extends Activity { + private static final String TAG = "ExampleActivity"; + + private PlatformViewAndroid flutterView; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + SkyMain.ensureInitialized(getApplicationContext(), null); + setContentView(R.layout.flutter_layout); + + flutterView = (PlatformViewAndroid) findViewById(R.id.flutter_view); + File appBundle = new File(PathUtils.getDataDirectory(this), SkyMain.APP_BUNDLE); + flutterView.runFromBundle(appBundle.getPath(), null); + + flutterView.addOnMessageListener("getLocation", + new PlatformViewAndroid.OnMessageListener() { + @Override + public String onMessage(String message) { + return onGetLocation(message); + } + }); + + Button getRandom = (Button) findViewById(R.id.get_random); + getRandom.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + sendGetRandom(); + } + }); + } + + @Override + protected void onDestroy() { + if (flutterView != null) { + flutterView.destroy(); + } + super.onDestroy(); + } + + @Override + protected void onPause() { + super.onPause(); + flutterView.onPause(); + } + + @Override + protected void onResume() { + super.onResume(); + flutterView.onResume(); + } + + private void sendGetRandom() { + JSONObject message = new JSONObject(); + try { + message.put("min", 1); + message.put("max", 1000); + } catch (JSONException e) { + Log.e(TAG, "JSON exception", e); + return; + } + + flutterView.sendToFlutter("getRandom", message.toString(), + new PlatformViewAndroid.MessageReplyCallback() { + @Override + public void onReply(String json) { + onRandomReply(json); + } + }); + } + + private void onRandomReply(String json) { + double value; + try { + JSONObject reply = new JSONObject(json); + value = reply.getDouble("value"); + } catch (JSONException e) { + Log.e(TAG, "JSON exception", e); + return; + } + + TextView randomValue = (TextView) findViewById(R.id.random_value); + randomValue.setText(Double.toString(value)); + } + + private String onGetLocation(String json) { + String provider; + try { + JSONObject message = new JSONObject(json); + provider = message.getString("provider"); + } catch (JSONException e) { + Log.e(TAG, "JSON exception", e); + return null; + } + + String locationProvider; + if (provider.equals("network")) { + locationProvider = LocationManager.NETWORK_PROVIDER; + } else if (provider.equals("gps")) { + locationProvider = LocationManager.GPS_PROVIDER; + } else { + return null; + } + + LocationManager locationManager = + (LocationManager) getSystemService(Context.LOCATION_SERVICE); + Location location = locationManager.getLastKnownLocation(locationProvider); + + JSONObject reply = new JSONObject(); + try { + reply.put("latitude", location.getLatitude()); + reply.put("longitude", location.getLongitude()); + } catch (JSONException e) { + Log.e(TAG, "JSON exception", e); + return null; + } + + return reply.toString(); + } +} diff --git a/examples/hello_android/app/src/main/java/com/example/flutter/FlutterActivity.java b/examples/hello_android/app/src/main/java/com/example/flutter/FlutterActivity.java deleted file mode 100644 index 255e4ed322..0000000000 --- a/examples/hello_android/app/src/main/java/com/example/flutter/FlutterActivity.java +++ /dev/null @@ -1,38 +0,0 @@ -// 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. - -package com.example.flutter; - -import android.app.Activity; -import android.os.Bundle; - -import org.chromium.base.PathUtils; -import org.domokit.sky.shell.SkyMain; -import org.domokit.sky.shell.PlatformViewAndroid; - -import java.io.File; - -public class FlutterActivity extends Activity { - private PlatformViewAndroid flutterView; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - SkyMain.ensureInitialized(getApplicationContext(), null); - setContentView(R.layout.flutter_layout); - - flutterView = (PlatformViewAndroid) findViewById(R.id.flutter_view); - File appBundle = new File(PathUtils.getDataDirectory(this), SkyMain.APP_BUNDLE); - flutterView.runFromBundle(appBundle.getPath(), null); - } - - @Override - protected void onDestroy() { - if (flutterView != null) { - flutterView.destroy(); - } - super.onDestroy(); - } -} diff --git a/examples/hello_android/app/src/main/res/layout/flutter_layout.xml b/examples/hello_android/app/src/main/res/layout/flutter_layout.xml index bc8c11d239..dbdcf3fc33 100644 --- a/examples/hello_android/app/src/main/res/layout/flutter_layout.xml +++ b/examples/hello_android/app/src/main/res/layout/flutter_layout.xml @@ -4,15 +4,36 @@ android:layout_width="fill_parent" android:layout_height="fill_parent" > + + + +