start of UploadService

This commit is contained in:
Brad Fitzpatrick 2010-07-19 17:10:52 -07:00
parent 542216b0f6
commit c731e964fd
5 changed files with 130 additions and 3 deletions

View File

@ -9,6 +9,10 @@
<application android:icon="@drawable/icon" android:label="@string/app_name">
<service android:name=".UploadService"
android:exported="false"
android:label="Camlistore Upload Service" />
<activity android:name=".CamliActivity"
android:label="@string/app_name">
<intent-filter>

View File

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

View File

@ -0,0 +1,6 @@
package com.danga.camli;
oneway interface IStatusCallback {
void logToClient(String stuff);
void onUploadStatusChange(boolean uploading);
}

View File

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

View File

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