New BillingCycleRepository

Migrate BillingCyclePreference to BillingCycleRepository first, will
also migrate DataUsageList in future cl.

Also fix an issue that the BillingCyclePreference initial enable state
not set.

Bug: 290856342
Test: manual - on mobile settings
Test: unit test
Change-Id: Idd171fefbc30763010afb7bfb68543612f7b9b1a
This commit is contained in:
Chaohui Wang
2023-09-21 12:23:53 +08:00
parent 2b21582fd3
commit ba1ec910ac
7 changed files with 314 additions and 203 deletions

View File

@@ -0,0 +1,53 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.datausage.lib
import android.content.Context
import android.os.INetworkManagementService
import android.os.ServiceManager
import android.telephony.TelephonyManager
import android.util.Log
import com.android.settingslib.spaprivileged.framework.common.userManager
class BillingCycleRepository(
context: Context,
private val networkService: INetworkManagementService =
INetworkManagementService.Stub.asInterface(
ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE)
),
) {
private val userManager = context.userManager
private val telephonyManager = context.getSystemService(TelephonyManager::class.java)!!
fun isModifiable(subId: Int): Boolean =
isBandwidthControlEnabled() && userManager.isAdminUser && isDataEnabled(subId)
fun isBandwidthControlEnabled(): Boolean = try {
networkService.isBandwidthControlEnabled
} catch (e: Exception) {
Log.w(TAG, "problem talking with INetworkManagementService: ", e)
false
}
private fun isDataEnabled(subId: Int): Boolean =
telephonyManager.createForSubscriptionId(subId)
.isDataEnabledForReason(TelephonyManager.DATA_ENABLED_REASON_USER)
companion object {
private const val TAG = "BillingCycleRepository"
}
}