• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.runtime.Composable
21 import androidx.compose.runtime.getValue
22 import androidx.compose.ui.res.stringResource
23 import androidx.lifecycle.compose.collectAsStateWithLifecycle
24 import com.android.settings.R
25 import com.android.settings.datausage.lib.DataUsageFormatter
26 import com.android.settings.datausage.lib.NetworkUsageDetailsData
27 import com.android.settings.spa.preference.ComposePreferenceController
28 import com.android.settingslib.spa.widget.preference.Preference
29 import com.android.settingslib.spa.widget.preference.PreferenceModel
30 import com.android.settingslib.spa.widget.ui.Category
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 = context.getPlaceholder()
41 
<lambda>null42     private val totalUsageFlow = dataFlow.map {
43         dataUsageFormatter.formatDataUsage(it.totalUsage)
44     }
45 
<lambda>null46     private val foregroundUsageFlow = dataFlow.map {
47         dataUsageFormatter.formatDataUsage(it.foregroundUsage)
48     }
49 
<lambda>null50     private val backgroundUsageFlow = dataFlow.map {
51         dataUsageFormatter.formatDataUsage(it.backgroundUsage)
52     }
53 
getAvailabilityStatusnull54     override fun getAvailabilityStatus() = AVAILABLE
55 
56     fun update(data: NetworkUsageDetailsData) {
57         dataFlow.value = data
58     }
59 
60     @Composable
Contentnull61     override fun Content() {
62         Category {
63             val totalUsage by totalUsageFlow.collectAsStateWithLifecycle(emptyDataUsage)
64             val foregroundUsage by foregroundUsageFlow.collectAsStateWithLifecycle(emptyDataUsage)
65             val backgroundUsage by backgroundUsageFlow.collectAsStateWithLifecycle(emptyDataUsage)
66             Preference(object : PreferenceModel {
67                 override val title = stringResource(R.string.total_size_label)
68                 override val summary = { totalUsage }
69             })
70             Preference(object : PreferenceModel {
71                 override val title = stringResource(R.string.data_usage_label_foreground)
72                 override val summary = { foregroundUsage }
73             })
74             Preference(object : PreferenceModel {
75                 override val title = stringResource(R.string.data_usage_label_background)
76                 override val summary = { backgroundUsage }
77             })
78         }
79     }
80 }
81