• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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.lib
18 
19 import android.content.Context
20 import android.text.format.DateUtils
21 import android.util.Range
22 import com.android.settings.R
23 
24 /**
25  * Base data structure representing usage data in a period.
26  */
27 data class NetworkUsageData(
28     val startTime: Long,
29     val endTime: Long,
30     val usage: Long,
31 ) {
32     val timeRange = Range(startTime, endTime)
33 
formatStartDatenull34     fun formatStartDate(context: Context): String =
35         DateUtils.formatDateTime(context, startTime, DATE_FORMAT)
36 
37     fun formatDateRange(context: Context): String =
38         DateUtils.formatDateRange(context, startTime, endTime, DATE_FORMAT)
39 
40     fun formatUsage(context: Context): String =
41         DataUsageFormatter(context).formatDataUsage(usage)
42 
43     fun getDataUsedString(context: Context): String =
44         context.getString(R.string.data_used_template, formatUsage(context))
45 
46     companion object {
47         val AllZero = NetworkUsageData(
48             startTime = 0L,
49             endTime = 0L,
50             usage = 0L,
51         )
52 
53         private const val DATE_FORMAT = DateUtils.FORMAT_SHOW_DATE or DateUtils.FORMAT_ABBREV_MONTH
54     }
55 }
56 
Listnull57 fun List<NetworkUsageData>.aggregate(): NetworkUsageData? = when {
58     isEmpty() -> null
59     else -> NetworkUsageData(
60         startTime = minOf { it.startTime },
61         endTime = maxOf { it.endTime },
62         usage = sumOf { it.usage },
63     )
64 }
65