Skip to content

Commit b83de20

Browse files
mschochChris Anderson
authored and
Chris Anderson
committed
add support for broadcast intents
Change-Id: I25e74a2c8858932c0c3a033abab713a028448cf9 Reviewed-on: http://review.couchbase.org/9919 Reviewed-by: Marty Schoch <[email protected]> Tested-by: Chris Anderson <[email protected]> Reviewed-by: Chris Anderson <[email protected]>
1 parent e93751d commit b83de20

File tree

5 files changed

+206
-16
lines changed

5 files changed

+206
-16
lines changed

README.markdown

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,47 @@ Now that your project supports Couchbase, starting Cocuhbase is accomplished by
7979

8080
startCouchbase();
8181
}
82-
</pre>
82+
</pre>
83+
84+
## Broadcast Intents
85+
86+
A Broadcast Receiver registered to listen for the right events can now be used instead of a delegate.
87+
88+
1. Optionally, use the CouchbaseMobile constructor without the delegate parameter
89+
90+
<pre>
91+
CouchbaseMobile couch = new CouchbaseMobile(getBaseContext());
92+
</pre>
93+
94+
2. Declare a Broadcast Receiver
95+
96+
<pre>
97+
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
98+
99+
@Override
100+
public void onReceive(Context context, Intent intent) {
101+
if(CouchbaseStarted.ACTION.equals(intent.getAction())) {
102+
103+
}
104+
else if(CouchbaseError.ACTION.equals(intent.getAction())) {
105+
106+
}
107+
}
108+
};
109+
</pre>
110+
111+
3. Register the receiver to listen for the appropriate events
112+
113+
<pre>
114+
public void onCreate(Bundle savedInstanceState) {
115+
super.onCreate(savedInstanceState);
116+
117+
...
118+
119+
registerReceiver(mReceiver, new IntentFilter(CouchbaseStarted.ACTION));
120+
registerReceiver(mReceiver, new IntentFilter(CouchbaseError.ACTION));
121+
}
122+
</pre>
83123

84124
## Examples
85125

doc/README.txt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,40 @@ Now that your project supports Couchbase, starting Cocuhbase is accomplished by
4747
startCouchbase();
4848
}
4949

50+
## Broadcast Intents
51+
52+
A Broadcast Receiver registered to listen for the right events can now be used instead of a delegate.
53+
54+
1. Optionally, use the CouchbaseMobile constructor without the delegate parameter
55+
56+
CouchbaseMobile couch = new CouchbaseMobile(getBaseContext());
57+
58+
2. Declare a Broadcast Receiver
59+
60+
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
61+
62+
@Override
63+
public void onReceive(Context context, Intent intent) {
64+
if(CouchbaseStarted.ACTION.equals(intent.getAction())) {
65+
66+
}
67+
else if(CouchbaseError.ACTION.equals(intent.getAction())) {
68+
69+
}
70+
}
71+
};
72+
73+
3. Register the receiver to listen for the appropriate events
74+
75+
public void onCreate(Bundle savedInstanceState) {
76+
super.onCreate(savedInstanceState);
77+
78+
...
79+
80+
registerReceiver(mReceiver, new IntentFilter(CouchbaseStarted.ACTION));
81+
registerReceiver(mReceiver, new IntentFilter(CouchbaseError.ACTION));
82+
}
83+
5084
## Examples
5185

5286
For examples please look at:

src/com/couchbase/android/CouchbaseMobile.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ public class CouchbaseMobile {
5353
*/
5454
private static Context ctx;
5555

56+
/**
57+
*
58+
*/
59+
public CouchbaseMobile(Context appCtx) {
60+
ctx = appCtx;
61+
appNamespace = ctx.getPackageName();
62+
}
63+
5664
/**
5765
* A few of the utility functions require some of the same context
5866
* that cannot be gotten automatically, so made this a class to

src/com/couchbase/android/CouchbaseService.java

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
import android.os.Message;
1717
import android.util.Log;
1818

19+
import com.couchbase.android.Intents.CouchbaseError;
20+
import com.couchbase.android.Intents.CouchbaseStarted;
21+
1922
/**
2023
* Implementation of the Couchbase service
2124
*
@@ -76,20 +79,8 @@ public void handleMessage(Message msg) {
7679

7780
switch (msg.what) {
7881
case ERROR:
79-
if(msg.obj instanceof Exception) {
80-
Exception e = (Exception) msg.obj;
81-
StringWriter sw = new StringWriter();
82-
e.printStackTrace(new PrintWriter(sw));
83-
String stacktrace = sw.toString();
84-
if (couchbaseDelegate != null) {
85-
couchbaseDelegate.exit(stacktrace);
86-
}
87-
}
88-
else if(msg.obj instanceof String){
89-
if (couchbaseDelegate != null) {
90-
couchbaseDelegate.exit((String)msg.obj);
91-
}
92-
}
82+
Exception e = (Exception) msg.obj;
83+
couchbaseError(e);
9384
break;
9485
case COMPLETE:
9586
couchbaseInstallThread = null;
@@ -148,8 +139,37 @@ public void startCouchbase(ICouchbaseDelegate cb) {
148139
* Nofity the delegate that Couchbase has started
149140
*/
150141
void couchbaseStarted() {
142+
143+
if(url != null) {
144+
//send broadcast intent
145+
Intent intent = new Intent(CouchbaseStarted.ACTION);
146+
intent.putExtra(CouchbaseStarted.HOST, url.getHost());
147+
intent.putExtra(CouchbaseStarted.PORT, url.getPort());
148+
getApplicationContext().sendBroadcast(intent);
149+
150+
//notify delegate
151+
if (couchbaseDelegate != null) {
152+
couchbaseDelegate.couchbaseStarted(url.getHost(), url.getPort());
153+
}
154+
}
155+
}
156+
157+
/**
158+
* Notify the delegate that Couchbase has encountered an error
159+
*/
160+
void couchbaseError(Exception e) {
161+
StringWriter sw = new StringWriter();
162+
e.printStackTrace(new PrintWriter(sw));
163+
String stacktrace = sw.toString();
164+
165+
//send broadcast intent
166+
Intent intent = new Intent(CouchbaseError.ACTION);
167+
intent.putExtra(CouchbaseError.MESSAGE, stacktrace);
168+
getApplicationContext().sendBroadcast(intent);
169+
170+
//notify delegate
151171
if (couchbaseDelegate != null) {
152-
couchbaseDelegate.couchbaseStarted(url.getHost(), url.getPort());
172+
couchbaseDelegate.exit(stacktrace);
153173
}
154174
}
155175

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.couchbase.android;
2+
3+
import android.content.Intent;
4+
5+
public class Intents {
6+
7+
/**
8+
* Do not instantiate this class
9+
*/
10+
private Intents() {}
11+
12+
/**
13+
* An intent used to notify that Couchbase has started
14+
*/
15+
public static final class CouchbaseStarted {
16+
17+
/**
18+
* Do not instantiate this class
19+
*/
20+
private CouchbaseStarted() {}
21+
22+
/**
23+
* The action name
24+
*/
25+
public final static String ACTION = "com.couchbase.android.COUCHBASE.STARTED";
26+
27+
/**
28+
* Name used in Intent Extra for storing host
29+
*/
30+
public final static String HOST = "host";
31+
32+
/**
33+
* Name used in Intent Extra for storing port
34+
*/
35+
public final static String PORT = "port";
36+
37+
/**
38+
* Utility to get the host Couchbase is listening on
39+
* @param intent the intent to parse
40+
* @return the hostname
41+
*/
42+
public static String getHost(Intent intent) {
43+
return intent.getStringExtra(HOST);
44+
}
45+
46+
/**
47+
* Utility to get the port Couchbase is listening on
48+
* @param intent the intent to parse
49+
* @return the port
50+
*/
51+
public static int getPort(Intent intent) {
52+
return intent.getIntExtra(PORT, -1);
53+
}
54+
55+
}
56+
57+
/**
58+
* An intent used to notify that Couchbase has encountered an error
59+
*/
60+
public static final class CouchbaseError {
61+
62+
/**
63+
* Do not instantiate
64+
*/
65+
private CouchbaseError() {}
66+
67+
/**
68+
* The action name
69+
*/
70+
public final static String ACTION = "com.couchbase.android.COUCHBASE.ERROR";
71+
72+
/**
73+
* Name used in Intent Extra for storing message
74+
*/
75+
public final static String MESSAGE = "message";
76+
77+
/**
78+
* Utility to get the message from Couchbase
79+
* @param intent the intent to parse
80+
* @return the message
81+
*/
82+
public static String getMessage(Intent intent) {
83+
return intent.getStringExtra(MESSAGE);
84+
}
85+
86+
}
87+
88+
}

0 commit comments

Comments
 (0)