-
-
Notifications
You must be signed in to change notification settings - Fork 48
CCCT-2439 Launch Connect Apps Without Showing The Login Screen #3753
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
Open
conroy-ricketts
wants to merge
25
commits into
master
Choose a base branch
from
CCCT-2439-connect-login-silent-launch-path
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 14 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
f34655d
CCCT-2439 Connect Login Silent Launch Path
conroy-ricketts 2d3bd52
CCCT-2439 Connect Login Silent Launch Path
conroy-ricketts ad7fdc2
CCCT-2439 Connect Login Silent Launch Path
conroy-ricketts 20f959f
CCCT-2439 Connect Login Silent Launch Path
conroy-ricketts 9e3aa2c
CCCT-2439 Connect Login Silent Launch Path
conroy-ricketts 7cafc14
CCCT-2439 Connect Login Silent Launch Path
conroy-ricketts 6a9a96f
CCCT-2439 Connect Login Silent Launch Path
conroy-ricketts 5393194
CCCT-2439 Connect Login Silent Launch Path
conroy-ricketts bc7ddc6
CCCT-2439 Connect Login Silent Launch Path
conroy-ricketts 2e373a6
CCCT-2439 Connect Login Silent Launch Path
conroy-ricketts 5af435e
CCCT-2439 Connect Login Silent Launch Path
conroy-ricketts 4f3657a
Add release notes for CCCT-2439
conroy-ricketts b847f7f
Add QA notes for CCCT-2439
conroy-ricketts 10082ee
CCCT-2439 Connect Login Silent Launch Path
conroy-ricketts ff83fa1
Fixed merge conflict with CCCT-2438-login-routing-extraction-and-inli…
conroy-ricketts 1568ebf
CCCT-2439 Connect Login Silent Launch Path
conroy-ricketts 18c9126
Merge branch 'CCCT-2438-login-routing-extraction-and-inline-app-seati…
conroy-ricketts aa9285c
Fixed merge conflict with CCCT-2438-login-routing-extraction-and-inli…
conroy-ricketts 502c326
Merge branch 'CCCT-2438-login-routing-extraction-and-inline-app-seati…
conroy-ricketts b331085
Merge remote-tracking branch 'origin/CCCT-2438-login-routing-extracti…
conroy-ricketts 8153e70
CCCT-2439 Connect Login Silent Launch Path
conroy-ricketts c663ef9
Merge branch 'master' into CCCT-2439-connect-login-silent-launch-path
conroy-ricketts 126daaf
CCCT-2439 Connect Login Silent Launch Path
conroy-ricketts 72aa6df
Merge branch 'master' of github.com:dimagi/commcare-android into CCCT…
conroy-ricketts cc723d3
Merge branch 'CCCT-2439-connect-login-silent-launch-path' of github.c…
conroy-ricketts File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| package org.commcare.connect | ||
|
|
||
| import android.content.Context | ||
| import androidx.lifecycle.LifecycleOwner | ||
| import androidx.lifecycle.lifecycleScope | ||
| import kotlinx.coroutines.Job | ||
| import kotlinx.coroutines.launch | ||
| import org.commcare.CommCareApplication | ||
| import org.commcare.activities.DataPullController.DataPullMode | ||
| import org.commcare.activities.LoginMode | ||
| import org.commcare.android.database.app.models.UserKeyRecord | ||
| import org.commcare.connect.database.ConnectUserDatabaseUtil | ||
| import org.commcare.google.services.analytics.FirebaseAnalyticsUtil | ||
| import org.commcare.login.AppSeater | ||
| import org.commcare.login.AuthSource | ||
| import org.commcare.login.LoginController | ||
| import org.commcare.login.LoginError | ||
| import org.commcare.login.LoginProgressSink | ||
| import org.commcare.login.LoginRequest | ||
| import org.commcare.login.LoginResult | ||
| import org.commcare.login.SeatResult | ||
| import java.util.Locale | ||
|
|
||
| /** | ||
| * Terminal result of a Connect launch. All non-token, non-seat errors fold into [Retryable]; | ||
| * since this path only does PersonalID password logins, there is no SyncFailed or Demo outcome. | ||
| */ | ||
| sealed class LaunchOutcome { | ||
| object Launched : LaunchOutcome() | ||
|
|
||
| object TokenDenied : LaunchOutcome() | ||
|
|
||
| object AppSeatFailed : LaunchOutcome() | ||
|
|
||
| object CredentialResolutionFailed : LaunchOutcome() | ||
|
|
||
| data class Retryable( | ||
| val error: LoginError, | ||
| ) : LaunchOutcome() | ||
| } | ||
|
|
||
| /** | ||
| * Seats and signs into a Connect app with the worker's PersonalID credentials without showing | ||
| * [org.commcare.activities.LoginActivity]. | ||
| */ | ||
| class ConnectAppLauncher internal constructor( | ||
| private val seatApp: suspend (String, LoginProgressSink) -> SeatResult, | ||
| private val performLogin: suspend (Context, LoginRequest, LoginProgressSink) -> LoginResult, | ||
| private val connectUsername: (Context) -> String?, | ||
| ) { | ||
| constructor() : this( | ||
| seatApp = { appId, sink -> AppSeater().seatIfNeeded(appId, sink) }, | ||
| performLogin = { context, request, sink -> LoginController(context).performLogin(request, sink) }, | ||
| connectUsername = { context -> ConnectUserDatabaseUtil.getUser(context)?.userId }, | ||
| ) | ||
|
|
||
| fun interface OutcomeCallback { | ||
| fun onOutcome(outcome: LaunchOutcome) | ||
| } | ||
|
|
||
| /** Fire-and-forget [awaitOutcome], scoped to [lifecycleOwner] so it cancels with the view. */ | ||
| fun start( | ||
| lifecycleOwner: LifecycleOwner, | ||
| context: Context, | ||
| appId: String, | ||
| isLearning: Boolean, | ||
| sink: LoginProgressSink, | ||
| callback: OutcomeCallback, | ||
| ): Job = | ||
| lifecycleOwner.lifecycleScope.launch { | ||
| callback.onOutcome(awaitOutcome(context, appId, isLearning, sink)) | ||
| } | ||
|
|
||
| suspend fun awaitOutcome( | ||
| context: Context, | ||
| appId: String, | ||
| isLearning: Boolean, | ||
| sink: LoginProgressSink, | ||
| ): LaunchOutcome { | ||
| CommCareApplication.instance().closeUserSession() | ||
| FirebaseAnalyticsUtil.reportCccAppLaunch( | ||
| if (isLearning) "Learn" else "Deliver", | ||
| appId, | ||
| ) | ||
|
|
||
| if (seatApp(appId, sink) is SeatResult.Failed) { | ||
| return LaunchOutcome.AppSeatFailed | ||
| } | ||
|
|
||
| val username = | ||
| connectUsername(context)?.trim()?.lowercase(Locale.ROOT) | ||
| if (username.isNullOrEmpty()) { | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| return LaunchOutcome.CredentialResolutionFailed | ||
| } | ||
|
|
||
| val request = | ||
| LoginRequest( | ||
| appId = appId, | ||
| username = username, | ||
| passwordOrPin = "", | ||
| credentialType = LoginMode.PASSWORD, | ||
| authSource = AuthSource.PersonalId, | ||
| restoreSession = false, | ||
| triggerMultipleUsersWarning = hasMultipleMatchingUsers(username), | ||
| blockRemoteKeyManagement = false, | ||
| dataPullMode = DataPullMode.NORMAL, | ||
| ) | ||
|
|
||
| return when (val result = performLogin(context, request, sink)) { | ||
| is LoginResult.Success -> LaunchOutcome.Launched | ||
| is LoginResult.Failed -> | ||
| when (result.error) { | ||
| is LoginError.TokenDenied -> LaunchOutcome.TokenDenied | ||
| else -> LaunchOutcome.Retryable(result.error) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun hasMultipleMatchingUsers(username: String): Boolean { | ||
| var count = 0 | ||
| for (record in CommCareApplication.instance().getAppStorage(UserKeyRecord::class.java)) { | ||
| if (record.username == username && ++count > 1) { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package org.commcare.connect | ||
|
|
||
| /** Actions a caller runs for a [LaunchOutcome]; a seam so the mapping is testable without a fragment. */ | ||
| interface LaunchActions { | ||
| fun dismissProgress() | ||
|
|
||
| fun launchHome() | ||
|
|
||
| fun handleTokenDenied() | ||
|
|
||
| fun recoverFromSeatFailure() | ||
|
|
||
| fun fallBackToLegacyLaunch() | ||
|
|
||
| fun reportFailure(reason: String) | ||
| } | ||
|
|
||
| /** Maps each [LaunchOutcome] to its [LaunchActions] calls, always dismissing progress first. */ | ||
| object LaunchOutcomeRouter { | ||
| fun dispatch( | ||
| outcome: LaunchOutcome, | ||
| actions: LaunchActions, | ||
| ) { | ||
| actions.dismissProgress() | ||
| when (outcome) { | ||
| LaunchOutcome.Launched -> actions.launchHome() | ||
| LaunchOutcome.TokenDenied -> actions.handleTokenDenied() | ||
| LaunchOutcome.AppSeatFailed -> actions.recoverFromSeatFailure() | ||
| LaunchOutcome.CredentialResolutionFailed -> { | ||
| actions.reportFailure("CredentialResolutionFailed") | ||
| actions.fallBackToLegacyLaunch() | ||
| } | ||
| is LaunchOutcome.Retryable -> { | ||
| actions.reportFailure(outcome.error::class.java.simpleName) | ||
| actions.fallBackToLegacyLaunch() | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.