Skip to content

Instantly share code, notes, and snippets.

@rajiv-singaseni
Created July 2, 2011 16:12
Show Gist options
  • Save rajiv-singaseni/1061003 to your computer and use it in GitHub Desktop.
Save rajiv-singaseni/1061003 to your computer and use it in GitHub Desktop.
Advanced broadcasting
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.webile.broadcasting"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity2"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
</manifest>
package com.webile.broadcasting;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.util.Log;
public class FormUploader {
private Context mContext;
private static final String TAG = "FormUploader";
private boolean shouldStop = false;
public FormUploader(Context context) {
this.mContext = context;
mContext.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.v(TAG, "Trying to stop upload");
shouldStop = true;
}
}, new IntentFilter(FormUploader.ACTION_STOP_UPLOAD));
}
public void uploadForm(int formId) {
willStartUpload(formId);
}
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 = 250;
private static final int FAKE_OPERATION_TIME = 200; //milli seconds.
private static Intent stickyIntent;
/*
* Broadcast form upload start
*/
private void willStartUpload(final int formId) {
// TODO: broadcast that we are uploading the form.
stickyIntent = new Intent(ACTION_FORM_UPLOAD);
stickyIntent.putExtra(EXTRA_FORM_ID, formId);
stickyIntent.putExtra(EXTRA_UPLOAD_STATUS, STATUS_BEGIN);
stickyIntent.putExtra(EXTRA_PERCENTAGE_DONE, (int) 0);
mContext.sendStickyBroadcast(stickyIntent);
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) / TOTAL_DOCUMENTS));
// mContext.sendBroadcast(intent);
if (shouldStop) {
//user wants to stop.
stopUploadingFormId(formId);
} else {
// TODO: fake upload operation.
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
doneUploadingDocument(formId, documentId);
}
}, FAKE_OPERATION_TIME);
}
}
private void stopUploadingFormId(int formId) {
Intent intent = new Intent(ACTION_FORM_UPLOAD);
intent.putExtra(EXTRA_FORM_ID, formId);
intent.putExtra(EXTRA_UPLOAD_STATUS, STATUS_CANCELLED);
intent.putExtra(EXTRA_REASON, "User cancelled upload");
mContext.sendBroadcast(intent);
doCleanup();
}
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));
mContext.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);
mContext.sendBroadcast(intent);
doCleanup();
}
private void doCleanup() {
// TODO: remove the sticky intent.
mContext.removeStickyBroadcast(stickyIntent);
}
public static final String ACTION_STOP_UPLOAD = "stop_upload";
}
<?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="startUpload" />
</LinearLayout>
<Button android:id="@android:id/button2" android:text="Stop current upload"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:enabled="false" android:onClick="stopUpload" />
<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.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends Activity {
ProgressBar progressBar;
TextView statusLabel;
Button stopButton;
Button startButton;
private static String TAG = "MainActivity";
/** 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(
FormUploader.ACTION_FORM_UPLOAD));
progressBar = (ProgressBar) findViewById(android.R.id.progress);
statusLabel = (TextView) findViewById(android.R.id.text1);
stopButton = (Button)findViewById(android.R.id.button2);
startButton = (Button)findViewById(android.R.id.button1);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(super.onOptionsItemSelected(item)) return true;
Intent intent = new Intent(this, MainActivity2.class);
startActivity(intent);
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add("The other activity");
return true;
}
public void startUpload(View v) {
new FormUploader(this).uploadForm(12345);
}
public void stopUpload(View v) {
//TODO: send a broadcast asking the formuploader to stop itself.
this.sendBroadcast(new Intent(FormUploader.ACTION_STOP_UPLOAD));
Log.v(TAG,"Requesting for stop upload");
}
private class UploadBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "UploadBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
int formId = intent.getIntExtra(FormUploader.EXTRA_FORM_ID, -1);
int formStatus = intent.getIntExtra(
FormUploader.EXTRA_UPLOAD_STATUS, -1);
int documentId = intent.getIntExtra(FormUploader.EXTRA_DOCUMENT_ID,
-1);
int percentageDone = intent.getIntExtra(
FormUploader.EXTRA_PERCENTAGE_DONE, 1);
Log.v(TAG, "OnReceive formId: " + formId + " document: "
+ documentId + " percentageDone: " + percentageDone);
switch (formStatus) {
case FormUploader.STATUS_BEGIN:
startedUpload(formId);
break;
case FormUploader.STATUS_RUNNING:
uploadInProgress(formId, documentId, percentageDone);
break;
case FormUploader.STATUS_COMPLETE:
doneWithUpload(formId);
break;
case FormUploader.STATUS_CANCELLED:
String reason = intent.getStringExtra(FormUploader.EXTRA_REASON);
cancelledUpload(formId,reason);
break;
}
}
private void startedUpload(int formId) {
progressBar.setProgress(0);
String statusMessage = "Stop uploading #" + formId;
stopButton.setText(statusMessage);
stopButton.setEnabled(true);
progressBar.setEnabled(true);
}
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);
stopButton.setText("No running uploads");
stopButton.setEnabled(false);
}
private void cancelledUpload(int formId, String reason) {
statusLabel.setText(reason);
stopButton.setText("No running uploads");
stopButton.setEnabled(false);
progressBar.setEnabled(false);
}
}
}
package com.webile.broadcasting;
import android.view.Menu;
public class MainActivity2 extends MainActivity {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment