Cleanup contributions state when service starts.

This will clean up stale 'Uploading' or 'Queued' entries that
failed to do so because of service crashes
This commit is contained in:
YuviPanda 2013-02-08 17:34:50 +05:30
parent 297f3277de
commit e6a8f4d626
2 changed files with 26 additions and 12 deletions

View file

@ -33,6 +33,8 @@ public class UploadService extends Service {
private static final int ACTION_UPLOAD_FILE = 1;
public static final String ACTION_START_SERVICE = EXTRA_PREFIX + ".upload";
private NotificationManager notificationManager;
private ContentProviderClient contributionsProviderClient;
private CommonsApplication app;
@ -197,12 +199,6 @@ public class UploadService extends Service {
contribution.setContentProviderClient(contributionsProviderClient);
contribution.save();
postMessage(ACTION_UPLOAD_FILE, contribution);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
toUpload++;
if (curProgressNotification != null && toUpload != 1) {
curProgressNotification.contentView.setTextViewText(R.id.uploadNotificationsCount, String.format(getString(R.string.uploads_pending_notification_indicator), toUpload));
@ -210,10 +206,26 @@ public class UploadService extends Service {
notificationManager.notify(NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification);
}
Log.d("Commons", "Received startcommand");
postMessage(ACTION_UPLOAD_FILE, contribution);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(intent.getAction() == ACTION_START_SERVICE) {
ContentValues failedValues = new ContentValues();
failedValues.put(Contribution.Table.COLUMN_STATE, Contribution.STATE_FAILED);
int updated = getContentResolver().update(ContributionsContentProvider.BASE_URI,
failedValues,
Contribution.Table.COLUMN_STATE + " = ? OR " + Contribution.Table.COLUMN_STATE + " = ?",
new String[]{ String.valueOf(Contribution.STATE_QUEUED), String.valueOf(Contribution.STATE_IN_PROGRESS) }
);
Log.d("Commons", "Set " + updated + " uploads to failed");
} else {
Contribution contribution = mediaFromIntent(intent);
queueContribution(contribution);
}
return START_REDELIVER_INTENT;
}

View file

@ -169,8 +169,10 @@ public class ContributionsActivity extends AuthenticatedActivity implements Load
.cacheInMemory()
.cacheOnDisc()
.resetViewBeforeLoading().build();
bindService(new Intent(this, UploadService.class), uploadServiceConnection, Context.BIND_AUTO_CREATE);
Intent uploadServiceIntent = new Intent(this, UploadService.class);
uploadServiceIntent.setAction(UploadService.ACTION_START_SERVICE);
startService(uploadServiceIntent);
bindService(uploadServiceIntent, uploadServiceConnection, Context.BIND_AUTO_CREATE);
Cursor allContributions = getContentResolver().query(ContributionsContentProvider.BASE_URI, Contribution.Table.ALL_FIELDS, CONTRIBUTION_SELECTION, null, CONTRIBUTION_SORT);
contributionsAdapter = new ContributionAdapter(this, allContributions, 0);