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:
@@ -27,6 +27,7 @@ public class UpdateDownload extends Update {
|
|||||||
private long mEta;
|
private long mEta;
|
||||||
private long mSpeed;
|
private long mSpeed;
|
||||||
private int mInstallProgress;
|
private int mInstallProgress;
|
||||||
|
private boolean mAvailableOnline;
|
||||||
|
|
||||||
public UpdateStatus getStatus() {
|
public UpdateStatus getStatus() {
|
||||||
return mStatus;
|
return mStatus;
|
||||||
@@ -91,4 +92,12 @@ public class UpdateDownload extends Update {
|
|||||||
public void setInstallProgress(int progress) {
|
public void setInstallProgress(int progress) {
|
||||||
mInstallProgress = progress;
|
mInstallProgress = progress;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean getAvailableOnline() {
|
||||||
|
return mAvailableOnline;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAvailableOnline(boolean availableOnline) {
|
||||||
|
mAvailableOnline = availableOnline;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -81,6 +81,9 @@ public class UpdatesActivity extends AppCompatActivity {
|
|||||||
UpdaterController.ACTION_INSTALL_PROGRESS.equals(intent.getAction())) {
|
UpdaterController.ACTION_INSTALL_PROGRESS.equals(intent.getAction())) {
|
||||||
String downloadId = intent.getStringExtra(UpdaterController.EXTRA_DOWNLOAD_ID);
|
String downloadId = intent.getStringExtra(UpdaterController.EXTRA_DOWNLOAD_ID);
|
||||||
mAdapter.notifyItemChanged(downloadId);
|
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_UPDATE_STATUS);
|
||||||
intentFilter.addAction(UpdaterController.ACTION_DOWNLOAD_PROGRESS);
|
intentFilter.addAction(UpdaterController.ACTION_DOWNLOAD_PROGRESS);
|
||||||
intentFilter.addAction(UpdaterController.ACTION_INSTALL_PROGRESS);
|
intentFilter.addAction(UpdaterController.ACTION_INSTALL_PROGRESS);
|
||||||
|
intentFilter.addAction(UpdaterController.ACTION_UPDATE_REMOVED);
|
||||||
LocalBroadcastManager.getInstance(this).registerReceiver(mBroadcastReceiver, intentFilter);
|
LocalBroadcastManager.getInstance(this).registerReceiver(mBroadcastReceiver, intentFilter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -88,6 +88,13 @@ public class UpdatesListAdapter extends RecyclerView.Adapter<UpdatesListAdapter.
|
|||||||
|
|
||||||
final String downloadId = mDownloadIds.get(i);
|
final String downloadId = mDownloadIds.get(i);
|
||||||
UpdateDownload update = mUpdaterController.getUpdate(downloadId);
|
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());
|
viewHolder.mName.setText(update.getName());
|
||||||
|
|
||||||
boolean indeterminate = update.getStatus() == UpdateStatus.STARTING ||
|
boolean indeterminate = update.getStatus() == UpdateStatus.STARTING ||
|
||||||
@@ -112,7 +119,8 @@ public class UpdatesListAdapter extends RecyclerView.Adapter<UpdatesListAdapter.
|
|||||||
Utils.canInstall(update) && !mUpdaterController.isVerifyingUpdate();
|
Utils.canInstall(update) && !mUpdaterController.isVerifyingUpdate();
|
||||||
int persistentStatus = update.getPersistentStatus();
|
int persistentStatus = update.getPersistentStatus();
|
||||||
if (persistentStatus == UpdateStatus.Persistent.INCOMPLETE) {
|
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);
|
setButtonAction(viewHolder.mButton2, Action.CANCEL, downloadId, enabled);
|
||||||
} else if (persistentStatus == UpdateStatus.Persistent.VERIFIED) {
|
} else if (persistentStatus == UpdateStatus.Persistent.VERIFIED) {
|
||||||
setButtonAction(viewHolder.mButton1, Action.INSTALL, downloadId, enabled);
|
setButtonAction(viewHolder.mButton1, Action.INSTALL, downloadId, enabled);
|
||||||
@@ -137,6 +145,13 @@ public class UpdatesListAdapter extends RecyclerView.Adapter<UpdatesListAdapter.
|
|||||||
notifyItemChanged(mDownloadIds.indexOf(downloadId));
|
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,
|
private void setButtonAction(Button button, Action action, final String downloadId,
|
||||||
boolean enabled) {
|
boolean enabled) {
|
||||||
switch (action) {
|
switch (action) {
|
||||||
|
@@ -40,6 +40,7 @@ public class UpdaterController implements UpdaterControllerInt {
|
|||||||
|
|
||||||
public static final String ACTION_DOWNLOAD_PROGRESS = "action_download_progress";
|
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_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 ACTION_UPDATE_STATUS = "action_update_status_change";
|
||||||
public static final String EXTRA_DOWNLOAD_ID = "extra_download_id";
|
public static final String EXTRA_DOWNLOAD_ID = "extra_download_id";
|
||||||
|
|
||||||
@@ -100,6 +101,13 @@ public class UpdaterController implements UpdaterControllerInt {
|
|||||||
mBroadcastManager.sendBroadcast(intent);
|
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) {
|
void notifyDownloadProgress(String downloadId) {
|
||||||
Intent intent = new Intent();
|
Intent intent = new Intent();
|
||||||
intent.setAction(ACTION_DOWNLOAD_PROGRESS);
|
intent.setAction(ACTION_DOWNLOAD_PROGRESS);
|
||||||
@@ -292,6 +300,8 @@ public class UpdaterController implements UpdaterControllerInt {
|
|||||||
Log.d(TAG, "Adding download: " + update.getDownloadId());
|
Log.d(TAG, "Adding download: " + update.getDownloadId());
|
||||||
if (mDownloads.containsKey(update.getDownloadId())) {
|
if (mDownloads.containsKey(update.getDownloadId())) {
|
||||||
Log.e(TAG, "Download (" + update.getDownloadId() + ") already added");
|
Log.e(TAG, "Download (" + update.getDownloadId() + ") already added");
|
||||||
|
UpdateDownload updateAdded = mDownloads.get(update.getDownloadId()).mUpdate;
|
||||||
|
updateAdded.setAvailableOnline(availableOnline && updateAdded.getAvailableOnline());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (!fixUpdateStatus(update) && !availableOnline) {
|
if (!fixUpdateStatus(update) && !availableOnline) {
|
||||||
@@ -301,6 +311,7 @@ public class UpdaterController implements UpdaterControllerInt {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
mDownloads.put(update.getDownloadId(), new DownloadEntry(update));
|
mDownloads.put(update.getDownloadId(), new DownloadEntry(update));
|
||||||
|
mDownloads.get(update.getDownloadId()).mUpdate.setAvailableOnline(availableOnline);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -367,16 +378,15 @@ public class UpdaterController implements UpdaterControllerInt {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void deleteUpdateAsync(final String downloadId) {
|
private void deleteUpdateAsync(final UpdateDownload update) {
|
||||||
new Thread(new Runnable() {
|
new Thread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
UpdateDownload update = mDownloads.get(downloadId).mUpdate;
|
|
||||||
File file = update.getFile();
|
File file = update.getFile();
|
||||||
if (file.exists() && !file.delete()) {
|
if (file.exists() && !file.delete()) {
|
||||||
Log.e(TAG, "Could not delete " + file.getAbsolutePath());
|
Log.e(TAG, "Could not delete " + file.getAbsolutePath());
|
||||||
}
|
}
|
||||||
mUpdatesDbHelper.removeUpdate(downloadId);
|
mUpdatesDbHelper.removeUpdate(update.getDownloadId());
|
||||||
}
|
}
|
||||||
}).start();
|
}).start();
|
||||||
}
|
}
|
||||||
@@ -391,8 +401,16 @@ public class UpdaterController implements UpdaterControllerInt {
|
|||||||
update.setStatus(UpdateStatus.DELETED);
|
update.setStatus(UpdateStatus.DELETED);
|
||||||
update.setProgress(0);
|
update.setProgress(0);
|
||||||
update.setPersistentStatus(UpdateStatus.Persistent.UNKNOWN);
|
update.setPersistentStatus(UpdateStatus.Persistent.UNKNOWN);
|
||||||
deleteUpdateAsync(downloadId);
|
deleteUpdateAsync(update);
|
||||||
|
|
||||||
|
if (!update.getAvailableOnline()) {
|
||||||
|
Log.d(TAG, "Download no longer available online, removing");
|
||||||
|
mDownloads.remove(downloadId);
|
||||||
|
notifyUpdateDelete(downloadId);
|
||||||
|
} else {
|
||||||
notifyUpdateChange(downloadId);
|
notifyUpdateChange(downloadId);
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -23,6 +23,7 @@ import android.content.Context;
|
|||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.IntentFilter;
|
import android.content.IntentFilter;
|
||||||
import android.os.Binder;
|
import android.os.Binder;
|
||||||
|
import android.os.Bundle;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
import android.support.v4.content.LocalBroadcastManager;
|
import android.support.v4.content.LocalBroadcastManager;
|
||||||
import android.support.v7.app.NotificationCompat;
|
import android.support.v7.app.NotificationCompat;
|
||||||
@@ -89,6 +90,10 @@ public class UpdaterService extends Service {
|
|||||||
if (UpdaterController.ACTION_UPDATE_STATUS.equals(intent.getAction())) {
|
if (UpdaterController.ACTION_UPDATE_STATUS.equals(intent.getAction())) {
|
||||||
UpdateDownload update = mUpdaterController.getUpdate(downloadId);
|
UpdateDownload update = mUpdaterController.getUpdate(downloadId);
|
||||||
mNotificationBuilder.setContentTitle(update.getName());
|
mNotificationBuilder.setContentTitle(update.getName());
|
||||||
|
mNotificationBuilder.setContentTitle(update.getName());
|
||||||
|
Bundle extras = new Bundle();
|
||||||
|
extras.putString(UpdaterController.EXTRA_DOWNLOAD_ID, downloadId);
|
||||||
|
mNotificationBuilder.setExtras(extras);
|
||||||
handleUpdateStatusChange(update);
|
handleUpdateStatusChange(update);
|
||||||
} else if (UpdaterController.ACTION_DOWNLOAD_PROGRESS.equals(intent.getAction())) {
|
} else if (UpdaterController.ACTION_DOWNLOAD_PROGRESS.equals(intent.getAction())) {
|
||||||
UpdateDownload update = mUpdaterController.getUpdate(downloadId);
|
UpdateDownload update = mUpdaterController.getUpdate(downloadId);
|
||||||
@@ -97,6 +102,13 @@ public class UpdaterService extends Service {
|
|||||||
UpdateDownload update = mUpdaterController.getUpdate(downloadId);
|
UpdateDownload update = mUpdaterController.getUpdate(downloadId);
|
||||||
mNotificationBuilder.setContentTitle(update.getName());
|
mNotificationBuilder.setContentTitle(update.getName());
|
||||||
handleInstallProgress(update);
|
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_DOWNLOAD_PROGRESS);
|
||||||
intentFilter.addAction(UpdaterController.ACTION_INSTALL_PROGRESS);
|
intentFilter.addAction(UpdaterController.ACTION_INSTALL_PROGRESS);
|
||||||
intentFilter.addAction(UpdaterController.ACTION_UPDATE_STATUS);
|
intentFilter.addAction(UpdaterController.ACTION_UPDATE_STATUS);
|
||||||
|
intentFilter.addAction(UpdaterController.ACTION_UPDATE_REMOVED);
|
||||||
LocalBroadcastManager.getInstance(this).registerReceiver(mBroadcastReceiver, intentFilter);
|
LocalBroadcastManager.getInstance(this).registerReceiver(mBroadcastReceiver, intentFilter);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user