Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Service support

  • Loading branch information...
commit c8d075a89d67b8e701d81e2965c44766da1e90bf 1 parent 667d1d9
Aaron Miller apage43 authored
2  .gitignore
View
@@ -0,0 +1,2 @@
+bin
+libs/armeabi
4 AndroidManifest.xml
View
@@ -13,7 +13,9 @@
</activity>
<activity android:name="CouchInstallActivity"></activity>
-<service android:name="CouchService" android:exported="true" android:enabled="true"></service>
+<service android:name="CouchService" android:exported="true" android:enabled="true"><intent-filter><action android:name="org.couchdb.android.COUCHDB_SERVICE"></action>
+</intent-filter>
+</service>
</application>
<uses-sdk android:minSdkVersion="7" />
92 gen/org/couchdb/android/ICouchService.java
View
@@ -0,0 +1,92 @@
+/*
+ * This file is auto-generated. DO NOT MODIFY.
+ * Original file: /Users/apage43/Documents/workspace/CouchDB/src/org/couchdb/android/ICouchService.aidl
+ */
+package org.couchdb.android;
+public interface ICouchService extends android.os.IInterface
+{
+/** Local-side IPC implementation stub class. */
+public static abstract class Stub extends android.os.Binder implements org.couchdb.android.ICouchService
+{
+private static final java.lang.String DESCRIPTOR = "org.couchdb.android.ICouchService";
+/** Construct the stub at attach it to the interface. */
+public Stub()
+{
+this.attachInterface(this, DESCRIPTOR);
+}
+/**
+ * Cast an IBinder object into an org.couchdb.android.ICouchService interface,
+ * generating a proxy if needed.
+ */
+public static org.couchdb.android.ICouchService asInterface(android.os.IBinder obj)
+{
+if ((obj==null)) {
+return null;
+}
+android.os.IInterface iin = (android.os.IInterface)obj.queryLocalInterface(DESCRIPTOR);
+if (((iin!=null)&&(iin instanceof org.couchdb.android.ICouchService))) {
+return ((org.couchdb.android.ICouchService)iin);
+}
+return new org.couchdb.android.ICouchService.Stub.Proxy(obj);
+}
+public android.os.IBinder asBinder()
+{
+return this;
+}
+@Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException
+{
+switch (code)
+{
+case INTERFACE_TRANSACTION:
+{
+reply.writeString(DESCRIPTOR);
+return true;
+}
+case TRANSACTION_getPort:
+{
+data.enforceInterface(DESCRIPTOR);
+int _result = this.getPort();
+reply.writeNoException();
+reply.writeInt(_result);
+return true;
+}
+}
+return super.onTransact(code, data, reply, flags);
+}
+private static class Proxy implements org.couchdb.android.ICouchService
+{
+private android.os.IBinder mRemote;
+Proxy(android.os.IBinder remote)
+{
+mRemote = remote;
+}
+public android.os.IBinder asBinder()
+{
+return mRemote;
+}
+public java.lang.String getInterfaceDescriptor()
+{
+return DESCRIPTOR;
+}
+public int getPort() throws android.os.RemoteException
+{
+android.os.Parcel _data = android.os.Parcel.obtain();
+android.os.Parcel _reply = android.os.Parcel.obtain();
+int _result;
+try {
+_data.writeInterfaceToken(DESCRIPTOR);
+mRemote.transact(Stub.TRANSACTION_getPort, _data, _reply, 0);
+_reply.readException();
+_result = _reply.readInt();
+}
+finally {
+_reply.recycle();
+_data.recycle();
+}
+return _result;
+}
+}
+static final int TRANSACTION_getPort = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
+}
+public int getPort() throws android.os.RemoteException;
+}
4 src/org/couchdb/android/CouchDB.java
View
@@ -57,7 +57,7 @@ public void onCreate(Bundle savedInstanceState) {
startButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
- startService(new Intent(getApplicationContext(), CouchService.class));
+ startService(new Intent("org.couchdb.android.COUCHDB_SERVICE"));
startButton.setEnabled(false);
stopButton.setEnabled(true);
}
@@ -66,7 +66,7 @@ public void onClick(View v) {
stopButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
- stopService(new Intent(getApplicationContext(), CouchService.class));
+ stopService(new Intent("org.couchdb.android.COUCHDB_SERVICE"));
startButton.setEnabled(true);
stopButton.setEnabled(false);
}
62 src/org/couchdb/android/CouchService.java
View
@@ -21,6 +21,7 @@
public class CouchService extends Service {
final String TAG = "CouchDB";
+ final int NO_NOTIFY = 1;
private NotificationManager mNM;
CouchProcess couch;
@@ -30,8 +31,11 @@
FileDescriptor fd;
PrintStream out;
BufferedReader in;
-
- public void start(String binary, String arg1, String arg2) {
+ boolean started = false;
+ boolean notify;
+
+ public void start(String binary, String arg1, String arg2, boolean donotify) {
+ notify = donotify;
int[] pidbuffer = new int[1];
fd = Exec.createSubprocess(binary, arg1, arg2, pidbuffer);
pid = pidbuffer[0];
@@ -56,14 +60,18 @@ public void run() {
int icon = R.drawable.icon;
CharSequence tickerText = "CouchDB Running";
long when = System.currentTimeMillis();
- Notification notification = new Notification(icon, tickerText, when);
- notification.flags = Notification.FLAG_ONGOING_EVENT;
- Intent i = new Intent(Intent.ACTION_VIEW);
- i.setData(Uri.parse("http://127.0.0.1:5984/_utils"));
- notification.setLatestEventInfo(getApplicationContext(), "CouchDB Running", "Press to open Futon", PendingIntent.getActivity(getApplicationContext(), 0, i, PendingIntent.FLAG_CANCEL_CURRENT));
- mNM.cancel(1);
- mNM.notify(2, notification);
- startForeground(2, notification);
+ if(notify)
+ {
+ Notification notification = new Notification(icon, tickerText, when);
+ notification.flags = Notification.FLAG_ONGOING_EVENT;
+ Intent i = new Intent(Intent.ACTION_VIEW);
+ i.setData(Uri.parse("http://127.0.0.1:5984/_utils"));
+ notification.setLatestEventInfo(getApplicationContext(), "CouchDB Running", "Press to open Futon", PendingIntent.getActivity(getApplicationContext(), 0, i, PendingIntent.FLAG_CANCEL_CURRENT));
+ mNM.cancel(1);
+ mNM.notify(2, notification);
+ startForeground(2, notification);
+ }
+ started = true;
}
}
}
@@ -75,17 +83,21 @@ public void run() {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
+ if(couch != null) return 0;
+ boolean donotify = (flags != 1);
mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
couch = new CouchProcess();
int icon = R.drawable.icon;
CharSequence tickerText = "CouchDB Starting";
long when = System.currentTimeMillis();
- Notification notification = new Notification(icon, tickerText, when);
- Intent notificationIntent = new Intent(this, CouchDB.class);
- PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
- notification.setLatestEventInfo(getApplicationContext(), "CouchDB Starting", "Please Wait...", contentIntent);
- mNM.notify(1, notification);
- couch.start("/system/bin/sh", "/sdcard/couch/bin/couchdb", "");
+ if(donotify) {
+ Notification notification = new Notification(icon, tickerText, when);
+ Intent notificationIntent = new Intent(this, CouchDB.class);
+ PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
+ notification.setLatestEventInfo(getApplicationContext(), "CouchDB Starting", "Please Wait...", contentIntent);
+ mNM.notify(1, notification);
+ }
+ couch.start("/system/bin/sh", "/sdcard/couch/bin/couchdb", "", donotify);
return START_STICKY;
}
@@ -93,8 +105,9 @@ public int onStartCommand(Intent intent, int flags, int startId) {
@Override
public void onDestroy() {
try {
- Runtime.getRuntime().exec("/system/bin/kill " + couch.pid);
- Runtime.getRuntime().exec("/system/bin/killall -9 beam"); //This is safe since couch can only kill couch.
+ couch.out.close();
+ android.os.Process.killProcess(couch.pid);
+ couch.in.close();
} catch (IOException e) {
//Failed to kill couch?
}
@@ -103,8 +116,17 @@ public void onDestroy() {
@Override
public IBinder onBind(Intent intent) {
- // TODO Auto-generated method stub
- return null;
+ this.onStartCommand(null, NO_NOTIFY, 0);
+ return mBinder;
}
+ private final ICouchService.Stub mBinder = new ICouchService.Stub()
+ {
+ public int getPort()
+ {
+ while(couch == null || !couch.started) Thread.yield(); //Horrible blocking whee!
+ return 5984;
+ }
+ };
+
}
6 src/org/couchdb/android/ICouchService.aidl
View
@@ -0,0 +1,6 @@
+package org.couchdb.android;
+
+interface ICouchService
+{
+ int getPort();
+}
Please sign in to comment.
Something went wrong with that request. Please try again.