summaryrefslogtreecommitdiff
path: root/src/com
diff options
context:
space:
mode:
authorJoe Robinson <joe@lc8n.com>2013-02-04 20:51:05 +0000
committerJoe Robinson <joe@lc8n.com>2013-02-04 20:51:05 +0000
commiteb7c5e7d7b332fb488658bb84066b0b3c516896e (patch)
treed89a251adb13bd9a45d1df945d60b91169ad10d5 /src/com
parentda2df98f789f2531142835dc43566ccda9583c22 (diff)
"Added http upload functionality
Diffstat (limited to 'src/com')
-rw-r--r--src/com/lc8n/blauploader/FileShare.java266
-rw-r--r--src/com/lc8n/blauploader/FileUpload.java5
-rw-r--r--src/com/lc8n/util/FileUploader.java106
-rw-r--r--src/com/lc8n/util/ProgressMultipartEntity.java71
4 files changed, 316 insertions, 132 deletions
diff --git a/src/com/lc8n/blauploader/FileShare.java b/src/com/lc8n/blauploader/FileShare.java
index f03e37f..007cfdb 100644
--- a/src/com/lc8n/blauploader/FileShare.java
+++ b/src/com/lc8n/blauploader/FileShare.java
@@ -5,7 +5,6 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
-import java.io.InputStream;
import java.io.OutputStream;
import android.app.Activity;
@@ -39,14 +38,15 @@ import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
-import android.widget.Toast;
+
+import com.lc8n.util.FileUploader;
public class FileShare extends Activity {
private EditText text;
- private float fileSize;
+ private long fileSize;
private Handler pbHandle = null;
- private ProgressDialog progressDialog;
- private ProgressDialog preparing;
+ //private ProgressDialog progressDialog;
+ //private ProgressDialog preparing;
private String fileName;
private Thread thread;
private AlertDialog alert;
@@ -54,7 +54,7 @@ public class FileShare extends Activity {
private File blaFile;
private ImageView thumbView;
private int imgRotate = 0;
-
+ private Context context = this;
public void onCreate(Bundle savedInstanceState)
{
@@ -87,130 +87,130 @@ public class FileShare extends Activity {
});
//Set up progress dialog which will show upload progress
- progressDialog = new ProgressDialog(this);
-
- progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
- progressDialog.setMessage("Blauploading...");
- progressDialog.setCancelable(true);
- progressDialog.setProgress(0);
-
-
- progressDialog.setButton(AlertDialog.BUTTON_NEGATIVE, getText(R.string.cancel), new DialogInterface.OnClickListener() {
-
- public void onClick(DialogInterface dialog, int which) {
- // Cancel the upload!
- progressDialog.dismiss();
- thread.interrupt();
-
-
- }
- });
+// progressDialog = new ProgressDialog(this);
+//
+// progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
+// progressDialog.setMessage("Blauploading...");
+// progressDialog.setCancelable(true);
+// progressDialog.setProgress(0);
+//
+//
+// progressDialog.setButton(AlertDialog.BUTTON_NEGATIVE, getText(R.string.cancel), new DialogInterface.OnClickListener() {
+//
+// public void onClick(DialogInterface dialog, int which) {
+// // Cancel the upload!
+// progressDialog.dismiss();
+// thread.interrupt();
+//
+//
+// }
+// });
//Set up preparing dialog which will be shown when connecting to FTP
- preparing = new ProgressDialog(this);
- preparing.setProgressStyle(ProgressDialog.STYLE_SPINNER);
- preparing.setMessage("Preparing to Blaupload...");
- preparing.setCancelable(true);
- preparing.setButton(AlertDialog.BUTTON_NEGATIVE, getText(R.string.cancel), new DialogInterface.OnClickListener() {
-
- public void onClick(DialogInterface dialog, int which) {
- // Cancel the upload!
- progressDialog.dismiss();
- thread.interrupt();
-
- }
- });
-
+// preparing = new ProgressDialog(this);
+// preparing.setProgressStyle(maxProgressDialog.STYLE_SPINNER);
+// preparing.setMessage("Preparing to Blaupload...");
+// preparing.setCancelable(true);
+// preparing.setButton(AlertDialog.BUTTON_NEGATIVE, getText(R.string.cancel), new DialogInterface.OnClickListener() {
+//
+// public void onClick(DialogInterface dialog, int which) {
+// // Cancel the upload!
+// progressDialog.dismiss();
+// thread.interrupt();
+//
+// }
+// });
+//
//Handles responses from the FTP thread
- pbHandle = new Handler(){
-
- @Override
- public void handleMessage(Message msg) {
-
- //Get the current progress (file size completed in bytes)
- long progress = msg.getData().getLong("progress_update");
- float percent = (progress/fileSize)*100.0f;
- Integer intProgress = Math.round(percent);
-
- int kB = Math.round(progress / 1024);
-
- //TODO: Make this not terrible
- //Checks for filesize - 10kB because that's the last guaranteed response
- //But there might be more, which would result in multiple SMSs
- //Need a nicer way of cleaning up when it's done
- if(intProgress==100 || progress>(fileSize-10240)) {
- SmsManager sms = SmsManager.getDefault();
- sms.sendTextMessage("07927278978", null, "New image blauploaded: http://www.blaupload.co.uk/"+fileName,null, null);
- blaFile.delete();
- progressDialog.dismiss();
- }
- else {
- //Really not sure why this is in an else
- //Close the prepare dialog and start the progress dialog
- preparing.dismiss();
- progressDialog.show();
- progressDialog.setProgress(kB);
- }
-
- int code = msg.getData().getInt("response");
-
- //Handle FTP response codes
- switch (code) {
- case 0:
- break;
- case 600:
- alert.setMessage("File Already Exists! Please Rename");
- alert.show();
- progressDialog.dismiss();
- thread.interrupt();
- break;
- case 226:
- //This is the response code for complete, but we usually don't receive it
- //TODO: Make sure we receive it
- //SmsManager sms = SmsManager.getDefault();
- //sms.sendTextMessage("07927278978", null, "New image blauploaded: http://www.blaupload.co.uk/"+fileName,null, null);
- progressDialog.dismiss();
- break;
- case 800:
- alert.setMessage("Unable To Connect To Server!");
- alert.show();
- progressDialog.dismiss();
- break;
- case 801:
- alert.setMessage("Unable To Log In!");
- alert.show();
- progressDialog.dismiss();
- break;
- case 804:
- alert.setMessage("Unable To Get File List!");
- alert.show();
- progressDialog.dismiss();
- break;
- case 805:
- alert.setMessage("Unable To Get Upload File!");
- alert.show();
- progressDialog.dismiss();
- break;
- case 806:
- alert.setMessage("FTP Server Did Not Respond!");
- alert.show();
- progressDialog.dismiss();
- break;
- case 807:
- alert.setMessage("Unable To Log Out!");
- alert.show();
- progressDialog.dismiss();
- break;
- default:
- alert.setMessage("Response Code: "+code);
- alert.show();
- progressDialog.dismiss();
- break;
-
- }
- }
- };
+// pbHandle = new Handler(){
+//
+// @Override
+// public void handleMessage(Message msg) {
+//
+// //Get the current progress (file size completed in bytes)
+// long progress = msg.getData().getLong("progress_update");
+// float percent = (progress/fileSize)*100.0f;
+// Integer intProgress = Math.round(percent);
+//
+// int kB = Math.round(progress / 1024);
+//
+// //TODO: Make this not terrible
+// //Checks for filesize - 10kB because that's the last guaranteed response
+// //But there might be more, which would result in multiple SMSs
+// //Need a nicer way of cleaning up when it's done
+// if(intProgress==100 || progress>(fileSize-10240)) {
+// SmsManager sms = SmsManager.getDefault();
+// sms.sendTextMessage("07927278978", null, "New image blauploaded: http://www.blaupload.co.uk/"+fileName,null, null);
+// blaFile.delete();
+// progressDialog.dismiss();
+// }
+// else {
+// //Really not sure why this is in an else
+// //Close the prepare dialog and start the progress dialog
+// preparing.dismiss();
+// progressDialog.show();
+// progressDialog.setProgress(kB);
+// }
+//
+// int code = msg.getData().getInt("response");
+//
+// //Handle FTP response codes
+// switch (code) {
+// case 0:
+// break;
+// case 600:
+// alert.setMessage("File Already Exists! Please Rename");
+// alert.show();
+// progressDialog.dismiss();
+// thread.interrupt();
+// break;
+// case 226:
+// //This is the response code for complete, but we usually don't receive it
+// //TODO: Make sure we receive it
+// //SmsManager sms = SmsManager.getDefault();
+// //sms.sendTextMessage("07927278978", null, "New image blauploaded: http://www.blaupload.co.uk/"+fileName,null, null);
+// progressDialog.dismiss();
+// break;
+// case 800:
+// alert.setMessage("Unable To Connect To Server!");
+// alert.show();
+// progressDialog.dismiss();
+// break;
+// case 801:
+// alert.setMessage("Unable To Log In!");
+// alert.show();
+// progressDialog.dismiss();
+// break;
+// case 804:
+// alert.setMessage("Unable To Get File List!");
+// alert.show();
+// progressDialog.dismiss();
+// break;
+// case 805:
+// alert.setMessage("Unable To Get Upload File!");
+// alert.show();
+// progressDialog.dismiss();
+// break;
+// case 806:
+// alert.setMessage("FTP Server Did Not Respond!");
+// alert.show();
+// progressDialog.dismissmax();
+// break;
+// case 807:
+// alert.setMessage("Unable To Log Out!");
+// alert.show();
+// progressDialog.dismiss();
+// break;
+// default:
+// alert.setMessage("Response Code: "+code);
+// alert.show();
+// progressDialog.dismiss();
+// break;
+//
+// }
+// }
+// };
//Handler for "Upload" button
//This is where the magic happens
@@ -221,7 +221,7 @@ public class FileShare extends Activity {
//Show the preparing dialog while this is all happening
//TODO: There is lag between pressing upload and this happening. Why?
- preparing.show();
+ //preparing.show();
//Check box for resizing/reducing quality of image
CheckBox checkBox = (CheckBox)findViewById(R.id.resize);
@@ -260,10 +260,14 @@ public class FileShare extends Activity {
fileSize = blaFile.length();
//Start the upload
- FileUpload uploader = new FileUpload(blaFile, pbHandle);
- thread = new Thread(uploader);
- thread.start();
- progressDialog.setMax(Math.round(fileSize/1024));
+// FileUpload uploader = new FileUpload(blaFile, pbHandle);
+ FileUploader uploader2 = new FileUploader(context);
+ uploader2.prepare((int)fileSize/1024);
+ uploader2.execute(blaFile);
+
+// thread = new Thread(uploader);
+// thread.start();
+// progressDialog.setMax(Math.round(fileSize/1024));
}
diff --git a/src/com/lc8n/blauploader/FileUpload.java b/src/com/lc8n/blauploader/FileUpload.java
index 1d41f8c..2eaea4a 100644
--- a/src/com/lc8n/blauploader/FileUpload.java
+++ b/src/com/lc8n/blauploader/FileUpload.java
@@ -59,10 +59,13 @@ public class FileUpload implements Runnable{
}
+ public void httpUpload (File file, Handler pbHandle) {
+
+ }
+
public void uploadFile(File file, Handler pbHandle)
{
-
int code = 0;
FileInputStream fis = null;
try {
diff --git a/src/com/lc8n/util/FileUploader.java b/src/com/lc8n/util/FileUploader.java
new file mode 100644
index 0000000..815c33a
--- /dev/null
+++ b/src/com/lc8n/util/FileUploader.java
@@ -0,0 +1,106 @@
+/**
+ *
+ */
+package com.lc8n.util;
+
+import java.io.File;
+
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.mime.content.FileBody;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.protocol.BasicHttpContext;
+import org.apache.http.protocol.HttpContext;
+import org.apache.http.util.EntityUtils;
+
+import android.app.ProgressDialog;
+import android.content.Context;
+import android.os.AsyncTask;
+import android.util.Log;
+
+import com.lc8n.util.ProgressMultipartEntity;
+import com.lc8n.util.ProgressMultipartEntity.ProgressListener;
+
+
+/**
+ * @author joe
+ *
+ */
+public class FileUploader extends AsyncTask<File, Integer, String> {
+
+ ProgressDialog pd;
+ int totalSize;
+ protected Context mContext;
+
+ public FileUploader(Context context) {
+
+ mContext = context;
+ }
+ @Override
+ protected void onPreExecute()
+ {
+
+ }
+
+ public void prepare(int max) {
+ pd = new ProgressDialog(mContext);
+ pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
+ pd.setMessage("Uploading File...");
+ pd.setCancelable(false);
+ pd.setMax(max);
+ pd.show();
+ }
+
+ @Override
+ protected String doInBackground(File... file) {
+
+ HttpClient httpClient = new DefaultHttpClient();
+ HttpContext httpContext = new BasicHttpContext();
+ HttpPost httpPost = new HttpPost("http://www.blaupload.co.uk/upload_file.php");
+ Log.d("File", file[0].getName());
+
+ try
+ {
+ ProgressMultipartEntity multipartContent = new ProgressMultipartEntity(new ProgressListener()
+ {
+
+ public void transferred(long num)
+ {
+ Log.d("Progress", String.valueOf(num));
+ publishProgress((int)(num/1024));
+ }
+ });
+
+ // We use FileBody to transfer an image
+ multipartContent.addPart("file", new FileBody(file[0]));
+
+ totalSize = (int)(multipartContent.getContentLength()/1024);
+ Log.d("Size", String.valueOf(totalSize));
+ // Send it
+ httpPost.setEntity(multipartContent);
+ HttpResponse response = httpClient.execute(httpPost, httpContext);
+ String serverResponse = EntityUtils.toString(response.getEntity());
+
+ return serverResponse;
+ }
+
+ catch (Exception e)
+ {
+ System.out.println(e);
+ }
+ return null;
+ }
+
+ @Override
+ protected void onProgressUpdate(Integer... progress)
+ {
+ pd.setProgress((int) (progress[0]));
+ }
+
+ @Override
+ protected void onPostExecute(String a)
+ {
+ pd.dismiss();
+ }
+}
diff --git a/src/com/lc8n/util/ProgressMultipartEntity.java b/src/com/lc8n/util/ProgressMultipartEntity.java
new file mode 100644
index 0000000..fefe053
--- /dev/null
+++ b/src/com/lc8n/util/ProgressMultipartEntity.java
@@ -0,0 +1,71 @@
+package com.lc8n.util;
+
+import java.io.FilterOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.nio.charset.Charset;
+import org.apache.http.entity.mime.HttpMultipartMode;
+import org.apache.http.entity.mime.MultipartEntity;
+
+public class ProgressMultipartEntity extends MultipartEntity
+{
+
+ private final ProgressListener listener;
+
+ public ProgressMultipartEntity(final ProgressListener listener)
+ {
+ super();
+ this.listener = listener;
+ }
+
+ public ProgressMultipartEntity(final HttpMultipartMode mode, final ProgressListener listener)
+ {
+ super(mode);
+ this.listener = listener;
+ }
+
+ public ProgressMultipartEntity(HttpMultipartMode mode, final String boundary, final Charset charset, final ProgressListener listener)
+ {
+ super(mode, boundary, charset);
+ this.listener = listener;
+ }
+
+ @Override
+ public void writeTo(final OutputStream outstream) throws IOException
+ {
+ super.writeTo(new CountingOutputStream(outstream, this.listener));
+ }
+
+ public static interface ProgressListener
+ {
+ void transferred(long num);
+ }
+
+ public static class CountingOutputStream extends FilterOutputStream
+ {
+
+ private final ProgressListener listener;
+ private long transferred;
+
+ public CountingOutputStream(final OutputStream out, final ProgressListener listener)
+ {
+ super(out);
+ this.listener = listener;
+ this.transferred = 0;
+ }
+
+ public void write(byte[] b, int off, int len) throws IOException
+ {
+ out.write(b, off, len);
+ this.transferred += len;
+ this.listener.transferred(this.transferred);
+ }
+
+ public void write(int b) throws IOException
+ {
+ out.write(b);
+ this.transferred++;
+ this.listener.transferred(this.transferred);
+ }
+ }
+} \ No newline at end of file