Updates : Fix an issue where app updates continue to be download post cancellation

https://gitlab.com/AuroraOSS/AuroraStore/-/issues/323
This commit is contained in:
Rahul Kumar Patel
2020-03-25 17:43:35 +05:30
parent 7fddd0a9c9
commit 95384bb214
2 changed files with 65 additions and 11 deletions

View File

@@ -26,10 +26,14 @@ import com.aurora.store.BuildConfig;
import com.aurora.store.Constants;
import com.aurora.store.util.NetworkInterceptor;
import com.aurora.store.util.Util;
import com.tonyodev.fetch2.Download;
import com.tonyodev.fetch2.Fetch;
import com.tonyodev.fetch2.FetchConfiguration;
import com.tonyodev.fetch2.FetchListener;
import com.tonyodev.fetch2okhttp.OkHttpDownloader;
import java.util.List;
import okhttp3.OkHttpClient;
public class DownloadManager {
@@ -79,5 +83,19 @@ public class DownloadManager {
}
return builder.build();
}
public static void updateOngoingDownloads(Fetch fetch, List<String> packageList, Download download,
FetchListener fetchListener) {
if (packageList.contains(download.getTag())) {
final String packageName = download.getTag();
if (packageName != null) {
fetch.deleteGroup(packageName.hashCode());
packageList.remove(packageName);
}
}
if (packageList.size() == 0) {
fetch.removeListener(fetchListener);
}
}
}

View File

@@ -56,9 +56,13 @@ import com.mikepenz.fastadapter.FastAdapter;
import com.mikepenz.fastadapter.adapters.ItemAdapter;
import com.mikepenz.fastadapter.diff.FastAdapterDiffUtil;
import com.mikepenz.fastadapter.select.SelectExtension;
import com.tonyodev.fetch2.AbstractFetchListener;
import com.tonyodev.fetch2.Download;
import com.tonyodev.fetch2.Fetch;
import com.tonyodev.fetch2.FetchListener;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.HashSet;
@@ -285,24 +289,56 @@ public class UpdatesFragment extends BaseFragment {
}
}
private void attachFetchCancelListener() {
boolean selectiveUpdate = selectExtension.getSelectedItems().size() > 0;
Observable.fromIterable(selectiveUpdate
? selectedItems
: itemAdapter.getAdapterItems())
.map(UpdatesItem::getPackageName)
.toList()
.doOnSuccess(packages -> {
final List<String> packageList = new ArrayList<>(packages);
final FetchListener fetchListener = new AbstractFetchListener() {
@Override
public void onAdded(@NotNull Download download) {
DownloadManager.updateOngoingDownloads(fetch, packageList, download, this);
}
@Override
public void onQueued(@NotNull Download download, boolean b) {
DownloadManager.updateOngoingDownloads(fetch, packageList, download, this);
}
@Override
public void onProgress(@NotNull Download download, long etaInMilliSeconds, long downloadedBytesPerSecond) {
super.onProgress(download, etaInMilliSeconds, downloadedBytesPerSecond);
DownloadManager.updateOngoingDownloads(fetch, packageList, download, this);
}
};
fetch.addListener(fetchListener);
//Clear ongoing update list
AuroraApplication.setOngoingUpdateList(new ArrayList<>());
//Start BulkUpdate cancellation request
Util.stopBulkUpdate(requireContext());
})
.subscribe();
}
private void updateButtonActions() {
btnAction.setOnClickListener(null);
btnAction.setEnabled(true);
if (AuroraApplication.isBulkUpdateAlive()) {
btnAction.setText(getString(R.string.action_cancel));
btnAction.setOnClickListener(v ->
Observable
.fromIterable(AuroraApplication.getOngoingUpdateList())
.doOnNext(app -> {
fetch.deleteGroup(app.getPackageName().hashCode());
})
.doOnComplete(() -> {
AuroraApplication.setOngoingUpdateList(new ArrayList<>());
Util.stopBulkUpdate(requireContext());
})
.subscribe());
btnAction.setOnClickListener(v -> {
attachFetchCancelListener();
btnAction.setEnabled(false);
});
} else {
boolean selectiveUpdate = selectExtension.getSelectedItems().size() > 0;
btnAction.setOnClickListener(v -> {
btnAction.setEnabled(false);
IgnoreListManager ignoreListManager = new IgnoreListManager(requireContext());
Observable.fromIterable(selectiveUpdate
? selectedItems