more upload work

This commit is contained in:
Brad Fitzpatrick 2010-07-21 14:04:35 -07:00
parent 724b32beaa
commit 1d389344b4
4 changed files with 173 additions and 4 deletions

View File

@ -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;
}
}

View File

@ -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);
}

View File

@ -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 {

View File

@ -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();
}
}