Commit 93c6dbb9 authored by szakarias's avatar szakarias Committed by GitHub

Add platform_services sample (#8058)

* Add platform_services sample

* update build.gradle to use latest scripts

* use FlutterActivity

* Updated ExampleInstrumentedTest.java and deleted FlutterPlugin.groovy

* Remove getRandom code from main.dart

* remove unused import
parent 07045c20
.DS_Store
.atom/
.idea
.packages
.pub/
build/
packages/
pubspec.lock
# Example of calling platform services from Flutter
This project demonstrates how to connect a Flutter app to platform specific services.
You can read more about
[accessing platform and third-party services in Flutter](https://flutter.io/platform-services/).
## iOS
### Configure
Create an `ios/Flutter/Generated.xcconfig` file with this entry:
* `FLUTTER_ROOT=[absolute path to the Flutter SDK]`
There are a number of other parameters you can control with this file:
* `FLUTTER_APPLICATION_PATH`: The path to the directory that contains your
`pubspec.yaml` file relative to your `xcodeproj` file.
* `FLUTTER_BUILD_MODE`: Whether to build for `debug`, `profile`, or `release`.
Defaults to `release`.
* `FLUTTER_TARGET`: The path to your `main.dart` relative to your
`pubspec.yaml`. Defaults to `lib/main.dart`.
* `FLUTTER_FRAMEWORK_DIR`: The absolute path to the directory that contains
`Flutter.framework`. Defaults to the `ios-release` version of
`Flutter.framework` in the `bin/cache` directory of the Flutter SDK.
### Build
Once you've configured your project, you can open `ios/Runner.xcodeproj`
in Xcode and build the project as usual.
## Android
### Configure
Create an `android/local.properties` file with these entries:
* `sdk.dir=[path to the Android SDK]`
* `flutter.sdk=[path to the Flutter SDK]`
There are a number of other parameters you can control with this file:
* `flutter.buildMode`: Whether to build for `debug`, `profile`, or `release`.
Defaults to `release`.
* `flutter.jar`: The path to `flutter.jar`. Defaults to the
`android-arm-release` version of `flutter.jar` in the `bin/cache` directory
of the Flutter SDK.
See `android/app/build.gradle` for project specific settings, including:
* `source`: The path to the directory that contains your `pubspec.yaml` file
relative to your `build.gradle` file.
* `target`: The path to your `main.dart` relative to your `pubspec.yaml`.
Defaults to `lib/main.dart`.
### Build
To build directly with `gradle`, use the following commands:
* `cd android`
* `gradle wrapper`
* `./gradlew build`
To build with Android Studio, open the `android` folder in Android Studio and
build the project as usual.
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
/gradle
/gradlew
/gradlew.bat
def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
localPropertiesFile.withInputStream { stream ->
localProperties.load(stream)
}
}
def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}
apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
android {
compileSdkVersion 25
buildToolsVersion '25.0.2'
lintOptions {
disable 'InvalidPackage'
}
defaultConfig {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
// TODO: Add your own signing config for the release build.
// Signing with the debug keys for now, so `flutter run --release` works.
signingConfig signingConfigs.debug
}
}
}
flutter {
source '../..'
}
dependencies {
androidTestCompile 'com.android.support:support-annotations:25.0.0'
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5'
}
package com.example.flutter;
import android.graphics.Bitmap;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import io.flutter.view.FlutterView;
import android.app.Instrumentation;
import android.support.test.InstrumentationRegistry;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Rule
public ActivityTestRule<ExampleActivity> activityRule =
new ActivityTestRule<>(ExampleActivity.class);
@Test
public void testBitmap() {
final Instrumentation instr = InstrumentationRegistry.getInstrumentation();
final BitmapPoller poller = new BitmapPoller(5);
instr.runOnMainSync(new Runnable() {
public void run() {
final FlutterView flutterView = (FlutterView) activityRule.getActivity()
.getFlutterView();
// Call onPostResume to start the engine's renderer even if the activity
// is paused in the test environment.
flutterView.onPostResume();
poller.start(flutterView);
}
});
Bitmap bitmap = null;
try {
bitmap = poller.waitForBitmap();
} catch (InterruptedException e) {
fail(e.getMessage());
}
assertNotNull(bitmap);
assertTrue(bitmap.getWidth() > 0);
assertTrue(bitmap.getHeight() > 0);
// Check that a pixel matches the default Material background color.
assertTrue(bitmap.getPixel(bitmap.getWidth() - 1, bitmap.getHeight() - 1) == 0xFFFAFAFA);
}
// Waits on a FlutterView until it is able to produce a bitmap.
private class BitmapPoller {
private int triesPending;
private int waitMsec;
private FlutterView flutterView;
private Bitmap bitmap;
private CountDownLatch latch = new CountDownLatch(1);
private final int delayMsec = 1000;
BitmapPoller(int tries) {
triesPending = tries;
waitMsec = delayMsec * tries + 100;
}
void start(FlutterView flutterView) {
this.flutterView = flutterView;
flutterView.postDelayed(checkBitmap, delayMsec);
}
Bitmap waitForBitmap() throws InterruptedException {
latch.await(waitMsec, TimeUnit.MILLISECONDS);
return bitmap;
}
private Runnable checkBitmap = new Runnable() {
public void run() {
bitmap = flutterView.getBitmap();
triesPending--;
if (bitmap != null || triesPending == 0) {
latch.countDown();
} else {
flutterView.postDelayed(checkBitmap, delayMsec);
}
}
};
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.flutter"
android:versionCode="1"
android:versionName="1.0.0" >
<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="22" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application android:name="io.flutter.app.FlutterApplication" android:label="@string/app_name" >
<activity
android:name=".ExampleActivity"
android:launchMode="singleTop"
android:theme="@android:style/Theme.Black.NoTitleBar"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
// 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.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
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 io.flutter.app.FlutterActivity;
import io.flutter.view.FlutterMain;
import io.flutter.view.FlutterView;
import java.io.File;
import org.json.JSONException;
import org.json.JSONObject;
public class ExampleActivity extends FlutterActivity {
private static final String TAG = "ExampleActivity";
private FlutterView flutterView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
flutterView = getFlutterView();
flutterView.addOnMessageListener("getLocation",
new FlutterView.OnMessageListener() {
@Override
public String onMessage(FlutterView view, String message) {
return onGetLocation(message);
}
});
}
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;
}
String permission = "android.permission.ACCESS_FINE_LOCATION";
Location location = null;
if (checkCallingOrSelfPermission(permission) == PackageManager.PERMISSION_GRANTED) {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
location = locationManager.getLastKnownLocation(locationProvider);
}
JSONObject reply = new JSONObject();
try {
if (location != null) {
reply.put("latitude", location.getLatitude());
reply.put("longitude", location.getLongitude());
} else {
reply.put("latitude", 0);
reply.put("longitude", 0);
}
} catch (JSONException e) {
Log.e(TAG, "JSON exception", e);
return null;
}
return reply.toString();
}
}
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Platform Services</string>
<string name="title">Flutter Application</string>
</resources>
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
task wrapper(type: Wrapper) {
gradleVersion = '2.14.1'
}
repositories {
jcenter()
}
dependencies {
compile "com.android.tools.build:gradle:2.2.3"
}
.idea/
.vagrant/
.sconsign.dblite
.svn/
.DS_Store
*.swp
*.lock
profile
DerivedData/
build/
*.pbxuser
*.mode1v3
*.mode2v3
*.perspectivev3
!default.pbxuser
!default.mode1v3
!default.mode2v3
!default.perspectivev3
xcuserdata
*.moved-aside
*.pyc
*sync/
Icon?
.tags*
/Flutter/app.flx
/Flutter/app.dylib
/Flutter/app.zip
/Flutter/Flutter.framework
/Flutter/Generated.xcconfig
/ServiceDefinitions.json
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "self:PlatformServices.xcodeproj">
</FileRef>
</Workspace>
// 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 <UIKit/UIKit.h>
#import <Flutter/Flutter.h>
@interface AppDelegate : FlutterAppDelegate
@end
// 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 "AppDelegate.h"
#import <Flutter/Flutter.h>
#import "LocationProvider.h"
@implementation AppDelegate {
LocationProvider* _locationProvider;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
FlutterDartProject* project = [[FlutterDartProject alloc] initFromDefaultSourceForConfiguration];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
FlutterViewController* flutterController = [[FlutterViewController alloc] initWithProject:project
nibName:nil
bundle:nil];
_locationProvider = [[LocationProvider alloc] init];
[flutterController addMessageListener:_locationProvider];
self.window.rootViewController = flutterController;
[self.window makeKeyAndVisible];
return YES;
}
@end
{
"images" : [
{
"idiom" : "iphone",
"size" : "29x29",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "29x29",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "40x40",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "40x40",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "60x60",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "60x60",
"scale" : "3x"
},
{
"idiom" : "ipad",
"size" : "29x29",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "29x29",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "40x40",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "40x40",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "76x76",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "76x76",
"scale" : "2x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10117" systemVersion="15F34" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" initialViewController="01J-lp-oVM">
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="EHf-IW-A2E">
<objects>
<viewController id="01J-lp-oVM" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="Llm-lL-Icb"/>
<viewControllerLayoutGuide type="bottom" id="xb3-aO-Qok"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="Ze5-6b-2t3">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="iYj-Kq-Ea1" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="53" y="375"/>
</scene>
</scenes>
</document>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6211" systemVersion="14A298i" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6204"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="tne-QT-ifu">
<objects>
<viewController id="BYZ-38-t0r" customClass="ViewController" customModuleProvider="" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="y3c-jy-aDJ"/>
<viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
</objects>
</scene>
</scenes>
</document>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>PlatformServices</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
<string>Main</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>armv7</string>
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
</dict>
</plist>
// 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 <Flutter/Flutter.h>
@interface LocationProvider : NSObject <FlutterMessageListener>
@end
// 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 "LocationProvider.h"
#import <CoreLocation/CoreLocation.h>
@implementation LocationProvider {
CLLocationManager* _locationManager;
}
@synthesize messageName = _messageName;
- (instancetype) init {
self = [super init];
if (self)
self->_messageName = @"getLocation";
return self;
}
- (NSString*)didReceiveString:(NSString*)message {
if (_locationManager == nil) {
_locationManager = [[CLLocationManager alloc] init];
[_locationManager startMonitoringSignificantLocationChanges];
}
CLLocation* location = _locationManager.location;
NSDictionary* response = @{
@"latitude": @(location.coordinate.latitude),
@"longitude": @(location.coordinate.longitude),
};
NSData* data = [NSJSONSerialization dataWithJSONObject:response options:0 error:nil];
return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
@end
// 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 <UIKit/UIKit.h>
#import <Flutter/Flutter.h>
#import "AppDelegate.h"
int main(int argc, char * argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv, nil,
NSStringFromClass([AppDelegate class]));
}
}
// 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:flutter/material.dart';
import 'package:flutter/services.dart';
class PlatformServices extends StatefulWidget {
@override
_PlatformServicesState createState() => new _PlatformServicesState();
}
class _PlatformServicesState extends State<PlatformServices> {
double _latitude;
double _longitude;
@override
Widget build(BuildContext context) {
return new Material(
child: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
new Text('Hello from Flutter!'),
new RaisedButton(
child: new Text('Get Location'),
onPressed: _getLocation
),
new Text('Latitude: $_latitude, Longitude: $_longitude'),
]
)
)
);
}
Future<Null> _getLocation() async {
final Map<String, String> message = <String, String>{'provider': 'network'};
final Map<String, dynamic> reply = await PlatformMessages.sendJSON('getLocation', message);
// If the widget was removed from the tree while the message was in flight,
// we want to discard the reply rather than calling setState to update our
// non-existent appearance.
if (!mounted)
return;
setState(() {
_latitude = reply['latitude'].toDouble();
_longitude = reply['longitude'].toDouble();
});
}
}
void main() {
runApp(new PlatformServices());
}
name: platform_services
dependencies:
flutter:
sdk: flutter
dev_dependencies:
flutter_test:
sdk: flutter
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