Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[battery] Support the v2 Android embedder.
  • Loading branch information
amirh committed Oct 4, 2019
commit 0e5d91e462dcb3e78e93766fea722e7c1e555199
4 changes: 4 additions & 0 deletions packages/battery/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.3.0+6

* Support the v2 Android embedder.

## 0.3.0+5

* Fix Gradle version.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import android.os.BatteryManager;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.EventChannel.EventSink;
import io.flutter.plugin.common.EventChannel.StreamHandler;
Expand All @@ -22,25 +24,42 @@
import io.flutter.plugin.common.PluginRegistry;

/** BatteryPlugin */
public class BatteryPlugin implements MethodCallHandler, StreamHandler {
public class BatteryPlugin implements MethodCallHandler, StreamHandler, FlutterPlugin {

private Context applicationContext;
private BroadcastReceiver chargingStateChangeReceiver;
private MethodChannel methodChannel;
private EventChannel eventChannel;

/** Plugin registration. */
public static void registerWith(PluginRegistry.Registrar registrar) {
final MethodChannel methodChannel =
new MethodChannel(registrar.messenger(), "plugins.flutter.io/battery");
final EventChannel eventChannel =
new EventChannel(registrar.messenger(), "plugins.flutter.io/charging");
final BatteryPlugin instance = new BatteryPlugin(registrar);
eventChannel.setStreamHandler(instance);
methodChannel.setMethodCallHandler(instance);
final BatteryPlugin instance = new BatteryPlugin();
instance.onAttachedToEngine(registrar.context(), registrar.messenger());
}

BatteryPlugin(PluginRegistry.Registrar registrar) {
this.registrar = registrar;
@Override
public void onAttachedToEngine(FlutterPluginBinding binding) {
applicationContext = binding.getApplicationContext();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is redundant with this.applicationContext = applicationContext; in the other onAttachedToEngine

onAttachedToEngine(
binding.getApplicationContext(), binding.getFlutterEngine().getDartExecutor());
}

private final PluginRegistry.Registrar registrar;
private BroadcastReceiver chargingStateChangeReceiver;
private void onAttachedToEngine(Context applicationContext, BinaryMessenger messenger) {
this.applicationContext = applicationContext;
methodChannel = new MethodChannel(messenger, "plugins.flutter.io/battery");
eventChannel = new EventChannel(messenger, "plugins.flutter.io/charging");
eventChannel.setStreamHandler(this);
methodChannel.setMethodCallHandler(this);
}

@Override
public void onDetachedFromEngine(FlutterPluginBinding binding) {
applicationContext = null;
methodChannel.setMethodCallHandler(null);
methodChannel = null;
eventChannel.setStreamHandler(null);
eventChannel = null;
}

@Override
public void onMethodCall(MethodCall call, Result result) {
Expand All @@ -60,28 +79,25 @@ public void onMethodCall(MethodCall call, Result result) {
@Override
public void onListen(Object arguments, EventSink events) {
chargingStateChangeReceiver = createChargingStateChangeReceiver(events);
registrar
.context()
.registerReceiver(
chargingStateChangeReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
applicationContext.registerReceiver(
chargingStateChangeReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}

@Override
public void onCancel(Object arguments) {
registrar.context().unregisterReceiver(chargingStateChangeReceiver);
applicationContext.unregisterReceiver(chargingStateChangeReceiver);
chargingStateChangeReceiver = null;
}

private int getBatteryLevel() {
int batteryLevel = -1;
Context context = registrar.context();
if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
BatteryManager batteryManager =
(BatteryManager) context.getSystemService(context.BATTERY_SERVICE);
(BatteryManager) applicationContext.getSystemService(applicationContext.BATTERY_SERVICE);
batteryLevel = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
} else {
Intent intent =
new ContextWrapper(context)
new ContextWrapper(applicationContext)
.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
batteryLevel =
(intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) * 100)
Expand Down
11 changes: 11 additions & 0 deletions packages/battery/example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@
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>
<activity
android:name=".EmbedderV2Activity"
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"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.flutter.plugins.batteryexample;

import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugins.battery.BatteryPlugin;

public class EmbedderV2Activity extends FlutterActivity {
@Override
public void configureFlutterEngine(FlutterEngine flutterEngine) {
flutterEngine.getPlugins().add(new BatteryPlugin());
}
}
2 changes: 1 addition & 1 deletion packages/battery/example/android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
org.gradle.jvmargs=-Xmx1536M
android.enableR8=true
android.useAndroidX=true
android.enableJetifier=true
2 changes: 1 addition & 1 deletion packages/battery/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Flutter plugin for accessing information about the battery state
(full, charging, discharging) on Android and iOS.
author: Flutter Team <[email protected]>
homepage: https://github.com/flutter/plugins/tree/master/packages/battery
version: 0.3.0+5
version: 0.3.0+6

flutter:
plugin:
Expand Down