1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 
15 package com.android.settings.datausage
16 
17 import android.app.settings.SettingsEnums
18 import android.content.Context
19 import android.net.NetworkTemplate
20 import android.os.Bundle
21 import android.util.AttributeSet
22 import androidx.compose.runtime.getValue
23 import androidx.compose.runtime.remember
24 import androidx.compose.ui.res.stringResource
25 import androidx.lifecycle.compose.collectAsStateWithLifecycle
26 import com.android.settings.R
27 import com.android.settings.core.SubSettingLauncher
28 import com.android.settings.datausage.lib.BillingCycleRepository
29 import com.android.settings.spa.preference.ComposePreference
30 import com.android.settingslib.spa.widget.preference.Preference
31 import com.android.settingslib.spa.widget.preference.PreferenceModel
32 
33 /**
34  * Preference which displays billing cycle of subscription
35  *
36  * @param context Context of preference
37  * @param attrs   The attributes of the XML tag that is inflating the preference
38  */
39 class BillingCyclePreference @JvmOverloads constructor(
40     context: Context,
41     attrs: AttributeSet?,
42     private val repository: BillingCycleRepository = BillingCycleRepository(context),
43 ) : ComposePreference(context, attrs), TemplatePreference {
44 
setTemplatenull45     override fun setTemplate(template: NetworkTemplate, subId: Int) {
46         setContent {
47             val isModifiable by remember(subId) {
48                 repository.isModifiableFlow(subId)
49             }.collectAsStateWithLifecycle(initialValue = false)
50 
51             Preference(object : PreferenceModel {
52                 override val title = stringResource(R.string.billing_cycle)
53                 override val enabled = { isModifiable }
54                 override val onClick = { launchBillingCycleSettings(template) }
55             })
56         }
57     }
58 
launchBillingCycleSettingsnull59     private fun launchBillingCycleSettings(template: NetworkTemplate) {
60         val args = Bundle().apply {
61             putParcelable(DataUsageList.EXTRA_NETWORK_TEMPLATE, template)
62         }
63         SubSettingLauncher(context).apply {
64             setDestination(BillingCycleSettings::class.java.name)
65             setArguments(args)
66             setTitleRes(R.string.billing_cycle)
67             setSourceMetricsCategory(SettingsEnums.PAGE_UNKNOWN)
68         }.launch()
69     }
70 }
71