1 /*
2  * Copyright (C) 2024 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.settings.datausage
18 
19 import android.content.Context
20 import androidx.compose.foundation.layout.Column
21 import androidx.compose.runtime.Composable
22 import androidx.compose.runtime.getValue
23 import androidx.compose.ui.res.stringResource
24 import androidx.lifecycle.compose.collectAsStateWithLifecycle
25 import com.android.settings.R
26 import com.android.settings.datausage.lib.DataUsageFormatter
27 import com.android.settings.datausage.lib.NetworkUsageDetailsData
28 import com.android.settings.spa.preference.ComposePreferenceController
29 import com.android.settingslib.spa.widget.preference.Preference
30 import com.android.settingslib.spa.widget.preference.PreferenceModel
31 import com.android.settingslib.spaprivileged.framework.compose.getPlaceholder
32 import kotlinx.coroutines.flow.MutableStateFlow
33 import kotlinx.coroutines.flow.map
34 
35 class AppDataUsageSummaryController(context: Context, preferenceKey: String) :
36     ComposePreferenceController(context, preferenceKey) {
37 
38     private val dataFlow = MutableStateFlow(NetworkUsageDetailsData.AllZero)
39     private val dataUsageFormatter = DataUsageFormatter(context)
40     private val emptyDataUsage =
41         DataUsageFormatter.FormattedDataUsage(context.getPlaceholder(), context.getPlaceholder())
42 
<lambda>null43     private val totalUsageFlow = dataFlow.map {
44         dataUsageFormatter.formatDataUsage(it.totalUsage)
45     }
46 
<lambda>null47     private val foregroundUsageFlow = dataFlow.map {
48         dataUsageFormatter.formatDataUsage(it.foregroundUsage)
49     }
50 
<lambda>null51     private val backgroundUsageFlow = dataFlow.map {
52         dataUsageFormatter.formatDataUsage(it.backgroundUsage)
53     }
54 
getAvailabilityStatusnull55     override fun getAvailabilityStatus() = AVAILABLE
56 
57     fun update(data: NetworkUsageDetailsData) {
58         dataFlow.value = data
59     }
60 
61     @Composable
Contentnull62     override fun Content() {
63         Column {
64             val totalUsage by totalUsageFlow.collectAsStateWithLifecycle(emptyDataUsage)
65             val foregroundUsage by foregroundUsageFlow.collectAsStateWithLifecycle(emptyDataUsage)
66             val backgroundUsage by backgroundUsageFlow.collectAsStateWithLifecycle(emptyDataUsage)
67             Preference(object : PreferenceModel {
68                 override val title = stringResource(R.string.total_size_label)
69                 override val summary = { totalUsage.displayText }
70                 override val summaryContentDescription = { totalUsage.contentDescription }
71             })
72             Preference(object : PreferenceModel {
73                 override val title = stringResource(R.string.data_usage_label_foreground)
74                 override val summary = { foregroundUsage.displayText }
75                 override val summaryContentDescription = { foregroundUsage.contentDescription }
76             })
77             Preference(object : PreferenceModel {
78                 override val title = stringResource(R.string.data_usage_label_background)
79                 override val summary = { backgroundUsage.displayText }
80                 override val summaryContentDescription = { backgroundUsage.contentDescription }
81             })
82         }
83     }
84 }
85