LoginActivity: startActivityForResult deprecated.
Migrate startActivityForResult/onActivityResult to StartActivityForResult/registerForActivityResult in LoginActivity/OAuth. This moves responsibility for scheduling the starting Intent from OAuth to LoginActivity. OAuth still generates the Intent. But instead of starting the intent directly in OAuth, the intent is returned to LoginActivity. This better associates processing the activity result with its invocation. OAuthTest module updated to accommodate internal API change.
This commit is contained in:
parent
38a3183b9d
commit
bea1d1f397
|
@ -5,6 +5,7 @@ import android.content.res.Configuration
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
|
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.core.view.doOnLayout
|
import androidx.core.view.doOnLayout
|
||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
|
@ -40,26 +41,21 @@ class LoginActivity : AppCompatActivity() {
|
||||||
limitContainerWidth()
|
limitContainerWidth()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
private var resultLauncher =
|
||||||
super.onActivityResult(requestCode, resultCode, data)
|
registerForActivityResult(StartActivityForResult()) { result ->
|
||||||
|
result.data?.let {
|
||||||
|
oAuth.exchange(this, it) {
|
||||||
|
PowerPreference
|
||||||
|
.getFileByName(AppContext.PREFS_CREDENTIALS)
|
||||||
|
.setBoolean("anonymous", false)
|
||||||
|
|
||||||
data?.let {
|
lifecycleScope.launch(Main) {
|
||||||
when (requestCode) {
|
Userinfo.get(this@LoginActivity, oAuth)?.let {
|
||||||
0 -> {
|
startActivity(Intent(this@LoginActivity, MainActivity::class.java))
|
||||||
oAuth.exchange(this, data) {
|
|
||||||
PowerPreference
|
|
||||||
.getFileByName(AppContext.PREFS_CREDENTIALS)
|
|
||||||
.setBoolean("anonymous", false)
|
|
||||||
|
|
||||||
lifecycleScope.launch(Main) {
|
return@launch finish()
|
||||||
Userinfo.get(this@LoginActivity, oAuth)?.let {
|
|
||||||
startActivity(Intent(this@LoginActivity, MainActivity::class.java))
|
|
||||||
|
|
||||||
return@launch finish()
|
|
||||||
}
|
|
||||||
throw Exception(getString(R.string.login_error_userinfo))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
throw Exception(getString(R.string.login_error_userinfo))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -134,7 +130,7 @@ class LoginActivity : AppCompatActivity() {
|
||||||
oAuth.init(hostname)
|
oAuth.init(hostname)
|
||||||
return oAuth.register {
|
return oAuth.register {
|
||||||
PowerPreference.getFileByName(AppContext.PREFS_CREDENTIALS).setString("hostname", hostname)
|
PowerPreference.getFileByName(AppContext.PREFS_CREDENTIALS).setString("hostname", hostname)
|
||||||
oAuth.authorize(this)
|
resultLauncher.launch(oAuth.authorizeIntent(this))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -184,11 +184,10 @@ class OAuth(private val authorizationServiceFactory: AuthorizationServiceFactory
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun authorize(activity: Activity) {
|
fun authorizeIntent(activity: Activity): Intent? {
|
||||||
val authService = service(activity)
|
val authService = service(activity)
|
||||||
authorizationRequest()?.let { it ->
|
return authorizationRequest()?.let { it ->
|
||||||
val intent = authService.getAuthorizationRequestIntent(it)
|
authService.getAuthorizationRequestIntent(it)
|
||||||
activity.startActivityForResult(intent, 0)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -27,6 +27,7 @@ import org.junit.Before
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
import strikt.api.expectThat
|
import strikt.api.expectThat
|
||||||
import strikt.api.expectThrows
|
import strikt.api.expectThrows
|
||||||
|
import strikt.assertions.isA
|
||||||
import strikt.assertions.isEqualTo
|
import strikt.assertions.isEqualTo
|
||||||
import strikt.assertions.isFalse
|
import strikt.assertions.isFalse
|
||||||
import strikt.assertions.isNotNull
|
import strikt.assertions.isNotNull
|
||||||
|
@ -282,7 +283,7 @@ class OAuthTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `authorize() should start activity for result`() {
|
fun `authorizeIntent() should return an Intent`() {
|
||||||
|
|
||||||
mockkStatic(PowerPreference::class)
|
mockkStatic(PowerPreference::class)
|
||||||
every { PowerPreference.getFileByName(any()) } returns mockPreference
|
every { PowerPreference.getFileByName(any()) } returns mockPreference
|
||||||
|
@ -302,9 +303,7 @@ class OAuthTest {
|
||||||
|
|
||||||
val activity = mockk<Activity>(relaxed = true)
|
val activity = mockk<Activity>(relaxed = true)
|
||||||
|
|
||||||
oAuth.authorize(activity)
|
expectThat(oAuth.authorizeIntent(activity)).isNotNull().isA<Intent>()
|
||||||
|
|
||||||
verify { activity.startActivityForResult(mockkIntent, 0) }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun <T> deserializeJson(
|
private fun <T> deserializeJson(
|
||||||
|
|
Loading…
Reference in New Issue