Courtesy: GitHub

compileSdkVersion 21

applicationId "com.google.android.gms.accounts.sample.quickstart"

minSdkVersion 9

targetSdkVersion 21

versionCode 1

versionName "1.0"

compile fileTree(dir: 'libs', include: ['*.jar'])

compile 'com.android.support:appcompat-v7:21.+'

// compile 'com.google.android.gms:google-play-services:6.5.+'

compile 'com.google.android.gms:google-play-services-base:6.5.+'

compile 'com.google.android.gms:google-play-services-identity:6.5.+'

compile 'com.google.android.gms:google-play-services-plus:6.5.+'

package com.google.android.gms.accounts.sample.quickstart

MainActivity

Copyright 2013, 2014 Google Inc. All Rights Reserved. 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.

Google Play Services Accounts sample. Demonstrates Google Sign-In and usage of some other Google APIs. The sample utilizes GoogleApiClient to store the Google Account selected by the user and shows how to retrieve the associated Google Account ID. The Account ID should be used as the key to associate any local or remote data to the Google Account. This will ensure logout actions do not mix user data. Note that logout actions can occur without direct user intervention on this device, such as when a Google Account is assigned a new primary email address in the Google Apps for Business product. This sample app WILL NOT WORK unless you have: 1.) Created an Application on the Google Developer Console 2.) Defined a Consent Screen in the Google Developer Console for your app. 3.) Registered your Android app's SHA1 certificate fingerprint and package name for your app (even for testing). 4.) Enabled the Google+ API in the Developer Console. Detailed instructions can be found here: https://developers.google.com/+/quickstart/android

GoogleApiClient wraps our service connection to Google Play Services and provides access to the user's sign in state as well as the Google's APIs.

We use mSignInProgress to track whether user has clicked sign in. mSignInProgress can be one of three values:

STATE_DEFAULT: The default state of the application before the user has clicked 'sign in', or after they have clicked 'sign out'. In this state we will not attempt to resolve sign in errors so we will display our Activity in a signed out state. STATE_SIGN_IN: This state indicates that the user has clicked 'sign in', so resolve successive errors preventing sign in until the user has successfully authorized an account for our app. STATE_IN_PROGRESS: This state indicates that we have started an intent to resolve an error, and so we should not start further intents until the current intent completes.

Used to store the PendingIntent most recently returned by Google Play Services until the user clicks 'sign in'.

Used to store the error code most recently returned by Google Play Services until the user clicks 'sign in'.

Construct a client. * @return un-connected client.

Delete user data to comply with using Google Account Terms and Conditions.

Called when the Activity successfully connects to Google Play Services. When the function is triggered, an account was selected on the device, the selected account has granted requested permissions to the app, and the app has established a connection to Google Play Services. * @param connectionHint can be inspected for additional connection info

Called when the Activity could not connect to Google Play services. The callback indicates that the user needs to select an account, grant permissions, or resolve an error in order to sign in. * @param result can be inspected to determine the cause of the failure

Starts an appropriate intent or dialog for user interaction to resolve the current error preventing the user from being signed in. This could be a dialog allowing the user to select an account, an activity allowing the user to consent to the permissions being requested by your app, a setting to enable device networking, etc.

Update the UI to reflect that the user is signed out.