From 63b9f56c55fde4314d4549029a1c272c2ce97bd1 Mon Sep 17 00:00:00 2001 From: Sarah Zakarias Date: Mon, 6 Mar 2017 11:52:31 +0100 Subject: [PATCH] Update platform_services to query battery level instead of location. (#8554) --- examples/platform_services/README.md | 61 ++--------------- .../com/example/flutter/ExampleActivity.java | 67 +++++++------------ .../ios/Runner.xcodeproj/project.pbxproj | 6 -- .../ios/Runner/AppDelegate.m | 12 ---- .../ios/Runner/Base.lproj/Main.storyboard | 19 ++++-- .../ios/Runner/LocationProvider.h | 9 --- .../ios/Runner/LocationProvider.m | 39 ----------- examples/platform_services/lib/main.dart | 38 ++++++----- 8 files changed, 64 insertions(+), 187 deletions(-) delete mode 100644 examples/platform_services/ios/Runner/LocationProvider.h delete mode 100644 examples/platform_services/ios/Runner/LocationProvider.m diff --git a/examples/platform_services/README.md b/examples/platform_services/README.md index 38ed07ae798..6c3d5ee4d72 100644 --- a/examples/platform_services/README.md +++ b/examples/platform_services/README.md @@ -6,61 +6,12 @@ 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. +You can use the commands `flutter build` and `flutter run` from the app's root +directory to build/run the app or you can open `ios/Runner.xcworkspace` in Xcode +and build/run 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. +You can use the commands `flutter build` and `flutter run` from the app's root +directory to build/run the app or to build with Android Studio, open the +`android` folder in Android Studio and build the project as usual. diff --git a/examples/platform_services/android/app/src/main/java/com/example/flutter/ExampleActivity.java b/examples/platform_services/android/app/src/main/java/com/example/flutter/ExampleActivity.java index 45cc796ee78..c223da80219 100644 --- a/examples/platform_services/android/app/src/main/java/com/example/flutter/ExampleActivity.java +++ b/examples/platform_services/android/app/src/main/java/com/example/flutter/ExampleActivity.java @@ -4,10 +4,9 @@ package com.example.flutter; -import android.content.Context; -import android.content.pm.PackageManager; -import android.location.Location; -import android.location.LocationManager; +import android.os.BatteryManager; +import android.os.Build.VERSION; +import android.os.Build.VERSION_CODES; import android.os.Bundle; import io.flutter.app.FlutterActivity; @@ -15,49 +14,33 @@ import io.flutter.plugin.common.FlutterMethodChannel; import io.flutter.plugin.common.FlutterMethodChannel.MethodCallHandler; import io.flutter.plugin.common.FlutterMethodChannel.Response; import io.flutter.plugin.common.MethodCall; -import io.flutter.view.FlutterView; public class ExampleActivity extends FlutterActivity { - private FlutterView flutterView; + private static final String CHANNEL = "battery"; - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - new FlutterMethodChannel(getFlutterView(), "geo").setMethodCallHandler(new MethodCallHandler() { - @Override - public void onMethodCall(MethodCall call, Response response) { - if (call.method.equals("getLocation")) { - if (!(call.arguments instanceof String)) { - throw new IllegalArgumentException("Invalid argument type, String expected"); - } - getLocation((String) call.arguments, response); - } else { - throw new IllegalArgumentException("Unknown method " + call.method); - } - } - }); - } + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); - private void getLocation(String provider, Response response) { - String locationProvider; - if (provider.equals("network")) { - locationProvider = LocationManager.NETWORK_PROVIDER; - } else if (provider.equals("gps")) { - locationProvider = LocationManager.GPS_PROVIDER; - } else { - throw new IllegalArgumentException("Unknown provider " + provider); - } - String permission = "android.permission.ACCESS_FINE_LOCATION"; - if (checkCallingOrSelfPermission(permission) == PackageManager.PERMISSION_GRANTED) { - LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); - Location location = locationManager.getLastKnownLocation(locationProvider); - if (location != null) { - response.success(new double[] { location.getLatitude(), location.getLongitude() }); + new FlutterMethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler( + new MethodCallHandler() { + @Override + public void onMethodCall(MethodCall call, Response response) { + if (call.method.equals("getBatteryLevel")) { + getBatteryLevel(response); } else { - response.error("unknown", "Location unknown", null); + throw new IllegalArgumentException("Unknown method " + call.method); } - } else { - response.error("permission", "Access denied", null); - } + } + }); + } + + private void getBatteryLevel(Response response) { + BatteryManager batteryManager = (BatteryManager) getSystemService(BATTERY_SERVICE); + if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { + response.success(batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY)); + } else { + response.error("Not available", "Battery level not available.", null); } + } } diff --git a/examples/platform_services/ios/Runner.xcodeproj/project.pbxproj b/examples/platform_services/ios/Runner.xcodeproj/project.pbxproj index 9c7318c7e6c..8248e74f8d9 100644 --- a/examples/platform_services/ios/Runner.xcodeproj/project.pbxproj +++ b/examples/platform_services/ios/Runner.xcodeproj/project.pbxproj @@ -13,7 +13,6 @@ 9740EEB41CF90195004384FC /* Flutter.xcconfig in Resources */ = {isa = PBXBuildFile; fileRef = 9740EEB21CF90195004384FC /* Flutter.xcconfig */; }; 9740EEB51CF90195004384FC /* Generated.xcconfig in Resources */ = {isa = PBXBuildFile; fileRef = 9740EEB31CF90195004384FC /* Generated.xcconfig */; }; 9740EEBB1CF902C7004384FC /* app.flx in Resources */ = {isa = PBXBuildFile; fileRef = 9740EEB71CF902C7004384FC /* app.flx */; }; - 977505191CFDF23500BC28DA /* LocationProvider.m in Sources */ = {isa = PBXBuildFile; fileRef = 977505181CFDF23500BC28DA /* LocationProvider.m */; }; 97A38A351CFDEC880099F1B4 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 97A38A341CFDEC880099F1B4 /* AppDelegate.m */; }; 97C146F31CF9000F007C117D /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 97C146F21CF9000F007C117D /* main.m */; }; 97C146FC1CF9000F007C117D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FA1CF9000F007C117D /* Main.storyboard */; }; @@ -42,8 +41,6 @@ 9740EEB71CF902C7004384FC /* app.flx */ = {isa = PBXFileReference; lastKnownFileType = file; name = app.flx; path = Flutter/app.flx; sourceTree = ""; }; 9740EEB81CF902C7004384FC /* app.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = app.dylib; path = Flutter/app.dylib; sourceTree = ""; }; 9740EEBA1CF902C7004384FC /* Flutter.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Flutter.framework; path = Flutter/Flutter.framework; sourceTree = ""; }; - 977505171CFDF21E00BC28DA /* LocationProvider.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = LocationProvider.h; sourceTree = ""; }; - 977505181CFDF23500BC28DA /* LocationProvider.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LocationProvider.m; sourceTree = ""; }; 97A38A331CFDEC680099F1B4 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 97A38A341CFDEC880099F1B4 /* AppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 97C146EE1CF9000F007C117D /* Runner.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Runner.app; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -105,8 +102,6 @@ 97C146F11CF9000F007C117D /* Supporting Files */, 97A38A331CFDEC680099F1B4 /* AppDelegate.h */, 97A38A341CFDEC880099F1B4 /* AppDelegate.m */, - 977505171CFDF21E00BC28DA /* LocationProvider.h */, - 977505181CFDF23500BC28DA /* LocationProvider.m */, ); path = Runner; sourceTree = ""; @@ -212,7 +207,6 @@ files = ( 97A38A351CFDEC880099F1B4 /* AppDelegate.m in Sources */, 97C146F31CF9000F007C117D /* main.m in Sources */, - 977505191CFDF23500BC28DA /* LocationProvider.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/examples/platform_services/ios/Runner/AppDelegate.m b/examples/platform_services/ios/Runner/AppDelegate.m index bf8e1915c5f..1904dd06e03 100644 --- a/examples/platform_services/ios/Runner/AppDelegate.m +++ b/examples/platform_services/ios/Runner/AppDelegate.m @@ -5,23 +5,11 @@ #import "AppDelegate.h" #import -#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; } diff --git a/examples/platform_services/ios/Runner/Base.lproj/Main.storyboard b/examples/platform_services/ios/Runner/Base.lproj/Main.storyboard index f56d2f3bb56..9962b2e4357 100644 --- a/examples/platform_services/ios/Runner/Base.lproj/Main.storyboard +++ b/examples/platform_services/ios/Runner/Base.lproj/Main.storyboard @@ -1,21 +1,26 @@ - - + + + + + - + + + - + - + - + - + diff --git a/examples/platform_services/ios/Runner/LocationProvider.h b/examples/platform_services/ios/Runner/LocationProvider.h deleted file mode 100644 index e646fdf4fc5..00000000000 --- a/examples/platform_services/ios/Runner/LocationProvider.h +++ /dev/null @@ -1,9 +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. - -#import - -@interface LocationProvider : NSObject - -@end diff --git a/examples/platform_services/ios/Runner/LocationProvider.m b/examples/platform_services/ios/Runner/LocationProvider.m deleted file mode 100644 index e6e0cc7b628..00000000000 --- a/examples/platform_services/ios/Runner/LocationProvider.m +++ /dev/null @@ -1,39 +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. - -#import "LocationProvider.h" - -#import - -@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 diff --git a/examples/platform_services/lib/main.dart b/examples/platform_services/lib/main.dart index 22a397ab907..f1a7ab032b7 100644 --- a/examples/platform_services/lib/main.dart +++ b/examples/platform_services/lib/main.dart @@ -3,6 +3,7 @@ // found in the LICENSE file. import 'dart:async'; +import 'dart:io'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; @@ -13,20 +14,23 @@ class PlatformServices extends StatefulWidget { } class _PlatformServicesState extends State { - static const PlatformMethodChannel platform = const PlatformMethodChannel('geo'); - String _location = 'Unknown location.'; + static const PlatformMethodChannel platform = const PlatformMethodChannel('battery'); + String _batteryLevel = 'Unknown battery level.'; - Future _getLocation() async { - String location; - try { - final List result = await platform.invokeMethod('getLocation', 'network'); - location = 'Latitude ${result[0]}, Longitude ${result[1]}.'; - } on PlatformException catch (e) { - location = "Failed to get location: '${e.message}'."; + Future _getBatteryLevel() async { + String batteryLevel; + if (Platform.isIOS) { + batteryLevel = "iOS is not supported yet."; + } else { + try { + final int result = await platform.invokeMethod('getBatteryLevel'); + batteryLevel = 'Battery level at $result. %'; + } on PlatformException catch (e) { + batteryLevel = "Failed to get battery level: '${e.message}'."; + } } - setState(() { - _location = location; + _batteryLevel = batteryLevel; }); } @@ -38,17 +42,17 @@ class _PlatformServicesState extends State { mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ new RaisedButton( - child: new Text('Get Location'), - onPressed: _getLocation, + child: new Text('Get Battery Level'), + onPressed: _getBatteryLevel, ), - new Text(_location) + new Text(_batteryLevel) ], - ), - ), + ) + ) ); } } - + void main() { runApp(new PlatformServices()); }