Author: jerome
Date: 2009-10-20 16:14:38 +0200 (Tue, 20 Oct 2009)
New Revision: 5752
Added:
software_suite_v3/software/plugin/plugin-facebook/branches/october_release/src/com/kysoh/plugins/facebook/connection/BareBonesBrowserLaunch.java
Modified:
software_suite_v3/software/plugin/plugin-facebook/branches/october_release/src/com/kysoh/plugins/facebook/FacebookConnection.java
software_suite_v3/software/plugin/plugin-facebook/branches/october_release/src/com/kysoh/plugins/facebook/FacebookFunctions.java
Log:
* Updated facebook functions.
* Big code cleanup.
* Added some new functions.
Modified:
software_suite_v3/software/plugin/plugin-facebook/branches/october_release/src/com/kysoh/plugins/facebook/FacebookConnection.java
===================================================================
---
software_suite_v3/software/plugin/plugin-facebook/branches/october_release/src/com/kysoh/plugins/facebook/FacebookConnection.java
2009-10-20 13:30:31 UTC (rev 5751)
+++
software_suite_v3/software/plugin/plugin-facebook/branches/october_release/src/com/kysoh/plugins/facebook/FacebookConnection.java
2009-10-20 14:14:38 UTC (rev 5752)
@@ -26,6 +26,7 @@
import com.google.code.facebookapi.FacebookException;
import com.google.code.facebookapi.FacebookJsonRestClient;
+import com.kysoh.plugins.facebook.connection.BareBonesBrowserLaunch;
import com.kysoh.plugins.facebook.connection.FacebookSessionUtils;
public class FacebookConnection {
@@ -42,14 +43,12 @@
try
{
+
String sessionKey =
FacebookSessionUtils.getValidSessionID( true );
//Generate session secret, ready to pass to desktop app
client
String sessionSecret =
FacebookSessionUtils.lastClientUsed.getCacheSessionSecret();
- //TODO remove this one.
- System.out.println("Session secret: " +
String.valueOf(sessionSecret));
-
client = new
FacebookJsonRestClient(FacebookSessionUtils.api_key, sessionSecret, sessionKey);
if((sessionSecret == null) || (client == null))
@@ -61,11 +60,11 @@
}
catch (FacebookException e)
{
- ;
+ e.printStackTrace();
}
catch (IOException e)
{
- ;
+ e.printStackTrace();
}
return false;
@@ -99,4 +98,21 @@
{
return client;
}
+
+
+ /**
+ * Prompt for plugin facebook authorizations.
+ */
+ public void promptAuthorizations()
+ {
+ String url = "http://www.facebook.com/login.php?api_key=+" +
+ FacebookSessionUtils.api_key +
+ "&connect_display=popup&v=1.0" +
+
"&next=http://www.facebook.com/connect/login_success.html" +
+
"&cancel_url=http://www.facebook.com/connect/login_failure.html" +
+ "&fbconnect=true&return_session=true" +
+
"&req_perms=read_stream,publish_stream,offline_access,read_mailbox";
+
+ BareBonesBrowserLaunch.openURL(url);
+ }
}
Modified:
software_suite_v3/software/plugin/plugin-facebook/branches/october_release/src/com/kysoh/plugins/facebook/FacebookFunctions.java
===================================================================
---
software_suite_v3/software/plugin/plugin-facebook/branches/october_release/src/com/kysoh/plugins/facebook/FacebookFunctions.java
2009-10-20 13:30:31 UTC (rev 5751)
+++
software_suite_v3/software/plugin/plugin-facebook/branches/october_release/src/com/kysoh/plugins/facebook/FacebookFunctions.java
2009-10-20 14:14:38 UTC (rev 5752)
@@ -23,383 +23,347 @@
package com.kysoh.plugins.facebook;
-import java.util.EnumSet;
+import java.util.Collections;
import java.util.Hashtable;
-import java.util.List;
+import java.util.Iterator;
import java.util.Vector;
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+
import com.google.code.facebookapi.FacebookException;
-import com.google.code.facebookapi.ProfileField;
-import com.google.code.facebookapi.schema.User;
-import com.google.code.facebookapi.schema.UsersGetInfoResponse;
+import com.google.code.facebookapi.FacebookJsonRestClient;
public class FacebookFunctions {
- private FacebookConnection connection;
- private Hashtable<String, Vector<String>> notifications = new
Hashtable<String, Vector<String>>();
+ //Messages constants.
+ public static final byte MESSAGE = 0;
+ public static final byte RESPONSE = 1;
+ private FacebookJsonRestClient client;
+ private JSONObject notifications = null;
+
/**
* Class constructor.
* @param connection
*/
- public FacebookFunctions(FacebookConnection connection)
+ public FacebookFunctions(FacebookJsonRestClient aClient)
{
- this.connection = connection;
- this.notifications = this.getUserNotification();
+ this.client = aClient;
+
+ //Getting standard notifications.
+ try
+ {
+ notifications = (JSONObject) client.notifications_get();
+ }
+ catch (FacebookException e)
+ {
+ notifications = null;
+ }
}
/**
- * Gets all notifications.
+ * Add requested feed to the user wall.
*/
- private Hashtable<String, Vector<String>> getUserNotification()
+ public boolean publishFeed(String aFeed)
{
- //Will contain all availables notifications.
- //availables notifications : messages, pokes, shares,
friends_requests, group_invites, event_invites
- Hashtable<String, Vector<String>> notifications = new
Hashtable<String, Vector<String>>();
-
- try
+ try
{
- connection.getClient().notifications_get();
- String notifs = connection.getClient().getRawResponse();
-
- /**
- * Getting messages notifications.
- */
- if(notifs.contains("<messages"))
- {
- String messagesString = notifs.substring(
-
notifs.indexOf("<messages"),
-
notifs.indexOf("</messages>"));
-
- Vector<String> msDatas = new Vector<String>();
-
- String unreaded = messagesString.substring(
-
messagesString.indexOf("<unread>") + "<unread>".length(),
-
messagesString.indexOf("</unread>"));
-
- msDatas.add(unreaded);
-
-
- if(Integer.valueOf(unreaded).intValue() > 0)
- {
- notifications.put("messages", msDatas);
- }
- }
-
- /**
- * Get the friend_requests notifications.
- */
- if(notifs.contains("<friend_requests"))
- {
-
- String friendsString = "";
- try
- {
- friendsString =
notifs.substring(notifs.indexOf("<friend_requests"),
notifs.indexOf("</friend_requests>"));
- }
- catch(StringIndexOutOfBoundsException no)
- {
- friendsString =
notifs.substring(notifs.indexOf("<friend_requests"), notifs.indexOf("/>",
notifs.indexOf("<friend_requests")));
- }
-
- Vector<String> friendsDatas = new
Vector<String>();
- Vector<Long> friendsDatasAsLong = new
Vector<Long>();
-
- //Getting friends uid that ask for friends.
- if(friendsString.contains("<uid>"))
- {
- while(friendsString.contains("<uid>"))
- {
- String friend =
friendsString.substring(
-
friendsString.indexOf("<uid>") + "<uid>".length(),
-
friendsString.indexOf("</uid>"));
-
- friendsString =
friendsString.replace("<uid>" + friend + "</uid>", "");
-
-
friendsDatasAsLong.add(Long.valueOf(friend).longValue());
- }
-
-
- // Go fetch the information for the
user list of user ids
-
connection.getClient().users_getInfo(friendsDatasAsLong,
EnumSet.of(ProfileField.NAME));
-
- UsersGetInfoResponse userResponse =
(UsersGetInfoResponse) connection.getClient().getResponsePOJO();
-
- // Print out the user information
- List<User> users =
userResponse.getUser();
- for (User user : users)
- {
-
friendsDatas.add(user.getName());
- }
-
- notifications.put("friend_requests",
friendsDatas);
- }
- }
-
- /**
- * Getting new groups invitations.
- */
- if(notifs.contains("<group_invites"))
- {
- String groupString;
-
- try
- {
- groupString =
notifs.substring(notifs.indexOf("<group_invites"),
notifs.indexOf("</group_invites>"));
- }
- catch(IndexOutOfBoundsException indexError)
- {
- groupString =
notifs.substring(notifs.indexOf("<group_invites"), notifs.indexOf("/>",
notifs.indexOf("<group_invites")));
- }
-
- Vector<Long> groupsDatasAsLong = new
Vector<Long>();
- Vector<String> groupsDatas = new
Vector<String>();
-
- //Getting friends uid that ask for friends.
- if(groupString.contains("<gid>"))
- {
- while(groupString.contains("<gid>"))
- {
- String friend =
groupString.substring(
-
groupString.indexOf("<gid>") + "<gid>".length(),
-
groupString.indexOf("</gid>"));
-
- groupString =
groupString.replace("<gid>" + friend + "</gid>", "");
-
-
groupsDatasAsLong.add(Long.valueOf(friend).longValue());
- }
-
-
groupsDatas.add(String.valueOf(groupsDatasAsLong.size()));
-
- for(Long gid : groupsDatasAsLong)
- {
- try
- {
- Thread.sleep(200);
-
connection.getClient().fql_query("Select name from group WHERE gid=" +
gid.longValue());
- String response =
connection.getClient().getRawResponse();
-
if(response.contains("<group>") && response.contains("<name>"))
- {
- String
groupName = response.substring(
-
response.indexOf("<name>") + "<name>".length(),
-
response.indexOf("</name>", response.indexOf("<name>")));
-
groupsDatas.add(groupName);
- }
- }
- catch(Exception fqlError)
- {
-
groupsDatas.add("Unknown group");
- }
- }
-
- //Only if they have some group invites.
- if(groupsDatasAsLong.size() > 0)
- {
-
notifications.put("group_invites", groupsDatas);
- }
- }
- }
-
- /**
- * Getting new events invitations.
- */
- if(notifs.contains("<event_invites"))
- {
- String eventString = "";
- try
- {
- eventString =
notifs.substring(notifs.indexOf("<event_invites"), notifs.indexOf("/>",
notifs.indexOf("<event_invites")));
- }
- catch(IndexOutOfBoundsException indexError)
- {
- eventString =
notifs.substring(notifs.indexOf("<event_invites"),
notifs.indexOf("</event_invites>"));
- }
-
- Vector<Long> eventsDatasAsLong = new
Vector<Long>();
- Vector<String> eventsDatas = new
Vector<String>();
-
- //Getting friends uid that ask for friends.
- if(eventString.contains("<eid>") ||
(eventString.contains("<event_invites_elt>")))
- {
- while(eventString.contains("<eid>"))
- {
- String event =
eventString.substring(
-
eventString.indexOf("<eid>") + "<eid>".length(),
-
eventString.indexOf("</eid>"));
-
- eventString =
eventString.replace("<eid>" + event + "</eid>", "");
-
-
eventsDatasAsLong.add(Long.valueOf(event).longValue());
- }
-
-
-
while(eventString.contains("<event_invites_elt>"))
- {
- String event =
eventString.substring(
-
eventString.indexOf("<event_invites_elt>") + "<event_invites_elt>".length(),
-
eventString.indexOf("</event_invites_elt>"));
-
- eventString =
eventString.replace("<event_invites_elt>" + event + "</event_invites_elt>", "");
-
eventsDatasAsLong.add(Long.valueOf(event).longValue());
- }
-
- //Adding the number of event invites.
-
eventsDatas.add(String.valueOf(eventsDatasAsLong.size()));
-
- for(Long eid : eventsDatasAsLong)
- {
- try
- {
- Thread.sleep(200);
-
connection.getClient().fql_query("Select name from event WHERE eid=" +
eid.longValue());
- String response =
connection.getClient().getRawResponse();
-
if(response.contains("<event>") && response.contains("<name>"))
- {
- String
groupName = response.substring(
-
response.indexOf("<name>") + "<name>".length(),
-
response.indexOf("</name>", response.indexOf("<name>")));
-
eventsDatas.add(groupName);
- }
- }
- catch(Exception fqlError)
- {
-
eventsDatas.add("Unknown event");
- }
-
- }
-
- if(eventsDatasAsLong.size() > 0)
- {
-
notifications.put("event_invites", eventsDatas);
- }
- }
- }
- }
+ client.users_setStatus(aFeed);
+ return true;
+ }
catch (FacebookException e)
{
;
- }
-
- return notifications;
+ }
+ return false;
}
/**
* Return email notifications.
*/
- public Integer getEmailNotifications()
+ public Hashtable<String, Vector<String>> getEmailNotifications()
{
- if(this.notifications.containsKey("messages"))
+ Hashtable<String, Vector<String>> result = new
Hashtable<String, Vector<String>>();
+
+ String query = "SELECT thread_id, subject, recipients,
viewer_id FROM " +
+ "thread WHERE folder_id = 0 AND
unread != 0";
+ String userQuery = "SELECT name from user WHERE uid=";
+
+ try
{
- return
Integer.valueOf(this.notifications.get("messages").get(0));
+ JSONArray emails = (JSONArray) client.fql_query(query);
+
+ for(int x = 0; x < emails.length(); x++)
+ {
+ String thread = (String)
emails.getJSONObject( x ).get("thread_id");
+ String subject = (String)
emails.getJSONObject( x ).get("subject");
+ JSONArray recipients = (JSONArray)
emails.getJSONObject( x ).get("recipients");
+ String viewer = (String)
emails.getJSONObject( x ).get("viewer_id");
+
+ String senderID = viewer;
+ String sender = "Undefined";
+
+ for(int y = 0 ; y < recipients.length(); y++)
+ {
+ senderID = recipients.getString( y );
+ if( ! senderID.equalsIgnoreCase(viewer))
+ {
+ JSONArray username = (JSONArray)
client.fql_query(userQuery + senderID);
+ sender = username.getJSONObject( 0
).getString("name");
+ }
+ }
+
+ Vector<String> threadDatas = new Vector<String>();
+ threadDatas.add(subject);
+ threadDatas.add(sender);
+
+ result.put(thread, threadDatas);
+ }
+ }
+ catch (FacebookException e)
+ {
+ ;
+ }
+ catch (JSONException e)
+ {
+ ;
}
- else
+ catch (ClassCastException e)
{
- return 0;
+ ;
}
+
+ return sortHash(result);
}
/**
- * Return the number of friend requests.
+ * Return friend names of friend requests.
* @return
*/
- public Integer getFriendRequestsCount()
+ public Vector<String> getFriendRequests()
{
- if(this.notifications.containsKey("friend_requests"))
+ Vector<String> result = new Vector<String>();
+
+ if (notifications == null )
+ return result;
+
+ String query_name = "SELECT name FROM user WHERE uid=";
+ String name = "";
+
+ try
{
- return
Integer.valueOf(this.notifications.get("friend_requests").size());
+ String user_id =
String.valueOf(client.users_getLoggedInUser());
+ JSONArray friends =
(JSONArray)notifications.get("friend_requests");
+ //Getting friend requests user ID.
+
+ for(int x = 0; x < friends.length(); x++)
+ {
+ user_id = String.valueOf(friends.get( x ));
+ name = ((JSONArray) client.fql_query(query_name +
user_id)).getJSONObject( 0 ).getString("name");
+
+ result.add(name);
+ }
+
+ }
+ catch (FacebookException e)
+ {
+ ;
+ }
+ catch (JSONException e)
+ {
+ ;
}
- else
+ catch (ClassCastException e)
{
- return 0;
+ ;
}
+ return result;
}
/**
- * Return the name of people that ask to be a friend of user.
+ * Return availables group invites names.
* @return
*/
- public Vector<String> getFriendRequestNames()
+ public Vector<String> getGroupsInvites()
{
- Vector<String> friends = new Vector<String>();
+ Vector<String> result = new Vector<String>();
- if(this.notifications.containsKey("friend_requests"))
+ if (notifications == null )
+ return result;
+
+ String query_name = "SELECT name FROM group WHERE gid=";
+ String name = "";
+
+ try
{
- Vector<String> dat =
this.notifications.get("friend_requests");
- friends = dat;
+ String group_id =
String.valueOf(client.users_getLoggedInUser());
+ JSONArray groups =
(JSONArray)notifications.get("group_invites");
+ //Getting friend requests user ID.
+
+ for(int x = 0; x < groups.length(); x++)
+ {
+ group_id = String.valueOf(groups.get( x ));
+ name = ((JSONArray) client.fql_query(query_name +
group_id)).getJSONObject( 0 ).getString("name");
+
+ result.add(name);
+ }
+
+ }
+ catch (FacebookException e)
+ {
+ ;
+ }
+ catch (JSONException e)
+ {
+ ;
}
- return friends;
+ catch (ClassCastException e)
+ {
+ ;
+ }
+ return result;
+
}
/**
- * Return the number of availables groups invites.
+ * Return event invites names.
* @return
*/
- public Integer getGroupsInvitesCount()
+ public Vector<String> getEventsInvites()
{
- if(this.notifications.containsKey("group_invites"))
+ Vector<String> result = new Vector<String>();
+
+ if (notifications == null )
+ return result;
+
+ String query_name = "SELECT name FROM event WHERE eid=";
+ String name = "";
+
+ try
{
- return
Integer.valueOf(this.notifications.get("group_invites").get(0));
+ String event_id =
String.valueOf(client.users_getLoggedInUser());
+ JSONArray events =
(JSONArray)notifications.get("event_invites");
+ //Getting friend requests user ID.
+
+ for(int x = 0; x < events.length(); x++)
+ {
+ event_id = String.valueOf(events.get( x ));
+ name = ((JSONArray) client.fql_query(query_name +
event_id)).getJSONObject( 0 ).getString("name");
+
+ result.add(name);
+ }
+
+ }
+ catch (FacebookException e)
+ {
+ ;
+ }
+ catch (JSONException e)
+ {
+ ;
}
- else
+ catch (ClassCastException e)
{
- return 0;
+ ;
}
+ return result;
}
+
/**
- * Return the name of groups invites.
+ * Return a Facebook name for the given uid.
+ * @param uid
* @return
*/
- public Vector<String> getGroupInvitesNames()
+ public String getFacebookName(String uid)
{
- Vector<String> groups = new Vector<String>();
+ String result = "Unknown";
+ String query = "SELECT name from user WHERE uid=";
- if(this.notifications.containsKey("group_invites"))
+ try
{
- Vector<String> dat =
this.notifications.get("group_invites");
- dat.remove(0);
- groups = dat;
+ JSONArray username = (JSONArray) client.fql_query(query
+ uid);
+
+ result = username.getJSONObject( 0 ).getString("name");
+ }
+ catch (FacebookException e)
+ {
+ ;
}
- return groups;
+ catch (ClassCastException e)
+ {
+ ;
+ }
+ catch (JSONException e)
+ {
+ ;
+ }
+
+ return result;
}
+
/**
- * Return the number of events invites.
+ * Sort an hashtable by names.
+ * @param aHashtable
* @return
*/
- public Integer getEventsInvitesCount()
+ private Hashtable<String, Vector<String>> sortHash(Hashtable<String,
Vector<String>> aHashtable)
{
- if(this.notifications.containsKey("event_invites"))
- {
- return
Integer.valueOf(this.notifications.get("event_invites").get(0));
- }
- else
- {
- return 0;
- }
+ Hashtable<String, Vector<String>> result = new
Hashtable<String, Vector<String>>();
+ Vector<String> v = new Vector<String>(aHashtable.keySet());
+ Collections.sort(v);
+ Iterator<String> it = v.iterator();
+
+ while (it.hasNext())
+ {
+ String element = (String)it.next();
+ result.put(element, aHashtable.get(element));
+ }
+ return result;
}
- /**
- * Return the name of events invites.
- * @return
+
+
+ /***
+ * Concat user messages.
+ * @param list
*/
- public Vector<String> getEventInvitesNames()
+ public Hashtable<String, Integer> getByNames(Hashtable<String,
Vector<String>> baseHash)
{
- Vector<String> events = new Vector<String>();
+ Hashtable<String, Integer> result = new Hashtable<String,
Integer>();
+ Vector<String> v = new Vector<String>(baseHash.keySet());
- if(this.notifications.containsKey("event_invites"))
+ //Handling no datas into hashtable.
+ if ( v.size() == 0)
{
- Vector<String> dat =
this.notifications.get("event_invites");
- dat.remove(0);
- events = dat;
+ return null;
}
- return events;
+
+ //Counting by names.
+ for( int i = 0 ; i != v.size() ; i ++)
+ {
+ if (result.containsKey(v.get(i)))
+ {
+ Integer value =
Integer.valueOf(result.get(v.get(i)) + 1);
+ result.remove(v.get(i));
+ result.put(v.get(i), value);
+ }
+ else
+ {
+ result.put(v.get(i), Integer.valueOf(1));
+ }
+ }
+
+ return result;
}
}
Added:
software_suite_v3/software/plugin/plugin-facebook/branches/october_release/src/com/kysoh/plugins/facebook/connection/BareBonesBrowserLaunch.java
===================================================================
---
software_suite_v3/software/plugin/plugin-facebook/branches/october_release/src/com/kysoh/plugins/facebook/connection/BareBonesBrowserLaunch.java
(rev 0)
+++
software_suite_v3/software/plugin/plugin-facebook/branches/october_release/src/com/kysoh/plugins/facebook/connection/BareBonesBrowserLaunch.java
2009-10-20 14:14:38 UTC (rev 5752)
@@ -0,0 +1,54 @@
+package com.kysoh.plugins.facebook.connection;
+
+/////////////////////////////////////////////////////////
+// Bare Bones Browser Launch //
+// Version 1.5 //
+// December 10, 2005 //
+// Supports: Mac OS X, GNU/Linux, Unix, Windows XP //
+// Example Usage: //
+// String url = "http://www.centerkey.com/"; //
+// BareBonesBrowserLaunch.openURL(url); //
+// Public Domain Software -- Free to Use as You Like //
+/////////////////////////////////////////////////////////
+
+import java.lang.reflect.Method;
+
+public class BareBonesBrowserLaunch
+{
+
+ @SuppressWarnings("unchecked")
+ public static void openURL(String url)
+ {
+ String osName = System.getProperty("os.name");
+ try
+ {
+ if (osName.startsWith("Mac OS"))
+ {
+ Class fileMgr =
Class.forName("com.apple.eio.FileManager");
+ Method openURL =
fileMgr.getDeclaredMethod("openURL",
+ new Class[] {String.class});
+ openURL.invoke(null, new Object[] {url});
+ }
+ else if (osName.startsWith("Windows"))
+ Runtime.getRuntime().exec("rundll32
url.dll,FileProtocolHandler " + url);
+ else
+ { //assume Unix or Linux
+ String[] browsers = {
+ "firefox", "opera",
"konqueror", "epiphany", "mozilla", "netscape" };
+ String browser = null;
+ for (int count = 0; count < browsers.length &&
browser == null; count++)
+ if (Runtime.getRuntime().exec(
+ new String[] {"which",
browsers[count]}).waitFor() == 0)
+ browser = browsers[count];
+ if (browser == null)
+ throw new Exception("Could not find web
browser");
+ else
+ Runtime.getRuntime().exec(new String[]
{browser, url});
+ }
+ }
+ catch (Exception e)
+ {
+ }
+ }
+}
+
------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Tux-droid-svn mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tux-droid-svn