I'm developing an application in which i have integrated google plus. So far Its working fine, I am able to retrieve the user profile.
But now i want to do the following:
1)I have two activity signInActivity
and shareActivity
.
2)If user is already signin using signInActivity
then it should not ask for signin again in
shareActivity
and should directly share the content.
3)If user is not signedin in the signInActivity
and try to share data using shareActivity
then app should signin the user and then only share the data. In this case if user goes back to the signInActivity
then app should show that "you have already signedin"
In short i want user signin to be Central within application so that if it is alrady signedin it should be accessible from any activity.
I have heard about the access token but i dont know how to use it and document says that it expires in an hour which is not what i want.
How can i make central google plus signin? is it possible? or i need to authenticate user in each activity?
For anyone reading this question you can also check this answer by Ian Barber and also the one below, answered by Lee, that explains three broad ways of working with Google plus login and multiple activies which I found very useful actually.
Managing a separate instance of GoogleApiClient in each activity will not result in the user being asked to sign in multiple times.
Google+ Sign-in (ie. GoogleApiClient) provides an interface to the Google accounts on the device and the Google Play services core service - it doesn't have state per GoogleApiClient instance. So once a device account has been authenticated for your app, new instances of GoogleApiClient will access the same state. GoogleApiClient is specifically designed to be a lightweight way to access the central state managed by Google Play services.
You're in luck regarding access tokens! Google Play services takes care of all token management for you. So although access tokens only last for one hour, as you say, if you try to use your PlusClient to access a Google API and your access token has expired, Google Play services will transparently request a new access token for you and complete the call.
Take a look at the first part of this Google I/O talk for more details:
http://www.youtube.com/watch?v=_KBHf1EODuk
0. TL;DR
For the impatient coder, a working version of the following implementation can be found on GitHub. This is the same answer written on another Stack Overflow post.
After rewriting the login activity code several times in many different apps, the easy (and not so elegant) solution was create the Google API client as a Application class object. But, since the connection state affect the UX flow, I never was happy about with this approach.
Reducing our problem only to the connection concept, we may consider that:
1. Proxy Pattern
Since the
Connection
encapsulates theGoogleApiClient
, it will implement theConnectionCallbacks
andOnConnectionFailedListener
:Activities can communicate to the Connection class through the methods
connect
,disconnect
, andrevoke
, but their behaviors are decided by the current state. The following methods are required by the state machine:2. State Pattern
This is a behavioral pattern the allow an object to alter its behavior when its internal state changes. The GoF Design Patterns book describes how a TCP connection can be represent by this pattern (which is also our case).
A state from a state machine should be a
singleton
, and the easiest away of doing it in Java was to createEnum
namedState
as follows:The
Connection
class holds the context, i.e. the current state, which defines how theConnection
methodsconnect
,disconnect
, andrevoke
will behave:3. Singleton Pattern
Since there is not need to recreate this class repeatedly, we provide it as a singleton:
4. Observable Pattern
The
Connection
class extends JavaObservable
, so 1 or more activities can observe the state changes: