Back to project page SignUpViaGooglePlusOrFacebook.
The source code is released under:
Apache License
If you think the Android project SignUpViaGooglePlusOrFacebook listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * Copyright (C) 2011 iMellon/* w w w . j a v a 2 s . co m*/ * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you 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.amitsid.signupwithgandfb; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; 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.os.Bundle; import android.util.Base64; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import com.facebook.AppEventsLogger; import com.facebook.FacebookAuthorizationException; import com.facebook.FacebookOperationCanceledException; import com.facebook.Session; import com.facebook.SessionState; import com.facebook.UiLifecycleHelper; import com.facebook.model.GraphUser; import com.facebook.widget.FacebookDialog; import com.facebook.widget.LoginButton; import com.facebook.widget.ProfilePictureView; public class GooglePlusActivity extends Activity { private final String PENDING_ACTION_BUNDLE_KEY = "com.facebook.samples.hellofacebook:PendingAction"; private LoginButton loginButton; private TextView greeting; private PendingAction pendingAction = PendingAction.NONE; private GraphUser user; private ProfilePictureView profilePictureView; private Button launchOauth; private enum PendingAction { NONE, POST_PHOTO, POST_STATUS_UPDATE } 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!"); } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); try { PackageInfo info = getPackageManager().getPackageInfo( "com.amitsid.signupwithgandfb", PackageManager.GET_SIGNATURES); // Your package name // here for (Signature signature : info.signatures) { MessageDigest md = MessageDigest.getInstance("SHA"); md.update(signature.toByteArray()); Log.i("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT)); } } catch (NameNotFoundException e) { } catch (NoSuchAlgorithmException e) { } launchOauth = (Button) findViewById(R.id.btn_launch_oauth); launchOauth.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { startActivity(new Intent().setClass(v.getContext(), OAuthActivity.class)); } }); profilePictureView = (ProfilePictureView) findViewById(R.id.profilePicture); greeting = (TextView) findViewById(R.id.greeting); uiHelper = new UiLifecycleHelper(this, callback); uiHelper.onCreate(savedInstanceState); if (savedInstanceState != null) { String name = savedInstanceState .getString(PENDING_ACTION_BUNDLE_KEY); pendingAction = PendingAction.valueOf(name); } loginButton = (LoginButton) findViewById(R.id.login_button); loginButton .setUserInfoChangedCallback(new LoginButton.UserInfoChangedCallback() { @Override public void onUserInfoFetched(GraphUser user) { GooglePlusActivity.this.user = user; updateUI(); } }); greeting = (TextView) findViewById(R.id.greeting); } @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); } @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(GooglePlusActivity.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) { } updateUI(); } private void updateUI() { Session session = Session.getActiveSession(); boolean enableButtons = (session != null && session.isOpened()); if (enableButtons && user != null) { profilePictureView.setVisibility(ProfilePictureView.VISIBLE); profilePictureView.setProfileId(user.getId()); greeting.setText(getString(R.string.hello_user, user.getFirstName())); } else { profilePictureView.setProfileId(null); greeting.setText(null); } } }