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 boolean mDownloading;
private boolean mCancelDownload;
private boolean mApplyImmediately;
public UpdateService() {
@ -85,10 +86,11 @@ public class UpdateService extends IntentService {
@Override
public void onCreate () {
super.onCreate();
mPrefs = getSharedPreferences(PREFS_NAME, 0);
mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
mConnectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
mCancelDownload = false;
}
@Override
@ -101,6 +103,8 @@ public class UpdateService extends IntentService {
mApplyImmediately = true;
showDownloadNotification();
} else if (UpdateServiceHelper.ACTION_CANCEL_DOWNLOAD.equals(intent.getAction())) {
mCancelDownload = true;
} else {
super.onStartCommand(intent, flags, startId);
}
@ -203,7 +207,7 @@ public class UpdateService extends IntentService {
}
Log.i(LOGTAG, "update available, buildID = " + info.buildID);
int connectionType = netInfo.getType();
int autoDownloadPolicy = getAutoDownloadPolicy();
@ -374,16 +378,21 @@ public class UpdateService extends IntentService {
Intent notificationIntent = new Intent(UpdateServiceHelper.ACTION_APPLY_UPDATE);
notificationIntent.setClass(this, UpdateService.class);
Intent cancelIntent = new Intent(UpdateServiceHelper.ACTION_CANCEL_DOWNLOAD);
cancelIntent.setClass(this, UpdateService.class);
if (downloadFile != null)
notificationIntent.putExtra(UpdateServiceHelper.EXTRA_PACKAGE_PATH_NAME, downloadFile.getAbsolutePath());
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.setContentTitle(getResources().getString(R.string.updater_downloading_title))
.setContentText(mApplyImmediately ? "" : getResources().getString(R.string.updater_downloading_select))
.setSmallIcon(android.R.drawable.stat_sys_download)
.setContentIntent(contentIntent);
.setContentIntent(contentIntent)
.setDeleteIntent(deleteIntent);
mBuilder.setProgress(100, 0, true);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
@ -400,7 +409,7 @@ public class UpdateService extends IntentService {
notification.setLatestEventInfo(this, getResources().getString(R.string.updater_downloading_title_failed),
getResources().getString(R.string.updater_downloading_retry),
contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, notification);
}
@ -428,6 +437,7 @@ public class UpdateService extends IntentService {
InputStream input = null;
mDownloading = true;
mCancelDownload = false;
showDownloadNotification(downloadFile);
try {
@ -443,7 +453,7 @@ public class UpdateService extends IntentService {
int bytesRead = 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);
bytesRead += len;
// 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);
return downloadFile;
// 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;
}
} catch (Exception e) {
downloadFile.delete();
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_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_CANCEL_DOWNLOAD = AppConstants.ANDROID_PACKAGE_NAME + ".CANCEL_DOWNLOAD";
// Flags for ACTION_CHECK_FOR_UPDATE
public static final int FLAG_FORCE_DOWNLOAD = 1;