From be53a6c129b3b4292b6cf055e5a8d4943203b319 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Thu, 24 May 2018 14:42:36 +0200 Subject: [PATCH] Android: Add a test MethodCall This is just to test if the Android bridge is working correctly. --- .../com/example/journal/MainActivity.java | 50 +++++++++++++++++++ android/build.gradle | 2 +- .../gradle/wrapper/gradle-wrapper.properties | 4 +- 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/android/app/src/main/java/com/example/journal/MainActivity.java b/android/app/src/main/java/com/example/journal/MainActivity.java index 87eea9bb..badd522d 100644 --- a/android/app/src/main/java/com/example/journal/MainActivity.java +++ b/android/app/src/main/java/com/example/journal/MainActivity.java @@ -5,10 +5,60 @@ import android.os.Bundle; import io.flutter.app.FlutterActivity; import io.flutter.plugins.GeneratedPluginRegistrant; +// For MethodChannel +import io.flutter.plugin.common.MethodCall; +import io.flutter.plugin.common.MethodChannel; +import io.flutter.plugin.common.MethodChannel.MethodCallHandler; +import io.flutter.plugin.common.MethodChannel.Result; + +// For battery stuf +import android.content.ContextWrapper; +import android.content.Intent; +import android.content.IntentFilter; +import android.os.BatteryManager; +import android.os.Build.VERSION; +import android.os.Build.VERSION_CODES; +import android.os.Bundle; + public class MainActivity extends FlutterActivity { + private static final String CHANNEL = "samples.flutter.io/battery"; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); GeneratedPluginRegistrant.registerWith(this); + + new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler( + new MethodCallHandler() { + @Override + public void onMethodCall(MethodCall call, Result result) { + if (call.method.equals("getBatteryLevel")) { + int batteryLevel = getBatteryLevel(); + + if (batteryLevel != -1) { + result.success(batteryLevel); + } else { + result.error("UNAVAILABLE", "Battery level not available.", null); + } + } else { + result.notImplemented(); + } + } + }); + } + + private int getBatteryLevel() { + int batteryLevel = -1; + if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { + BatteryManager batteryManager = (BatteryManager) getSystemService(BATTERY_SERVICE); + batteryLevel = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY); + } else { + Intent intent = new ContextWrapper(getApplicationContext()). + registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); + batteryLevel = (intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) * 100) / + intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); + } + + return batteryLevel; } } diff --git a/android/build.gradle b/android/build.gradle index 44768875..d4225c79 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -5,7 +5,7 @@ buildscript { } dependencies { - classpath 'com.android.tools.build:gradle:3.0.1' + classpath 'com.android.tools.build:gradle:3.1.2' } } diff --git a/android/gradle/wrapper/gradle-wrapper.properties b/android/gradle/wrapper/gradle-wrapper.properties index aa901e1e..5b6d37d3 100644 --- a/android/gradle/wrapper/gradle-wrapper.properties +++ b/android/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Fri Jun 23 08:50:38 CEST 2017 +#Thu May 24 14:30:51 CEST 2018 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip