From d46efb1d8414d9331f166d7147255752bf35bb2d Mon Sep 17 00:00:00 2001 From: Peter Serwylo Date: Mon, 21 Mar 2016 22:26:58 +1100 Subject: [PATCH] Correctly start and stop listening for repo update events during swap. This fixes the following bugs: * `BroadcastReceiver` was never being created due to incorrect guard condition `if (pollForUpdatesReceiver != null)` (should have been `== null`). * Called `unregisterReceiver` rather than `registerReceiver`. * Even if it did work, it didn't make an effort to unregister the receiver. In addition, the creation and listening with the `BroadcastReceiver is now done in a way similar to the other swap views: * Create it as a `final` member variable. * `registerReceiver` when view is inflated. * `unregisterReceiver` when view is detached. --- .../fdroid/views/swap/SwapAppsView.java | 72 ++++++++++--------- 1 file changed, 40 insertions(+), 32 deletions(-) diff --git a/F-Droid/src/org/fdroid/fdroid/views/swap/SwapAppsView.java b/F-Droid/src/org/fdroid/fdroid/views/swap/SwapAppsView.java index 6d955039f..399c8621e 100644 --- a/F-Droid/src/org/fdroid/fdroid/views/swap/SwapAppsView.java +++ b/F-Droid/src/org/fdroid/fdroid/views/swap/SwapAppsView.java @@ -118,10 +118,22 @@ public class SwapAppsView extends ListView implements displayImageOptions = Utils.getImageLoadingOptions().build(); + LocalBroadcastManager.getInstance(getActivity()).registerReceiver( + pollForUpdatesReceiver, new IntentFilter(UpdateService.LOCAL_ACTION_STATUS)); + schedulePollForUpdates(); } - private BroadcastReceiver pollForUpdatesReceiver; + /** + * Remove relevant listeners/receivers/etc so that they do not receive and process events + * when this view is not in use. + */ + @Override + public void onDetachedFromWindow() { + super.onDetachedFromWindow(); + + LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(pollForUpdatesReceiver); + } private void pollForUpdates() { if (adapter.getCount() > 1 || @@ -132,37 +144,6 @@ public class SwapAppsView extends ListView implements Utils.debugLog(TAG, "Polling swap repo to see if it has any updates."); getActivity().getService().refreshSwap(); - if (pollForUpdatesReceiver != null) { - pollForUpdatesReceiver = new BroadcastReceiver() { - @Override - public void onReceive(Context context, Intent intent) { - int statusCode = intent.getIntExtra(UpdateService.EXTRA_STATUS_CODE, -1); - switch (statusCode) { - case UpdateService.STATUS_COMPLETE_WITH_CHANGES: - Utils.debugLog(TAG, "Swap repo has updates, notifying the list adapter."); - getActivity().runOnUiThread(new Runnable() { - @Override - public void run() { - adapter.notifyDataSetChanged(); - } - }); - LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(pollForUpdatesReceiver); - break; - - case UpdateService.STATUS_ERROR_GLOBAL: - // TODO: Well, if we can't get the index, we probably can't swapp apps. - // Tell the user somethign helpful? - break; - - case UpdateService.STATUS_COMPLETE_AND_SAME: - schedulePollForUpdates(); - break; - } - } - }; - // TODO: Unregister this properly, not just when successful (see swithc statement above) - LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(pollForUpdatesReceiver); - } } private void schedulePollForUpdates() { @@ -486,4 +467,31 @@ public class SwapAppsView extends ListView implements } } + private final BroadcastReceiver pollForUpdatesReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + int statusCode = intent.getIntExtra(UpdateService.EXTRA_STATUS_CODE, -1); + switch (statusCode) { + case UpdateService.STATUS_COMPLETE_WITH_CHANGES: + Utils.debugLog(TAG, "Swap repo has updates, notifying the list adapter."); + getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + adapter.notifyDataSetChanged(); + } + }); + break; + + case UpdateService.STATUS_ERROR_GLOBAL: + // TODO: Well, if we can't get the index, we probably can't swapp apps. + // Tell the user something helpful? + break; + + case UpdateService.STATUS_COMPLETE_AND_SAME: + schedulePollForUpdates(); + break; + } + } + }; + }