register event receivers for SwapAppsView when Apk is available

This was crashing when coming to SwapAppsView because some of the flow
changed related to the new DownloaderService and InstallManagerService.

Also, this lazy loading is a tiny optimization that we cannot afford right
now, there are far too many lifecycle bugs with swap.
This commit is contained in:
Hans-Christoph Steiner 2016-05-12 16:29:18 +02:00
parent cad7a9687d
commit 768b3d7688

View File

@ -234,9 +234,6 @@ public class SwapAppsView extends ListView implements
private final LocalBroadcastManager localBroadcastManager; private final LocalBroadcastManager localBroadcastManager;
private App app; private App app;
@Nullable
private Apk apkToInstall;
ProgressBar progressView; ProgressBar progressView;
TextView nameView; TextView nameView;
ImageView iconView; ImageView iconView;
@ -289,33 +286,32 @@ public class SwapAppsView extends ListView implements
Activity activity = getActivity(); Activity activity = getActivity();
if (activity != null) { if (activity != null) {
app = AppProvider.Helper.findByPackageName(getActivity().getContentResolver(), app.packageName); app = AppProvider.Helper.findByPackageName(getActivity().getContentResolver(), app.packageName);
apkToInstall = null; // Force lazy loading to fetch correct apk next time.
resetView(); resetView();
} }
} }
}; };
ViewHolder() { ViewHolder() {
// TODO: Unregister receivers correctly... localBroadcastManager = LocalBroadcastManager.getInstance(getContext());
Apk apk = getApkToInstall();
String url = apk.getUrl();
localBroadcastManager = LocalBroadcastManager.getInstance(getActivity());
localBroadcastManager.registerReceiver(appListViewResetReceiver,
DownloaderService.getIntentFilter(url, Downloader.ACTION_STARTED));
localBroadcastManager.registerReceiver(downloadProgressReceiver,
DownloaderService.getIntentFilter(url, Downloader.ACTION_PROGRESS));
localBroadcastManager.registerReceiver(appListViewResetReceiver,
DownloaderService.getIntentFilter(url, Downloader.ACTION_COMPLETE));
localBroadcastManager.registerReceiver(interruptedReceiver,
DownloaderService.getIntentFilter(url, Downloader.ACTION_INTERRUPTED));
} }
public void setApp(@NonNull App app) { public void setApp(@NonNull App app) {
if (this.app == null || !this.app.packageName.equals(app.packageName)) { if (this.app == null || !this.app.packageName.equals(app.packageName)) {
this.app = app; this.app = app;
apkToInstall = null; // Force lazy loading to fetch the correct apk next time.
Context context = getContext();
Apk apk = ApkProvider.Helper.find(context, app.packageName, app.suggestedVersionCode);
String urlString = apk.getUrl();
// TODO unregister receivers? or will they just die with this instance
localBroadcastManager.registerReceiver(appListViewResetReceiver,
DownloaderService.getIntentFilter(urlString, Downloader.ACTION_STARTED));
localBroadcastManager.registerReceiver(downloadProgressReceiver,
DownloaderService.getIntentFilter(urlString, Downloader.ACTION_PROGRESS));
localBroadcastManager.registerReceiver(appListViewResetReceiver,
DownloaderService.getIntentFilter(urlString, Downloader.ACTION_COMPLETE));
localBroadcastManager.registerReceiver(interruptedReceiver,
DownloaderService.getIntentFilter(urlString, Downloader.ACTION_INTERRUPTED));
// NOTE: Instead of continually unregistering and re-registering the observer // NOTE: Instead of continually unregistering and re-registering the observer
// (with a different URI), this could equally be done by only having one // (with a different URI), this could equally be done by only having one
@ -329,17 +325,6 @@ public class SwapAppsView extends ListView implements
resetView(); resetView();
} }
/**
* Lazily load the apk from the database the first time it is requested. Means it wont
* be loaded unless we receive a download event from the {@link ApkDownloader}.
*/
private Apk getApkToInstall() {
if (apkToInstall == null) {
apkToInstall = ApkProvider.Helper.find(getActivity(), app.packageName, app.suggestedVersionCode);
}
return apkToInstall;
}
private void resetView() { private void resetView() {
if (app == null) { if (app == null) {