Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle Multiple Social Media Logins and sessions Flow on Android?

I have created an android app that has a Navigation Drawer with Fragments.

I then started with Social Media Integrations, that is Facebook, Twitter and Google Plus. I am using the official respective SDK's.

Not Social Media Sessions and calls to acquire data are made in the following activities:

  • Mainactivity : Where if any of the sessions are lost or revoked, it needs to redirect to LoginActivity. This is done during activity lifecycle methods.

  • LoginActivity : Where the permission are granted and redirects to Main after success

  • NavigationDrawerFragment : The Navigation drawer, I use this to display the user's profile image, name and email address.

In these 3 classes I am making calls to the respective SDK methods and managing the sessions so there is a lot of code duplication.

Also the 3 SDK's have significantly different ways of providing authentication.

  • Facebook: uses the UiLifeCycleHelper that is implemented on all your standard lifecycle functions of an activity.
  • Google Plus: You have to implement GooglePlay store callbacks in the activity and create a GoogleApiClient on all activities.
  • Twitter: Has less of a "taking over your app" attitude and uses retrofit in the same manner of a usual REST API call on android: with Retrofit. However you have to give permissions on every login.

So All of these functions for handling the different social media logins are included in all the required activities making the application code really bulky and hard to manage.

Do you have any suggestions on how to abstract these auth methods into a easily maintainable solutions that makes manging sessions easier? Would I be better off to have separate activities based on what login was used?

like image 379
tread Avatar asked Apr 01 '15 13:04

tread


2 Answers

I have done it several times and the best approach I can recommend you is:

  • Extract what part of the integration you want to add. It is only sharing, do you want to let the user start a session, do you need any kind of API keys.
  • Once done the first step, you can extract some abstraction layer methods that are common to all the social platforms (there is always almost always a common function).
  • Generate a factory for every platform like FacebookFactory or TwitterFactory that are capable to generate prepared objects for a given task. Imagine you want to login, then ask to the concrete factory for the LoginTask and expose common actions like requestOAuthToken, getSession, etc. If for another reason there is something that cannot be abstracted, you can always downcast knowing that it will not break your application.
  • You can generate to feel more confortable a second abstraction layer by using a Facade pattern, which is constructed via Context object (some networks like facebook are really invasive and need to know many things), deciding which is the social network you want to work on by an enum.

Here it is a mock example on how your code can look like:

SocialFacade facade = SocialFacade.getInstance();
SocialSession session = facade.getSession(Network.Twitter);
String token = session.requestToken(apiId);
facade.getShare(Network.Twitter).sharePost(apiId, message);

Of cours you can use some kind of third party library, but this is the approach I use when nothing suits my needs.

like image 138
droidpl Avatar answered Nov 03 '22 12:11

droidpl


i also face this kind of problames. let me tell what i have done.

  1. try to find facebook, google and Twitter app from user device.
  2. if you found then login, post by using that applicatoin session. it will too easy to manage. no need to store session and all. just go to app and take or post required data from app.
  3. if you do not found app from the device than you have to go for Dialog login using libr. which are also able to store session of social media data. like facebook and twitter lib have own function to store and expire that session.
like image 40
Sameer Donga Avatar answered Nov 03 '22 12:11

Sameer Donga