Skip to content

Instantly share code, notes, and snippets.

@afarber
Last active September 16, 2015 15:38
Show Gist options
  • Save afarber/0fa3346e570645e74c19 to your computer and use it in GitHub Desktop.
Save afarber/0fa3346e570645e74c19 to your computer and use it in GitHub Desktop.
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentSender;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.plus.Plus;
import com.google.android.gms.plus.model.people.Person;
import com.squareup.picasso.Picasso;
import java.util.List;
public class GoogleFragment extends Fragment
implements
View.OnClickListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
public final static String TAG = "GoogleFragment";
public final static int REQUEST_RESOLVE_ERROR = 1001;
private static final String ARGS_DIALOG_ERROR = "ARGS_DIALOG_ERROR";
private static final String TAG_DIALOG_ERROR = "TAG_DIALOG_ERROR";
private static final String STATE_RESOLVING_ERROR = "STATE_RESOLVING_ERROR";
private GoogleApiClient mGoogleApiClient;
private boolean mResolvingError = false;
private ImageButton mLoginButton;
private ImageButton mLogoutButton;
public GoogleFragment() {
// Required empty public constructor
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
Log.d(TAG, "onAttach");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_google, container, false);
mLoginButton = (ImageButton) v.findViewById(R.id.login_button);
mLoginButton.setOnClickListener(this);
mLogoutButton = (ImageButton) v.findViewById(R.id.logout_button);
mLogoutButton.setOnClickListener(this);
if (savedInstanceState == null) {
mResolvingError = false;
} else {
mResolvingError = savedInstanceState.getBoolean(STATE_RESOLVING_ERROR, false);
}
return v;
}
@Override
public void onSaveInstanceState(final Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(STATE_RESOLVING_ERROR, mResolvingError);
}
private void googleLogin() {
mGoogleApiClient = new GoogleApiClient.Builder(getContext())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_PROFILE)
.build();
mGoogleApiClient.connect();
}
private void googleLogout() {
if (mGoogleApiClient != null &&
(mGoogleApiClient.isConnecting() ||
mGoogleApiClient.isConnected())) {
mGoogleApiClient.disconnect();
mGoogleApiClient = null;
}
}
public void onResult(int resultCode) {
mResolvingError = false;
if (resultCode == Activity.RESULT_OK &&
mGoogleApiClient != null &&
!mGoogleApiClient.isConnecting() &&
!mGoogleApiClient.isConnected()) {
mGoogleApiClient.connect();
}
}
@Override
public void onClick(View v) {
if (v == mLoginButton)
googleLogin();
else
googleLogout();
}
@Override
public void onConnected(Bundle bundle) {
if (mGoogleApiClient == null)
return;
Person me = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
Utils.logd("onConnected " + me);
if (me != null) {
String id = me.getId();
Person.Name name = me.getName();
String given = name.getGivenName();
String family = name.getFamilyName();
boolean female = (me.hasGender() && me.getGender() == 1);
String photo = null;
if (me.hasImage() && me.getImage().hasUrl()) {
photo = me.getImage().getUrl();
}
String city = "Unknown city";
List<Person.PlacesLived> places = me.getPlacesLived();
if (places != null) {
for (Person.PlacesLived place : places) {
city = place.getValue();
if (place.isPrimary())
break;
}
}
Log.d(TAG, "Given: " + given + ", Family: " + family + ", Female: " + female + ", City: " + city);
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
if (mResolvingError) {
// Already attempting to resolve an error.
return;
} else if (connectionResult.hasResolution()) {
try {
mResolvingError = true;
connectionResult.startResolutionForResult(getActivity(), REQUEST_RESOLVE_ERROR);
} catch (IntentSender.SendIntentException e) {
// There was an error with the resolution intent. Try again.
if (mGoogleApiClient != null)
mGoogleApiClient.connect();
}
} else {
// Show dialog using GoogleApiAvailability.getErrorDialog()
showErrorDialog(connectionResult.getErrorCode());
mResolvingError = true;
}
}
private void showErrorDialog(int errorCode) {
// Create a fragment for the error dialog
ErrorDialogFragment dialogFragment = new ErrorDialogFragment();
// Pass the error that should be displayed
Bundle args = new Bundle();
args.putInt(ARGS_DIALOG_ERROR, errorCode);
dialogFragment.setArguments(args);
dialogFragment.show(getActivity().getSupportFragmentManager(), TAG_DIALOG_ERROR);
}
/* A fragment to display an error dialog */
public class ErrorDialogFragment extends DialogFragment {
public ErrorDialogFragment() {
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Get the error code and retrieve the appropriate dialog
int errorCode = this.getArguments().getInt(ARGS_DIALOG_ERROR);
return GoogleApiAvailability.getInstance().getErrorDialog(
this.getActivity(),
errorCode,
REQUEST_RESOLVE_ERROR);
}
@Override
public void onDismiss(DialogInterface dialog) {
mResolvingError = false; // DOES NOT COMPILE
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment