1 /*
<lambda>null2  * Copyright (C) 2023 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.0N
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.Context
20 import android.content.Intent
21 import android.content.pm.PackageManager
22 import android.credentials.selection.RequestInfo
23 import android.util.Log
24 import com.android.credentialmanager.ktx.appLabel
25 import com.android.credentialmanager.ktx.cancelUiRequest
26 import com.android.credentialmanager.ktx.requestInfo
27 import com.android.credentialmanager.mapper.toGet
28 import com.android.credentialmanager.model.Request
29 
30 fun Intent.parse(
31     context: Context,
32 ): Request {
33     return parseCancelUiRequest(context.packageManager)
34         ?: parseRequestInfo(context)
35 }
36 
parseCancelUiRequestnull37 fun Intent.parseCancelUiRequest(packageManager: PackageManager): Request? =
38     this.cancelUiRequest?.let { cancelUiRequest ->
39         val showCancel = cancelUiRequest.shouldShowCancellationExplanation().apply {
40             Log.d(TAG, "Received UI cancel request, shouldShowCancellationUi: $this")
41         }
42         if (showCancel) {
43             val appLabel = packageManager.appLabel(cancelUiRequest.packageName)
44             if (appLabel == null) {
45                 Log.d(TAG, "Received UI cancel request with an invalid package name.")
46                 null
47             } else {
48                 Request.Cancel(appName = appLabel, token = cancelUiRequest.token)
49             }
50         } else {
51             Request.Close(cancelUiRequest.token)
52         }
53     }
54 
parseRequestInfonull55 fun Intent.parseRequestInfo(context: Context): Request =
56     requestInfo.let{ info ->
57         when (info?.type) {
58             RequestInfo.TYPE_CREATE -> Request.Create(info.token)
59             RequestInfo.TYPE_GET -> toGet(context)
60             else -> {
61                 throw IllegalStateException("Unrecognized request type: ${info?.type}")
62             }
63         }
64     }
65 
66