📜 ⬆️ ⬇️

AndroidSocialNetworks - convenient work with social networks


AndroidSocialNetworks is a library for Android that does work with social services. networking is easier. If you have ever tried to work with social. networks, you know how hard it is. Need to read the documentation for each soc. network, download and connect SDK or third-party library, take care of the life cycle of http requests. AndroidSocialNetwork should make your life easier, it contains a common interface for Facebook, Twitter, LinkedIn and Google Plus - just slick SocialNetworkManager using SocialNetworkManager.Build -> add it to FragmentManager -> configure AndroidManifest.xml -> work with customized social. networks. You can do login, post status update, post pictures, add / delete friends.

How to use


First we need to initialize SocialNetworkManager, this is done using SocialNetworkManager.Builder. SocialNetworkManager is a Fragment with retainedInstance (true).
This is necessary in order to be able to follow the life cycle.
After we initialize the SocialNetworkManager, you need to add it to the FragmentManager. Please note that for compatibility you must use the SupportFragmentManager.

mSocialNetworkManager = (SocialNetworkManager) getFragmentManager().findFragmentByTag(SOCIAL_NETWORK_TAG); if (mSocialNetworkManager == null) { mSocialNetworkManager = SocialNetworkManager.Builder.from(getActivity()) .twitter(<api_token>, <api_secret>) .linkedIn(<api_token>, <api_secret>, "r_basicprofile+rw_nus+r_network+w_messages") .facebook() .googlePlus() .build(); getFragmentManager().beginTransaction().add(mSocialNetworkManager, SOCIAL_NETWORK_TAG).commit(); } 

Since the addition of a fragment is a transaction, we cannot immediately start using the SocialNetworkManager, there is a callback called SocialNetworkManager.OnInitializationCompleteListener for this.

 mSocialNetworkManager.setOnInitializationCompleteListener(this); 

If we, for example, need to show whether Twitter is connected, we need to update the UI in onSocialNetworkManagerInitialized.
')
Each request method may receive a listener, or may not receive, in 2 cases the global one will be called if it is installed:
Install the global listener on the Login
 @Override public void onSocialNetworkManagerInitialized() { super.onSocialNetworkManagerInitialized(); for (SocialNetwork socialNetwork : mSocialNetworkManager.getInitializedSocialNetworks()) { socialNetwork.setOnLoginCompleteListener(this); } } 

Call LinkedIn Login
 mSocialNetworkManager.getLinkedInSocialNetwork().requestLogin(); 

The result is a global callback.
 @Override public void onLoginSuccess(int socialNetworkID) { } @Override public void onError(int socialNetworkID, String requestID, String errorMessage, Object data) { } 

If we do not want to use global callbacks, we can use local:
 mSocialNetworkManager.getGooglePlusSocialNetwork().requestLogin(new DemoOnLoginCompleteListener()); private class DemoOnLoginCompleteListener implements OnLoginCompleteListener { @Override public void onLoginSuccess(int socialNetworkID) { } @Override public void onError(int socialNetworkID, String requestID, String errorMessage, Object data) { } } 

Such a mechanism can be used not only for requestLogin, all main methods support 2 types of callback tasks.

Its implementation of soc. network

If you do not have enough available social. networks, and you want to add another support, you can do it using the addSocialNetwork (SocialNetwork socialNetwork) method.
Note that you need to do this in onSocialNetworkManagerInitialized. Also note that your social ID. network must be unique. 1, 2, 3, 4 - already reserved.

Available methods



Full list



Social support by networks



How it works


As you can see, SocialNetworkManager is a fragment (from support v4 for compatibility). This is necessary so that you can conveniently monitor the life cycle of the application. It is also made retainedInstance (true) in order for network requests to reliably survive changes in the lifecycle ...

SocialNetworkManager.Builder just prepares the Bundle with parameters.
 public SocialNetworkManager build() { Bundle args = new Bundle(); if (!TextUtils.isEmpty(twitterConsumerKey) && !TextUtils.isEmpty(twitterConsumerSecret)) { args.putString(PARAM_TWITTER_KEY, twitterConsumerKey); args.putString(PARAM_TWITTER_SECRET, twitterConsumerSecret); } if (!TextUtils.isEmpty(linkedInConsumerKey) && !TextUtils.isEmpty(linkedInConsumerSecret) && !TextUtils.isEmpty(linkedInPermissions)) { args.putString(PARAM_LINKEDIN_KEY, linkedInConsumerKey); args.putString(PARAM_LINKEDIN_SECRET, linkedInConsumerSecret); args.putString(PARAM_LINKEDIN_PERMISSIONS, linkedInPermissions); } if (facebook) { args.putBoolean(PARAM_FACEBOOK, true); } if (googlePlus) { args.putBoolean(PARAM_GOOGLE_PLUS, true); } SocialNetworkManager socialNetworkManager = new SocialNetworkManager(); socialNetworkManager.setArguments(args); return socialNetworkManager; } 

Inside the SocialNetworkManager all initialized social. networks are stored in HashMap
 private Map<Integer, SocialNetwork> mSocialNetworksMap = new HashMap<Integer, SocialNetwork>(); 

In callbacks lifecycle we go through all social services. networks and forwarding values:
 public void onStop() { … for (SocialNetwork socialNetwork : mSocialNetworksMap.values()) { socialNetwork.onStop(); } } 

Every soc. the network must inherit from the abstract class SocialNetwork.

It is necessary to override the getID () method, precisely by the return value of this social method. networks and will vary. In my implementations, I made the ID public for convenience so that you can use socialNetwork.getID () in a switch.
 public class GooglePlusSocialNetwork extends SocialNetworkpublic static final int ID = 3; 

The lifecycle methods are not required to override, only when necessary.

To implement the methods requestLogin, requestPerson, etc. It is necessary to redefine methods with callbacks. This is necessary because of the support of both global and local listeners.

Callbacks are stored in Maps
 protected Map<String, SocialNetworkListener> mGlobalListeners = new HashMap<String, SocialNetworkListener>(); protected Map<String, SocialNetworkListener> mLocalListeners = new HashMap<String, SocialNetworkListener>(); 

When we add a global callback, it is added to mGlobalListeners, then for any call without a callback, we call the callback method, passing null.

 public void requestSocialPerson(String userID) { requestSocialPerson(userID, null); } 

The callback method registers a listener.
 public void requestSocialPerson(String userID, OnRequestSocialPersonCompleteListener onRequestSocialPersonCompleteListener) { registerListener(REQUEST_GET_PERSON, onRequestSocialPersonCompleteListener); } 

The implementation of registerListener is fairly simple:
 private void registerListener(String listenerID, SocialNetworkListener socialNetworkListener) { if (socialNetworkListener != null) { mLocalListeners.put(listenerID, socialNetworkListener); } else { mLocalListeners.put(listenerID, mGlobalListeners.get(listenerID)); } } 

Network requests

To perform network requests, use either the SDK methods:
 Request request = Request.newMeRequest(currentSession, new Request.GraphUserCallback() { @Override public void onCompleted(GraphUser me, Response response) { … request.executeAsync(); 

or AsyncTasks, do not be afraid of asink tasks, if you remember, we use them in the Fragment in retainedInstance (true). Good article on this topic: http://www.androiddesignpatterns.com/2013/04/retaining-objects-across-config-changes.html

Important




Link to the library: https://github.com/antonkrasov/AndroidSocialNetworks
Sample application: AndroidSocialNetworks APIDemos

Source: https://habr.com/ru/post/223837/


All Articles