Show upload progress in my contributions

Abandoned local broadcast based approach, and 'went with the flow'.
Now have a 'transferred' column in sqlite database & the
content provider
This commit is contained in:
YuviPanda 2013-02-04 01:28:30 +05:30
parent d1f0539bbe
commit 959cff3ae2
4 changed files with 36 additions and 38 deletions

View file

@ -39,7 +39,8 @@
<string name="title_activity_contributions">My Contributions</string> <string name="title_activity_contributions">My Contributions</string>
<string name="contribution_state_queued">Queued</string> <string name="contribution_state_queued">Queued</string>
<string name="contribution_state_failed">Failed</string> <string name="contribution_state_failed">Failed</string>
<string name="contribution_state_in_progress">Uploading</string> <string name="contribution_state_in_progress">%1$d%% complete</string>
<string name="contribution_state_starting">Uploading</string>
<string name="menu_from_gallery">From Gallery</string> <string name="menu_from_gallery">From Gallery</string>
<string name="menu_from_camera">Take Photo</string> <string name="menu_from_camera">Take Photo</string>
</resources> </resources>

View file

@ -4,7 +4,6 @@ import java.io.*;
import java.text.*; import java.text.*;
import java.util.Date; import java.util.Date;
import android.support.v4.content.LocalBroadcastManager;
import org.mediawiki.api.*; import org.mediawiki.api.*;
import org.wikimedia.commons.contributions.Contribution; import org.wikimedia.commons.contributions.Contribution;
import org.wikimedia.commons.contributions.ContributionsActivity; import org.wikimedia.commons.contributions.ContributionsActivity;
@ -26,10 +25,6 @@ import android.net.*;
public class UploadService extends IntentService { public class UploadService extends IntentService {
private static final String EXTRA_PREFIX = "org.wikimedia.commons.upload"; private static final String EXTRA_PREFIX = "org.wikimedia.commons.upload";
public static final String INTENT_CONTRIBUTION_STATE_CHANGED = EXTRA_PREFIX + ".progress";
public static final String EXTRA_CONTRIBUTION_ID = EXTRA_PREFIX + ".filename";
public static final String EXTRA_TRANSFERRED_BYTES = EXTRA_PREFIX + ".progress.transferred";
public static final String EXTRA_MEDIA_URI = EXTRA_PREFIX + ".uri"; public static final String EXTRA_MEDIA_URI = EXTRA_PREFIX + ".uri";
public static final String EXTRA_TARGET_FILENAME = EXTRA_PREFIX + ".filename"; public static final String EXTRA_TARGET_FILENAME = EXTRA_PREFIX + ".filename";
@ -38,7 +33,6 @@ public class UploadService extends IntentService {
public static final String EXTRA_MIMETYPE = EXTRA_PREFIX + ".mimetype"; public static final String EXTRA_MIMETYPE = EXTRA_PREFIX + ".mimetype";
private NotificationManager notificationManager; private NotificationManager notificationManager;
private LocalBroadcastManager localBroadcastManager;
private ContentProviderClient contributionsProviderClient; private ContentProviderClient contributionsProviderClient;
private CommonsApplication app; private CommonsApplication app;
@ -91,7 +85,6 @@ public class UploadService extends IntentService {
} }
notificationTitleChanged = true; notificationTitleChanged = true;
contribution.setState(Contribution.STATE_IN_PROGRESS); contribution.setState(Contribution.STATE_IN_PROGRESS);
contribution.save();
} }
if(transferred == total) { if(transferred == total) {
// Completed! // Completed!
@ -100,12 +93,9 @@ public class UploadService extends IntentService {
} else { } else {
curNotification.contentView.setProgressBar(R.id.uploadNotificationProgress, 100, (int) (((double) transferred / (double) total) * 100), false); curNotification.contentView.setProgressBar(R.id.uploadNotificationProgress, 100, (int) (((double) transferred / (double) total) * 100), false);
notificationManager.notify(NOTIFICATION_DOWNLOAD_IN_PROGRESS, curNotification); notificationManager.notify(NOTIFICATION_DOWNLOAD_IN_PROGRESS, curNotification);
Intent mediaUploadProgressIntent = new Intent(INTENT_CONTRIBUTION_STATE_CHANGED);
// mediaUploadProgressIntent.putExtra(EXTRA_MEDIA contribution);
mediaUploadProgressIntent.putExtra(EXTRA_TRANSFERRED_BYTES, transferred);
localBroadcastManager.sendBroadcast(mediaUploadProgressIntent);
} }
contribution.setTransferred(transferred);
contribution.save();
} }
} }
@ -121,7 +111,6 @@ public class UploadService extends IntentService {
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
localBroadcastManager = LocalBroadcastManager.getInstance(this);
app = (CommonsApplication) this.getApplicationContext(); app = (CommonsApplication) this.getApplicationContext();
contributionsProviderClient = this.getContentResolver().acquireContentProviderClient(ContributionsContentProvider.AUTHORITY); contributionsProviderClient = this.getContentResolver().acquireContentProviderClient(ContributionsContentProvider.AUTHORITY);
} }
@ -184,7 +173,6 @@ public class UploadService extends IntentService {
Intent mediaUploadQueuedIntent = new Intent(); Intent mediaUploadQueuedIntent = new Intent();
mediaUploadQueuedIntent.putExtra("dummy-data", contribution); // FIXME: Move to separate handler, do not inherit from IntentService mediaUploadQueuedIntent.putExtra("dummy-data", contribution); // FIXME: Move to separate handler, do not inherit from IntentService
localBroadcastManager.sendBroadcast(mediaUploadQueuedIntent);
return super.onStartCommand(mediaUploadQueuedIntent, flags, startId); return super.onStartCommand(mediaUploadQueuedIntent, flags, startId);
} }

View file

@ -21,10 +21,23 @@ public class Contribution extends Media {
private ContentProviderClient client; private ContentProviderClient client;
private Uri contentUri; private Uri contentUri;
public long getTransferred() {
return transferred;
}
public void setTransferred(long transferred) {
this.transferred = transferred;
}
private long transferred;
public String getEditSummary() { public String getEditSummary() {
return editSummary; return editSummary;
} }
public Uri getContentUri() {
return contentUri;
}
private String editSummary; private String editSummary;
public Date getTimestamp() { public Date getTimestamp() {
@ -104,6 +117,7 @@ public class Contribution extends Media {
cv.put(Table.COLUMN_LENGTH, getDataLength()); cv.put(Table.COLUMN_LENGTH, getDataLength());
cv.put(Table.COLUMN_TIMESTAMP, getTimestamp().getTime()); cv.put(Table.COLUMN_TIMESTAMP, getTimestamp().getTime());
cv.put(Table.COLUMN_STATE, getState()); cv.put(Table.COLUMN_STATE, getState());
cv.put(Table.COLUMN_TRANSFERRED, transferred);
return cv; return cv;
} }
@ -119,6 +133,7 @@ public class Contribution extends Media {
public static final String COLUMN_STATE = "state"; public static final String COLUMN_STATE = "state";
public static final String COLUMN_LENGTH = "length"; public static final String COLUMN_LENGTH = "length";
public static final String COLUMN_UPLOADED = "uploaded"; public static final String COLUMN_UPLOADED = "uploaded";
public static final String COLUMN_TRANSFERRED = "transferred"; // Currently transferred number of bytes
private static final String CREATE_TABLE_STATEMENT = "CREATE TABLE " + TABLE_NAME + " (" private static final String CREATE_TABLE_STATEMENT = "CREATE TABLE " + TABLE_NAME + " ("
@ -129,7 +144,8 @@ public class Contribution extends Media {
+ "uploaded INTEGER," + "uploaded INTEGER,"
+ "timestamp INTEGER," + "timestamp INTEGER,"
+ "state INTEGER," + "state INTEGER,"
+ "length INTEGER" + "length INTEGER,"
+ "transferred INTEGER"
+ ");"; + ");";

View file

@ -7,7 +7,6 @@ import android.provider.MediaStore;
import android.support.v4.app.LoaderManager; import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader; import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader; import android.support.v4.content.Loader;
import android.content.BroadcastReceiver;
import android.content.*; import android.content.*;
import android.database.Cursor; import android.database.Cursor;
import android.os.Bundle; import android.os.Bundle;
@ -50,6 +49,8 @@ public class ContributionsActivity extends AuthenticatedActivity implements Load
private final static int SELECT_FROM_GALLERY = 1; private final static int SELECT_FROM_GALLERY = 1;
private final static int SELECT_FROM_CAMERA = 2; private final static int SELECT_FROM_CAMERA = 2;
private TextView progressUpdateTextView;
public ContributionsActivity() { public ContributionsActivity() {
super(WikiAccountAuthenticator.COMMONS_ACCOUNT_TYPE); super(WikiAccountAuthenticator.COMMONS_ACCOUNT_TYPE);
} }
@ -60,12 +61,17 @@ public class ContributionsActivity extends AuthenticatedActivity implements Load
private final int COLUMN_LOCALURI; private final int COLUMN_LOCALURI;
private final int COLUMN_STATE; private final int COLUMN_STATE;
private final int COLUMN_UPLOADED; private final int COLUMN_UPLOADED;
private final int COLUMN_TRANSFERRED;
private final int COLUMN_LENGTH;
public ContributionAdapter(Context context, Cursor c, int flags) { public ContributionAdapter(Context context, Cursor c, int flags) {
super(context, c, flags); super(context, c, flags);
COLUMN_FILENAME = c.getColumnIndex(Contribution.Table.COLUMN_FILENAME); COLUMN_FILENAME = c.getColumnIndex(Contribution.Table.COLUMN_FILENAME);
COLUMN_STATE = c.getColumnIndex(Contribution.Table.COLUMN_STATE); COLUMN_STATE = c.getColumnIndex(Contribution.Table.COLUMN_STATE);
COLUMN_LOCALURI = c.getColumnIndex(Contribution.Table.COLUMN_LOCAL_URI); COLUMN_LOCALURI = c.getColumnIndex(Contribution.Table.COLUMN_LOCAL_URI);
COLUMN_UPLOADED = c.getColumnIndex(Contribution.Table.COLUMN_UPLOADED); COLUMN_UPLOADED = c.getColumnIndex(Contribution.Table.COLUMN_UPLOADED);
COLUMN_LENGTH = c.getColumnIndex(Contribution.Table.COLUMN_LENGTH);
COLUMN_TRANSFERRED = c.getColumnIndex(Contribution.Table.COLUMN_TRANSFERRED);
} }
@Override @Override
@ -97,7 +103,11 @@ public class ContributionsActivity extends AuthenticatedActivity implements Load
stateView.setText(R.string.contribution_state_queued); stateView.setText(R.string.contribution_state_queued);
break; break;
case Contribution.STATE_IN_PROGRESS: case Contribution.STATE_IN_PROGRESS:
stateView.setText(R.string.contribution_state_in_progress); stateView.setText(R.string.contribution_state_starting);
long total = cursor.getLong(COLUMN_LENGTH);
long transferred = cursor.getLong(COLUMN_TRANSFERRED);
String stateString = String.format(getString(R.string.contribution_state_in_progress), (int)(((double)transferred / (double)total) * 100));
stateView.setText(stateString);
break; break;
case Contribution.STATE_FAILED: case Contribution.STATE_FAILED:
stateView.setText(R.string.contribution_state_failed); stateView.setText(R.string.contribution_state_failed);
@ -106,24 +116,20 @@ public class ContributionsActivity extends AuthenticatedActivity implements Load
} }
} }
private LocalBroadcastManager localBroadcastManager;
private GridView contributionsList; private GridView contributionsList;
private ContributionAdapter contributionsAdapter; private ContributionAdapter contributionsAdapter;
private DisplayImageOptions contributionDisplayOptions; private DisplayImageOptions contributionDisplayOptions;
private String[] broadcastsToReceive = {
UploadService.INTENT_CONTRIBUTION_STATE_CHANGED
};
private String[] CONTRIBUTIONS_PROJECTION = { private String[] CONTRIBUTIONS_PROJECTION = {
Contribution.Table.COLUMN_ID, Contribution.Table.COLUMN_ID,
Contribution.Table.COLUMN_FILENAME, Contribution.Table.COLUMN_FILENAME,
Contribution.Table.COLUMN_LOCAL_URI, Contribution.Table.COLUMN_LOCAL_URI,
Contribution.Table.COLUMN_STATE, Contribution.Table.COLUMN_STATE,
Contribution.Table.COLUMN_UPLOADED Contribution.Table.COLUMN_UPLOADED,
Contribution.Table.COLUMN_LENGTH,
Contribution.Table.COLUMN_TRANSFERRED
}; };
private String CONTRIBUTION_SELECTION = ""; private String CONTRIBUTION_SELECTION = "";
@ -137,26 +143,14 @@ public class ContributionsActivity extends AuthenticatedActivity implements Load
*/ */
private String CONTRIBUTION_SORT = Contribution.Table.COLUMN_STATE + " DESC, (" + Contribution.Table.COLUMN_TIMESTAMP + " * " + Contribution.Table.COLUMN_STATE + ")"; private String CONTRIBUTION_SORT = Contribution.Table.COLUMN_STATE + " DESC, (" + Contribution.Table.COLUMN_TIMESTAMP + " * " + Contribution.Table.COLUMN_STATE + ")";
private BroadcastReceiver messageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
}
};
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
for(int i=0; i < broadcastsToReceive.length; i++) {
localBroadcastManager.registerReceiver(messageReceiver, new IntentFilter(broadcastsToReceive[i]));
}
} }
@Override @Override
protected void onPause() { protected void onPause() {
super.onPause(); super.onPause();
for(int i=0; i < broadcastsToReceive.length; i++) {
localBroadcastManager.unregisterReceiver(messageReceiver);
}
} }
@Override @Override
@ -177,7 +171,6 @@ public class ContributionsActivity extends AuthenticatedActivity implements Load
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setTitle(R.string.title_activity_contributions); setTitle(R.string.title_activity_contributions);
localBroadcastManager = LocalBroadcastManager.getInstance(this);
setContentView(R.layout.activity_contributions); setContentView(R.layout.activity_contributions);
contributionsList = (GridView)findViewById(R.id.contributionsList); contributionsList = (GridView)findViewById(R.id.contributionsList);