Protect the data of UpdaterController
Create copies of the objects not to allow other classes change them, excluding those of controller package.
This commit is contained in:
@@ -53,13 +53,13 @@ class ABUpdateInstaller {
|
|||||||
case UpdateEngine.UpdateStatusConstants.DOWNLOADING:
|
case UpdateEngine.UpdateStatusConstants.DOWNLOADING:
|
||||||
case UpdateEngine.UpdateStatusConstants.FINALIZING: {
|
case UpdateEngine.UpdateStatusConstants.FINALIZING: {
|
||||||
int progress = Math.round(percent * 100);
|
int progress = Math.round(percent * 100);
|
||||||
mUpdaterController.getUpdate(mDownloadId).setInstallProgress(progress);
|
mUpdaterController.getActualUpdate(mDownloadId).setInstallProgress(progress);
|
||||||
mUpdaterController.notifyInstallProgress(mDownloadId);
|
mUpdaterController.notifyInstallProgress(mDownloadId);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case UpdateEngine.UpdateStatusConstants.REPORTING_ERROR_EVENT: {
|
case UpdateEngine.UpdateStatusConstants.REPORTING_ERROR_EVENT: {
|
||||||
UpdateDownload update = mUpdaterController.getUpdate(mDownloadId);
|
UpdateDownload update = mUpdaterController.getActualUpdate(mDownloadId);
|
||||||
update.setInstallProgress(0);
|
update.setInstallProgress(0);
|
||||||
update.setStatus(UpdateStatus.INSTALLATION_FAILED);
|
update.setStatus(UpdateStatus.INSTALLATION_FAILED);
|
||||||
mUpdaterController.notifyUpdateChange(mDownloadId);;
|
mUpdaterController.notifyUpdateChange(mDownloadId);;
|
||||||
@@ -73,7 +73,7 @@ class ABUpdateInstaller {
|
|||||||
sIsInstallingUpdate = false;
|
sIsInstallingUpdate = false;
|
||||||
switch (errorCode) {
|
switch (errorCode) {
|
||||||
case UpdateEngine.ErrorCodeConstants.SUCCESS: {
|
case UpdateEngine.ErrorCodeConstants.SUCCESS: {
|
||||||
UpdateDownload update = mUpdaterController.getUpdate(mDownloadId);
|
UpdateDownload update = mUpdaterController.getActualUpdate(mDownloadId);
|
||||||
update.setInstallProgress(0);
|
update.setInstallProgress(0);
|
||||||
update.setStatus(UpdateStatus.INSTALLED);
|
update.setStatus(UpdateStatus.INSTALLED);
|
||||||
mUpdaterController.notifyUpdateChange(mDownloadId);
|
mUpdaterController.notifyUpdateChange(mDownloadId);
|
||||||
@@ -81,7 +81,7 @@ class ABUpdateInstaller {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
default: {
|
default: {
|
||||||
UpdateDownload update = mUpdaterController.getUpdate(mDownloadId);
|
UpdateDownload update = mUpdaterController.getActualUpdate(mDownloadId);
|
||||||
update.setInstallProgress(0);
|
update.setInstallProgress(0);
|
||||||
update.setStatus(UpdateStatus.INSTALLATION_FAILED);
|
update.setStatus(UpdateStatus.INSTALLATION_FAILED);
|
||||||
mUpdaterController.notifyUpdateChange(mDownloadId);
|
mUpdaterController.notifyUpdateChange(mDownloadId);
|
||||||
@@ -111,13 +111,13 @@ class ABUpdateInstaller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private boolean startUpdate() {
|
private boolean startUpdate() {
|
||||||
File file = mUpdaterController.getUpdate(mDownloadId).getFile();
|
File file = mUpdaterController.getActualUpdate(mDownloadId).getFile();
|
||||||
if (!file.exists()) {
|
if (!file.exists()) {
|
||||||
Log.e(TAG, "The given update doesn't exist");
|
Log.e(TAG, "The given update doesn't exist");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
mUpdaterController.getUpdate(mDownloadId).setStatus(UpdateStatus.INSTALLING);
|
mUpdaterController.getActualUpdate(mDownloadId).setStatus(UpdateStatus.INSTALLING);
|
||||||
mUpdaterController.notifyUpdateChange(mDownloadId);
|
mUpdaterController.notifyUpdateChange(mDownloadId);
|
||||||
|
|
||||||
long offset;
|
long offset;
|
||||||
@@ -139,7 +139,8 @@ class ABUpdateInstaller {
|
|||||||
zipFile.close();
|
zipFile.close();
|
||||||
} catch (IOException | IllegalArgumentException e) {
|
} catch (IOException | IllegalArgumentException e) {
|
||||||
Log.e(TAG, "Could not prepare " + file, e);
|
Log.e(TAG, "Could not prepare " + file, e);
|
||||||
mUpdaterController.getUpdate(mDownloadId).setStatus(UpdateStatus.INSTALLATION_FAILED);
|
mUpdaterController.getActualUpdate(mDownloadId)
|
||||||
|
.setStatus(UpdateStatus.INSTALLATION_FAILED);
|
||||||
mUpdaterController.notifyUpdateChange(mDownloadId);
|
mUpdaterController.notifyUpdateChange(mDownloadId);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -212,11 +212,12 @@ public class UpdaterController implements UpdaterControllerInt {
|
|||||||
@Override
|
@Override
|
||||||
public void update(long bytesRead, long contentLength, long speed, long eta,
|
public void update(long bytesRead, long contentLength, long speed, long eta,
|
||||||
boolean done) {
|
boolean done) {
|
||||||
|
UpdateDownload update = mDownloads.get(downloadId).mUpdate;
|
||||||
if (contentLength <= 0) {
|
if (contentLength <= 0) {
|
||||||
if (getUpdate(downloadId).getFileSize() <= 0) {
|
if (update.getFileSize() <= 0) {
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
contentLength = getUpdate(downloadId).getFileSize();
|
contentLength = update.getFileSize();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (contentLength <= 0) {
|
if (contentLength <= 0) {
|
||||||
@@ -227,9 +228,9 @@ public class UpdaterController implements UpdaterControllerInt {
|
|||||||
if (progress != mProgress || mLastUpdate - now > MAX_REPORT_INTERVAL_MS) {
|
if (progress != mProgress || mLastUpdate - now > MAX_REPORT_INTERVAL_MS) {
|
||||||
mProgress = progress;
|
mProgress = progress;
|
||||||
mLastUpdate = now;
|
mLastUpdate = now;
|
||||||
getUpdate(downloadId).setProgress(progress);
|
update.setProgress(progress);
|
||||||
getUpdate(downloadId).setEta(eta);
|
update.setEta(eta);
|
||||||
getUpdate(downloadId).setSpeed(speed);
|
update.setSpeed(speed);
|
||||||
notifyDownloadProgress(downloadId);
|
notifyDownloadProgress(downloadId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -341,8 +342,9 @@ public class UpdaterController implements UpdaterControllerInt {
|
|||||||
Log.d(TAG, update.getDownloadId() + " had an invalid status and is not online");
|
Log.d(TAG, update.getDownloadId() + " had an invalid status and is not online");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
mDownloads.put(update.getDownloadId(), new DownloadEntry(update));
|
update.setAvailableOnline(availableOnline);
|
||||||
mDownloads.get(update.getDownloadId()).mUpdate.setAvailableOnline(availableOnline);
|
UpdateDownload updateCopy = new UpdateDownload(update);
|
||||||
|
mDownloads.put(update.getDownloadId(), new DownloadEntry(updateCopy));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -458,13 +460,18 @@ public class UpdaterController implements UpdaterControllerInt {
|
|||||||
public List<UpdateDownload> getUpdates() {
|
public List<UpdateDownload> getUpdates() {
|
||||||
List<UpdateDownload> updates = new ArrayList<>();
|
List<UpdateDownload> updates = new ArrayList<>();
|
||||||
for (DownloadEntry entry : mDownloads.values()) {
|
for (DownloadEntry entry : mDownloads.values()) {
|
||||||
updates.add(entry.mUpdate);
|
updates.add(new UpdateDownload(entry.mUpdate));
|
||||||
}
|
}
|
||||||
return updates;
|
return updates;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public UpdateDownload getUpdate(String downloadId) {
|
public UpdateDownload getUpdate(String downloadId) {
|
||||||
|
DownloadEntry entry = mDownloads.get(downloadId);
|
||||||
|
return entry != null ? new UpdateDownload(entry.mUpdate) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
UpdateDownload getActualUpdate(String downloadId) {
|
||||||
DownloadEntry entry = mDownloads.get(downloadId);
|
DownloadEntry entry = mDownloads.get(downloadId);
|
||||||
return entry != null ? entry.mUpdate : null;
|
return entry != null ? entry.mUpdate : null;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user