Bug 923238: Cancel update download on notification swype; r=snorp

This commit is contained in:
Eduard Neculaesi 2014-01-03 18:58:59 +02:00
Родитель 4641b211fe
Коммит c1afc93a0e
2 изменённых файлов: 27 добавлений и 8 удалений

Просмотреть файл

@ -76,6 +76,7 @@ public class UpdateService extends IntentService {
private Builder mBuilder; private Builder mBuilder;
private boolean mDownloading; private boolean mDownloading;
private boolean mCancelDownload;
private boolean mApplyImmediately; private boolean mApplyImmediately;
public UpdateService() { public UpdateService() {
@ -89,6 +90,7 @@ public class UpdateService extends IntentService {
mPrefs = getSharedPreferences(PREFS_NAME, 0); mPrefs = getSharedPreferences(PREFS_NAME, 0);
mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
mConnectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); mConnectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
mCancelDownload = false;
} }
@Override @Override
@ -101,6 +103,8 @@ public class UpdateService extends IntentService {
mApplyImmediately = true; mApplyImmediately = true;
showDownloadNotification(); showDownloadNotification();
} else if (UpdateServiceHelper.ACTION_CANCEL_DOWNLOAD.equals(intent.getAction())) {
mCancelDownload = true;
} else { } else {
super.onStartCommand(intent, flags, startId); super.onStartCommand(intent, flags, startId);
} }
@ -374,16 +378,21 @@ public class UpdateService extends IntentService {
Intent notificationIntent = new Intent(UpdateServiceHelper.ACTION_APPLY_UPDATE); Intent notificationIntent = new Intent(UpdateServiceHelper.ACTION_APPLY_UPDATE);
notificationIntent.setClass(this, UpdateService.class); notificationIntent.setClass(this, UpdateService.class);
Intent cancelIntent = new Intent(UpdateServiceHelper.ACTION_CANCEL_DOWNLOAD);
cancelIntent.setClass(this, UpdateService.class);
if (downloadFile != null) if (downloadFile != null)
notificationIntent.putExtra(UpdateServiceHelper.EXTRA_PACKAGE_PATH_NAME, downloadFile.getAbsolutePath()); notificationIntent.putExtra(UpdateServiceHelper.EXTRA_PACKAGE_PATH_NAME, downloadFile.getAbsolutePath());
PendingIntent contentIntent = PendingIntent.getService(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent contentIntent = PendingIntent.getService(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent deleteIntent = PendingIntent.getService(this, 0, cancelIntent, PendingIntent.FLAG_CANCEL_CURRENT);
mBuilder = new NotificationCompat.Builder(this); mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentTitle(getResources().getString(R.string.updater_downloading_title)) mBuilder.setContentTitle(getResources().getString(R.string.updater_downloading_title))
.setContentText(mApplyImmediately ? "" : getResources().getString(R.string.updater_downloading_select)) .setContentText(mApplyImmediately ? "" : getResources().getString(R.string.updater_downloading_select))
.setSmallIcon(android.R.drawable.stat_sys_download) .setSmallIcon(android.R.drawable.stat_sys_download)
.setContentIntent(contentIntent); .setContentIntent(contentIntent)
.setDeleteIntent(deleteIntent);
mBuilder.setProgress(100, 0, true); mBuilder.setProgress(100, 0, true);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
@ -428,6 +437,7 @@ public class UpdateService extends IntentService {
InputStream input = null; InputStream input = null;
mDownloading = true; mDownloading = true;
mCancelDownload = false;
showDownloadNotification(downloadFile); showDownloadNotification(downloadFile);
try { try {
@ -443,7 +453,7 @@ public class UpdateService extends IntentService {
int bytesRead = 0; int bytesRead = 0;
int lastNotify = 0; int lastNotify = 0;
while ((len = input.read(buf, 0, BUFSIZE)) > 0) { while ((len = input.read(buf, 0, BUFSIZE)) > 0 && !mCancelDownload) {
output.write(buf, 0, len); output.write(buf, 0, len);
bytesRead += len; bytesRead += len;
// Updating the notification takes time so only do it every 1MB // Updating the notification takes time so only do it every 1MB
@ -454,11 +464,19 @@ public class UpdateService extends IntentService {
} }
} }
Log.i(LOGTAG, "completed update download!");
mNotificationManager.cancel(NOTIFICATION_ID); mNotificationManager.cancel(NOTIFICATION_ID);
// if the download was canceled by the user
// delete the update package
if (mCancelDownload) {
Log.i(LOGTAG, "download canceled by user!");
downloadFile.delete();
return null;
} else {
Log.i(LOGTAG, "completed update download!");
return downloadFile; return downloadFile;
}
} catch (Exception e) { } catch (Exception e) {
downloadFile.delete(); downloadFile.delete();
showDownloadFailure(); showDownloadFailure();

Просмотреть файл

@ -27,6 +27,7 @@ public class UpdateServiceHelper {
public static final String ACTION_CHECK_UPDATE_RESULT = AppConstants.ANDROID_PACKAGE_NAME + ".CHECK_UPDATE_RESULT"; public static final String ACTION_CHECK_UPDATE_RESULT = AppConstants.ANDROID_PACKAGE_NAME + ".CHECK_UPDATE_RESULT";
public static final String ACTION_DOWNLOAD_UPDATE = AppConstants.ANDROID_PACKAGE_NAME + ".DOWNLOAD_UPDATE"; public static final String ACTION_DOWNLOAD_UPDATE = AppConstants.ANDROID_PACKAGE_NAME + ".DOWNLOAD_UPDATE";
public static final String ACTION_APPLY_UPDATE = AppConstants.ANDROID_PACKAGE_NAME + ".APPLY_UPDATE"; public static final String ACTION_APPLY_UPDATE = AppConstants.ANDROID_PACKAGE_NAME + ".APPLY_UPDATE";
public static final String ACTION_CANCEL_DOWNLOAD = AppConstants.ANDROID_PACKAGE_NAME + ".CANCEL_DOWNLOAD";
// Flags for ACTION_CHECK_FOR_UPDATE // Flags for ACTION_CHECK_FOR_UPDATE
public static final int FLAG_FORCE_DOWNLOAD = 1; public static final int FLAG_FORCE_DOWNLOAD = 1;