Don't modify the objects of the controller directly

Add a method to the controller that allows to set a list of
downloads as not available online.
This commit is contained in:
Gabriele M
2017-07-08 21:39:10 +02:00
parent b4b8409766
commit a2919a8947
4 changed files with 26 additions and 12 deletions

View File

@@ -166,7 +166,7 @@ public class UpdatesActivity extends AppCompatActivity {
List<UpdateDownload> updates = Utils.parseJson(jsonFile, true); List<UpdateDownload> updates = Utils.parseJson(jsonFile, true);
List<UpdateDownload> updatesNotAvailable = LegacySupport.importDownloads(this, updates); List<String> importedNotAvailableOnline = LegacySupport.importDownloads(this, updates);
List<String> updatesOnline = new ArrayList<>(); List<String> updatesOnline = new ArrayList<>();
for (UpdateDownload update : updates) { for (UpdateDownload update : updates) {
@@ -174,10 +174,9 @@ public class UpdatesActivity extends AppCompatActivity {
updatesOnline.add(update.getDownloadId()); updatesOnline.add(update.getDownloadId());
} }
if (updatesNotAvailable != null) { if (importedNotAvailableOnline != null) {
for (UpdateDownload update : updatesNotAvailable) { updatesOnline.removeAll(importedNotAvailableOnline);
update.setAvailableOnline(false); controller.setUpdatesNotAvailableOnline(importedNotAvailableOnline);
}
} }
controller.setUpdatesAvailableOnline(updatesOnline, true); controller.setUpdatesAvailableOnline(updatesOnline, true);

View File

@@ -293,6 +293,16 @@ public class UpdaterController implements UpdaterControllerInt {
return true; return true;
} }
@Override
public void setUpdatesNotAvailableOnline(List<String> downloadIds) {
for (String downloadId : downloadIds) {
DownloadEntry update = mDownloads.get(downloadId);
if (update != null) {
update.mUpdate.setAvailableOnline(false);
}
}
}
@Override @Override
public void setUpdatesAvailableOnline(List<String> downloadIds, boolean purgeList) { public void setUpdatesAvailableOnline(List<String> downloadIds, boolean purgeList) {
List<String> toRemove = new ArrayList<>(); List<String> toRemove = new ArrayList<>();

View File

@@ -32,6 +32,8 @@ public interface UpdaterControllerInt {
void setUpdatesAvailableOnline(List<String> downloadIds, boolean purgeList); void setUpdatesAvailableOnline(List<String> downloadIds, boolean purgeList);
void setUpdatesNotAvailableOnline(List<String> downloadIds);
boolean startDownload(String downloadId); boolean startDownload(String downloadId);
boolean pauseDownload(String downloadId); boolean pauseDownload(String downloadId);

View File

@@ -46,15 +46,17 @@ public final class LegacySupport {
} }
/** /**
* This imports the updates downloaded with CMUpdater. It accepts in input a list of * This method imports the updates downloaded with CMUpdater and it adds them to the
* updates to be downloaded and updates its entries when it finds a matching imported * updates database. It accepts in input a list of updates which it updates with the
* update (i.e. same filename). * data of matching imported updates (i.e. same filename). If for a given imported
* update this method can't find any matching update, it adds a new entry to the
* given list.
* *
* @param context * @param context
* @param updatesJson List of updates to be downloaded * @param updatesJson List of updates to be downloaded
* @return List of updates that weren't used to replace entries of updatesJson * @return A list with the IDs of the imported updates with no matching updates
*/ */
public static List<UpdateDownload> importDownloads(Context context, public static List<String> importDownloads(Context context,
List<UpdateDownload> updatesJson) { List<UpdateDownload> updatesJson) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
if (preferences.getBoolean(IMPORT_DONE, false)) { if (preferences.getBoolean(IMPORT_DONE, false)) {
@@ -63,7 +65,7 @@ public final class LegacySupport {
Log.d(TAG, "Importing downloads"); Log.d(TAG, "Importing downloads");
List<UpdateDownload> notReplacing = new ArrayList<>(); List<String> notReplacing = new ArrayList<>();
File updatesDir = new File(context.getDataDir(), "updates/"); File updatesDir = new File(context.getDataDir(), "updates/");
if (updatesDir.isDirectory()) { if (updatesDir.isDirectory()) {
UpdatesDbHelper dbHelper = new UpdatesDbHelper(context); UpdatesDbHelper dbHelper = new UpdatesDbHelper(context);
@@ -93,7 +95,8 @@ public final class LegacySupport {
} else { } else {
try { try {
UpdateDownload update = createUpdateFromFile(file); UpdateDownload update = createUpdateFromFile(file);
notReplacing.add(update); notReplacing.add(update.getDownloadId());
updatesJson.add(update);
dbHelper.addUpdate(update); dbHelper.addUpdate(update);
} catch (IllegalFilenameException e) { } catch (IllegalFilenameException e) {
Log.e(TAG, "Deleting " + file.getAbsolutePath(), e); Log.e(TAG, "Deleting " + file.getAbsolutePath(), e);