Skip to content
Browse files

Added generic service provider

  • Loading branch information...
1 parent ec75a45 commit 84e3f9231fe4a53e40a81bb775822c0ae7c28795 Dale Harvey committed Jun 16, 2011
View
47 src/com/couchbase/libcouch/CouchDB.java
@@ -0,0 +1,47 @@
+package com.couchbase.libcouch;
+
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.content.ServiceConnection;
+import android.os.IBinder;
+import android.os.RemoteException;
+
+public class CouchDB {
+
+ private static String releaseUrl;
+ private static String releaseName;
+
+ private static ICouchService couchService;
+ private static ICouchClient couchClient;
+
+ /*
+ * This holds the connection to the CouchDB Service
+ */
+ private final static ServiceConnection mConnection = new ServiceConnection() {
+ @Override
+ public void onServiceConnected(ComponentName className, final IBinder service) {
+ try {
+ couchService = ICouchService.Stub.asInterface(service);
+ couchService.initCouchDB(couchClient, releaseUrl, releaseName);
+ } catch (RemoteException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ @Override
+ public void onServiceDisconnected(ComponentName className) {
+ couchService = null;
+ }
+ };
+
+ public static ServiceConnection getService(Context ctx, String url, String release, ICouchClient client) {
+ releaseUrl = url;
+ releaseName = release;
+ couchClient = client;
+ ctx.bindService(new Intent(ctx, CouchService.class), mConnection, Context.BIND_AUTO_CREATE);
+ return mConnection;
+ }
+
+}
View
10 src/com/couchbase/libcouch/CouchProcess.java
@@ -12,6 +12,8 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+import android.os.Handler;
+import android.os.Message;
import android.util.Log;
import com.google.ase.Exec;
@@ -30,7 +32,7 @@
private PrintStream out;
private BufferedReader in;
- public void start(String binary, String arg1, String arg2) {
+ public void start(String binary, String arg1, String arg2, final Handler handler) {
int[] pidbuffer = new int[1];
final FileDescriptor fd = Exec.createSubprocess(binary, arg1, arg2, pidbuffer);
@@ -48,12 +50,13 @@ public void run() {
if (line.contains("has started on")) {
started = true;
url = new URL(matchURLs(line).get(0));
- service.couchStarted();
+ Message.obtain(handler, CouchService.COUCH_STARTED, url)
+ .sendToTarget();
}
}
} catch (Exception e) {
Log.v(TAG, "CouchDB has stopped unexpectedly");
- e.printStackTrace();
+ Message.obtain(handler, CouchService.ERROR, e).sendToTarget();
}
}
}).start();
@@ -70,7 +73,6 @@ public void stop() {
started = false;
}
-
public String url() {
return url.toString();
}
View
32 src/com/couchbase/libcouch/CouchService.java
@@ -1,5 +1,9 @@
package com.couchbase.libcouch;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.net.URL;
+
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
@@ -14,6 +18,7 @@
public final static int ERROR = 0;
public final static int PROGRESS = 1;
public final static int COMPLETE = 2;
+ public final static int COUCH_STARTED = 5;
public final static int INSTALLING = 3;
public final static int INITIALIZING = 4;
@@ -27,18 +32,26 @@ public void handleMessage(Message msg) {
try {
switch (msg.what) {
case ERROR:
- client.progress(ERROR, 0, 0);
+ Exception e = (Exception) msg.obj;
+ StringWriter sw = new StringWriter();
+ e.printStackTrace(new PrintWriter(sw));
+ String stacktrace = sw.toString();
+ client.exit(stacktrace);
break;
case PROGRESS:
- client.progress(status, msg.arg1, msg.arg2);
+ client.installing(msg.arg1, msg.arg2);
break;
case COMPLETE:
if (status == INSTALLING) {
initCouch();
} else {
- couch.start("/system/bin/sh", CouchInstaller.couchPath() + "/bin/couchdb", "");
+ startCouch();
}
break;
+ case COUCH_STARTED:
+ URL url = (URL) msg.obj;
+ client.couchStarted(url.getHost(), url.getPort());
+ break;
}
} catch (RemoteException e) {
e.printStackTrace();
@@ -80,9 +93,7 @@ public IBinder onBind(Intent intent) {
@Override
public void initCouchDB(ICouchClient cb, final String url, final String pkg) throws RemoteException {
-
client = cb;
-
if (!CouchInstaller.checkInstalled(pkg)) {
installCouch(url, pkg);
} else if (!CouchInitializer.isEnvironmentInitialized()) {
@@ -91,7 +102,7 @@ public void initCouchDB(ICouchClient cb, final String url, final String pkg) thr
if (couch.started == true) {
couchStarted();
} else {
- couch.start("/system/bin/sh", CouchInstaller.couchPath() + "/bin/couchdb", "");
+ startCouch();
}
}
}
@@ -110,6 +121,10 @@ void couchStarted() throws RemoteException {
client.couchStarted(couch.url.getHost(), couch.url.getPort());
}
+ void startCouch() {
+ couch.start("/system/bin/sh", CouchInstaller.couchPath() + "/bin/couchdb", "", mHandler);
+ }
+
void installCouch(final String url, final String pkg) {
status = INSTALLING;
new Thread() {
@@ -118,11 +133,6 @@ public void run() {
try {
CouchInstaller.doInstall(url, pkg, mHandler);
} catch (Exception e) {
- try {
- client.progress(ERROR, 0, 0);
- } catch (RemoteException e1) {
- e1.printStackTrace();
- }
e.printStackTrace();
}
}

0 comments on commit 84e3f92

Please sign in to comment.
Something went wrong with that request. Please try again.