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.lib
18 
19 import android.content.Context
20 import android.net.NetworkTemplate
21 import android.telephony.SubscriptionManager
22 import androidx.annotation.StringRes
23 import com.android.settings.R
24 import com.android.settings.datausage.DataUsageUtils
25 
26 interface INetworkTemplates {
27     /**
28      * Returns the default network template based on the availability of mobile data, Wifi. Returns
29      * ethernet template if both mobile data and Wifi are not available.
30      */
getDefaultTemplatenull31     fun getDefaultTemplate(context: Context): NetworkTemplate
32 }
33 
34 object NetworkTemplates : INetworkTemplates {
35     @JvmStatic
36     @StringRes
37     fun NetworkTemplate.getTitleResId(): Int =
38         when (matchRule) {
39             NetworkTemplate.MATCH_MOBILE,
40             NetworkTemplate.MATCH_CARRIER -> R.string.cellular_data_usage
41 
42             NetworkTemplate.MATCH_WIFI -> R.string.wifi_data_usage
43             NetworkTemplate.MATCH_ETHERNET -> R.string.ethernet_data_usage
44             else -> R.string.data_usage_app_summary_title
45         }
46 
47     /**
48      * Returns the default network template based on the availability of mobile data, Wifi. Returns
49      * ethernet template if both mobile data and Wifi are not available.
50      */
51     override fun getDefaultTemplate(context: Context): NetworkTemplate =
52         DataUsageUtils.getDefaultTemplate(
53             context,
54             SubscriptionManager.getDefaultDataSubscriptionId(),
55         )
56 }
57