From 1d389344b440b946c653be588b5ff671a5834e48 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Wed, 21 Jul 2010 14:04:35 -0700 Subject: [PATCH] more upload work --- .../src/com/danga/camli/HostPort.java | 44 ++++++++ .../src/com/danga/camli/IUploadService.aidl | 4 +- .../src/com/danga/camli/UploadService.java | 29 ++++- .../src/com/danga/camli/UploadThread.java | 100 ++++++++++++++++++ 4 files changed, 173 insertions(+), 4 deletions(-) create mode 100644 clients/android/uploader/src/com/danga/camli/HostPort.java create mode 100644 clients/android/uploader/src/com/danga/camli/UploadThread.java diff --git a/clients/android/uploader/src/com/danga/camli/HostPort.java b/clients/android/uploader/src/com/danga/camli/HostPort.java new file mode 100644 index 000000000..ed57c38e3 --- /dev/null +++ b/clients/android/uploader/src/com/danga/camli/HostPort.java @@ -0,0 +1,44 @@ +package com.danga.camli; + +public class HostPort { + private final boolean mValid; + private final String mHost; + private final int mPort; + + public HostPort(String hostPort) { + String[] parts = hostPort.split(":"); + if (parts.length == 2) { + mHost = parts[0]; + mPort = new Integer(parts[1]).intValue(); + mValid = true; + } else if (parts.length > 2 || parts.length == 0) { + mValid = false; + mHost = null; + mPort = 0; + } else { + mValid = true; + mHost = hostPort; + mPort = 80; + } + } + + public int port() { + return mPort; + } + + public String host() { + return mHost; + } + + public boolean isValid() { + return mValid; + } + + @Override + public String toString() { + if (!mValid) { + return "[invalid HostPort]"; + } + return mHost + ":" + mPort; + } +} diff --git a/clients/android/uploader/src/com/danga/camli/IUploadService.aidl b/clients/android/uploader/src/com/danga/camli/IUploadService.aidl index 51bde65cd..a97a40426 100644 --- a/clients/android/uploader/src/com/danga/camli/IUploadService.aidl +++ b/clients/android/uploader/src/com/danga/camli/IUploadService.aidl @@ -11,5 +11,7 @@ interface IUploadService { void stop(); void resume(); - void addFile(in ParcelFileDescriptor pfd); + + // Returns false if server not configured. + boolean addFile(in ParcelFileDescriptor pfd); } diff --git a/clients/android/uploader/src/com/danga/camli/UploadService.java b/clients/android/uploader/src/com/danga/camli/UploadService.java index 526549908..37dc9a92e 100644 --- a/clients/android/uploader/src/com/danga/camli/UploadService.java +++ b/clients/android/uploader/src/com/danga/camli/UploadService.java @@ -4,6 +4,7 @@ import java.io.IOException; import android.app.Service; import android.content.Intent; +import android.content.SharedPreferences; import android.os.IBinder; import android.os.ParcelFileDescriptor; import android.os.RemoteException; @@ -19,7 +20,27 @@ public class UploadService extends Service { private final IUploadService.Stub service = new IUploadService.Stub() { - public void addFile(ParcelFileDescriptor pfd) throws RemoteException { + // Guarded by 'this': + private boolean mUploading = false; + private UploadThread mUploadThread = null; + + + public boolean addFile(ParcelFileDescriptor pfd) throws RemoteException { + SharedPreferences sp = getSharedPreferences(Preferences.NAME, 0); + HostPort hp = new HostPort(sp.getString(Preferences.HOST, "")); + if (!hp.isValid()) { + return false; + } + + String password = sp.getString(Preferences.PASSWORD, ""); + + synchronized (this) { + if (!mUploading) { + mUploading = true; + mUploadThread = new UploadThread(hp, password); + mUploadThread.start(); + } + } Log.d(TAG, "addFile for " + pfd + "; size=" + pfd.getStatSize()); try { pfd.close(); @@ -27,11 +48,13 @@ public class UploadService extends Service { // TODO Auto-generated catch block e.printStackTrace(); } + return true; } public boolean isUploading() throws RemoteException { - // TODO Auto-generated method stub - return false; + synchronized (this) { + return mUploading; + } } public void registerCallback(IStatusCallback cb) throws RemoteException { diff --git a/clients/android/uploader/src/com/danga/camli/UploadThread.java b/clients/android/uploader/src/com/danga/camli/UploadThread.java new file mode 100644 index 000000000..f349fd6ae --- /dev/null +++ b/clients/android/uploader/src/com/danga/camli/UploadThread.java @@ -0,0 +1,100 @@ +package com.danga.camli; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.ProtocolException; +import java.net.URL; +import java.util.concurrent.atomic.AtomicBoolean; + +import org.apache.http.client.HttpClient; +import org.apache.http.impl.client.DefaultHttpClient; + +import android.util.Log; + +public class UploadThread extends Thread { + private static final String TAG = "UploadThread"; + + private final HostPort mHostPort; + private final String mPassword; + + private final AtomicBoolean mStopRequested = new AtomicBoolean(false); + + public UploadThread(HostPort hp, String password) { + mHostPort = hp; + mPassword = password; + } + + public void stopPlease() { + mStopRequested.set(false); + } + + @Override + public void run() { + if (!mHostPort.isValid()) { + return; + } + Log.d(TAG, "Running UploadThread for " + mHostPort); + + URL url; + try { + url = new URL("http://" + mHostPort + "/camli/preupload"); + } catch (MalformedURLException e) { + Log.d(TAG, "Bogus URL:" + e); + return; + } + + HttpClient ua = new DefaultHttpClient(); + + HttpURLConnection conn; + try { + conn = (HttpURLConnection) url.openConnection(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return; + } + try { + conn.setRequestMethod("POST"); + } catch (ProtocolException e) { + Log.w(TAG, "Bogus method:" + e); + return; + } + conn.setDoInput(true); + conn.setDoOutput(true); + try { + conn.connect(); + } catch (IOException e) { + Log.w(TAG, "Connect error:" + e); + return; + } + + Log.d(TAG, "Connected!"); + try { + // read the result from the server + BufferedReader rd = new BufferedReader(new InputStreamReader(conn + .getInputStream())); + StringBuilder sb = new StringBuilder(); + String line; + + while ((line = rd.readLine()) != null) { + sb.append(line + '\n'); + Log.d(TAG, "Got line: " + line); + } + + Log.d(TAG, "Got response: " + sb); + + Log.d(TAG, "response status: " + conn.getResponseCode()); + Log.d(TAG, "response message: " + conn.getResponseMessage()); + + Object o = conn.getContent(); + Log.d(TAG, "Got object: " + o); + } catch (IOException e) { + Log.w(TAG, "IO error:" + e); + return; + } + conn.disconnect(); + } +}