Created
June 24, 2015 14:02
-
-
Save sw1ftc0d3r/b7e4074452279a296d99 to your computer and use it in GitHub Desktop.
GooglePlus
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @author Andrea Cappelli | |
*/ | |
public class GooglePlusUtilsConnection implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { | |
private GoogleApiClient mGoogleApiClient; | |
public boolean mIntentInProgress; | |
public boolean mSignInClicked; | |
private ConnectionResult mConnectionResult; | |
private Context mContext; | |
private RadialMenuItem mMenuItem; | |
public GooglePlusUtilsConnection(Context context, RadialMenuItem menuItem) { | |
mContext = context; | |
mMenuItem = menuItem; | |
} | |
public void init() { | |
mGoogleApiClient = new GoogleApiClient.Builder(mContext) | |
.addConnectionCallbacks(this) | |
.addOnConnectionFailedListener(this).addApi(Plus.API) | |
.addScope(Plus.SCOPE_PLUS_LOGIN).build(); | |
} | |
@Override | |
public void onConnected(Bundle bundle) { | |
mSignInClicked = false; | |
SharedPreferencesAdapter.setGoogleLoggedIn(mContext, true); | |
if(!SharedPreferencesAdapter.getPhotoLoaded(mContext)) { | |
setProfiLeImageUrlBySize(mMenuItem, 200); | |
SharedPreferencesAdapter.setPhotoLoaded(mContext, true); | |
} | |
} | |
@Override | |
public void onConnectionSuspended(int i) { | |
mGoogleApiClient.connect(); | |
} | |
@Override | |
public void onConnectionFailed(ConnectionResult connectionResult) { | |
if (!connectionResult.hasResolution()) { | |
GooglePlayServicesUtil.getErrorDialog(connectionResult.getErrorCode(), (Activity) mContext, 0).show(); | |
return; | |
} | |
if (!mIntentInProgress) { | |
// Store the ConnectionResult for later usage | |
mConnectionResult = connectionResult; | |
if (mSignInClicked) { | |
// The user has already clicked 'sign-in' so we attempt to | |
// resolve all | |
// errors until the user is signed in, or they cancel. | |
resolveSignInError(); | |
} | |
} | |
} | |
public boolean signInWithGplus() { | |
if (!mGoogleApiClient.isConnecting()) { | |
mSignInClicked = true; | |
resolveSignInError(); | |
return true; | |
} | |
return false; | |
} | |
public void resolveSignInError() { | |
if (mConnectionResult != null && mConnectionResult.hasResolution()) { | |
try { | |
mIntentInProgress = true; | |
mConnectionResult.startResolutionForResult((Activity) mContext, Costants.RC_SIGN_IN); | |
SharedPreferencesAdapter.setGoogleLoggedIn(mContext, true); | |
} catch (IntentSender.SendIntentException e) { | |
mIntentInProgress = false; | |
mGoogleApiClient.connect(); | |
} | |
} | |
} | |
public boolean signOutFromGplus() { | |
if (mGoogleApiClient.isConnected()) { | |
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient); | |
mGoogleApiClient.disconnect(); | |
mGoogleApiClient.connect(); | |
return true; | |
} | |
return false; | |
} | |
public boolean revokeGplusAccess() { | |
if (mGoogleApiClient.isConnected()) { | |
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient); | |
Plus.AccountApi.revokeAccessAndDisconnect(mGoogleApiClient) | |
.setResultCallback(new ResultCallback<Status>() { | |
@Override | |
public void onResult(Status arg0) { | |
Log.e(TAG, "User access revoked!"); | |
SharedPreferencesAdapter.setGoogleLoggedIn(mContext, false); | |
mGoogleApiClient.connect(); | |
} | |
}); | |
return true; | |
} | |
return false; | |
} | |
public void googleApiConnect () { | |
mGoogleApiClient.connect(); | |
} | |
public void googleApiDisconnect () { | |
mGoogleApiClient.disconnect(); | |
} | |
public GoogleApiClient getGoogleApiClient() { | |
return mGoogleApiClient; | |
} | |
public Person getProfileInformation() { | |
Person currentPerson = null; | |
try { | |
if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) { | |
currentPerson = Plus.PeopleApi | |
.getCurrentPerson(mGoogleApiClient); | |
String personName = currentPerson.getDisplayName(); | |
String personPhotoUrl = currentPerson.getImage().getUrl(); | |
String personGooglePlusProfile = currentPerson.getUrl(); | |
String email = Plus.AccountApi.getAccountName(mGoogleApiClient); | |
Log.e(TAG, "Name: " + personName + ", plusProfile: " | |
+ personGooglePlusProfile + ", email: " + email | |
+ ", Image: " + personPhotoUrl); | |
} else { | |
Toast.makeText(mContext, | |
"Person information is null", Toast.LENGTH_SHORT).show(); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return currentPerson; | |
} | |
public void getProfiLeImageUrlBySize(int size) { | |
Person currentPerson = null; | |
try { | |
if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) { | |
currentPerson = Plus.PeopleApi | |
.getCurrentPerson(mGoogleApiClient); | |
String personName = currentPerson.getDisplayName(); | |
String personPhotoUrl = currentPerson.getImage().getUrl(); | |
String personGooglePlusProfile = currentPerson.getUrl(); | |
String email = Plus.AccountApi.getAccountName(mGoogleApiClient); | |
String replaced = personPhotoUrl.replace("?sz=50", "?sz="+ size); | |
Log.e(TAG, "Name: " + personName + ", plusProfile: " | |
+ personGooglePlusProfile + ", email: " + email | |
+ ", Image: " + replaced); | |
} else { | |
Toast.makeText(mContext, | |
"Person information is null", Toast.LENGTH_SHORT).show(); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
public void setProfiLeImageUrlBySize(final RadialMenuItem item, int size) { | |
Person currentPerson = null; | |
try { | |
if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) { | |
currentPerson = Plus.PeopleApi | |
.getCurrentPerson(mGoogleApiClient); | |
String personName = currentPerson.getDisplayName(); | |
String personPhotoUrl = currentPerson.getImage().getUrl(); | |
String personGooglePlusProfile = currentPerson.getUrl(); | |
String email = Plus.AccountApi.getAccountName(mGoogleApiClient); | |
String imageUrl = personPhotoUrl.replace("?sz=50", "?sz="+ size); | |
ImageLoader imageLoader = ImageCacheManager.getInstance().getImageLoader(); | |
imageLoader.get(imageUrl, new ImageLoader.ImageListener() { | |
@Override | |
public void onResponse(ImageLoader.ImageContainer imageContainer, boolean b) { | |
if (!SharedPreferencesAdapter.getPhotoLoaded(mContext)) | |
item.setIconDrawable(new BitmapDrawable(mContext.getResources(), imageContainer.getBitmap())); | |
} | |
@Override | |
public void onErrorResponse(VolleyError volleyError) { | |
} | |
}); | |
Log.e(TAG, "Name: " + personName + ", plusProfile: " | |
+ personGooglePlusProfile + ", email: " + email | |
+ ", Image: " + imageUrl); | |
} else { | |
Toast.makeText(mContext, | |
"Person information is null", Toast.LENGTH_SHORT).show(); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
private static final String TAG = GooglePlusUtilsConnection.class.getName(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment