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

Commit 90f8ab6d authored by shuanghao's avatar shuanghao
Browse files

Replace Credential model from Password to shared ProviderInfo.

BUG: 313497665
Test: Manual on Wear with AddressBook test app.
Change-Id: I8af08369161a50742cab8687e83613833dd9f742
parent 498a8738
Loading
Loading
Loading
Loading
+6 −5
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.credentialmanager

import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.credentials.ui.RequestInfo
@@ -27,10 +28,10 @@ import com.android.credentialmanager.mapper.toGet
import com.android.credentialmanager.model.Request

fun Intent.parse(
    packageManager: PackageManager,
    context: Context,
): Request {
    return parseCancelUiRequest(packageManager)
        ?: parseRequestInfo()
    return parseCancelUiRequest(context.packageManager)
        ?: parseRequestInfo(context)
}

fun Intent.parseCancelUiRequest(packageManager: PackageManager): Request? =
@@ -51,11 +52,11 @@ fun Intent.parseCancelUiRequest(packageManager: PackageManager): Request? =
        }
    }

fun Intent.parseRequestInfo(): Request =
fun Intent.parseRequestInfo(context: Context): Request =
    requestInfo.let{ info ->
        when (info?.type) {
            RequestInfo.TYPE_CREATE -> Request.Create(info.token)
            RequestInfo.TYPE_GET -> toGet()
            RequestInfo.TYPE_GET -> toGet(context)
            else -> {
                throw IllegalStateException("Unrecognized request type: ${info?.type}")
            }
+4 −3
Original line number Diff line number Diff line
@@ -16,8 +16,8 @@

package com.android.credentialmanager.client.impl

import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.credentials.ui.BaseDialogResult
import android.credentials.ui.UserSelectionDialogResult
import android.os.Bundle
@@ -26,12 +26,13 @@ import com.android.credentialmanager.TAG
import com.android.credentialmanager.model.Request
import com.android.credentialmanager.parse
import com.android.credentialmanager.client.CredentialManagerClient
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import javax.inject.Inject

class CredentialManagerClientImpl @Inject constructor(
        private val packageManager: PackageManager,
    @ApplicationContext private val context: Context,
) : CredentialManagerClient {

    private val _requests = MutableStateFlow<Request?>(null)
@@ -40,7 +41,7 @@ class CredentialManagerClientImpl @Inject constructor(

    override fun updateRequest(intent: Intent) {
        val request = intent.parse(
            packageManager = packageManager,
            context = context,
        )
        Log.d(TAG, "Request parsed: $request, client instance: $this")
        if (request is Request.Cancel || request is Request.Close) {
+9 −8
Original line number Diff line number Diff line
@@ -43,20 +43,21 @@ import com.android.credentialmanager.model.ActionEntryInfo
import com.android.credentialmanager.model.AuthenticationEntryInfo
import com.android.credentialmanager.model.CredentialEntryInfo
import com.android.credentialmanager.model.CredentialType
import com.android.credentialmanager.model.Password
import com.android.credentialmanager.model.ProviderInfo
import com.android.credentialmanager.model.RemoteEntryInfo
import com.android.credentialmanager.TAG

fun Password.getIntentSenderRequest(
fun CredentialEntryInfo.getIntentSenderRequest(
    isAutoSelected: Boolean = false
): IntentSenderRequest {
    val entryIntent = entry.frameworkExtrasIntent
    entryIntent?.putExtra(IS_AUTO_SELECTED_KEY, isAutoSelected)
): IntentSenderRequest? {
    val entryIntent = fillInIntent?.putExtra(IS_AUTO_SELECTED_KEY, isAutoSelected)

    return IntentSenderRequest.Builder(
        pendingIntent = passwordCredentialEntry.pendingIntent
    ).setFillInIntent(entryIntent).build()
    return pendingIntent?.let{
        IntentSenderRequest
            .Builder(pendingIntent = it)
            .setFillInIntent(entryIntent)
            .build()
    }
}

// Returns the list (potentially empty) of enabled provider.
+5 −35
Original line number Diff line number Diff line
@@ -16,48 +16,18 @@

package com.android.credentialmanager.mapper

import android.content.Context
import android.content.Intent
import android.credentials.ui.Entry
import androidx.credentials.provider.PasswordCredentialEntry
import com.android.credentialmanager.factory.fromSlice
import com.android.credentialmanager.ktx.getCredentialProviderDataList
import com.android.credentialmanager.ktx.requestInfo
import com.android.credentialmanager.ktx.resultReceiver
import com.android.credentialmanager.model.Password
import com.android.credentialmanager.ktx.toProviderList
import com.android.credentialmanager.model.Request
import com.google.common.collect.ImmutableList
import com.google.common.collect.ImmutableMap

fun Intent.toGet(): Request.Get {
    val credentialEntries = mutableListOf<Pair<String, Entry>>()
    for (providerData in getCredentialProviderDataList) {
        for (credentialEntry in providerData.credentialEntries) {
            credentialEntries.add(
                Pair(providerData.providerFlattenedComponentName, credentialEntry)
            )
        }
    }

    val passwordEntries = mutableListOf<Password>()
    for ((providerId, entry) in credentialEntries) {
        val slice = fromSlice(entry.slice)
        if (slice is PasswordCredentialEntry) {
            passwordEntries.add(
                Password(
                    providerId = providerId,
                    entry = entry,
                    passwordCredentialEntry = slice
                )
            )
        }
    }

fun Intent.toGet(context: Context): Request.Get {
    return Request.Get(
        token = requestInfo?.token,
        resultReceiver = this.resultReceiver,
        providers = ImmutableMap.copyOf(
            getCredentialProviderDataList.associateBy { it.providerFlattenedComponentName }
        ),
        passwordEntries = ImmutableList.copyOf(passwordEntries)
        resultReceiver = resultReceiver,
        providerInfos = getCredentialProviderDataList.toProviderList(context)
    )
}
+0 −26
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0N
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.credentialmanager.model

import android.credentials.ui.Entry
import androidx.credentials.provider.PasswordCredentialEntry

data class Password(
    val providerId: String,
    val entry: Entry,
    val passwordCredentialEntry: PasswordCredentialEntry,
)
Loading