Skip to content

Commit 2b38f81

Browse files
tanajitanaji
tanaji
authored and
tanaji
committed
CL-108:Add Presence 3.2 to Android
1 parent 3b7f131 commit 2b38f81

File tree

6 files changed

+207
-13
lines changed

6 files changed

+207
-13
lines changed

.project

+6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@
55
<projects>
66
</projects>
77
<buildSpec>
8+
<buildCommand>
9+
<name>org.python.pydev.PyDevBuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
813
</buildSpec>
914
<natures>
15+
<nature>org.python.pydev.pythonNature</nature>
1016
</natures>
1117
</projectDescription>

.pydevproject

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<?eclipse-pydev version="1.0"?><pydev_project>
3+
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
4+
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python 2.7</pydev_property>
5+
</pydev_project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#Fri Aug 03 13:54:15 IST 2012
2+
eclipse.preferences.version=1
3+
encoding//python/Pubnub.py=utf8

android/PubnubAndroidTest/res/layout/main.xml

+31-11
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,36 @@
5353
android:text="Time" />
5454
</TableRow>
5555
</TableLayout>
56-
<Button
57-
android:id="@+id/button1"
58-
android:layout_width="match_parent"
59-
android:layout_height="wrap_content"
60-
android:onClick="allMessageClick"
61-
android:text="All Message Publish" />
62-
<Button
63-
android:id="@+id/button2"
64-
android:layout_width="match_parent"
56+
<TableLayout
57+
android:layout_width="wrap_content"
6558
android:layout_height="wrap_content"
66-
android:onClick="runUnitTest"
67-
android:text="Run Unit-Test" />
59+
android:layout_span="2"
60+
android:stretchColumns="*" >
61+
<TableRow>
62+
<Button
63+
android:id="@+id/button1"
64+
android:layout_width="match_parent"
65+
android:layout_height="wrap_content"
66+
android:onClick="allMessageClick"
67+
android:text="All Message Publish" />
68+
<Button
69+
android:id="@+id/button2"
70+
android:layout_width="match_parent"
71+
android:layout_height="wrap_content"
72+
android:onClick="runUnitTest"
73+
android:text="Run Unit-Test" />
74+
</TableRow>
75+
<TableRow>
76+
<Button
77+
android:layout_width="fill_parent"
78+
android:layout_height="wrap_content"
79+
android:onClick="PresenceClick"
80+
android:text="Subscribes with presence" />
81+
<Button
82+
android:layout_width="fill_parent"
83+
android:layout_height="wrap_content"
84+
android:onClick="HereNowClick"
85+
android:text="Here Now" />
86+
</TableRow>
87+
</TableLayout>
6888
</LinearLayout>

android/PubnubAndroidTest/src/com/fbt/Pubnub.java

+85-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525
import org.apache.http.params.HttpConnectionParams;
2626
import org.apache.http.params.HttpParams;
2727
import org.json.JSONArray;
28+
import org.json.JSONException;
2829
import org.json.JSONObject;
2930

31+
import android.util.Log;
3032
import pubnub.PubnubCrypto;
3133

3234
/**
@@ -42,6 +44,7 @@ public class Pubnub {
4244
private String SECRET_KEY = "";
4345
private String CIPHER_KEY = "";
4446
private boolean SSL = false;
47+
private String UUIDs = null;
4548
private class ChannelStatus {
4649
String channel;
4750
boolean connected, first;
@@ -150,6 +153,7 @@ public void init(
150153
} else {
151154
this.ORIGIN = "http://" + this.ORIGIN;
152155
}
156+
UUIDs = uuid();
153157
}
154158

155159
/**
@@ -509,6 +513,73 @@ public JSONArray history( HashMap<String, Object> args ) {
509513
}
510514
}
511515

516+
/**
517+
* presence
518+
*
519+
* This is BLOCKING. Listen for presence events on a channel.
520+
*
521+
* @param HashMap
522+
* <String, Object> args with channel and callback.
523+
*/
524+
public void presence(HashMap<String, Object> args) {
525+
String channel = (String) args.get("channel");
526+
Callback callback;
527+
528+
// Validate Arguments
529+
if (args.get("callback") != null) {
530+
callback = (Callback) args.get("callback");
531+
} else {
532+
System.out.println("Invalid Callback.");
533+
return;
534+
}
535+
536+
if (channel == null || channel.equals("")) {
537+
callback.errorCallback(channel, "Invalid Channel.");
538+
return;
539+
}
540+
541+
HashMap<String, Object> param = new HashMap<String, Object>();
542+
param.put("channel", channel + "-pnpres");
543+
param.put("callback", callback);
544+
subscribe(param);
545+
}
546+
547+
/**
548+
* Here Now
549+
*
550+
* Load current occupancy from a channel
551+
*
552+
* @param HashMap
553+
* <String, Object> args with channel.
554+
*/
555+
public JSONArray here_now(HashMap<String, Object> args) {
556+
String channel = (String) args.get("channel");
557+
// Validate Arguments
558+
559+
if (channel == null || channel.equals("")) {
560+
JSONArray jsono = new JSONArray();
561+
try {
562+
jsono.put(0);
563+
jsono.put("Missing Channel");
564+
} catch (Exception jsone) {
565+
}
566+
return jsono;
567+
}
568+
569+
// Build URL
570+
List<String> url = new ArrayList<String>();
571+
url.add("v2");
572+
url.add("presence");
573+
url.add("sub_key");
574+
url.add(this.SUBSCRIBE_KEY);
575+
url.add("channel");
576+
url.add(channel);
577+
578+
// Return JSONArray
579+
return _request(url);
580+
581+
}
582+
512583
/**
513584
* Time
514585
*
@@ -566,7 +637,7 @@ private JSONArray _request(List<String> url_components) {
566637
String json = "";
567638
StringBuilder url = new StringBuilder();
568639
Iterator<String> url_iterator = url_components.iterator();
569-
640+
String _callFor = url_components.get(0);
570641
url.append(this.ORIGIN);
571642

572643
// Generate URL with UTF-8 Encoding
@@ -584,7 +655,9 @@ private JSONArray _request(List<String> url_components) {
584655
return jsono;
585656
}
586657
}
587-
658+
if (_callFor.equalsIgnoreCase("subscribe")) {
659+
url.append("/").append("?uuid=" + UUIDs);
660+
}
588661
try {
589662

590663
PubnubHttpRequest request = new PubnubHttpRequest(url.toString());
@@ -620,7 +693,17 @@ private JSONArray _request(List<String> url_components) {
620693

621694
return jsono;
622695
}
696+
if (_callFor.equalsIgnoreCase("V2")) {
697+
try {
698+
JSONArray arr = new JSONArray();
699+
700+
arr.put(new JSONObject(json));
623701

702+
json = arr.toString();
703+
} catch (JSONException e) {
704+
e.printStackTrace();
705+
}
706+
}
624707
// Parse JSON String
625708
try { return new JSONArray(json); }
626709
catch (Exception e) {

android/PubnubAndroidTest/src/com/fbt/PubnubTestActivity.java

+77
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,18 @@ public void allMessageClick(View v) {
237237
System.out.println(response);
238238
}
239239

240+
public void HereNowClick(View v) {
241+
HashMap<String, Object> args = new HashMap<String, Object>(1);
242+
args.put("channel", channel);
243+
myMessage = pubnub.here_now(args).toString();
244+
r.sendEmptyMessage(0);
245+
Log.e("Here Now", pubnub.here_now(args).toString());
246+
}
247+
248+
public void PresenceClick(View v) {
249+
new SubscribedWithPresence().execute("hello_channel");
250+
}
251+
240252
class RefreshHandler extends Handler {
241253
@Override
242254
public void handleMessage(Message msg) {
@@ -257,6 +269,71 @@ public void onClick(DialogInterface dialog, int which) {
257269
}
258270
};
259271

272+
class SubscribedWithPresence extends AsyncTask<String, Void, Boolean> {
273+
274+
@Override
275+
protected Boolean doInBackground(String... params) {
276+
try {
277+
// Android: (Subscribe)
278+
String passed = params[0];
279+
Log.e("Channel", passed);
280+
class ReceiverCallback implements Callback {
281+
public boolean subscribeCallback(String channel,
282+
Object message) {
283+
Log.i("Message Received", message.toString());
284+
myMessage = message.toString();
285+
r.sendEmptyMessage(0);
286+
return true;
287+
}
288+
289+
@Override
290+
public void errorCallback(String channel, Object message) {
291+
Log.e("ErrorCallback", "Channel:" + channel + "-"
292+
+ message.toString());
293+
}
294+
295+
@Override
296+
public void connectCallback(String channel) {
297+
Log.i("ConnectCallback", "Connected to channel :"
298+
+ channel);
299+
}
300+
301+
@Override
302+
public void reconnectCallback(String channel) {
303+
Log.i("ReconnectCallback", "Reconnecting to channel :"
304+
+ channel);
305+
}
306+
307+
@Override
308+
public void disconnectCallback(String channel) {
309+
Log.i("DisconnectCallback", "Disconnected to channel :"
310+
+ channel);
311+
}
312+
}
313+
314+
// Listen for Messages (Subscribe)
315+
HashMap<String, Object> args = new HashMap<String, Object>(2);
316+
args.put("channel", passed); // Channel Name
317+
args.put("callback", new ReceiverCallback()); // Callback to get
318+
// response
319+
pubnub.presence(args);
320+
321+
} catch (Exception e) {
322+
e.printStackTrace();
323+
Log.v("ERROR", "While downloading");
324+
}
325+
326+
return Boolean.TRUE;
327+
}
328+
329+
@Override
330+
protected void onPreExecute() {
331+
}
332+
333+
protected void onPostExecute(Boolean result) {
334+
}
335+
}
336+
260337
class XMLDownloader extends AsyncTask<String, Void, Boolean> {
261338

262339
@Override

0 commit comments

Comments
 (0)