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

making the login state app wide ? #39

Open
matantsu opened this issue Nov 29, 2014 · 13 comments
Open

making the login state app wide ? #39

matantsu opened this issue Nov 29, 2014 · 13 comments

Comments

@matantsu
Copy link
Contributor

i have integrated this library into my app , and it for now works great on google plus , i can call a function to login in my fragment and all is good.

just one question , how can i acsess the state of the user from anywhere in the app ? (eg. isConnected() etc...)

@antonkrasov
Copy link
Owner

@matantsu, SocialNetworkManager is just a fragment, so you can use something like this to get instance of it

public static SocialNetworkManager initializeSocialManager(Fragment hostFragment) {
        FragmentManager fragmentManager = hostFragment.getActivity().getSupportFragmentManager();
        SocialNetworkManager manager = (SocialNetworkManager) fragmentManager.findFragmentByTag(SN_MANAGER_TAG);
        if (manager == null) {
            manager = SocialNetworkManager.Builder
                    .from(hostFragment.getActivity())
                    .twitter(hostFragment.getString(R.string.twitter_key), hostFragment.getString(R.string.twitter_secret))
                    .googlePlus()
                    .facebook()
                    .linkedIn(hostFragment.getString(R.string.linkedin_consumer_key), hostFragment.getString(R.string.linkedin_consumer_secret), hostFragment.getString(R.string.linked_in_permissions))
                    .build();
            fragmentManager
                    .beginTransaction()
                    .add(manager, SN_MANAGER_TAG)
                    .commit();
        }
        return manager;
    }

And then just call getSocialNetwork().isConnected()

@matantsu
Copy link
Contributor Author

thanks , and just one more thing , is there a way to set up a primary social network ?
if a user prefers to log in with google but wants to post a picture on both facebook and google , than google would be the primary social network for that user.

@antonkrasov
Copy link
Owner

You want user to login with google and have an ability to post to facebook? This is not possible, to post something you need to login.

@matantsu
Copy link
Contributor Author

the will log in to both , and post to both , however the app will consider one social network as the primary and will use it's id , profile pic etc...

@antonkrasov
Copy link
Owner

Ah, no, library does provide so functionality. You will need to handle this by yourself.

@matantsu
Copy link
Contributor Author

ok. so i tried:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        CalligraphyConfig.initDefault("fonts/"+Config.DEFAULT_FONT_PATH, R.attr.fontPath);

        FragmentManager fragmentManager = getSupportFragmentManager();
        SocialNetworkManager manager = (SocialNetworkManager) fragmentManager.findFragmentByTag("aqghwhwrrwrwhrwhrhw");
        if (manager == null) {
            Log.i(TAG,"manager == null");
            manager = SocialNetworkManager.Builder
                    .from(this)
                    .googlePlus()
                    .build();
            fragmentManager
                    .beginTransaction()
                    .add(manager, "aqghwhwrrwrwhrwhrhw")
                    .commit();
        }
        manager.getGooglePlusSocialNetwork();
    }

app crashes at manager.getGooglePlusSocialNetwork();

logCat:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.stars.starsdesign/com.stars.starsdesign.main}: java.lang.IllegalStateException: Facebook wasn't initialized...
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.IllegalStateException: Facebook wasn't initialized...
            at com.androidsocialnetworks.lib.SocialNetworkManager.getGooglePlusSocialNetwork(SocialNetworkManager.java:179)
            at com.stars.starsdesign.AppActivity.onCreate(AppActivity.java:57)
            at com.stars.starsdesign.main.onCreate(main.java:114)
            at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

@antonkrasov
Copy link
Owner

Ah, sorry, this is just a typing error in code

public GooglePlusSocialNetwork getGooglePlusSocialNetwork() {
        if (!mSocialNetworksMap.containsKey(GooglePlusSocialNetwork.ID)) {
            throw new IllegalStateException("Facebook wasn't initialized...");
        }

        return (GooglePlusSocialNetwork) mSocialNetworksMap.get(GooglePlusSocialNetwork.ID);
    }

The issue is that SocialNetworkManager is a fragment, and initializing it is a transaction, you can call for getSocialNetwork method only after onSocialNetworkManagerInitialized called. You have an interface for this: OnInitializationCompleteListener. Check demos project for more info.

@matantsu
Copy link
Contributor Author

code:

@Override
    public void onSocialNetworkManagerInitialized() {
        Log.i(TAG,"onSocialNetworkManagerInitialized");
        if(socialNetworkManager.getGooglePlusSocialNetwork().isConnected())
            socialNetworkManager.getGooglePlusSocialNetwork().requestCurrentPerson(new OnRequestSocialPersonCompleteListener() {
                @Override
                public void onRequestSocialPersonSuccess(int i, SocialPerson socialPerson) {
                    Log.i(TAG,"onRequestSocialPersonSuccess");
                }

                @Override
                public void onError(int i, String s, String s2, Object o) {
                    Log.i(TAG,"onError");
                }
            });
    }

error:

java.lang.RuntimeException: Unable to resume activity {com.stars.starsdesign/com.stars.starsdesign.main}: java.lang.IllegalStateException: GoogleApiClient must be connected.
            at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2951)
            at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2982)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.IllegalStateException: GoogleApiClient must be connected.
            at com.google.android.gms.common.internal.o.a(Unknown Source)
            at com.google.android.gms.plus.Plus.a(Unknown Source)
            at com.google.android.gms.internal.nt.getCurrentPerson(Unknown Source)
            at com.androidsocialnetworks.lib.impl.GooglePlusSocialNetwork.requestCurrentPerson(GooglePlusSocialNetwork.java:102)
            at com.stars.starsdesign.AppActivity.onSocialNetworkManagerInitialized(AppActivity.java:87)
            at com.androidsocialnetworks.lib.SocialNetworkManager.onResume(SocialNetworkManager.java:99)
            at android.support.v4.app.Fragment.performResume(Fragment.java:1829)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:983)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
            at android.support.v4.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:1927)
            at android.support.v4.app.FragmentActivity.onResumeFragments(FragmentActivity.java:444)
            at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:433)
            at android.support.v7.app.ActionBarActivity.onPostResume(ActionBarActivity.java:140)
            at android.app.Activity.performResume(Activity.java:6032)
            at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2940)
            at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2982)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

@matantsu
Copy link
Contributor Author

if i trigger the request from a button click it works perfectly. but onSocialNetworkManagerInitialized not.

@antonkrasov
Copy link
Owner

Yeah, I see the issue, startResolutionForResult is called only during requestLogin.
For sure, this should be fixed, for now you can just save users info, after login and restore it, if it's already connected. This is how it works in my projects :)

@matantsu
Copy link
Contributor Author

This library could be so awesome if there weren't all this stupid minor bugs...

@matantsu
Copy link
Contributor Author

I think onSocialNetworkManagerInitialized should be called only when all social networks are ready to perform requests.

@antonkrasov
Copy link
Owner

This library could be so awesome if there weren't all this stupid minor bugs...

Haha, agree :) It was written for one of my projects, and it works fine there. If everyone here will make a pull request, when find the bug, library will become better. As for me, I don't have time to work on it, so I fix something or add new features, only when QA team create issues in projects but tracker.

I think onSocialNetworkManagerInitialized should be called only when all social networks are ready to perform requests.

This is how it works right now. You have issue with Google+ because you use it, not like all others. Default behaviour login->requestPerson->storeData. Then if we are connected(), we just load data from the storage. So in this scenario, we have initialized, mGooglePlusClient.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants