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.permissioncontroller.permission.data.v34 18 19 import android.app.Application 20 import android.os.Build 21 import android.provider.DeviceConfig 22 import androidx.annotation.RequiresApi 23 import com.android.permissioncontroller.permission.data.SmartAsyncMediatorLiveData 24 import com.android.permissioncontroller.permission.model.v34.AppDataSharingUpdate 25 import com.android.permissioncontroller.permission.model.v34.AppDataSharingUpdate.Companion.buildUpdateIfSignificantChange 26 import com.android.permissioncontroller.safetylabel.AppsSafetyLabelHistoryPersistence 27 import java.time.Duration 28 import java.time.Instant 29 import java.time.ZoneId 30 import kotlinx.coroutines.Job 31 32 /** LiveData for [AppDataSharingUpdate]s. */ 33 @RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE) 34 class AppDataSharingUpdatesLiveData(val app: Application) : 35 SmartAsyncMediatorLiveData<List<AppDataSharingUpdate>>(), 36 AppsSafetyLabelHistoryPersistence.ChangeListener { 37 loadDataAndPostValuenull38 override suspend fun loadDataAndPostValue(job: Job) { 39 val updatePeriod = 40 DeviceConfig.getLong( 41 DeviceConfig.NAMESPACE_PRIVACY, 42 PROPERTY_DATA_SHARING_UPDATE_PERIOD_MILLIS, 43 Duration.ofDays(DEFAULT_DATA_SHARING_UPDATE_PERIOD_DAYS).toMillis()) 44 val file = 45 AppsSafetyLabelHistoryPersistence.getSafetyLabelHistoryFile(app.applicationContext) 46 47 val appSafetyLabelDiffsFromPersistence = 48 AppsSafetyLabelHistoryPersistence.getAppSafetyLabelDiffs( 49 Instant.now().atZone(ZoneId.systemDefault()).toInstant().minusMillis(updatePeriod), 50 file) 51 val updatesFromPersistence = 52 appSafetyLabelDiffsFromPersistence.mapNotNull { it.buildUpdateIfSignificantChange() } 53 54 postValue(updatesFromPersistence) 55 } 56 onActivenull57 override fun onActive() { 58 super.onActive() 59 AppsSafetyLabelHistoryPersistence.addListener(this) 60 } 61 onInactivenull62 override fun onInactive() { 63 super.onInactive() 64 AppsSafetyLabelHistoryPersistence.removeListener(this) 65 } 66 onSafetyLabelHistoryChangednull67 override fun onSafetyLabelHistoryChanged() { 68 update() 69 } 70 71 /** Companion object for [AppDataSharingUpdatesLiveData]. */ 72 companion object { 73 private const val PROPERTY_DATA_SHARING_UPDATE_PERIOD_MILLIS = 74 "data_sharing_update_period_millis" 75 private const val DEFAULT_DATA_SHARING_UPDATE_PERIOD_DAYS: Long = 30 76 } 77 } 78