Make "batchUpdates" mode an implementation detail of the AddUpdateStatusManager class.

Because of the way that this can be misused without the compiler knowing
(e.g. by forgetting to call `endBatchUpdates()`) it may be safer to move
it to an internal implementation detail of the class.

It could probably be done away with completely if the `notify*` methods
were moved out of the respective `*ApkInternal()` methods, but that
requires more significant refactoring to get right without code
duplication.
This commit is contained in:
Peter Serwylo 2017-02-24 11:42:15 +11:00 committed by mvp76
parent aa945367c9
commit 30d3f8efcc
2 changed files with 15 additions and 6 deletions

View File

@ -19,6 +19,7 @@ import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List;
import java.util.Map; import java.util.Map;
public class AppUpdateStatusManager { public class AppUpdateStatusManager {
@ -187,6 +188,15 @@ public class AppUpdateStatusManager {
} }
} }
public void addApks(List<Apk> apksToUpdate, Status status) {
startBatchUpdates();
for (Apk apk : apksToUpdate) {
addApk(apk, status, null);
}
endBatchUpdates();
}
/** /**
* Add an Apk to the AppUpdateStatusManager manager. * Add an Apk to the AppUpdateStatusManager manager.
* @param apk The apk to add. * @param apk The apk to add.
@ -250,13 +260,13 @@ public class AppUpdateStatusManager {
} }
} }
void startBatchUpdates() { private void startBatchUpdates() {
synchronized (appMapping) { synchronized (appMapping) {
isBatchUpdating = true; isBatchUpdating = true;
} }
} }
void endBatchUpdates() { private void endBatchUpdates() {
synchronized (appMapping) { synchronized (appMapping) {
isBatchUpdating = false; isBatchUpdating = false;
notifyChange(); notifyChange();

View File

@ -489,14 +489,13 @@ public class UpdateService extends IntentService {
private void showAppUpdatesNotification(Cursor hasUpdates) { private void showAppUpdatesNotification(Cursor hasUpdates) {
if (hasUpdates != null) { if (hasUpdates != null) {
hasUpdates.moveToFirst(); hasUpdates.moveToFirst();
appUpdateStatusManager.startBatchUpdates(); List<Apk> apksToUpdate = new ArrayList<>(hasUpdates.getCount());
for (int i = 0; i < hasUpdates.getCount(); i++) { for (int i = 0; i < hasUpdates.getCount(); i++) {
App app = new App(hasUpdates); App app = new App(hasUpdates);
hasUpdates.moveToNext(); hasUpdates.moveToNext();
Apk apk = ApkProvider.Helper.findApkFromAnyRepo(this, app.packageName, app.suggestedVersionCode); apksToUpdate.add(ApkProvider.Helper.findApkFromAnyRepo(this, app.packageName, app.suggestedVersionCode));
appUpdateStatusManager.addApk(apk, AppUpdateStatusManager.Status.UpdateAvailable, null);
} }
appUpdateStatusManager.endBatchUpdates(); appUpdateStatusManager.addApks(apksToUpdate, AppUpdateStatusManager.Status.UpdateAvailable);
} }
} }