1 /*
<lambda>null2  * 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.os.INetworkManagementService
21 import android.os.ServiceManager
22 import android.util.Log
23 import androidx.annotation.OpenForTesting
24 import com.android.settings.network.telephony.TelephonyRepository
25 import com.android.settingslib.spaprivileged.framework.common.userManager
26 import kotlinx.coroutines.Dispatchers
27 import kotlinx.coroutines.flow.Flow
28 import kotlinx.coroutines.flow.conflate
29 import kotlinx.coroutines.flow.flowOn
30 import kotlinx.coroutines.flow.map
31 
32 @OpenForTesting
33 open class BillingCycleRepository @JvmOverloads constructor(
34     context: Context,
35     private val networkService: INetworkManagementService =
36         INetworkManagementService.Stub.asInterface(
37             ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE)
38         ),
39     private val telephonyRepository: TelephonyRepository = TelephonyRepository(context),
40 ) {
41     private val userManager = context.userManager
42 
43     fun isModifiableFlow(subId: Int): Flow<Boolean> =
44         telephonyRepository.isDataEnabledFlow(subId).map { isDataEnabled ->
45             isDataEnabled && isBandwidthControlEnabled() && userManager.isAdminUser
46         }.conflate().flowOn(Dispatchers.Default)
47 
48     open fun isBandwidthControlEnabled(): Boolean = try {
49         networkService.isBandwidthControlEnabled
50     } catch (e: Exception) {
51         Log.w(TAG, "problem talking with INetworkManagementService: ", e)
52         false
53     }
54 
55     companion object {
56         private const val TAG = "BillingCycleRepository"
57     }
58 }
59