Skip to content

Commit

Permalink
Fix leaking broadcast receiver bug
Browse files Browse the repository at this point in the history
  • Loading branch information
AgainPsychoX committed Jun 28, 2019
1 parent bf938db commit 87f188b
Showing 1 changed file with 23 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Map;
import java.net.NetworkInterface;

import io.flutter.view.FlutterNativeView;
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.EventChannel.StreamHandler;
import io.flutter.plugin.common.EventChannel.EventSink;
Expand All @@ -35,9 +36,10 @@
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;
import io.flutter.plugin.common.PluginRegistry.ViewDestroyListener;
import io.flutter.plugin.common.PluginRegistry.RequestPermissionsResultListener;

public class FlutterBluetoothSerialPlugin implements MethodCallHandler, RequestPermissionsResultListener {
public class FlutterBluetoothSerialPlugin implements MethodCallHandler, RequestPermissionsResultListener, ViewDestroyListener {
// Plugin
private static final String TAG = "FlutterBluePlugin";
private static final String PLUGIN_NAMESPACE = "flutter_bluetooth_serial";
Expand Down Expand Up @@ -74,19 +76,18 @@ public class FlutterBluetoothSerialPlugin implements MethodCallHandler, RequestP
/// Registers plugin in Flutter plugin system
public static void registerWith(Registrar registrar) {
final FlutterBluetoothSerialPlugin instance = new FlutterBluetoothSerialPlugin(registrar);

MethodChannel methodChannel = new MethodChannel(registrar.messenger(), PLUGIN_NAMESPACE + "/methods");
methodChannel.setMethodCallHandler(instance);

registrar.addRequestPermissionsResultListener(instance);
registrar.addViewDestroyListener(instance);
}

/// Constructs the plugin instance
FlutterBluetoothSerialPlugin(Registrar registrar) {
// Plugin
{
this.registrar = registrar;

MethodChannel methodChannel = new MethodChannel(registrar.messenger(), PLUGIN_NAMESPACE + "/methods");
methodChannel.setMethodCallHandler(this);
}

this.registrar = registrar;

// General Bluetooth
{
this.bluetoothManager = (BluetoothManager) registrar.activity().getSystemService(Context.BLUETOOTH_SERVICE);
Expand Down Expand Up @@ -128,7 +129,6 @@ public void onReceive(Context context, Intent intent) {
public void onListen(Object o, EventSink eventSink) {
stateSink = eventSink;

// @TODO . leak :C
registrar.activeContext().registerReceiver(stateReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
}
@Override
Expand Down Expand Up @@ -660,6 +660,19 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
}
}

@Override
public boolean onViewDestroy(FlutterNativeView view) {
// Unregister all ongoing receivers
try {
registrar.activeContext().unregisterReceiver(stateReceiver);
}
catch (IllegalArgumentException ex) {
// Ignore `Receiver not registered` exception
}

return false;
}



/// Helper function to get string out of exception
Expand Down

0 comments on commit 87f188b

Please sign in to comment.