Send an event when update are canceled

This allows to properly update the status of the application.
In particular, when an update is deleted:
 - Cancel the associated notification, if any.
 - Remove the entry from the controller if the update is no longer
   available online.
 - Disable the resume button if download is partially downloaded
   and is no longer available online.
This commit is contained in:
Gabriele M
2017-07-07 22:05:22 +02:00
parent 08a7dfade9
commit d327d61cc6
5 changed files with 65 additions and 6 deletions

View File

@@ -27,6 +27,7 @@ public class UpdateDownload extends Update {
private long mEta;
private long mSpeed;
private int mInstallProgress;
private boolean mAvailableOnline;
public UpdateStatus getStatus() {
return mStatus;
@@ -91,4 +92,12 @@ public class UpdateDownload extends Update {
public void setInstallProgress(int progress) {
mInstallProgress = progress;
}
public boolean getAvailableOnline() {
return mAvailableOnline;
}
public void setAvailableOnline(boolean availableOnline) {
mAvailableOnline = availableOnline;
}
}

View File

@@ -81,6 +81,9 @@ public class UpdatesActivity extends AppCompatActivity {
UpdaterController.ACTION_INSTALL_PROGRESS.equals(intent.getAction())) {
String downloadId = intent.getStringExtra(UpdaterController.EXTRA_DOWNLOAD_ID);
mAdapter.notifyItemChanged(downloadId);
} else if (UpdaterController.ACTION_UPDATE_REMOVED.equals(intent.getAction())) {
String downloadId = intent.getStringExtra(UpdaterController.EXTRA_DOWNLOAD_ID);
mAdapter.removeItem(downloadId);
}
}
};
@@ -99,6 +102,7 @@ public class UpdatesActivity extends AppCompatActivity {
intentFilter.addAction(UpdaterController.ACTION_UPDATE_STATUS);
intentFilter.addAction(UpdaterController.ACTION_DOWNLOAD_PROGRESS);
intentFilter.addAction(UpdaterController.ACTION_INSTALL_PROGRESS);
intentFilter.addAction(UpdaterController.ACTION_UPDATE_REMOVED);
LocalBroadcastManager.getInstance(this).registerReceiver(mBroadcastReceiver, intentFilter);
}

View File

@@ -88,6 +88,13 @@ public class UpdatesListAdapter extends RecyclerView.Adapter<UpdatesListAdapter.
final String downloadId = mDownloadIds.get(i);
UpdateDownload update = mUpdaterController.getUpdate(downloadId);
if (update == null) {
// The update was deleted
viewHolder.mButton1.setEnabled(false);
viewHolder.mButton2.setEnabled(false);
return;
}
viewHolder.mName.setText(update.getName());
boolean indeterminate = update.getStatus() == UpdateStatus.STARTING ||
@@ -112,7 +119,8 @@ public class UpdatesListAdapter extends RecyclerView.Adapter<UpdatesListAdapter.
Utils.canInstall(update) && !mUpdaterController.isVerifyingUpdate();
int persistentStatus = update.getPersistentStatus();
if (persistentStatus == UpdateStatus.Persistent.INCOMPLETE) {
setButtonAction(viewHolder.mButton1, Action.RESUME, downloadId, enabled);
setButtonAction(viewHolder.mButton1, Action.RESUME, downloadId,
enabled && update.getAvailableOnline());
setButtonAction(viewHolder.mButton2, Action.CANCEL, downloadId, enabled);
} else if (persistentStatus == UpdateStatus.Persistent.VERIFIED) {
setButtonAction(viewHolder.mButton1, Action.INSTALL, downloadId, enabled);
@@ -137,6 +145,13 @@ public class UpdatesListAdapter extends RecyclerView.Adapter<UpdatesListAdapter.
notifyItemChanged(mDownloadIds.indexOf(downloadId));
}
public void removeItem(String downloadId) {
int position = mDownloadIds.indexOf(downloadId);
mDownloadIds.remove(downloadId);
notifyItemRemoved(position);
notifyItemRangeChanged(position, getItemCount());
}
private void setButtonAction(Button button, Action action, final String downloadId,
boolean enabled) {
switch (action) {

View File

@@ -40,6 +40,7 @@ public class UpdaterController implements UpdaterControllerInt {
public static final String ACTION_DOWNLOAD_PROGRESS = "action_download_progress";
public static final String ACTION_INSTALL_PROGRESS = "action_install_progress";
public static final String ACTION_UPDATE_REMOVED = "action_update_removed";
public static final String ACTION_UPDATE_STATUS = "action_update_status_change";
public static final String EXTRA_DOWNLOAD_ID = "extra_download_id";
@@ -100,6 +101,13 @@ public class UpdaterController implements UpdaterControllerInt {
mBroadcastManager.sendBroadcast(intent);
}
void notifyUpdateDelete(String downloadId) {
Intent intent = new Intent();
intent.setAction(ACTION_UPDATE_REMOVED);
intent.putExtra(EXTRA_DOWNLOAD_ID, downloadId);
mBroadcastManager.sendBroadcast(intent);
}
void notifyDownloadProgress(String downloadId) {
Intent intent = new Intent();
intent.setAction(ACTION_DOWNLOAD_PROGRESS);
@@ -292,6 +300,8 @@ public class UpdaterController implements UpdaterControllerInt {
Log.d(TAG, "Adding download: " + update.getDownloadId());
if (mDownloads.containsKey(update.getDownloadId())) {
Log.e(TAG, "Download (" + update.getDownloadId() + ") already added");
UpdateDownload updateAdded = mDownloads.get(update.getDownloadId()).mUpdate;
updateAdded.setAvailableOnline(availableOnline && updateAdded.getAvailableOnline());
return false;
}
if (!fixUpdateStatus(update) && !availableOnline) {
@@ -301,6 +311,7 @@ public class UpdaterController implements UpdaterControllerInt {
return false;
}
mDownloads.put(update.getDownloadId(), new DownloadEntry(update));
mDownloads.get(update.getDownloadId()).mUpdate.setAvailableOnline(availableOnline);
return true;
}
@@ -367,16 +378,15 @@ public class UpdaterController implements UpdaterControllerInt {
return true;
}
private void deleteUpdateAsync(final String downloadId) {
private void deleteUpdateAsync(final UpdateDownload update) {
new Thread(new Runnable() {
@Override
public void run() {
UpdateDownload update = mDownloads.get(downloadId).mUpdate;
File file = update.getFile();
if (file.exists() && !file.delete()) {
Log.e(TAG, "Could not delete " + file.getAbsolutePath());
}
mUpdatesDbHelper.removeUpdate(downloadId);
mUpdatesDbHelper.removeUpdate(update.getDownloadId());
}
}).start();
}
@@ -391,8 +401,16 @@ public class UpdaterController implements UpdaterControllerInt {
update.setStatus(UpdateStatus.DELETED);
update.setProgress(0);
update.setPersistentStatus(UpdateStatus.Persistent.UNKNOWN);
deleteUpdateAsync(downloadId);
notifyUpdateChange(downloadId);
deleteUpdateAsync(update);
if (!update.getAvailableOnline()) {
Log.d(TAG, "Download no longer available online, removing");
mDownloads.remove(downloadId);
notifyUpdateDelete(downloadId);
} else {
notifyUpdateChange(downloadId);
}
return true;
}

View File

@@ -23,6 +23,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.NotificationCompat;
@@ -89,6 +90,10 @@ public class UpdaterService extends Service {
if (UpdaterController.ACTION_UPDATE_STATUS.equals(intent.getAction())) {
UpdateDownload update = mUpdaterController.getUpdate(downloadId);
mNotificationBuilder.setContentTitle(update.getName());
mNotificationBuilder.setContentTitle(update.getName());
Bundle extras = new Bundle();
extras.putString(UpdaterController.EXTRA_DOWNLOAD_ID, downloadId);
mNotificationBuilder.setExtras(extras);
handleUpdateStatusChange(update);
} else if (UpdaterController.ACTION_DOWNLOAD_PROGRESS.equals(intent.getAction())) {
UpdateDownload update = mUpdaterController.getUpdate(downloadId);
@@ -97,6 +102,13 @@ public class UpdaterService extends Service {
UpdateDownload update = mUpdaterController.getUpdate(downloadId);
mNotificationBuilder.setContentTitle(update.getName());
handleInstallProgress(update);
} else if (UpdaterController.ACTION_UPDATE_REMOVED.equals(intent.getAction())) {
Bundle extras = mNotificationBuilder.getExtras();
if (extras != null && downloadId.equals(
extras.getString(UpdaterController.EXTRA_DOWNLOAD_ID))) {
mNotificationBuilder.setExtras(null);
mNotificationManager.cancel(NOTIFICATION_ID);
}
}
}
};
@@ -104,6 +116,7 @@ public class UpdaterService extends Service {
intentFilter.addAction(UpdaterController.ACTION_DOWNLOAD_PROGRESS);
intentFilter.addAction(UpdaterController.ACTION_INSTALL_PROGRESS);
intentFilter.addAction(UpdaterController.ACTION_UPDATE_STATUS);
intentFilter.addAction(UpdaterController.ACTION_UPDATE_REMOVED);
LocalBroadcastManager.getInstance(this).registerReceiver(mBroadcastReceiver, intentFilter);
}