1 /* 2 * Copyright (C) 2022 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.ondevicepersonalization.services.policyengine.data.impl 18 19 import android.adservices.ondevicepersonalization.AppInfo 20 import android.adservices.ondevicepersonalization.UserData 21 import android.util.ArrayMap 22 import com.android.libraries.pcc.chronicle.api.Connection 23 import com.android.libraries.pcc.chronicle.api.ConnectionProvider 24 import com.android.libraries.pcc.chronicle.api.ConnectionRequest 25 import com.android.libraries.pcc.chronicle.api.DataType 26 import com.android.libraries.pcc.chronicle.api.ManagedDataType 27 import com.android.libraries.pcc.chronicle.api.ManagementStrategy 28 import com.android.libraries.pcc.chronicle.api.StorageMedia 29 import com.android.ondevicepersonalization.services.data.user.RawUserData 30 import com.android.ondevicepersonalization.services.policyengine.data.USER_DATA_GENERATED_DTD 31 import com.android.ondevicepersonalization.services.policyengine.data.UserDataReader 32 import java.time.Duration 33 34 /** [ConnectionProvider] implementation for ODA use data. */ 35 class UserDataConnectionProvider() : ConnectionProvider { 36 override val dataType: DataType = 37 ManagedDataType( 38 USER_DATA_GENERATED_DTD, 39 ManagementStrategy.Stored(false, StorageMedia.MEMORY, Duration.ofDays(30)), 40 setOf(UserDataReader::class.java) 41 ) 42 getConnectionnull43 override fun getConnection(connectionRequest: ConnectionRequest<out Connection>): Connection { 44 return UserDataReaderImpl() 45 } 46 47 class UserDataReaderImpl : UserDataReader { readUserDatanull48 override fun readUserData(): UserData? { 49 return getUserData(false); 50 } 51 readUserDataWithAppInstallnull52 override fun readUserDataWithAppInstall(): UserData? { 53 return getUserData(true); 54 } 55 getUserDatanull56 private fun getUserData(appInstall: Boolean): UserData? { 57 val rawUserData: RawUserData = RawUserData.getInstance() ?: return null 58 // TODO(b/267013762): more privacy-preserving processing may be needed 59 val builder: UserData.Builder = UserData.Builder() 60 .setTimezoneUtcOffsetMins(rawUserData.utcOffset) 61 .setOrientation(rawUserData.orientation) 62 .setAvailableStorageBytes(rawUserData.availableStorageBytes) 63 .setBatteryPercentage(rawUserData.batteryPercentage) 64 .setCarrier(rawUserData.carrier.toString()) 65 .setDataNetworkType(rawUserData.dataNetworkType) 66 67 // TODO (b/299683848): follow up the codegen bug 68 if (rawUserData.networkCapabilities != null) { 69 builder.setNetworkCapabilities(rawUserData.networkCapabilities) 70 } 71 if (appInstall) { 72 builder.setAppInfos(getInstallApps(rawUserData.installedApps)) 73 } 74 return builder.build() 75 } 76 getInstallAppsnull77 private fun getInstallApps(installedApps: Set<String>): Map<String, AppInfo> { 78 var res = ArrayMap<String, AppInfo>() 79 for (appName in installedApps) { 80 res[appName] = AppInfo.Builder() 81 .setInstalled(true) 82 .build() 83 } 84 return res 85 } 86 } 87 } 88