Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 41b40527 authored by Shuang Hao's avatar Shuang Hao Committed by Android (Google) Code Review
Browse files

Merge changes I24e7511a,I986d6652 into main

* changes:
  Single login screen refactoring: 1. introduce flowEngine. 2. make use of CredentialManager::sendEntrySelectionResult api. 3. git rid of Screen ViewModel since there is no dynamic content in this screen.
  Add interface CredentialManager::sendEntrySelectionResult.
parents eced6d98 e83bd36c
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.credentialmanager.client
import android.content.Intent
import android.credentials.selection.BaseDialogResult
import android.credentials.selection.UserSelectionDialogResult
import com.android.credentialmanager.model.EntryInfo
import com.android.credentialmanager.model.Request
import kotlinx.coroutines.flow.StateFlow

@@ -54,4 +55,20 @@ interface CredentialManagerClient {
     * @throws [IllegalStateException] if [requests] is not [Request.Get].
     */
    fun sendResult(result: UserSelectionDialogResult)

    /**
     * Sends a response to the system service with a selected [EntryInfo].
     *
     * @return if the current [Request.Get] flow can be ended peacefully.
     * if not, App has to keep reacting to the further update from [requests] until [Request.Cancel]
     * or [Request.Close] is received.
     *
     * @throws [IllegalStateException] if [requests] is not [Request.Get].
     */
    fun sendEntrySelectionResult(
        entryInfo: EntryInfo,
        resultCode: Int? = null,
        resultData: Intent? = null,
        isAutoSelected: Boolean = false,
    ): Boolean
}
 No newline at end of file
+62 −0
Original line number Diff line number Diff line
@@ -16,16 +16,24 @@

package com.android.credentialmanager.client.impl

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.credentials.selection.BaseDialogResult
import android.credentials.selection.BaseDialogResult.RESULT_CODE_DIALOG_USER_CANCELED
import android.credentials.selection.Constants
import android.credentials.selection.ProviderPendingIntentResponse
import android.credentials.selection.UserSelectionDialogResult
import android.os.Bundle
import android.os.IBinder
import android.os.ResultReceiver
import android.util.Log
import com.android.credentialmanager.TAG
import com.android.credentialmanager.model.Request
import com.android.credentialmanager.parse
import com.android.credentialmanager.client.CredentialManagerClient
import com.android.credentialmanager.model.EntryInfo

import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
@@ -69,4 +77,58 @@ class CredentialManagerClientImpl @Inject constructor(
            )
        }
    }

    override fun sendEntrySelectionResult(
        entryInfo: EntryInfo,
        resultCode: Int?,
        resultData: Intent?,
        isAutoSelected: Boolean,
    ): Boolean {
        Log.d(TAG, "sendEntrySelectionResult, resultCode: $resultCode, resultData: $resultData," +
                " entryInfo: $entryInfo")
        val currentRequest = requests.value
        check(currentRequest is Request.Get) { "current request is not get." }
        if (resultCode == Activity.RESULT_CANCELED) {
            if (isAutoSelected) {
                currentRequest.sendCancellationCode(RESULT_CODE_DIALOG_USER_CANCELED)
            }
            return isAutoSelected
        }
        val userSelectionDialogResult = UserSelectionDialogResult(
            currentRequest.token,
            entryInfo.providerId,
            entryInfo.entryKey,
            entryInfo.entrySubkey,
            if (resultCode != null) ProviderPendingIntentResponse(
                resultCode,
                resultData
            ) else null
        )
        sendResult(userSelectionDialogResult)
        return entryInfo.shouldTerminateUiUponSuccessfulProviderResult
    }

    private fun Request.Get.sendCancellationCode(cancelCode: Int) {
        sendCancellationCode(
            cancelCode = cancelCode,
            requestToken = token,
            resultReceiver = resultReceiver,
            finalResponseReceiver = finalResponseReceiver
        )
    }

    private fun sendCancellationCode(
        cancelCode: Int,
        requestToken: IBinder?,
        resultReceiver: ResultReceiver?,
        finalResponseReceiver: ResultReceiver?
    ) {
        if (requestToken != null && resultReceiver != null) {
            val resultData = Bundle().apply {
                putParcelable(Constants.EXTRA_FINAL_RESPONSE_RECEIVER, finalResponseReceiver)
            }
            BaseDialogResult.addToBundle(BaseDialogResult(requestToken), resultData)
            resultReceiver.send(cancelCode, resultData)
        }
    }
}
+6 −0
Original line number Diff line number Diff line
@@ -54,3 +54,9 @@ val Intent.resultReceiver: ResultReceiver?
        Constants.EXTRA_RESULT_RECEIVER,
        ResultReceiver::class.java
    )

val Intent.finalResponseReceiver: ResultReceiver?
    get() = this.getParcelableExtra(
        Constants.EXTRA_FINAL_RESPONSE_RECEIVER,
        ResultReceiver::class.java
    )
+2 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import android.content.Context
import android.content.Intent
import com.android.credentialmanager.ktx.getCredentialProviderDataList
import com.android.credentialmanager.ktx.requestInfo
import com.android.credentialmanager.ktx.finalResponseReceiver
import com.android.credentialmanager.ktx.resultReceiver
import com.android.credentialmanager.ktx.toProviderList
import com.android.credentialmanager.model.Request
@@ -28,6 +29,7 @@ fun Intent.toGet(context: Context): Request.Get {
    return Request.Get(
        token = requestInfo?.token,
        resultReceiver = resultReceiver,
        finalResponseReceiver = finalResponseReceiver,
        providerInfos = getCredentialProviderDataList.toProviderList(context)
    )
}
+1 −0
Original line number Diff line number Diff line
@@ -49,6 +49,7 @@ sealed class Request private constructor(
    data class Get(
        override val token: IBinder?,
        val resultReceiver: ResultReceiver?,
        val finalResponseReceiver: ResultReceiver?,
        val providerInfos: List<ProviderInfo>,
    ) : Request(token)
    /**
Loading