1 package com.android.managedprovisioning.contracts
2 
3 import android.content.ComponentName
4 import com.android.managedprovisioning.annotations.LegacyApi
5 import com.android.managedprovisioning.model.PackageDownloadInfo
6 import com.android.managedprovisioning.model.ProvisioningParams
7 import com.android.managedprovisioning.model.WifiInfo
8 import com.android.onboarding.contracts.NodeAwareIntentScope
9 import com.android.onboarding.contracts.NodeAwareIntentSerializer
10 import com.android.onboarding.contracts.NodeId
11 import com.android.onboarding.contracts.OnboardingNodeId
12 import com.android.onboarding.contracts.provisioning.EXTRAS
13 import javax.inject.Inject
14 
15 interface WithProvisioningParams : WithOptionalProvisioningParams {
16     override val provisioningParams: ProvisioningParams
17 }
18 
19 interface WithOptionalProvisioningParams {
20     /** Only needed to preserve data parity when passing provisioning params bundle along */
21     val provisioningParams: ProvisioningParams?
22 }
23 
24 enum class FlowType {
25     Unspecified,
26     Legacy,
27     AdminIntegrated
28 }
29 
30 interface BaseProvisioningArguments : WithProvisioningParams {
31     val flowType: FlowType
32     val deviceAdminDownloadInfo: PackageDownloadInfo?
33     val deviceAdminComponentName: ComponentName?
34     val wifiInfo: WifiInfo?
35     val useMobileData: Boolean
36     val isNfc: Boolean
37     val isQr: Boolean
38 
39     companion object {
40         @JvmStatic
41         @JvmName("of")
invokenull42         operator fun invoke(
43                 provisioningParams: ProvisioningParams,
44                 flowType: FlowType,
45                 deviceAdminDownloadInfo: PackageDownloadInfo?,
46                 deviceAdminComponentName: ComponentName?,
47                 wifiInfo: WifiInfo?,
48                 useMobileData: Boolean,
49                 isNfc: Boolean,
50                 isQr: Boolean,
51         ): BaseProvisioningArguments = object : BaseProvisioningArguments {
52             override val provisioningParams = provisioningParams
53             override val flowType = flowType
54             override val deviceAdminDownloadInfo = deviceAdminDownloadInfo
55             override val deviceAdminComponentName = deviceAdminComponentName
56             override val wifiInfo = wifiInfo
57             override val useMobileData = useMobileData
58             override val isNfc = isNfc
59             override val isQr = isQr
60         }
61 
62         @LegacyApi
63         @JvmStatic
64         @JvmName("of")
invokenull65         operator fun invoke(
66                 provisioningParams: ProvisioningParams
67         ): BaseProvisioningArguments = invoke(
68                 provisioningParams = provisioningParams,
69                 flowType = FlowType.entries[provisioningParams.flowType],
70                 deviceAdminDownloadInfo = provisioningParams.deviceAdminDownloadInfo,
71                 deviceAdminComponentName = provisioningParams.deviceAdminComponentName,
72                 wifiInfo = provisioningParams.wifiInfo,
73                 useMobileData = provisioningParams.useMobileData,
74                 isNfc = provisioningParams.isNfc,
75                 isQr = provisioningParams.isQrProvisioning,
76         )
77     }
78 }
79 
80 class ProvisioningArgumentsSerializer @Inject constructor(@OnboardingNodeId override val nodeId: NodeId) :
81         NodeAwareIntentSerializer<BaseProvisioningArguments> {
82 
83     override fun NodeAwareIntentScope.write(value: BaseProvisioningArguments) {
84         val params =
85                 value::provisioningParams.map {
86                     it
87                             .toBuilder()
88                             .setFlowType(value.flowType.ordinal)
89                             .setDeviceAdminDownloadInfo(value.deviceAdminDownloadInfo)
90                             .setDeviceAdminComponentName(value.deviceAdminComponentName)
91                             .setWifiInfo(value.wifiInfo)
92                             .setUseMobileData(value.useMobileData)
93                             .setIsNfc(value.isNfc)
94                             .setIsQrProvisioning(value.isQr)
95                             .build()
96                 }
97         this[EXTRAS.EXTRA_PROVISIONING_PARAMS] = params
98     }
99 
100     override fun NodeAwareIntentScope.read(): BaseProvisioningArguments {
101         val params = parcelable<ProvisioningParams>(EXTRAS.EXTRA_PROVISIONING_PARAMS).required
102         return object : BaseProvisioningArguments {
103             override val provisioningParams by params
104             override val flowType by params.map(ProvisioningParams::flowType).map(FlowType.entries::get)
105             override val deviceAdminDownloadInfo by params.map(ProvisioningParams::deviceAdminDownloadInfo)
106             override val deviceAdminComponentName by params.map(ProvisioningParams::deviceAdminComponentName)
107             override val wifiInfo by params.map(ProvisioningParams::wifiInfo)
108             override val useMobileData by params.map(ProvisioningParams::useMobileData)
109             override val isNfc by params.map(ProvisioningParams::isNfc)
110             override val isQr by params.map(ProvisioningParams::isQrProvisioning)
111         }
112     }
113 
114 }
115