Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

- Sns logged check added (user is logged or not). #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 143 additions & 2 deletions socialauth-android/src/org/brickred/socialauth/android/SocialAuthAdapter.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ of this software and associated documentation files (the "Software"), to deal
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import org.brickred.socialauth.Album;
import org.brickred.socialauth.AuthProvider;
Expand Down Expand Up @@ -504,6 +509,47 @@ public void onClick(View v) {
}
}

/**
* This SNS is Connected? (logged)
* @param ctx
* @param provider
* @return
*/
public boolean isConnected(Context ctx, Provider provider) {
currentProvider = provider;

// Initialize socialauth manager if not already done
if (socialAuthManager != null) {
// If SocialAuthManager is not null and contains Provider Id, send
// response to listener
if (socialAuthManager.getConnectedProvidersIds().contains(currentProvider.toString())) {
Log.d("SocialAuthAdapter", "Provider already connected");
return true;
}

// If SocialAuthManager is not null and not contains Provider Id
// connect provider
else {
return checkConnectProvider(ctx, provider);
}
}
// If SocialAuthManager is null, create new socialauthmanager , load
// configuration and connect provider
else {
Log.d("SocialAuthAdapter", "Loading keys and secrets from configuration");

socialAuthManager = new SocialAuthManager();
try {
loadConfig(ctx);

} catch (Exception e) {
Log.d("SocialAuthAdapter", "Could not load configuration");
}
return checkConnectProvider(ctx, provider);
}
}


/**
* Method to handle configuration , Use directly for CustomUI
*
Expand Down Expand Up @@ -583,7 +629,7 @@ public void addConfig(Provider provider, String key, String secret, String permi
/**
* Internal method to load config
*
* @param context
* @param ctx
* The Android Activity context
*
*/
Expand Down Expand Up @@ -658,12 +704,97 @@ public void run() {
new Thread(runnable).start();
}

/**
* Internal method to check connect provider. The method check for access token If
* available it connects manager with AccessGrant
*
* @param ctx
* The Android Activity that will parent the auth dialog.
* @param provider
* Provider being checked
*
* @return true: connected | false: don't connected to Sns
*/

private boolean checkConnectProvider(final Context ctx, final Provider provider) {

SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(ctx);

if (pref.contains(provider.toString() + " key")) {
tokenMap = new HashMap<String, Object>();

for (Map.Entry entry : pref.getAll().entrySet())
tokenMap.put(entry.getKey().toString(), entry.getValue());

// If Access Token is available , connect using Access Token
try {

HashMap<String, Object> attrMap = null;
attrMap = new HashMap<String, Object>();

String key = (String) tokenMap.get(provider.toString() + " key");
String secret = (String) tokenMap.get(provider.toString() + " secret");
String providerid = (String) tokenMap.get(provider.toString() + " providerid");

String temp = provider.toString() + "attribute";
for (String attr : tokenMap.keySet()) {
System.out.println("Attr " + attr);

if (attr.startsWith(temp)) {
int startLocation = attr.indexOf(temp) + temp.length() + 1;
attrMap.put(attr.substring(startLocation), tokenMap.get(attr));
}

}

for (Map.Entry entry : attrMap.entrySet()) {
System.out.println(entry.getKey() + ", " + entry.getValue());
}

// create new AccessGrant Object
final AccessGrant accessGrant = new AccessGrant(key, secret);
accessGrant.setProviderId(providerid);
accessGrant.setAttributes(attrMap);

Log.d("SocialAuthAdapter", "Loading from AccessToken");

// Use future for connect to sns for check the login and block ui thread
ExecutorService executor = Executors.newFixedThreadPool(1);
Future<Boolean> checkResult = executor.submit(new Callable<Boolean>() {
public Boolean call() {
try {

// connect manager with accessGrant
socialAuthManager.connect(accessGrant);

// To check validity of Access Token
getCurrentProvider().getUserProfile().getValidatedId();
return true;
} catch (Exception e) {
return false;
}
}
});
executor.shutdown();
executor.awaitTermination(Integer.MAX_VALUE, TimeUnit.SECONDS);
return checkResult.get();

} catch (Exception e) {
return false;
}
}
// If Access Token is not available , Open Authentication Dialog
else {
return false;
}
}

/**
* Internal method to connect provider. The method check for access token If
* available it connects manager with AccessGrant else create new manager
* and open webview
*
* @param context
* @param ctx
* The Android Activity that will parent the auth dialog.
* @param provider
* Provider being authenticated
Expand Down Expand Up @@ -779,6 +910,16 @@ public void setDialogSize(float width, float height) {
SocialAuthDialog.height = height;
}


/**
* Signs out the user out of current provider
*
* @return Status of signing out
*/
public boolean signOut(Context ctx, Provider provider){
return signOut(ctx, provider.toString());
}

/**
* Signs out the user out of current provider
*
Expand Down