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
18 
19 import android.content.Context
20 import android.net.NetworkTemplate
21 import androidx.preference.PreferenceScreen
22 import com.android.settings.core.BasePreferenceController
23 import com.android.settings.datausage.lib.INetworkCycleDataRepository
24 import com.android.settings.datausage.lib.NetworkCycleChartData
25 import com.android.settings.datausage.lib.NetworkCycleDataRepository
26 
27 class ChartDataUsagePreferenceController(context: Context, preferenceKey: String) :
28     BasePreferenceController(context, preferenceKey) {
29 
30     private lateinit var repository: INetworkCycleDataRepository
31     private lateinit var preference: ChartDataUsagePreference
32 
initnull33     fun init(template: NetworkTemplate) {
34         this.repository = NetworkCycleDataRepository(mContext, template)
35     }
36 
getAvailabilityStatusnull37     override fun getAvailabilityStatus() = AVAILABLE
38 
39     override fun displayPreference(screen: PreferenceScreen) {
40         super.displayPreference(screen)
41         preference = screen.findPreference(preferenceKey)!!
42     }
43 
44     /**
45      * Sets whether billing cycle modifiable.
46      *
47      * Don't bind warning / limit sweeps if not modifiable.
48      */
setBillingCycleModifiablenull49     fun setBillingCycleModifiable(isModifiable: Boolean) {
50         preference.setNetworkPolicy(
51             if (isModifiable) repository.getPolicy() else null
52         )
53     }
54 
55     /** Updates chart to show selected cycle. */
updatenull56     fun update(chartData: NetworkCycleChartData) {
57         preference.setTime(chartData.total.startTime, chartData.total.endTime)
58         preference.setNetworkCycleData(chartData)
59     }
60 }
61