Permalink
Browse files

Added classes that will be necessary for facebook sharing

  • Loading branch information...
1 parent 8ab213c commit 8b87535b8c01a57b3425a6f2eaaa3447a5c17a2c Thom Andretti committed Dec 1, 2011
View
@@ -3,5 +3,6 @@
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="gen"/>
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
- <classpathentry kind="output" path="bin"/>
+ <classpathentry kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
+ <classpathentry kind="output" path="bin/classes"/>
</classpath>
@@ -4,8 +4,9 @@
# This file must be checked in Version Control Systems.
#
# To customize properties used by the Ant build system use,
-# "build.properties", and override values to adapt the script to your
+# "ant.properties", and override values to adapt the script to your
# project structure.
# Project target.
target=android-8
+android.library.reference.1=../../com_facebook_android
@@ -0,0 +1,41 @@
+package com.explor.sharing;
+
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.net.MalformedURLException;
+
+import android.util.Log;
+
+import com.facebook.android.AsyncFacebookRunner.RequestListener;
+import com.facebook.android.FacebookError;
+
+/**
+ * Skeleton base class for RequestListeners, providing default error
+ * handling. Applications should handle these error conditions.
+ *
+ */
+public abstract class BaseRequestListener implements RequestListener {
+
+ public void onFacebookError(FacebookError e, final Object state) {
+ Log.e("Facebook", e.getMessage());
+ e.printStackTrace();
+ }
+
+ public void onFileNotFoundException(FileNotFoundException e,
+ final Object state) {
+ Log.e("Facebook", e.getMessage());
+ e.printStackTrace();
+ }
+
+ public void onIOException(IOException e, final Object state) {
+ Log.e("Facebook", e.getMessage());
+ e.printStackTrace();
+ }
+
+ public void onMalformedURLException(MalformedURLException e,
+ final Object state) {
+ Log.e("Facebook", e.getMessage());
+ e.printStackTrace();
+ }
+
+}
@@ -0,0 +1,117 @@
+package com.explor.sharing;
+
+import java.io.IOException;
+
+import android.app.Activity;
+import android.content.Context;
+import android.os.Bundle;
+import android.os.Handler;
+
+import com.explor.sharing.SessionEvents.AuthListener;
+import com.explor.sharing.SessionEvents.LogoutListener;
+import com.facebook.android.AsyncFacebookRunner;
+import com.facebook.android.DialogError;
+import com.facebook.android.Facebook;
+import com.facebook.android.Facebook.DialogListener;
+import com.facebook.android.FacebookError;
+
+public class FacebookConnector {
+
+ private Facebook facebook = null;
+ private String[] permissions;
+ private Activity activity;
+ private Context context;
+ private Handler mHandler;
+ private SessionListener mSessionListener = new SessionListener();
+
+ public FacebookConnector(String appId, Activity activity, Context context, String[] permissions) {
+ this.facebook = new Facebook(appId);
+
+ SessionStore.restore(facebook, context);
+ SessionEvents.addAuthListener(mSessionListener);
+ SessionEvents.addLogoutListener(mSessionListener);
+
+ this.context = context;
+ this.permissions = permissions;
+ this.mHandler = new Handler();
+ this.activity = activity;
+ }
+
+ public void login() {
+ if (!facebook.isSessionValid()) {
+ facebook.authorize(this.activity, this.permissions,new LoginDialogListener());
+ }
+ }
+
+ public void logout() {
+ SessionEvents.onLogoutBegin();
+ AsyncFacebookRunner asyncRunner = new AsyncFacebookRunner(this.facebook);
+ asyncRunner.logout(this.context, new LogoutRequestListener());
+ }
+
+ public void postMessageOnWall(String msg) {
+ if (facebook.isSessionValid()) {
+ Bundle parameters = new Bundle();
+ parameters.putString("message", msg);
+ try {
+ String response = facebook.request("me/feed", parameters,"POST");
+ System.out.println(response);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ } else {
+ login();
+ }
+ }
+
+ private final class LoginDialogListener implements DialogListener {
+ public void onComplete(Bundle values) {
+ SessionEvents.onLoginSuccess();
+ }
+
+ public void onFacebookError(FacebookError error) {
+ SessionEvents.onLoginError(error.getMessage());
+ }
+
+ public void onError(DialogError error) {
+ SessionEvents.onLoginError(error.getMessage());
+ }
+
+ public void onCancel() {
+ SessionEvents.onLoginError("Action Canceled");
+ }
+ }
+
+ public class LogoutRequestListener extends BaseRequestListener {
+ public void onComplete(String response, final Object state) {
+ // callback should be run in the original thread,
+ // not the background thread
+ mHandler.post(new Runnable() {
+ public void run() {
+ SessionEvents.onLogoutFinish();
+ }
+ });
+ }
+ }
+
+ private class SessionListener implements AuthListener, LogoutListener {
+
+ public void onAuthSucceed() {
+ SessionStore.save(facebook, context);
+ }
+
+ public void onAuthFail(String error) {
+ }
+
+ public void onLogoutBegin() {
+ }
+
+ public void onLogoutFinish() {
+ SessionStore.clear(context);
+ }
+ }
+
+ public Facebook getFacebook() {
+ return this.facebook;
+ }
+}
@@ -0,0 +1,146 @@
+/*
+ * Copyright 2010 Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.explor.sharing;
+
+import java.util.LinkedList;
+
+public class SessionEvents {
+
+ private static LinkedList<AuthListener> mAuthListeners =
+ new LinkedList<AuthListener>();
+ private static LinkedList<LogoutListener> mLogoutListeners =
+ new LinkedList<LogoutListener>();
+
+ /**
+ * Associate the given listener with this Facebook object. The listener's
+ * callback interface will be invoked when authentication events occur.
+ *
+ * @param listener
+ * The callback object for notifying the application when auth
+ * events happen.
+ */
+ public static void addAuthListener(AuthListener listener) {
+ mAuthListeners.add(listener);
+ }
+
+ /**
+ * Remove the given listener from the list of those that will be notified
+ * when authentication events occur.
+ *
+ * @param listener
+ * The callback object for notifying the application when auth
+ * events happen.
+ */
+ public static void removeAuthListener(AuthListener listener) {
+ mAuthListeners.remove(listener);
+ }
+
+ /**
+ * Associate the given listener with this Facebook object. The listener's
+ * callback interface will be invoked when logout occurs.
+ *
+ * @param listener
+ * The callback object for notifying the application when log out
+ * starts and finishes.
+ */
+ public static void addLogoutListener(LogoutListener listener) {
+ mLogoutListeners.add(listener);
+ }
+
+ /**
+ * Remove the given listener from the list of those that will be notified
+ * when logout occurs.
+ *
+ * @param listener
+ * The callback object for notifying the application when log out
+ * starts and finishes.
+ */
+ public static void removeLogoutListener(LogoutListener listener) {
+ mLogoutListeners.remove(listener);
+ }
+
+ public static void onLoginSuccess() {
+ for (AuthListener listener : mAuthListeners) {
+ listener.onAuthSucceed();
+ }
+ }
+
+ public static void onLoginError(String error) {
+ for (AuthListener listener : mAuthListeners) {
+ listener.onAuthFail(error);
+ }
+ }
+
+ public static void onLogoutBegin() {
+ for (LogoutListener l : mLogoutListeners) {
+ l.onLogoutBegin();
+ }
+ }
+
+ public static void onLogoutFinish() {
+ for (LogoutListener l : mLogoutListeners) {
+ l.onLogoutFinish();
+ }
+ }
+
+ /**
+ * Callback interface for authorization events.
+ *
+ */
+ public static interface AuthListener {
+
+ /**
+ * Called when a auth flow completes successfully and a valid OAuth
+ * Token was received.
+ *
+ * Executed by the thread that initiated the authentication.
+ *
+ * API requests can now be made.
+ */
+ public void onAuthSucceed();
+
+ /**
+ * Called when a login completes unsuccessfully with an error.
+ *
+ * Executed by the thread that initiated the authentication.
+ */
+ public void onAuthFail(String error);
+ }
+
+ /**
+ * Callback interface for logout events.
+ *
+ */
+ public static interface LogoutListener {
+ /**
+ * Called when logout begins, before session is invalidated.
+ * Last chance to make an API call.
+ *
+ * Executed by the thread that initiated the logout.
+ */
+ public void onLogoutBegin();
+
+ /**
+ * Called when the session information has been cleared.
+ * UI should be updated to reflect logged-out state.
+ *
+ * Executed by the thread that initiated the logout.
+ */
+ public void onLogoutFinish();
+ }
+
+}
@@ -0,0 +1,55 @@
+
+
+/*
+ * Copyright 2010 Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.explor.sharing;
+
+import com.facebook.android.Facebook;
+import android.content.Context;
+import android.content.SharedPreferences;
+import android.content.SharedPreferences.Editor;
+
+public class SessionStore {
+
+ private static final String TOKEN = "access_token";
+ private static final String EXPIRES = "expires_in";
+ private static final String KEY = "facebook-session";
+
+ public static boolean save(Facebook session, Context context) {
+ Editor editor =
+ context.getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
+ editor.putString(TOKEN, session.getAccessToken());
+ editor.putLong(EXPIRES, session.getAccessExpires());
+ return editor.commit();
+ }
+
+ public static boolean restore(Facebook session, Context context) {
+ SharedPreferences savedSession =
+ context.getSharedPreferences(KEY, Context.MODE_PRIVATE);
+ session.setAccessToken(savedSession.getString(TOKEN, null));
+ session.setAccessExpires(savedSession.getLong(EXPIRES, 0));
+ return session.isSessionValid();
+ }
+
+ public static void clear(Context context) {
+ Editor editor =
+ context.getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
+ editor.clear();
+ editor.commit();
+ }
+
+}
Oops, something went wrong.

0 comments on commit 8b87535

Please sign in to comment.