Update platform_services to query battery level instead of location. (#8554)
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user