Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

CL-108:Add Presence 3.2 to Android

  • Loading branch information...
commit 2b38f81696d5dd0323fc1888d8474281eeba5501 1 parent 3b7f131
tanaji authored
6 .project
View
@@ -5,7 +5,13 @@
<projects>
</projects>
<buildSpec>
+ <buildCommand>
+ <name>org.python.pydev.PyDevBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
</buildSpec>
<natures>
+ <nature>org.python.pydev.pythonNature</nature>
</natures>
</projectDescription>
5 .pydevproject
View
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<?eclipse-pydev version="1.0"?><pydev_project>
+<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
+<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python 2.7</pydev_property>
+</pydev_project>
3  .settings/org.eclipse.core.resources.prefs
View
@@ -0,0 +1,3 @@
+#Fri Aug 03 13:54:15 IST 2012
+eclipse.preferences.version=1
+encoding//python/Pubnub.py=utf8
42 android/PubnubAndroidTest/res/layout/main.xml
View
@@ -53,16 +53,36 @@
android:text="Time" />
</TableRow>
</TableLayout>
- <Button
- android:id="@+id/button1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:onClick="allMessageClick"
- android:text="All Message Publish" />
- <Button
- android:id="@+id/button2"
- android:layout_width="match_parent"
+ <TableLayout
+ android:layout_width="wrap_content"
android:layout_height="wrap_content"
- android:onClick="runUnitTest"
- android:text="Run Unit-Test" />
+ android:layout_span="2"
+ android:stretchColumns="*" >
+ <TableRow>
+ <Button
+ android:id="@+id/button1"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:onClick="allMessageClick"
+ android:text="All Message Publish" />
+ <Button
+ android:id="@+id/button2"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:onClick="runUnitTest"
+ android:text="Run Unit-Test" />
+ </TableRow>
+ <TableRow>
+ <Button
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:onClick="PresenceClick"
+ android:text="Subscribes with presence" />
+ <Button
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:onClick="HereNowClick"
+ android:text="Here Now" />
+ </TableRow>
+ </TableLayout>
</LinearLayout>
87 android/PubnubAndroidTest/src/com/fbt/Pubnub.java
View
@@ -25,8 +25,10 @@
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
+import org.json.JSONException;
import org.json.JSONObject;
+import android.util.Log;
import pubnub.PubnubCrypto;
/**
@@ -42,6 +44,7 @@
private String SECRET_KEY = "";
private String CIPHER_KEY = "";
private boolean SSL = false;
+ private String UUIDs = null;
private class ChannelStatus {
String channel;
boolean connected, first;
@@ -150,6 +153,7 @@ public void init(
} else {
this.ORIGIN = "http://" + this.ORIGIN;
}
+ UUIDs = uuid();
}
/**
@@ -510,6 +514,73 @@ public JSONArray history( HashMap<String, Object> args ) {
}
/**
+ * presence
+ *
+ * This is BLOCKING. Listen for presence events on a channel.
+ *
+ * @param HashMap
+ * <String, Object> args with channel and callback.
+ */
+ public void presence(HashMap<String, Object> args) {
+ String channel = (String) args.get("channel");
+ Callback callback;
+
+ // Validate Arguments
+ if (args.get("callback") != null) {
+ callback = (Callback) args.get("callback");
+ } else {
+ System.out.println("Invalid Callback.");
+ return;
+ }
+
+ if (channel == null || channel.equals("")) {
+ callback.errorCallback(channel, "Invalid Channel.");
+ return;
+ }
+
+ HashMap<String, Object> param = new HashMap<String, Object>();
+ param.put("channel", channel + "-pnpres");
+ param.put("callback", callback);
+ subscribe(param);
+ }
+
+ /**
+ * Here Now
+ *
+ * Load current occupancy from a channel
+ *
+ * @param HashMap
+ * <String, Object> args with channel.
+ */
+ public JSONArray here_now(HashMap<String, Object> args) {
+ String channel = (String) args.get("channel");
+ // Validate Arguments
+
+ if (channel == null || channel.equals("")) {
+ JSONArray jsono = new JSONArray();
+ try {
+ jsono.put(0);
+ jsono.put("Missing Channel");
+ } catch (Exception jsone) {
+ }
+ return jsono;
+ }
+
+ // Build URL
+ List<String> url = new ArrayList<String>();
+ url.add("v2");
+ url.add("presence");
+ url.add("sub_key");
+ url.add(this.SUBSCRIBE_KEY);
+ url.add("channel");
+ url.add(channel);
+
+ // Return JSONArray
+ return _request(url);
+
+ }
+
+ /**
* Time
*
* Timestamp from PubNub Cloud.
@@ -566,7 +637,7 @@ private JSONArray _request(List<String> url_components) {
String json = "";
StringBuilder url = new StringBuilder();
Iterator<String> url_iterator = url_components.iterator();
-
+ String _callFor = url_components.get(0);
url.append(this.ORIGIN);
// Generate URL with UTF-8 Encoding
@@ -584,7 +655,9 @@ private JSONArray _request(List<String> url_components) {
return jsono;
}
}
-
+ if (_callFor.equalsIgnoreCase("subscribe")) {
+ url.append("/").append("?uuid=" + UUIDs);
+ }
try {
PubnubHttpRequest request = new PubnubHttpRequest(url.toString());
@@ -620,7 +693,17 @@ private JSONArray _request(List<String> url_components) {
return jsono;
}
+ if (_callFor.equalsIgnoreCase("V2")) {
+ try {
+ JSONArray arr = new JSONArray();
+
+ arr.put(new JSONObject(json));
+ json = arr.toString();
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ }
// Parse JSON String
try { return new JSONArray(json); }
catch (Exception e) {
77 android/PubnubAndroidTest/src/com/fbt/PubnubTestActivity.java
View
@@ -237,6 +237,18 @@ public void allMessageClick(View v) {
System.out.println(response);
}
+ public void HereNowClick(View v) {
+ HashMap<String, Object> args = new HashMap<String, Object>(1);
+ args.put("channel", channel);
+ myMessage = pubnub.here_now(args).toString();
+ r.sendEmptyMessage(0);
+ Log.e("Here Now", pubnub.here_now(args).toString());
+ }
+
+ public void PresenceClick(View v) {
+ new SubscribedWithPresence().execute("hello_channel");
+ }
+
class RefreshHandler extends Handler {
@Override
public void handleMessage(Message msg) {
@@ -257,6 +269,71 @@ public void onClick(DialogInterface dialog, int which) {
}
};
+ class SubscribedWithPresence extends AsyncTask<String, Void, Boolean> {
+
+ @Override
+ protected Boolean doInBackground(String... params) {
+ try {
+ // Android: (Subscribe)
+ String passed = params[0];
+ Log.e("Channel", passed);
+ class ReceiverCallback implements Callback {
+ public boolean subscribeCallback(String channel,
+ Object message) {
+ Log.i("Message Received", message.toString());
+ myMessage = message.toString();
+ r.sendEmptyMessage(0);
+ return true;
+ }
+
+ @Override
+ public void errorCallback(String channel, Object message) {
+ Log.e("ErrorCallback", "Channel:" + channel + "-"
+ + message.toString());
+ }
+
+ @Override
+ public void connectCallback(String channel) {
+ Log.i("ConnectCallback", "Connected to channel :"
+ + channel);
+ }
+
+ @Override
+ public void reconnectCallback(String channel) {
+ Log.i("ReconnectCallback", "Reconnecting to channel :"
+ + channel);
+ }
+
+ @Override
+ public void disconnectCallback(String channel) {
+ Log.i("DisconnectCallback", "Disconnected to channel :"
+ + channel);
+ }
+ }
+
+ // Listen for Messages (Subscribe)
+ HashMap<String, Object> args = new HashMap<String, Object>(2);
+ args.put("channel", passed); // Channel Name
+ args.put("callback", new ReceiverCallback()); // Callback to get
+ // response
+ pubnub.presence(args);
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ Log.v("ERROR", "While downloading");
+ }
+
+ return Boolean.TRUE;
+ }
+
+ @Override
+ protected void onPreExecute() {
+ }
+
+ protected void onPostExecute(Boolean result) {
+ }
+ }
+
class XMLDownloader extends AsyncTask<String, Void, Boolean> {
@Override
Please sign in to comment.
Something went wrong with that request. Please try again.