Back to project page Yeah-MaterialLove.
The source code is released under:
Apache License
If you think the Android project Yeah-MaterialLove listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.yeahdev.materiallovetesting.activities; //w w w . j a va 2 s. c om import android.content.Intent; import android.content.IntentSender; import android.os.Build; import android.os.Bundle; import android.support.v4.app.ActivityCompat; import android.support.v4.app.ActivityOptionsCompat; import android.support.v7.app.ActionBarActivity; import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.LinearLayout; import android.widget.Toast; import com.google.android.gms.common.ConnectionResult; 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.common.api.GoogleApiClient.ConnectionCallbacks; import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener; import com.google.android.gms.common.api.ResultCallback; import com.google.android.gms.common.api.Status; import com.google.android.gms.plus.Plus; import com.google.android.gms.plus.model.people.Person; import com.readystatesoftware.systembartint.SystemBarTintManager; import com.williammora.snackbar.Snackbar; import com.yeahdev.materiallovetesting.R; /** * */ public class GPlusLoginActivity extends ActionBarActivity implements View.OnClickListener, ConnectionCallbacks, OnConnectionFailedListener { private static final int RC_SIGN_IN = 0; /** * Logcat tag */ private static final String TAG = "GPlusLoginActivity"; /** * Google client to interact with Google API */ private GoogleApiClient mGoogleApiClient; /** * A flag indicating that a PendingIntent is in progress and prevents us * from starting further intents. */ private boolean mIntentInProgress; private boolean mSignInClicked; /** * Connectionresult for GoogleApiClient */ private ConnectionResult mConnectionResult; /** * UI Elements */ private SignInButton btnSignIn; private Button btnSignOut, btnRevokeAccess, btnGoToMainActivity; /** * Variables for G+ Data for sending to another Activity */ private String personName = "John Doe"; private String email = "Joe.Doe@gmail.com"; private String personGooglePlusProfile; private String personPhotoUrl; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_gplus_login); /** * Tint Statusbar PreLollipop Devices */ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { SystemBarTintManager stm = new SystemBarTintManager(this); stm.setStatusBarTintEnabled(true); stm.setStatusBarTintColor(getResources().getColor(R.color.theme_default_primary_dark)); // LinearLayout llvMainContent = (LinearLayout) findViewById(R.id.llvGPlusLogin); SystemBarTintManager.SystemBarConfig config = stm.getConfig(); llvMainContent.setPadding(0, config. getPixelInsetTop(false), config.getPixelInsetRight(), config.getPixelInsetBottom()); } /** * Toolbar Love */ Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); if (toolbar != null) { toolbar.setTitle("Login"); setSupportActionBar(toolbar); } /** * Init UI Elements */ btnSignIn = (SignInButton) findViewById(R.id.btn_sign_in); btnSignOut = (Button) findViewById(R.id.btn_sign_out); btnRevokeAccess = (Button) findViewById(R.id.btn_revoke_access); btnGoToMainActivity = (Button) findViewById(R.id.btnGoToMain); /** * Button click listeners */ btnSignIn.setOnClickListener(this); btnSignOut.setOnClickListener(this); btnRevokeAccess.setOnClickListener(this); btnGoToMainActivity.setOnClickListener(this); /** * Initializing google plus api client */ mGoogleApiClient = new GoogleApiClient.Builder(this) .addConnectionCallbacks(this).addOnConnectionFailedListener(this) .addApi(Plus.API, new Plus.PlusOptions.Builder().build()) .addScope(Plus.SCOPE_PLUS_LOGIN).build(); } /** * Connect to GoogleApiClient */ protected void onStart() { super.onStart(); mGoogleApiClient.connect(); } /** * Disconnect to GoogleApiClient */ protected void onStop() { super.onStop(); if (mGoogleApiClient.isConnected()) { mGoogleApiClient.disconnect(); } } /** * Button on click listener */ @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_sign_in: // Signin button clicked signInWithGplus(); break; case R.id.btn_sign_out: // Signout button clicked signOutFromGplus(); break; case R.id.btn_revoke_access: // Revoke access button clicked revokeGplusAccess(); break; case R.id.btnGoToMain: // Start Mainactivity + Bundle G+ Data Intent mIntent = new Intent(GPlusLoginActivity.this, MainActivity.class); mIntent.putExtra("personName", personName); mIntent.putExtra("email", email); mIntent.putExtra("personPhotoUrl", personPhotoUrl); mIntent.putExtra("personGooglePlusProfile", personGooglePlusProfile); // Identifier for Transaction Animation String transactionName = getString(R.string.transaction_to_mainview); // Build Animation and Go to the Mainactivity ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(this, v, transactionName); ActivityCompat.startActivity(GPlusLoginActivity.this, mIntent, options.toBundle()); break; default: break; } } /** * Begin GoogleApiClient methods */ @Override public void onConnectionFailed(ConnectionResult result) { if (!result.hasResolution()) { GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this, 0).show(); return; } // if (!mIntentInProgress) { // Store the ConnectionResult for later usage mConnectionResult = result; // 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(); } } } @Override protected void onActivityResult(int requestCode, int responseCode, Intent intent) { if (requestCode == RC_SIGN_IN) { if (responseCode != RESULT_OK) { mSignInClicked = false; } // mIntentInProgress = false; // if (!mGoogleApiClient.isConnecting()) { mGoogleApiClient.connect(); } } } @Override public void onConnected(Bundle arg0) { mSignInClicked = false; // Get user's information getProfileInformation(); // Update the UI after signin updateUI(true); } @Override public void onConnectionSuspended(int arg0) { mGoogleApiClient.connect(); updateUI(false); } /** * End GoogleApiClient methods */ /** * Updating the UI, showing/hiding buttons and profile layout */ private void updateUI(boolean isSignedIn) { if (isSignedIn) { btnSignIn.setVisibility(View.GONE); btnSignOut.setVisibility(View.VISIBLE); btnRevokeAccess.setVisibility(View.VISIBLE); } else { btnSignIn.setVisibility(View.VISIBLE); btnSignOut.setVisibility(View.GONE); btnRevokeAccess.setVisibility(View.GONE); } } /** * Sign-in into google */ private void signInWithGplus() { if (!mGoogleApiClient.isConnecting()) { mSignInClicked = true; resolveSignInError(); } } /** * Method to resolve any signin errors */ private void resolveSignInError() { if (mConnectionResult.hasResolution()) { try { mIntentInProgress = true; mConnectionResult.startResolutionForResult(this, RC_SIGN_IN); } catch (IntentSender.SendIntentException e) { mIntentInProgress = false; mGoogleApiClient.connect(); } } } /** * Fetching user's information name, email, profile pic */ private void getProfileInformation() { try { if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) { Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient); personName = currentPerson.getDisplayName(); email = Plus.AccountApi.getAccountName(mGoogleApiClient); personPhotoUrl = currentPerson.getImage().getUrl(); personGooglePlusProfile = currentPerson.getUrl(); } else { Toast.makeText(getApplicationContext(), "Person information is null", Toast.LENGTH_LONG).show(); } } catch (Exception e) { e.printStackTrace(); } } /** * Sign-out from google */ private void signOutFromGplus() { if (mGoogleApiClient.isConnected()) { Plus.AccountApi.clearDefaultAccount(mGoogleApiClient); mGoogleApiClient.disconnect(); mGoogleApiClient.connect(); updateUI(false); } } /** * Revoking access from google */ private void 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!"); mGoogleApiClient.connect(); updateUI(false); } }); } } /** * Menu */ @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_gplus_login, menu); return true; } /** * Menu */ @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { Snackbar.with(this) .text("Nothing to see here!") .show(this); return true; } return super.onOptionsItemSelected(item); } }