Java tutorial
/** * Copyright 2010-present Facebook. * * 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 selfie.time; import java.io.ByteArrayOutputStream; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.List; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; import android.app.AlertDialog; import android.content.Intent; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.content.pm.Signature; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Typeface; import android.os.Bundle; import android.provider.MediaStore; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.text.TextUtils; import android.util.Base64; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.view.WindowManager; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import com.facebook.AppEventsLogger; import com.facebook.FacebookAuthorizationException; import com.facebook.FacebookException; import com.facebook.FacebookOperationCanceledException; import com.facebook.FacebookRequestError; import com.facebook.HttpMethod; import com.facebook.Request; import com.facebook.Request.Callback; import com.facebook.Response; import com.facebook.Session; import com.facebook.SessionState; import com.facebook.UiLifecycleHelper; import com.facebook.model.GraphObject; import com.facebook.model.GraphPlace; import com.facebook.model.GraphUser; import com.facebook.widget.FacebookDialog; import com.facebook.widget.FriendPickerFragment; import com.facebook.widget.LoginButton; import com.facebook.widget.PickerFragment; import com.facebook.widget.PlacePickerFragment; import com.facebook.widget.ProfilePictureView; public class HelloFacebookSampleActivity extends FragmentActivity { private static final String PERMISSION = "manage_pages"; private static final String PAGE_NAME = "YOUR PAGE NAME HERE"; // let's say Paul Walker, Minnions or whatever the name is static final int REQUEST_IMAGE_CAPTURE = 9900; private final String PENDING_ACTION_BUNDLE_KEY = "com.facebook.samples.hellofacebook:PendingAction"; private Button postStatusUpdateButton; private Button postPhotoButton; private Button pickFriendsButton; private Button postSelfieButton; private LoginButton loginButton; private ProfilePictureView profilePictureView; private TextView greeting; private PendingAction pendingAction = PendingAction.NONE; private ViewGroup controlsContainer; private GraphUser user; private GraphPlace place; private List<GraphUser> tags; private boolean canPresentShareDialog; private boolean canPresentShareDialogWithPhotos; private String accessToken = ""; private enum PendingAction { NONE, POST_PHOTO, POST_STATUS_UPDATE, POST_PHOTO_ON_PAGE } private UiLifecycleHelper uiHelper; private Session.StatusCallback callback = new Session.StatusCallback() { @Override public void call(Session session, SessionState state, Exception exception) { onSessionStateChange(session, state, exception); } }; private FacebookDialog.Callback dialogCallback = new FacebookDialog.Callback() { @Override public void onError(FacebookDialog.PendingCall pendingCall, Exception error, Bundle data) { Log.d("HelloFacebook", String.format("Error: %s", error.toString())); } @Override public void onComplete(FacebookDialog.PendingCall pendingCall, Bundle data) { Log.d("HelloFacebook", "Success!"); } }; private Activity context; private EditText et; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); uiHelper = new UiLifecycleHelper(this, callback); uiHelper.onCreate(savedInstanceState); if (savedInstanceState != null) { String name = savedInstanceState.getString(PENDING_ACTION_BUNDLE_KEY); pendingAction = PendingAction.valueOf(name); } context = this; setContentView(R.layout.main); try { PackageInfo info = context.getPackageManager().getPackageInfo("selfie.time", PackageManager.GET_SIGNATURES); //Your package name here for (Signature signature : info.signatures) { MessageDigest md = MessageDigest.getInstance("SHA"); md.update(signature.toByteArray()); Log.e("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT)); } } catch (NameNotFoundException e) { } catch (NoSuchAlgorithmException e) { } final Typeface mFont = Typeface.createFromAsset(getAssets(), "bg.otf"); final ViewGroup mContainer = (ViewGroup) findViewById(android.R.id.content).getRootView(); Util.setAppFont(mContainer, mFont, false); et = (EditText) findViewById(R.id.editText1); loginButton = (LoginButton) findViewById(R.id.login_button); loginButton.setUserInfoChangedCallback(new LoginButton.UserInfoChangedCallback() { @Override public void onUserInfoFetched(GraphUser user) { HelloFacebookSampleActivity.this.user = user; updateUI(); handlePendingAction(); } }); profilePictureView = (ProfilePictureView) findViewById(R.id.profilePicture); greeting = (TextView) findViewById(R.id.greeting); postStatusUpdateButton = (Button) findViewById(R.id.postStatusUpdateButton); postStatusUpdateButton.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { Session session = Session.getActiveSession(); if (session != null) onClickPostStatusUpdate(); else Util.showInfoDialog(context, "Please login with Facebook first."); } }); postPhotoButton = (Button) findViewById(R.id.postPhotoButton); postPhotoButton.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { onClickPostPhoto(); } }); pickFriendsButton = (Button) findViewById(R.id.pickFriendsButton); pickFriendsButton.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { onClickPickFriends(); } }); postSelfieButton = (Button) findViewById(R.id.postSelfieButton); postSelfieButton.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { Session session = Session.getActiveSession(); if (!Util.isNetworkAvailable(context)) Util.showWifiDialog(context); else if (et.getText().toString().length() == 0) Util.showInfoDialog(context, "Please enter text to share with this selfie."); else if (session == null || !session.isOpened()) Util.showInfoDialog(context, "Please login with Facebook first."); else initCapturePhoto(REQUEST_IMAGE_CAPTURE); } }); controlsContainer = (ViewGroup) findViewById(R.id.main_ui_container); final FragmentManager fm = getSupportFragmentManager(); Fragment fragment = fm.findFragmentById(R.id.fragment_container); if (fragment != null) { // If we're being re-created and have a fragment, we need to a) hide the main UI controls and // b) hook up its listeners again. controlsContainer.setVisibility(View.GONE); if (fragment instanceof FriendPickerFragment) { setFriendPickerListeners((FriendPickerFragment) fragment); } else if (fragment instanceof PlacePickerFragment) { setPlacePickerListeners((PlacePickerFragment) fragment); } } // Listen for changes in the back stack so we know if a fragment got popped off because the user // clicked the back button. fm.addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() { @Override public void onBackStackChanged() { if (fm.getBackStackEntryCount() == 0) { // We need to re-show our UI. controlsContainer.setVisibility(View.VISIBLE); } } }); // Can we present the share dialog for regular links? canPresentShareDialog = FacebookDialog.canPresentShareDialog(this, FacebookDialog.ShareDialogFeature.SHARE_DIALOG); // Can we present the share dialog for photos? canPresentShareDialogWithPhotos = FacebookDialog.canPresentShareDialog(this, FacebookDialog.ShareDialogFeature.PHOTOS); } protected void initCapturePhoto(int request_type) { Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (takePictureIntent.resolveActivity(getPackageManager()) != null) { startActivityForResult(takePictureIntent, request_type); } } @Override protected void onResume() { super.onResume(); uiHelper.onResume(); AppEventsLogger.activateApp(this); updateUI(); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); uiHelper.onSaveInstanceState(outState); outState.putString(PENDING_ACTION_BUNDLE_KEY, pendingAction.name()); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); uiHelper.onActivityResult(requestCode, resultCode, data, dialogCallback); if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) { Bundle extras = data.getExtras(); Util.bitmap = (Bitmap) extras.get("data"); if (Util.bitmap != null) { Toast.makeText(this, "Picture taken successfully. Post in progress...", Toast.LENGTH_LONG).show(); performPublish(PendingAction.POST_PHOTO_ON_PAGE, canPresentShareDialogWithPhotos); } } } @Override public void onPause() { super.onPause(); uiHelper.onPause(); AppEventsLogger.deactivateApp(this); } @Override public void onDestroy() { super.onDestroy(); uiHelper.onDestroy(); } private void onSessionStateChange(Session session, SessionState state, Exception exception) { if (pendingAction != PendingAction.NONE && (exception instanceof FacebookOperationCanceledException || exception instanceof FacebookAuthorizationException)) { new AlertDialog.Builder(HelloFacebookSampleActivity.this).setTitle(R.string.cancelled) .setMessage(R.string.permission_not_granted).setPositiveButton(R.string.ok, null).show(); pendingAction = PendingAction.NONE; } else if (state == SessionState.OPENED_TOKEN_UPDATED) { handlePendingAction(); } updateUI(); } private void updateUI() { Session session = Session.getActiveSession(); boolean enableButtons = (session != null && session.isOpened()); postStatusUpdateButton.setEnabled(enableButtons || canPresentShareDialog); postPhotoButton.setEnabled(enableButtons || canPresentShareDialogWithPhotos); pickFriendsButton.setEnabled(enableButtons); if (enableButtons && user != null) { profilePictureView.setProfileId(user.getId()); greeting.setText(getString(R.string.hello_user, user.getFirstName())); } else { profilePictureView.setProfileId(null); greeting.setText(null); } } @SuppressWarnings("incomplete-switch") private void handlePendingAction() { PendingAction previouslyPendingAction = pendingAction; pendingAction = PendingAction.NONE; switch (previouslyPendingAction) { case POST_PHOTO: postPhoto(); break; case POST_STATUS_UPDATE: postStatusUpdate(); break; case POST_PHOTO_ON_PAGE: if (accessToken != null && accessToken.length() != 0) onClickPublishPhotoOnPage(); else requestAccessToken(); break; } } private void requestAccessToken() { if (Session.getActiveSession() != null && Session.getActiveSession().isOpened()) { final ProgressBarDialog dialogBar = new ProgressBarDialog(this, "Cruising..."); dialogBar.show(); Request.newMeRequest(Session.getActiveSession(), new Request.GraphUserCallback() { @Override public void onCompleted(GraphUser user, Response response) { Log.e("Me", "Response : " + user); if (user != null && user.getId() != null) { Log.e("Me", "UserID : " + user.getId()); new Request(Session.getActiveSession(), "/" + user.getId() + "/accounts", null, HttpMethod.GET, new Request.Callback() { public void onCompleted(Response response) { dialogBar.dismiss(); if (response.getRawResponse() != null) { Log.e("Me", "UserID : " + response.getRawResponse()); try { JSONObject obj = new JSONObject(response.getRawResponse()); JSONArray arr = obj.getJSONArray("data"); for (int i = 0; i < arr.length(); i++) { if (arr.getJSONObject(i).getString("name") .equalsIgnoreCase(PAGE_NAME)) { accessToken = arr.getJSONObject(i) .getString("access_token"); break; } } } catch (JSONException e) { e.printStackTrace(); } if (accessToken != null && accessToken.length() != 0) onClickPublishPhotoOnPage(); else Util.showInfoDialog(context, "Oops, post unsuccessful. Please retry or contact App support team."); } else Util.showInfoDialog(context, "Oops, post unsuccessful. Please retry or contact App support team."); } }).executeAsync(); } else { dialogBar.dismiss(); Util.showInfoDialog(context, "Oops, post unsuccessful. Please retry or contact App support team."); } } }).executeAsync(); ; } } private interface GraphObjectWithId extends GraphObject { String getId(); } private void showPublishResult(String message, GraphObject result, FacebookRequestError error) { String title = null; String alertMessage = null; if (error == null) { title = getString(R.string.success); String id = result.cast(GraphObjectWithId.class).getId(); alertMessage = getString(R.string.successfully_posted_post, message, id); } else { title = getString(R.string.error); alertMessage = error.getErrorMessage(); } new AlertDialog.Builder(this).setTitle(title).setMessage(alertMessage).setPositiveButton(R.string.ok, null) .show(); } private void onClickPostStatusUpdate() { performPublish(PendingAction.POST_STATUS_UPDATE, canPresentShareDialog); } private FacebookDialog.ShareDialogBuilder createShareDialogBuilderForLink() { return new FacebookDialog.ShareDialogBuilder(this).setName("Hello Facebook") .setDescription("The 'Hello Facebook' sample application showcases simple Facebook integration") .setLink("http://developers.facebook.com/android"); } private void postStatusUpdate() { if (canPresentShareDialog) { FacebookDialog shareDialog = createShareDialogBuilderForLink().build(); uiHelper.trackPendingDialogCall(shareDialog.present()); } else if (user != null && hasPublishPermission()) { final String message = getString(R.string.status_update, user.getFirstName(), (new Date().toString())); Request request = Request.newStatusUpdateRequest(Session.getActiveSession(), message, place, tags, new Request.Callback() { @Override public void onCompleted(Response response) { showPublishResult(message, response.getGraphObject(), response.getError()); } }); request.executeAsync(); } else { pendingAction = PendingAction.POST_STATUS_UPDATE; } } private void onClickPostPhoto() { performPublish(PendingAction.POST_PHOTO, canPresentShareDialogWithPhotos); } private FacebookDialog.PhotoShareDialogBuilder createShareDialogBuilderForPhoto(Bitmap... photos) { return new FacebookDialog.PhotoShareDialogBuilder(this).addPhotos(Arrays.asList(photos)); } private void postPhoto() { Bitmap image = BitmapFactory.decodeResource(this.getResources(), R.drawable.icon); if (canPresentShareDialogWithPhotos) { FacebookDialog shareDialog = createShareDialogBuilderForPhoto(image).build(); uiHelper.trackPendingDialogCall(shareDialog.present()); } else if (hasPublishPermission()) { Request request = Request.newUploadPhotoRequest(Session.getActiveSession(), image, new Request.Callback() { @Override public void onCompleted(Response response) { showPublishResult(getString(R.string.photo_post), response.getGraphObject(), response.getError()); } }); request.executeAsync(); } else { pendingAction = PendingAction.POST_PHOTO; } } private void showPickerFragment(PickerFragment<?> fragment) { fragment.setOnErrorListener(new PickerFragment.OnErrorListener() { @Override public void onError(PickerFragment<?> pickerFragment, FacebookException error) { String text = getString(R.string.exception, error.getMessage()); Toast toast = Toast.makeText(HelloFacebookSampleActivity.this, text, Toast.LENGTH_SHORT); toast.show(); } }); FragmentManager fm = getSupportFragmentManager(); fm.beginTransaction().replace(R.id.fragment_container, fragment).addToBackStack(null).commit(); controlsContainer.setVisibility(View.GONE); fm.executePendingTransactions(); fragment.loadData(false); } private void onClickPickFriends() { final FriendPickerFragment fragment = new FriendPickerFragment(); setFriendPickerListeners(fragment); showPickerFragment(fragment); } private void setFriendPickerListeners(final FriendPickerFragment fragment) { fragment.setOnDoneButtonClickedListener(new FriendPickerFragment.OnDoneButtonClickedListener() { @Override public void onDoneButtonClicked(PickerFragment<?> pickerFragment) { onFriendPickerDone(fragment); } }); } private void onFriendPickerDone(FriendPickerFragment fragment) { FragmentManager fm = getSupportFragmentManager(); fm.popBackStack(); String results = ""; List<GraphUser> selection = fragment.getSelection(); tags = selection; if (selection != null && selection.size() > 0) { ArrayList<String> names = new ArrayList<String>(); for (GraphUser user : selection) { names.add(user.getName()); } results = TextUtils.join(", ", names); } else { results = getString(R.string.no_friends_selected); } showAlert(getString(R.string.you_picked), results); } private void onPlacePickerDone(PlacePickerFragment fragment) { FragmentManager fm = getSupportFragmentManager(); fm.popBackStack(); String result = ""; GraphPlace selection = fragment.getSelection(); if (selection != null) { result = selection.getName(); } else { result = getString(R.string.no_place_selected); } place = selection; showAlert(getString(R.string.you_picked), result); } private void onClickPublishPhotoOnPage() { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); Util.bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream); Bundle postParams = new Bundle(); postParams.putString("message", et.getText().toString()); postParams.putParcelable("picture", Util.bitmap); postParams.putString("access_token", accessToken); final ProgressBarDialog dialogBar = new ProgressBarDialog(this, "Cruising..."); dialogBar.show(); new Request(null, "/OSAF.FAA/photos", postParams, HttpMethod.POST, new Callback() { @Override public void onCompleted(Response response) { dialogBar.dismiss(); if (response.getError() != null) Log.e("FRAGACTIVITY", response.getError().toString()); if (response.getRawResponse() != null) Log.e("FRAGACTIVITY", response.getRawResponse()); if (response.getRawResponse() != null && response.getRawResponse().contains("post_id")) Util.showInfoDialog(context, "Selfie posted! Please check OSAF's Facebook page to view your post."); else Util.showInfoDialog(context, "Oops, post unsuccessful. Please retry or contact App support team."); refreshUI(); } }).executeAsync(); } protected void refreshUI() { et.setText(""); Util.bitmap = null; } private void setPlacePickerListeners(final PlacePickerFragment fragment) { fragment.setOnDoneButtonClickedListener(new PlacePickerFragment.OnDoneButtonClickedListener() { @Override public void onDoneButtonClicked(PickerFragment<?> pickerFragment) { onPlacePickerDone(fragment); } }); fragment.setOnSelectionChangedListener(new PlacePickerFragment.OnSelectionChangedListener() { @Override public void onSelectionChanged(PickerFragment<?> pickerFragment) { if (fragment.getSelection() != null) { onPlacePickerDone(fragment); } } }); } private void showAlert(String title, String message) { new AlertDialog.Builder(this).setTitle(title).setMessage(message).setPositiveButton(R.string.ok, null) .show(); } private boolean hasPublishPermission() { Session session = Session.getActiveSession(); return session != null && session.getPermissions().contains("manage_pages"); } private void performPublish(PendingAction action, boolean allowNoSession) { Session session = Session.getActiveSession(); if (session != null) { pendingAction = action; if (hasPublishPermission()) { handlePendingAction(); return; } else if (session.isOpened()) { session.requestNewPublishPermissions(new Session.NewPermissionsRequest(this, PERMISSION)); return; } } if (allowNoSession) { pendingAction = action; handlePendingAction(); } } }