• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.credentialmanager
18 
19 import android.content.Intent
20 import android.graphics.drawable.Drawable
21 import androidx.activity.result.IntentSenderRequest
22 import androidx.compose.runtime.Composable
23 import com.android.credentialmanager.model.EntryInfo
24 import com.android.credentialmanager.model.get.ActionEntryInfo
25 import com.android.credentialmanager.model.get.AuthenticationEntryInfo
26 import com.android.credentialmanager.model.get.CredentialEntryInfo
27 import kotlinx.coroutines.flow.StateFlow
28 
29 /** Engine of the credential selecting flow. */
30 interface FlowEngine {
31     /** UI state of the selector app */
32     val uiState: StateFlow<CredentialSelectorUiState>
33     /** Back from previous stage. */
backnull34     fun back()
35     /** Cancels the selection flow. */
36     fun cancel()
37     /** Opens secondary screen. */
38     fun openSecondaryScreen()
39     /**
40      * Sends [entryInfo] as long as result after launching [EntryInfo.pendingIntent] with
41      * [EntryInfo.fillInIntent].
42      *
43      * @param entryInfo: selected entry.
44      * @param resultCode: result code received after launch.
45      * @param resultData: data received after launch
46      * @param isAutoSelected: whether the entry is auto selected or by user.
47      */
48     fun sendSelectionResult(
49         entryInfo: EntryInfo,
50         resultCode: Int? = null,
51         resultData: Intent? = null,
52         isAutoSelected: Boolean = false,
53     )
54 
55     /**
56      * Helper function to get an entry selector.
57      *
58      * @return selector fun consumes selected [EntryInfo]. Once invoked, [IntentSenderRequest] would
59      * be launched and invocation of [sendSelectionResult] would happen right after launching result
60      * coming back.
61      */
62     @Composable
63     fun getEntrySelector(): (entry: EntryInfo, isAutoSelected: Boolean) -> Unit
64 }
65 
66 /** UI state of the selector app */
67 sealed class CredentialSelectorUiState {
68     /** Idle UI state, no request is going on. */
69     data object Idle : CredentialSelectorUiState()
70     /** Getting credential UI state. */
71     sealed class Get : CredentialSelectorUiState() {
72         /** Getting credential UI state when there is only one credential available. */
73         data class SingleEntry(val entry: CredentialEntryInfo) : Get()
74         /**
75          * Getting credential UI state on primary screen when there is are multiple accounts.
76          */
77         data class MultipleEntryPrimaryScreen(
78             val icon: Drawable?,
79             val sortedEntries: List<CredentialEntryInfo>,
80             val authenticationEntryList: List<AuthenticationEntryInfo>,
81             ) : Get()
82         /** Getting credential UI state on secondary screen when there are multiple accounts available. */
83         data class MultipleEntry(
84             val accounts: List<PerUserNameEntries>,
85             val actionEntryList: List<ActionEntryInfo>,
86             val authenticationEntryList: List<AuthenticationEntryInfo>,
87         ) : Get() {
88             data class PerUserNameEntries(
89                 val userName: String,
90                 val sortedCredentialEntryList: List<CredentialEntryInfo>,
91             )
92         }
93     }
94     /** Creating credential UI state. */
95     data object Create : CredentialSelectorUiState()
96     /** Request is cancelling by [appName]. */
97     data class Cancel(val appName: String) : CredentialSelectorUiState()
98     /** Request is closed peacefully. */
99     data object Close : CredentialSelectorUiState()
100 }