1 /* <lambda>null2 * 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.net.NetworkPolicy 20 import android.telephony.SubscriptionPlan 21 import com.android.settings.datausage.lib.INetworkCycleDataRepository 22 import com.android.settings.datausage.lib.NetworkCycleDataRepository.Companion.getCycles 23 import com.android.settings.datausage.lib.NetworkStatsRepository 24 25 interface DataPlanRepository { 26 fun getDataPlanInfo(policy: NetworkPolicy, plans: List<SubscriptionPlan>): DataPlanInfo 27 } 28 29 class DataPlanRepositoryImpl( 30 private val networkCycleDataRepository: INetworkCycleDataRepository, 31 ) : DataPlanRepository { getDataPlanInfonull32 override fun getDataPlanInfo( 33 policy: NetworkPolicy, 34 plans: List<SubscriptionPlan>, 35 ): DataPlanInfo { 36 getPrimaryPlan(plans)?.let { primaryPlan -> 37 val dataPlanSize = when (primaryPlan.dataLimitBytes) { 38 SubscriptionPlan.BYTES_UNLIMITED -> SubscriptionPlan.BYTES_UNKNOWN 39 else -> primaryPlan.dataLimitBytes 40 } 41 return DataPlanInfo( 42 dataPlanCount = plans.size, 43 dataPlanSize = dataPlanSize, 44 dataBarSize = dataPlanSize, 45 dataPlanUse = primaryPlan.dataUsageBytes, 46 cycleEnd = primaryPlan.cycleRule.end?.toInstant()?.toEpochMilli(), 47 snapshotTime = primaryPlan.dataUsageTime, 48 ) 49 } 50 51 val cycle = policy.getCycles().firstOrNull() 52 val dataUsage = networkCycleDataRepository.queryUsage( 53 cycle ?: NetworkStatsRepository.AllTimeRange 54 ).usage 55 return DataPlanInfo( 56 dataPlanCount = 0, 57 dataPlanSize = SubscriptionPlan.BYTES_UNKNOWN, 58 dataBarSize = maxOf(dataUsage, policy.limitBytes, policy.warningBytes), 59 dataPlanUse = dataUsage, 60 cycleEnd = cycle?.upper, 61 snapshotTime = SubscriptionPlan.TIME_UNKNOWN, 62 ) 63 } 64 65 companion object { 66 private const val PETA = 1_000_000_000_000_000L 67 getPrimaryPlannull68 private fun getPrimaryPlan(plans: List<SubscriptionPlan>): SubscriptionPlan? = 69 plans.firstOrNull()?.takeIf { plan -> 70 plan.dataLimitBytes > 0 && validSize(plan.dataUsageBytes) && plan.cycleRule != null 71 } 72 validSizenull73 private fun validSize(value: Long): Boolean = value in 0L until PETA 74 } 75 } 76