Java tutorial
/* * Author: cooperok * cooperok.so@gmail.com * * 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.cooperok.socialuser.fb; import java.net.URI; import java.net.URISyntaxException; import java.util.List; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; import com.cooperok.socialuser.SocialDataLoadListener; import com.cooperok.socialuser.SocialUserInfo; import com.cooperok.socialuser.SocialUserInterface; import com.cooperok.socialuser.User; import com.cooperok.socialuser.callbacks.LikeCallback; import com.cooperok.socialuser.callbacks.LikeGroupCallback; import com.cooperok.socialuser.callbacks.PostImageCallback; import com.facebook.HttpMethod; import com.facebook.LoggingBehavior; import com.facebook.Request; import com.facebook.Response; import com.facebook.Session; import com.facebook.SessionState; import com.facebook.Settings; import com.facebook.internal.ImageDownloader; import com.facebook.internal.ImageRequest; import com.facebook.internal.ImageResponse; import com.facebook.model.GraphObject; import com.facebook.model.GraphUser; public class FbAccount implements SocialUserInterface { private int mSocialType; private FbUserInfo mUserInfo; private SocialDataLoadListener mListener; public FbAccount() { mSocialType = User.SocialUserType.FACEBOOK; mUserInfo = new FbUserInfo(); } public boolean havePublishPermissions() { boolean have = false; List<String> perm = Session.getActiveSession().getPermissions(); for (String permission : perm) { if (Session.isPublishPermission(permission)) { have = true; } } return have; } public void restore(final Activity activity, Bundle savedInstanceState) { Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS); Session session = Session.getActiveSession(); if (session == null) { session = new Session(activity); Session.setActiveSession(session); if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) { session.openForRead(new Session.OpenRequest(activity).setCallback(new Session.StatusCallback() { @Override public void call(Session session, SessionState state, Exception exception) { if (state.isOpened()) { User.getInstance().onUserAuthorized(FbAccount.this, activity); } } })); } } } @Override public void loadUserData(Context context, SocialDataLoadListener listener) { mListener = listener; Session session = Session.getActiveSession(); if (session.isOpened()) { loadUserTitle(context); } } @Override public String getAccessToken() { return Session.getActiveSession().getAccessToken(); } @Override public String getSocialId() { return mUserInfo.getUserId(); } @Override public boolean isAuthorized() { return Session.getActiveSession().isOpened() && mUserInfo.dataExists(); } @Override public void postImage(Bitmap image, final PostImageCallback callback) { final Session session = Session.getActiveSession(); if (session != null && session.isOpened()) { Bundle params = new Bundle(); params.putString("message", ""); params.putParcelable("picture", image); params.putParcelable("source", image); Request request = new Request(Session.getActiveSession(), "me/photos", params, HttpMethod.POST); request.setCallback(new Request.Callback() { @Override public void onCompleted(Response response) { if (response.getError() == null) { String postId = null; try { postId = response.getGraphObject().getInnerJSONObject().getString("post_id"); } catch (JSONException e) { } callback.onImagePosted(FbAccount.this, postId); } else { callback.onImagePostedError(FbAccount.this); } } }); request.executeAsync(); } } @Override public int getSocialType() { return mSocialType; } @Override public SocialUserInfo getUserInfo() { return mUserInfo; } private void loadUserTitle(final Context context) { final Session session = Session.getActiveSession(); if (session != null && session.isOpened()) { // If the session is open, make an API call to get user data // and define a new callback to handle the response Request request = Request.newMeRequest(session, new Request.GraphUserCallback() { @Override public void onCompleted(GraphUser user, Response response) { // If the response is successful if (session == Session.getActiveSession()) { if (user != null) { mUserInfo.setUser(user); // ? ? , loadUserImage(context); } } } }); Request.executeBatchAsync(request); } } private void loadUserImage(Context context) { if (mUserInfo.getUserId() != null) { try { URI uri = ImageRequest.getProfilePictureUrl(mUserInfo.getUserId(), 200, 200); mUserInfo.setUserAvatar(uri.toString()); ImageRequest.Builder requestBuilder = new ImageRequest.Builder(context, uri); ImageRequest request = requestBuilder.setAllowCachedRedirects(true).setCallerTag(this) .setCallback(new ImageRequest.Callback() { @Override public void onCompleted(ImageResponse response) { mUserInfo.setUserPicture(response.getBitmap()); // ? ?, // , dispatchUserDataLoaded(); } }).build(); ImageDownloader.downloadAsync(request); } catch (URISyntaxException e) { } } } private void dispatchUserDataLoaded() { mListener.onDataLoad(this); } @Override public void likeGroup(Context context, final LikeGroupCallback callback) { try { context.getPackageManager().getPackageInfo("com.facebook.katana", 0); // TODO fb group id Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("NEED_DROUP_ID")); context.startActivity(i); } catch (Exception e) { // Need to lauch some activity with webview if we don't have isntalled facebook application // Intent i = new Intent(context, FacebookLigeGroupActivity.class); // context.startActivity(i); } } @Override public void checkLikePage(String pageId, final LikeCallback callback) { new Request(Session.getActiveSession(), "/me/likes/" + pageId, null, HttpMethod.GET, new Request.Callback() { public void onCompleted(Response response) { boolean result = false; GraphObject object = response.getGraphObject(); if (object != null) { JSONObject resp = object.getInnerJSONObject(); if (resp != null) { try { JSONArray data = resp.getJSONArray("data"); if (data.length() > 0) { result = true; } } catch (JSONException e) { } } } callback.onLikeChecked(result); } }).executeAsync(); } // @Override // public void likeGroup(Context context, final LikeGroupCallback callback) { // final Session session = Session.getActiveSession(); // if (session != null && session.isOpened()) { // Bundle params = new Bundle(); // params.putString("object", context.getResources().getString(R.string.facebook_group_id)); // // Request request = new Request(Session.getActiveSession(), "me/og.likes", params, HttpMethod.POST); // request.setCallback(new Request.Callback() { // @Override // public void onCompleted(Response response) { // if (response.getError() == null) { // Log.i("test", "everything is ok"); // callback.onLikeGroup(FbAccount.this); // } else { // Log.i("test", response.getError().toString()); // callback.onLikeGroupError(FbAccount.this); // } // } // }); // request.executeAsync(); // } // } }