1 /*
2  * 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.content.Context
20 import android.telephony.SubscriptionManager
21 import androidx.preference.PreferenceScreen
22 import com.android.settings.R
23 import com.android.settings.core.BasePreferenceController
24 import com.android.settings.datausage.lib.DataUsageLib.getMobileTemplate
25 import com.android.settings.network.telephony.MobileNetworkSettingsSearchIndex.MobileNetworkSettingsSearchItem
26 import com.android.settings.network.telephony.MobileNetworkSettingsSearchIndex.MobileNetworkSettingsSearchResult
27 
28 class BillingCyclePreferenceController(context: Context, preferenceKey: String) :
29     BasePreferenceController(context, preferenceKey) {
30     private var subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID
31 
initnull32     fun init(subId: Int) {
33         this.subId = subId
34     }
35 
getAvailabilityStatusnull36     override fun getAvailabilityStatus() =
37         if (DataUsageUtils.hasMobileData(mContext)) AVAILABLE else CONDITIONALLY_UNAVAILABLE
38 
39     override fun displayPreference(screen: PreferenceScreen) {
40         super.displayPreference(screen)
41         val preference = screen.findPreference<BillingCyclePreference>(preferenceKey)
42         val template = getMobileTemplate(mContext, subId)
43         preference?.setTemplate(template, subId)
44     }
45 
46     companion object {
47         class BillingCycleSearchItem(private val context: Context) :
48             MobileNetworkSettingsSearchItem {
getSearchResultnull49             override fun getSearchResult(subId: Int): MobileNetworkSettingsSearchResult? {
50                 if (!DataUsageUtils.hasMobileData(context)) return null
51                 return MobileNetworkSettingsSearchResult(
52                     key = "billing_preference",
53                     title = context.getString(R.string.billing_cycle),
54                 )
55             }
56         }
57     }
58 }
59