From c731e964fdd15293bf383f834f6a9b6474441e5a Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Mon, 19 Jul 2010 17:10:52 -0700 Subject: [PATCH] start of UploadService --- clients/android/uploader/AndroidManifest.xml | 4 ++ .../src/com/danga/camli/CamliActivity.java | 59 +++++++++++++++++++ .../src/com/danga/camli/IStatusCallback.aidl | 6 ++ .../src/com/danga/camli/IUploadService.aidl | 15 +++++ .../src/com/danga/camli/UploadService.java | 49 ++++++++++++++- 5 files changed, 130 insertions(+), 3 deletions(-) create mode 100644 clients/android/uploader/src/com/danga/camli/IStatusCallback.aidl create mode 100644 clients/android/uploader/src/com/danga/camli/IUploadService.aidl diff --git a/clients/android/uploader/AndroidManifest.xml b/clients/android/uploader/AndroidManifest.xml index 45e582042..d0df04acd 100644 --- a/clients/android/uploader/AndroidManifest.xml +++ b/clients/android/uploader/AndroidManifest.xml @@ -9,6 +9,10 @@ + + diff --git a/clients/android/uploader/src/com/danga/camli/CamliActivity.java b/clients/android/uploader/src/com/danga/camli/CamliActivity.java index c8c915b93..a866b7baa 100644 --- a/clients/android/uploader/src/com/danga/camli/CamliActivity.java +++ b/clients/android/uploader/src/com/danga/camli/CamliActivity.java @@ -6,11 +6,16 @@ import java.io.FileNotFoundException; import java.io.IOException; import android.app.Activity; +import android.content.ComponentName; import android.content.ContentResolver; +import android.content.Context; import android.content.Intent; +import android.content.ServiceConnection; import android.net.Uri; import android.os.Bundle; +import android.os.IBinder; import android.os.ParcelFileDescriptor; +import android.os.RemoteException; import android.util.Log; import android.view.Menu; import android.view.MenuItem; @@ -19,6 +24,37 @@ public class CamliActivity extends Activity { private static final String TAG = "CamliActivity"; private static final int MENU_SETTINGS = 1; + private IUploadService serviceStub = null; + + private IStatusCallback statusCallback = new IStatusCallback.Stub() { + public void logToClient(String stuff) throws RemoteException { + Log.d(TAG, "From service: " + stuff); + } + + public void onUploadStatusChange(boolean uploading) + throws RemoteException { + Log.d(TAG, "upload status change: " + uploading); + } + }; + + private final ServiceConnection serviceConnection = new ServiceConnection() { + + public void onServiceConnected(ComponentName name, IBinder service) { + serviceStub = IUploadService.Stub.asInterface(service); + Log.d(TAG, "Service connected"); + try { + serviceStub.registerCallback(statusCallback); + } catch (RemoteException e) { + e.printStackTrace(); + } + } + + public void onServiceDisconnected(ComponentName name) { + Log.d(TAG, "Service disconnected"); + serviceStub = null; + }; + }; + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -57,12 +93,24 @@ public class CamliActivity extends Activity { @Override protected void onPause() { super.onPause(); + try { + if (serviceStub != null) + serviceStub.unregisterCallback(statusCallback); + } catch (RemoteException e) { + // Ignore. + } + if (serviceConnection != null) { + unbindService(serviceConnection); + } } @Override protected void onResume() { super.onResume(); + bindService(new Intent(this, UploadService.class), serviceConnection, + Context.BIND_AUTO_CREATE); + Intent intent = getIntent(); String action = intent.getAction(); Log.d(TAG, "onResume; action=" + action); @@ -99,6 +147,11 @@ public class CamliActivity extends Activity { } private void startDownloadOfUri(Uri uri) { + if (serviceStub == null) { + Log.d(TAG, "serviceStub is null in startDownloadOfUri"); + return; + } + Log.d(TAG, "startDownloadOf: " + uri); ContentResolver cr = getContentResolver(); ParcelFileDescriptor pfd = null; @@ -109,6 +162,12 @@ public class CamliActivity extends Activity { return; } Log.d(TAG, "opened parcel fd = " + pfd); + try { + serviceStub.addFile(pfd); + } catch (RemoteException e) { + Log.d(TAG, "failure to enqueue upload", e); + } + FileDescriptor fd = pfd.getFileDescriptor(); FileInputStream fis = new FileInputStream(fd); diff --git a/clients/android/uploader/src/com/danga/camli/IStatusCallback.aidl b/clients/android/uploader/src/com/danga/camli/IStatusCallback.aidl new file mode 100644 index 000000000..23798c281 --- /dev/null +++ b/clients/android/uploader/src/com/danga/camli/IStatusCallback.aidl @@ -0,0 +1,6 @@ +package com.danga.camli; + +oneway interface IStatusCallback { + void logToClient(String stuff); + void onUploadStatusChange(boolean uploading); +} diff --git a/clients/android/uploader/src/com/danga/camli/IUploadService.aidl b/clients/android/uploader/src/com/danga/camli/IUploadService.aidl new file mode 100644 index 000000000..51bde65cd --- /dev/null +++ b/clients/android/uploader/src/com/danga/camli/IUploadService.aidl @@ -0,0 +1,15 @@ +package com.danga.camli; + +import com.danga.camli.IStatusCallback; +import android.os.ParcelFileDescriptor; + +interface IUploadService { + void registerCallback(IStatusCallback cb); + void unregisterCallback(IStatusCallback cb); + + boolean isUploading(); + + void stop(); + void resume(); + void 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 5caa22061..526549908 100644 --- a/clients/android/uploader/src/com/danga/camli/UploadService.java +++ b/clients/android/uploader/src/com/danga/camli/UploadService.java @@ -1,15 +1,58 @@ package com.danga.camli; +import java.io.IOException; + import android.app.Service; import android.content.Intent; import android.os.IBinder; +import android.os.ParcelFileDescriptor; +import android.os.RemoteException; +import android.util.Log; public class UploadService extends Service { + private static final String TAG = "UploadService"; @Override - public IBinder onBind(Intent arg0) { - // TODO Auto-generated method stub - return null; + public IBinder onBind(Intent intent) { + return service; } + private final IUploadService.Stub service = new IUploadService.Stub() { + + public void addFile(ParcelFileDescriptor pfd) throws RemoteException { + Log.d(TAG, "addFile for " + pfd + "; size=" + pfd.getStatSize()); + try { + pfd.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public boolean isUploading() throws RemoteException { + // TODO Auto-generated method stub + return false; + } + + public void registerCallback(IStatusCallback cb) throws RemoteException { + // TODO Auto-generated method stub + + } + + public void resume() throws RemoteException { + // TODO Auto-generated method stub + + } + + public void stop() throws RemoteException { + // TODO Auto-generated method stub + + } + + public void unregisterCallback(IStatusCallback cb) + throws RemoteException { + // TODO Auto-generated method stub + + } + }; }