facebook-android-sdk icon indicating copy to clipboard operation
facebook-android-sdk copied to clipboard

Cannot login using CustomTab with androidx.activity 1.5.1.

Open matsudamper opened this issue 1 year ago • 2 comments

Checklist before submitting a bug report

Java version

11

Android version

Android 13

Android SDK version

14.1.0

Installation platform & version

Gradle 7.4.1

Package

Login

Goals

I can log in with androidx.activity 1.5.1 without the Facebook app.

Expected results

I can retrieve the results of your login.

Actual results

Unable to retrieve login results. not called onActivityResult or ActivityResultCallback

Steps to reproduce

  • I have not installed the Facebook application.
  • call LoginManager#logInWithReadPermissions() or launch(Collection<String>)
  • Open CustomTab
  • Log in

Code samples & details

No problem in androidx.activity:activity:1.5.0 . Problem in androidx.activity:activity:1.5.1 .

Change between 1.5.0 and 1.5.1. https://android.googlesource.com/platform/frameworks/support/+log/e047538e9c1a9b12afec093910a556017720cb13..835baf76d995c6653478c7e00946a0558c678c45/activity

import androidx.activity.ComponentActivity
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContract
import androidx.lifecycle.lifecycleScope
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import com.facebook.CallbackManager
import com.facebook.FacebookException
import com.facebook.FacebookSdk
import com.facebook.login.LoginManager

class LoginFacebook(
    private val componentActivity: ComponentActivity
) {
    private val callbackManager = CallbackManager.Factory.create()
    private val facebookLoginManager = LoginManager.getInstance()

    private val launcher = object {
        private lateinit var launcher: ActivityResultLauncher<Collection<String>>
        init {
            val contract: ActivityResultContract<Collection<String>, CallbackManager.ActivityResultParameters> =
                facebookLoginManager.createLogInActivityResultContract(callbackManager = callbackManager)

            componentActivity.lifecycleScope.launchWhenCreated {
                launcher = componentActivity.registerForActivityResult(
                    contract
                ) {

                }
            }
        }

        fun launch() {
            launcher.launch(Permissions)
        }
    }
    suspend fun startLogin(): LoginResult {
        if (!FacebookSdk.isInitialized()) {
            @Suppress("DEPRECATION")
            FacebookSdk.sdkInitialize(componentActivity.application)
        }

        return withContext(Dispatchers.Default) {
            try {
                suspendCoroutine { continuation ->
                    val callback = object : com.facebook.FacebookCallback<com.facebook.login.LoginResult> {
                        override fun onCancel() {
                            continuation.resumeWithException(IllegalStateException("onCancel"))
                        }

                        override fun onError(error: FacebookException) {
                            continuation.resumeWithException(error)
                        }

                        override fun onSuccess(result: com.facebook.login.LoginResult) {
                            val token = result.accessToken.token
                            val userId = result.accessToken.userId

                            continuation.resume(LoginResult(userId = userId, token = token))
                        }
                    }
                    facebookLoginManager.registerCallback(callbackManager, callback)

                    launcher.launch()
                }
            } catch (e: Throwable) {
                e.printStackTrace()
                throw e
            } finally {
                facebookLoginManager.unregisterCallback(callbackManager)
            }
        }
    }

    data class LoginResult(
        val userId: String,
        val token: String,
    )

    companion object {
        private val Permissions = listOf("public_profile", "user_friends", "email")
    }
}

matsudamper avatar Jul 29 '22 20:07 matsudamper

not called https://github.com/facebook/facebook-android-sdk/blob/75d7583f7b02f51ed9034cb1b1f0ced486ec6ecd/facebook-common/src/main/java/com/facebook/login/LoginFragment.kt#L144-L147

matsudamper avatar Jul 30 '22 01:07 matsudamper

same issue in androidx.fragment:fragment:1.5.0

pistolcaffe avatar Aug 01 '22 06:08 pistolcaffe

login error in android 11 also

alihassan143 avatar Aug 04 '22 11:08 alihassan143

I had the same problem i downgrade temporally to the version 1.5.0 of androidx.activity and androidx.fragment

i used app:dependencies to see which library promote the version 1.5.1 and downgrade too

NearTox avatar Aug 16 '22 15:08 NearTox

seems to be fixed. https://github.com/facebook/facebook-android-sdk/commit/d7d4e0d241ea4f9e7035fb12b87b249a10e9af36 https://github.com/facebook/facebook-android-sdk/releases/tag/sdk-version-14.1.0

I will close it when others can confirm it. Please report.

matsudamper avatar Aug 31 '22 02:08 matsudamper

seems to be fixed. d7d4e0d https://github.com/facebook/facebook-android-sdk/releases/tag/sdk-version-14.1.0

I will close it when others can confirm it. Please report.

d7d4e0d is contained in 14.1.1 not 14.1.0 https://github.com/facebook/facebook-android-sdk/releases/tag/sdk-version-14.1.1

niusounds avatar Sep 01 '22 01:09 niusounds