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

Auth: Fix exceptions caused by folding screens #2644

Merged
merged 2 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion play-services-core/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@
android:enabled="true"
android:exported="true"
android:process=":ui"
android:configChanges="keyboardHidden|keyboard|orientation|screenSize"
android:configChanges="keyboardHidden|keyboard|orientation|screenSize|smallestScreenSize|layoutDirection"
android:launchMode="singleTask"
android:excludeFromRecents="false">
<intent-filter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,14 @@ class AssistedSignInActivity : AppCompatActivity() {
errorResult(Status(CommonStatusCodes.ERROR, "accounts is empty."))
return
}
AssistedSignInFragment(googleSignInOptions!!, beginSignInRequest!!, accounts, clientPackageName!!,
{ errorResult(it) },
{ loginResult(it) })
.show(supportFragmentManager, AssistedSignInFragment.TAG)
val fragment = supportFragmentManager.findFragmentByTag(AssistedSignInFragment.TAG)
if (fragment != null) {
val assistedSignInFragment = fragment as AssistedSignInFragment
assistedSignInFragment.cancelLogin(true)
} else {
AssistedSignInFragment.newInstance(clientPackageName!!, googleSignInOptions!!, beginSignInRequest!!)
.show(supportFragmentManager, AssistedSignInFragment.TAG)
}
return
}

Expand All @@ -114,15 +118,15 @@ class AssistedSignInActivity : AppCompatActivity() {
startActivityForResult(intent, REQUEST_CODE_SIGN_IN)
}

private fun errorResult(status: Status) {
fun errorResult(status: Status) {
Log.d(TAG, "errorResult: $status")
setResult(RESULT_CANCELED, Intent().apply {
putExtra(AuthConstants.STATUS, SafeParcelableSerializer.serializeToBytes(status))
})
finish()
}

private fun loginResult(googleSignInAccount: GoogleSignInAccount?) {
fun loginResult(googleSignInAccount: GoogleSignInAccount?) {
if (googleSignInAccount == null) {
errorResult(Status(CommonStatusCodes.CANCELED, "User cancelled."))
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package org.microg.gms.auth.signin

import android.accounts.Account
import android.accounts.AccountManager
import android.app.Dialog
import android.content.DialogInterface
import android.os.Bundle
Expand All @@ -19,6 +20,7 @@ import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.ProgressBar
import android.widget.TextView
import androidx.core.content.getSystemService
import androidx.lifecycle.lifecycleScope
import com.google.android.gms.R
import com.google.android.gms.auth.api.identity.BeginSignInRequest
Expand All @@ -35,22 +37,35 @@ import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.microg.gms.auth.AuthConstants
import org.microg.gms.people.PeopleManager
import org.microg.gms.utils.getApplicationLabel

class AssistedSignInFragment(
private val options: GoogleSignInOptions,
private val beginSignInRequest: BeginSignInRequest,
private val accounts: Array<Account>,
private val clientPackageName: String,
private val errorBlock: (Status) -> Unit,
private val loginBlock: (GoogleSignInAccount?) -> Unit
) : BottomSheetDialogFragment() {
class AssistedSignInFragment : BottomSheetDialogFragment() {

companion object {
const val TAG = "AssistedSignInFragment"
private const val KEY_PACKAGE_NAME = "clientPackageName"
private const val KEY_GOOGLE_SIGN_IN_OPTIONS = "googleSignInOptions"
private const val KEY_BEGIN_SIGN_IN_REQUEST = "beginSignInRequest"

fun newInstance(clientPackageName: String, options: GoogleSignInOptions, request: BeginSignInRequest): AssistedSignInFragment {
val fragment = AssistedSignInFragment()
val args = Bundle().apply {
putString(KEY_PACKAGE_NAME, clientPackageName)
putParcelable(KEY_GOOGLE_SIGN_IN_OPTIONS, options)
putParcelable(KEY_BEGIN_SIGN_IN_REQUEST, request)
}
fragment.arguments = args
return fragment
}
}

private lateinit var clientPackageName: String
private lateinit var options: GoogleSignInOptions
private lateinit var beginSignInRequest: BeginSignInRequest
private lateinit var accounts: Array<Account>

private var cancelBtn: ImageView? = null
private var container: FrameLayout? = null
private var loginJob: Job? = null
Expand All @@ -59,6 +74,16 @@ class AssistedSignInFragment(
private var lastChooseAccount: Account? = null
private var lastChooseAccountPermitted = false

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Log.d(TAG, "onCreate start")
clientPackageName = arguments?.getString(KEY_PACKAGE_NAME) ?: return errorResult()
options = arguments?.getParcelable(KEY_GOOGLE_SIGN_IN_OPTIONS) ?: return errorResult()
beginSignInRequest = arguments?.getParcelable(KEY_BEGIN_SIGN_IN_REQUEST) ?: return errorResult()
val accountManager = activity?.getSystemService<AccountManager>() ?: return errorResult()
accounts = accountManager.getAccountsByType(AuthConstants.DEFAULT_ACCOUNT_TYPE)
}

override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
Log.d(TAG, "onActivityCreated start")
Expand Down Expand Up @@ -91,7 +116,7 @@ class AssistedSignInFragment(
}
}.onFailure {
Log.d(TAG, "filterAccountsLogin: error", it)
errorBlock(Status(CommonStatusCodes.INTERNAL_ERROR, "auth error"))
errorResult()
return@launch
}
if (accounts.size == 1) {
Expand Down Expand Up @@ -244,7 +269,7 @@ class AssistedSignInFragment(
override fun onDismiss(dialog: DialogInterface) {
val assistedSignInActivity = requireContext() as AssistedSignInActivity
if (!assistedSignInActivity.isChangingConfigurations && !isSigningIn) {
errorBlock(Status(CommonStatusCodes.CANCELED, "User cancelled."))
errorResult()
}
cancelLogin()
super.onDismiss(dialog)
Expand All @@ -270,10 +295,10 @@ class AssistedSignInFragment(
prepareChooseLogin(account, showConsent = true, permitted = true)
return@launch
}
loginBlock(googleSignInAccount)
loginResult(googleSignInAccount)
}.onFailure {
Log.d(TAG, "startLogin: error", it)
errorBlock(Status(CommonStatusCodes.INTERNAL_ERROR, "signIn error"))
errorResult()
}
}
}
Expand All @@ -287,4 +312,20 @@ class AssistedSignInFragment(
}
}

private fun errorResult() {
if (activity != null && activity is AssistedSignInActivity) {
val assistedSignInActivity = activity as AssistedSignInActivity
assistedSignInActivity.errorResult(Status(CommonStatusCodes.INTERNAL_ERROR, "signIn error"))
}
activity?.finish()
}

private fun loginResult(googleSignInAccount: GoogleSignInAccount?) {
if (activity != null && activity is AssistedSignInActivity) {
val assistedSignInActivity = activity as AssistedSignInActivity
assistedSignInActivity.loginResult(googleSignInAccount)
}
activity?.finish()
}

}