Skip to content

Instantly share code, notes, and snippets.

@rajiv-singaseni
Created July 1, 2011 17:16
Show Gist options
  • Save rajiv-singaseni/1058991 to your computer and use it in GitHub Desktop.
Save rajiv-singaseni/1058991 to your computer and use it in GitHub Desktop.
An android activity which demonstrates uploding of data using Broadcaster and Receiver model. This works even when the upload doesnot happen in the same class.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<EditText android:id="@android:id/edit" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1"
android:text="12345" />
<Button android:id="@android:id/button1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="0"
android:text="Upload" android:onClick="onClick" />
</LinearLayout>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Progress" />
<ProgressBar android:max="100" android:id="@android:id/progress" style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<TextView android:id="@android:id/text1" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Upload status shows up here" />
</LinearLayout>
package com.webile.broadcasting;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends Activity {
ProgressBar progressBar;
TextView statusLabel;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
registerReceiver(new UploadBroadcastReceiver(), new IntentFilter(ACTION_FORM_UPLOAD));
progressBar = (ProgressBar)findViewById(android.R.id.progress);
statusLabel = (TextView) findViewById(android.R.id.text1);
}
public void onClick(View v) {
startUpload(12345);
}
private void startUpload(int formId) {
willStartUpload(formId);
}
/*
* Broadcast form upload start
*/
public static final String ACTION_FORM_UPLOAD = "form_upload";
public static final String EXTRA_FORM_ID = "form_id";
public static final String EXTRA_DOCUMENT_ID = "document_id";
public static final String EXTRA_PERCENTAGE_DONE = "percentage_done";
public static final String EXTRA_UPLOAD_STATUS = "upload_status";
public static final String EXTRA_REASON = "fail_reason";
public static final int STATUS_BEGIN = 0x1;
public static final int STATUS_RUNNING = 0x2;
public static final int STATUS_COMPLETE = 0x3;
public static final int STATUS_CANCELLED = 0x4;
private static final int TOTAL_DOCUMENTS = 10;
private void willStartUpload(final int formId) {
//TODO: broadcast that we are uploading the form.
Intent intent = new Intent(ACTION_FORM_UPLOAD);
intent.putExtra(EXTRA_FORM_ID, formId);
intent.putExtra(EXTRA_UPLOAD_STATUS, STATUS_BEGIN);
intent.putExtra(EXTRA_PERCENTAGE_DONE, (int)0);
sendBroadcast(intent);
uploadDocument(formId, 1);
}
/**
* A fake HTTP upload operation.
* @param formId
* @param documentId
*/
private void uploadDocument(final int formId, final int documentId) {
//TODO: broadcast that the document is being uploaded.
Intent intent = new Intent(ACTION_FORM_UPLOAD);
intent.putExtra(EXTRA_FORM_ID, formId);
intent.putExtra(EXTRA_UPLOAD_STATUS, STATUS_RUNNING);
intent.putExtra(EXTRA_DOCUMENT_ID, documentId);
intent.putExtra(EXTRA_PERCENTAGE_DONE, (int)(100 * (documentId-1) / TOTAL_DOCUMENTS));
sendBroadcast(intent);
//TODO: fake upload operation.
Handler handler = new Handler();
handler.postDelayed(new Runnable(){
public void run(){
doneUploadingDocument(formId, documentId);
}
}, 5000);
}
private void doneUploadingDocument(int formId, int documentId) {
Log.v("Upload", "Done uploading document: "+documentId);
//TODO: broadcast that the document is done uploading.
Intent intent = new Intent(ACTION_FORM_UPLOAD);
intent.putExtra(EXTRA_FORM_ID, formId);
intent.putExtra(EXTRA_DOCUMENT_ID, documentId);
intent.putExtra(EXTRA_UPLOAD_STATUS, STATUS_RUNNING);
intent.putExtra(EXTRA_PERCENTAGE_DONE, (int)(100 * documentId / TOTAL_DOCUMENTS));
sendBroadcast(intent);
if(documentId <= TOTAL_DOCUMENTS) {
uploadDocument(formId, documentId + 1);
} else {
didCompleteUpload(formId);
}
}
private void didCompleteUpload(int formId) {
//TODO: broadcast that the form got completed.
Intent intent = new Intent(ACTION_FORM_UPLOAD);
intent.putExtra(EXTRA_FORM_ID, formId);
intent.putExtra(EXTRA_UPLOAD_STATUS, STATUS_COMPLETE);
intent.putExtra(EXTRA_PERCENTAGE_DONE, (int)100);
sendBroadcast(intent);
}
private class UploadBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "UploadBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
int formId = intent.getIntExtra(EXTRA_FORM_ID, -1);
int formStatus = intent.getIntExtra(EXTRA_UPLOAD_STATUS, -1);
int documentId = intent.getIntExtra(EXTRA_DOCUMENT_ID, -1);
int percentageDone = intent.getIntExtra(EXTRA_PERCENTAGE_DONE, 1);
Log.v(TAG, "OnReceive formId: "+formId+" document: "+documentId+" percentageDone: "+percentageDone);
switch(formStatus) {
case STATUS_BEGIN:
startedUpload(formId);
break;
case STATUS_RUNNING:
uploadInProgress(formId,documentId, percentageDone);
break;
case STATUS_COMPLETE:
doneWithUpload(formId);
break;
case STATUS_CANCELLED:
break;
}
}
private void startedUpload(int formId) {
progressBar.setProgress(0);
String statusMessage = "Started uploading form #"+formId;
statusLabel.setText(statusMessage);
}
private void uploadInProgress(int formId, int documentId, int percentage) {
progressBar.setProgress(percentage);
statusLabel.setText("Done uploading document "+documentId+" of form #"+formId);
}
private void doneWithUpload(int formId) {
progressBar.setProgress(progressBar.getMax());
String statusMessage = "Done uploading form #"+formId;
statusLabel.setText(statusMessage);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment