Extract BroadcastReceivers to member variables, so they can be unregistered correctly.

Previously, they were registered, then forgotten. This means that each time
the start swap view was run, another receiver was registered. As a result,
they were being invoked multiple times.

It doesn't appear that this had any specific side effects which were terrible,
but they definitely have the potential to going forward.

Note that because we are not using `Fragments` with their convoluted, but at
least well documented API, I'm not 100% certain that I've unregistered the
receivers at the right location.
This commit is contained in:
Peter Serwylo 2016-02-28 13:57:46 +11:00
parent 1323e800b7
commit ad63e52e50

View File

@ -104,6 +104,7 @@ public class StartSwapView extends ScrollView implements SwapWorkflowActivity.In
private SwitchCompat wifiSwitch; private SwitchCompat wifiSwitch;
private SwitchCompat bluetoothSwitch; private SwitchCompat bluetoothSwitch;
private TextView textWifiVisible;
private TextView viewBluetoothId; private TextView viewBluetoothId;
private TextView textBluetoothVisible; private TextView textBluetoothVisible;
private TextView viewWifiId; private TextView viewWifiId;
@ -160,6 +161,10 @@ public class StartSwapView extends ScrollView implements SwapWorkflowActivity.In
if (bluetoothSwitch != null) { if (bluetoothSwitch != null) {
bluetoothSwitch.setOnCheckedChangeListener(null); bluetoothSwitch.setOnCheckedChangeListener(null);
} }
LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(onWifiSwapStateChanged);
LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(onBluetoothSwapStateChanged);
LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(onWifiNetworkChanged);
} }
@Override @Override
@ -176,17 +181,16 @@ public class StartSwapView extends ScrollView implements SwapWorkflowActivity.In
uiInitButtons(); uiInitButtons();
uiShowSearchingForPeers(); uiShowSearchingForPeers();
// TODO: Unregister this receiver at some point.
LocalBroadcastManager.getInstance(getActivity()).registerReceiver( LocalBroadcastManager.getInstance(getActivity()).registerReceiver(
new BroadcastReceiver() { onWifiNetworkChanged, new IntentFilter(WifiStateChangeService.BROADCAST));
}
private BroadcastReceiver onWifiNetworkChanged = new BroadcastReceiver() {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
uiUpdateWifiNetwork(); uiUpdateWifiNetwork();
} }
}, };
new IntentFilter(WifiStateChangeService.BROADCAST)
);
}
private void uiInitButtons() { private void uiInitButtons() {
findViewById(R.id.btn_send_fdroid).setOnClickListener(new OnClickListener() { findViewById(R.id.btn_send_fdroid).setOnClickListener(new OnClickListener() {
@ -257,8 +261,17 @@ public class StartSwapView extends ScrollView implements SwapWorkflowActivity.In
bluetoothSwitch.setOnCheckedChangeListener(onBluetoothSwitchToggled); bluetoothSwitch.setOnCheckedChangeListener(onBluetoothSwitchToggled);
setBluetoothSwitchState(getManager().isBluetoothDiscoverable(), true); setBluetoothSwitchState(getManager().isBluetoothDiscoverable(), true);
// TODO: Unregister receiver correctly... LocalBroadcastManager.getInstance(getContext()).registerReceiver(onBluetoothSwapStateChanged, new IntentFilter(SwapService.BLUETOOTH_STATE_CHANGE));
LocalBroadcastManager.getInstance(getContext()).registerReceiver(new BroadcastReceiver() {
} else {
findViewById(R.id.bluetooth_info).setVisibility(View.GONE);
}
}
/**
* @see StartSwapView#onWifiSwapStateChanged
*/
private BroadcastReceiver onBluetoothSwapStateChanged = new BroadcastReceiver() {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
if (intent.hasExtra(SwapService.EXTRA_STARTING)) { if (intent.hasExtra(SwapService.EXTRA_STARTING)) {
@ -279,12 +292,7 @@ public class StartSwapView extends ScrollView implements SwapWorkflowActivity.In
} }
} }
} }
}, new IntentFilter(SwapService.BLUETOOTH_STATE_CHANGE)); };
} else {
findViewById(R.id.bluetooth_info).setVisibility(View.GONE);
}
}
/** /**
* @see StartSwapView#setWifiSwitchState(boolean, boolean) * @see StartSwapView#setWifiSwitchState(boolean, boolean)
@ -328,7 +336,7 @@ public class StartSwapView extends ScrollView implements SwapWorkflowActivity.In
wifiSwitch.setOnCheckedChangeListener(onWifiSwitchToggled); wifiSwitch.setOnCheckedChangeListener(onWifiSwitchToggled);
setWifiSwitchState(getManager().isBonjourDiscoverable(), true); setWifiSwitchState(getManager().isBonjourDiscoverable(), true);
final TextView textWifiVisible = (TextView) findViewById(R.id.wifi_visible); textWifiVisible = (TextView) findViewById(R.id.wifi_visible);
int textResource = getManager().isBonjourDiscoverable() ? R.string.swap_visible_wifi : R.string.swap_not_visible_wifi; int textResource = getManager().isBonjourDiscoverable() ? R.string.swap_visible_wifi : R.string.swap_not_visible_wifi;
textWifiVisible.setText(textResource); textWifiVisible.setText(textResource);
@ -336,8 +344,25 @@ public class StartSwapView extends ScrollView implements SwapWorkflowActivity.In
// and the Bonjour service at the same time. Technically swap will work fine without // and the Bonjour service at the same time. Technically swap will work fine without
// Bonjour, and that is more of a convenience. Thus, we should show feedback once wifi // Bonjour, and that is more of a convenience. Thus, we should show feedback once wifi
// is ready, even if Bonjour is not yet. // is ready, even if Bonjour is not yet.
// TODO: Unregister receiver correctly... LocalBroadcastManager.getInstance(getContext()).registerReceiver(onWifiSwapStateChanged, new IntentFilter(SwapService.WIFI_STATE_CHANGE));
LocalBroadcastManager.getInstance(getContext()).registerReceiver(new BroadcastReceiver() {
viewWifiNetwork.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
getActivity().promptToSelectWifiNetwork();
}
});
uiUpdateWifiNetwork();
}
/**
* When the WiFi swap service is started or stopped, update the UI appropriately.
* This includes both the in-transit states of "Starting" and "Stopping". In these two cases,
* the UI should be disabled to prevent the user quickly switching back and forth - causing
* multiple start/stop actions to be sent to the swap service.
*/
private final BroadcastReceiver onWifiSwapStateChanged = new BroadcastReceiver() {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
if (intent.hasExtra(SwapService.EXTRA_STARTING)) { if (intent.hasExtra(SwapService.EXTRA_STARTING)) {
@ -361,17 +386,7 @@ public class StartSwapView extends ScrollView implements SwapWorkflowActivity.In
} }
uiUpdateWifiNetwork(); uiUpdateWifiNetwork();
} }
}, new IntentFilter(SwapService.WIFI_STATE_CHANGE)); };
viewWifiNetwork.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
getActivity().promptToSelectWifiNetwork();
}
});
uiUpdateWifiNetwork();
}
/** /**
* Helper function to set the "enable wifi" switch, but prevents the listeners from * Helper function to set the "enable wifi" switch, but prevents the listeners from