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             )
45         val file =
46             AppsSafetyLabelHistoryPersistence.getSafetyLabelHistoryFile(app.applicationContext)
47 
48         val appSafetyLabelDiffsFromPersistence =
49             AppsSafetyLabelHistoryPersistence.getAppSafetyLabelDiffs(
50                 Instant.now().atZone(ZoneId.systemDefault()).toInstant().minusMillis(updatePeriod),
51                 file
52             )
53         val updatesFromPersistence =
54             appSafetyLabelDiffsFromPersistence.mapNotNull { it.buildUpdateIfSignificantChange() }
55 
56         postValue(updatesFromPersistence)
57     }
58 
onActivenull59     override fun onActive() {
60         super.onActive()
61         AppsSafetyLabelHistoryPersistence.addListener(this)
62     }
63 
onInactivenull64     override fun onInactive() {
65         super.onInactive()
66         AppsSafetyLabelHistoryPersistence.removeListener(this)
67     }
68 
onSafetyLabelHistoryChangednull69     override fun onSafetyLabelHistoryChanged() {
70         update()
71     }
72 
73     /** Companion object for [AppDataSharingUpdatesLiveData]. */
74     companion object {
75         private const val PROPERTY_DATA_SHARING_UPDATE_PERIOD_MILLIS =
76             "data_sharing_update_period_millis"
77         private const val DEFAULT_DATA_SHARING_UPDATE_PERIOD_DAYS: Long = 30
78     }
79 }
80