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 com.smashedin.facebook; import com.smashedin.smashedin.R; import android.accounts.AccountManager; import android.app.Dialog; import android.app.ProgressDialog; import android.content.Context; 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.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.util.Base64; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.LinearLayout; import android.widget.ProgressBar; import android.widget.Toast; import com.facebook.*; import com.facebook.model.GraphUser; import com.facebook.widget.*; import com.google.android.gms.auth.GooglePlayServicesAvailabilityException; import com.google.android.gms.auth.UserRecoverableAuthException; import com.google.android.gms.common.AccountPicker; import com.google.android.gms.common.GooglePlayServicesUtil; import com.smashedin.async.SmashedAsyncClient; import com.smashedin.async.SmashedAsyncClient.OnResponseListener; import com.smashedin.smashed.Singleton; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class HelloFacebookSampleActivity extends FragmentActivity implements OnResponseListener { private LoginButton loginButton; private GraphUser user; private UiLifecycleHelper uiHelper; private String mEmail; private static final String SCOPE = "oauth2:https://www.googleapis.com/auth/userinfo.profile"; public static final String EXTRA_ACCOUNTNAME = "extra_accountname"; static final int REQUEST_CODE_PICK_ACCOUNT = 1000; static final int REQUEST_CODE_RECOVER_FROM_AUTH_ERROR = 1001; static final int REQUEST_CODE_RECOVER_FROM_PLAY_SERVICES_ERROR = 1002; ProgressDialog oPd; 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!"); } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); try { PackageInfo info = getPackageManager().getPackageInfo("com.smashedin.smashedin", PackageManager.GET_SIGNATURES); for (Signature signature : info.signatures) { MessageDigest md = MessageDigest.getInstance("SHA"); md.update(signature.toByteArray()); Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT)); } } catch (NameNotFoundException e) { } catch (NoSuchAlgorithmException e) { } uiHelper = new UiLifecycleHelper(this, callback); uiHelper.onCreate(savedInstanceState); setContentView(R.layout.main); loginButton = (LoginButton) findViewById(R.id.login_button); loginButton.setUserInfoChangedCallback(new LoginButton.UserInfoChangedCallback() { @Override public void onUserInfoFetched(GraphUser user) { HelloFacebookSampleActivity.this.user = user; updateUI(); } }); Bundle extras = getIntent().getExtras(); if (extras != null && extras.containsKey(EXTRA_ACCOUNTNAME)) { mEmail = extras.getString(EXTRA_ACCOUNTNAME); getTask(HelloFacebookSampleActivity.this, mEmail, SCOPE).execute(); } /* LinearLayout nevermind = (LinearLayout) findViewById(R.id.nevermind); nevermind.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Singleton.getInstance().m_bnevermind = true; finish(); } }); */ } @Override protected void onResume() { super.onResume(); uiHelper.onResume(); // Call the 'activateApp' method to log an app event for use in analytics and advertising reporting. Do so in // the onResume methods of the primary Activities that an app may be launched into. AppEventsLogger.activateApp(this); updateUI(); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); uiHelper.onActivityResult(requestCode, resultCode, data, dialogCallback); if (requestCode == REQUEST_CODE_PICK_ACCOUNT) { if (resultCode == RESULT_OK) { mEmail = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME); getUsername(); } else if (resultCode == RESULT_CANCELED) { Toast.makeText(this, "You must pick an account", Toast.LENGTH_SHORT).show(); } } else if ((requestCode == REQUEST_CODE_RECOVER_FROM_AUTH_ERROR || requestCode == REQUEST_CODE_RECOVER_FROM_PLAY_SERVICES_ERROR) && resultCode == RESULT_OK) { handleAuthorizeResult(resultCode, data); return; } } private void handleAuthorizeResult(int resultCode, Intent data) { if (data == null) { show("Unknown error, click the button again"); return; } if (resultCode == RESULT_OK) { getTask(this, mEmail, SCOPE).execute(); return; } if (resultCode == RESULT_CANCELED) { show("User rejected authorization."); return; } show("Unknown error, click the button again"); } @Override public void onPause() { super.onPause(); uiHelper.onPause(); } @Override public void onDestroy() { super.onDestroy(); uiHelper.onDestroy(); } private void onSessionStateChange(Session session, SessionState state, Exception exception) { updateUI(); } private void updateUI() { Session session = Session.getActiveSession(); boolean enableButtons = (session != null && session.isOpened()); if (enableButtons && user != null) { oPd = new ProgressDialog(this); oPd.setTitle("Trying to get Smashed..."); oPd.setMessage("Please wait."); oPd.setIndeterminate(true); oPd.setCancelable(false); oPd.show(); //logged in String accessToken = session.getAccessToken(); Singleton.getInstance().SetAccessToken(accessToken); String url = "http://www.smashed.in/auth/post/facebook?access_token=" + accessToken; SmashedAsyncClient oAsyncClient = new SmashedAsyncClient(); oAsyncClient.Attach(this); oAsyncClient.SetPersistantStorage(getApplicationContext()); oAsyncClient.MakeCall(url); } } @Override public void OnResponse(String response, String tag, Object obj) { if (oPd != null) oPd.dismiss(); Singleton.getInstance().parseJsonUserDetails(response); Singleton.getInstance().m_bnevermind = false; Singleton.getInstance().loggedIn = true; finish(); } public void greetTheUser(View view) { ProgressBar oProgress = (ProgressBar) findViewById(R.id.progressLog); oProgress.setVisibility(View.VISIBLE); getUsername(); } /** Attempt to get the user name. If the email address isn't known yet, * then call pickUserAccount() method so the user can pick an account. */ private void getUsername() { if (mEmail == null) { pickUserAccount(); } else { if (isDeviceOnline()) { getTask(HelloFacebookSampleActivity.this, mEmail, SCOPE).execute(); } else { Toast.makeText(this, "No network connection available", Toast.LENGTH_SHORT).show(); } } } /** Starts an activity in Google Play Services so the user can pick an account */ private void pickUserAccount() { String[] accountTypes = new String[] { "com.google" }; Intent intent = AccountPicker.newChooseAccountIntent(null, null, accountTypes, false, null, null, null, null); startActivityForResult(intent, REQUEST_CODE_PICK_ACCOUNT); } /** Checks whether the device currently has a network connection */ private boolean isDeviceOnline() { ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); if (networkInfo != null && networkInfo.isConnected()) { return true; } return false; } public void show(final String message) { runOnUiThread(new Runnable() { @Override public void run() { //mOut.setText(message); } }); } public void handleException(final Exception e) { runOnUiThread(new Runnable() { @Override public void run() { if (e instanceof GooglePlayServicesAvailabilityException) { // The Google Play services APK is old, disabled, or not present. // Show a dialog created by Google Play services that allows // the user to update the APK int statusCode = ((GooglePlayServicesAvailabilityException) e).getConnectionStatusCode(); Dialog dialog = GooglePlayServicesUtil.getErrorDialog(statusCode, HelloFacebookSampleActivity.this, REQUEST_CODE_RECOVER_FROM_PLAY_SERVICES_ERROR); dialog.show(); } else if (e instanceof UserRecoverableAuthException) { // Unable to authenticate, such as when the user has not yet granted // the app access to the account, but the user can fix this. // Forward the user to an activity in Google Play services. Intent intent = ((UserRecoverableAuthException) e).getIntent(); startActivityForResult(intent, REQUEST_CODE_RECOVER_FROM_PLAY_SERVICES_ERROR); } } }); } private AbstractGetNameTask getTask(HelloFacebookSampleActivity activity, String email, String scope) { return new GetNameInForeground(activity, email, scope); } public void AuthenticateGoogleFromSmashed(String token) { /*oPd = new ProgressDialog(this); oPd.setTitle("Trying to get Smashed..."); oPd.setMessage("Please wait."); oPd.setIndeterminate(true); oPd.setCancelable(false); oPd.show();*/ String accessToken = token; Singleton.getInstance().SetAccessTokenGoogle(accessToken); String url = "http://www.smashed.in/auth/post/google?access_token=" + accessToken; SmashedAsyncClient oAsyncClient = new SmashedAsyncClient(); oAsyncClient.Attach(this); oAsyncClient.SetPersistantStorage(getApplicationContext()); oAsyncClient.MakeCall(url); } @Override public void OnFailure() { if (oPd != null) oPd.dismiss(); } }