1 /*
2  * Copyright (C) 2022 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.systemui.statusbar.pipeline.mobile.data.model
18 
19 import android.content.Intent
20 import android.telephony.TelephonyManager.EXTRA_DATA_SPN
21 import android.telephony.TelephonyManager.EXTRA_PLMN
22 import android.telephony.TelephonyManager.EXTRA_SHOW_PLMN
23 import android.telephony.TelephonyManager.EXTRA_SHOW_SPN
24 import com.android.systemui.log.table.Diffable
25 import com.android.systemui.log.table.TableRowLogger
26 
27 /**
28  * Encapsulates the data needed to show a network name for a mobile network. The data is parsed from
29  * the intent sent by [android.telephony.TelephonyManager.ACTION_SERVICE_PROVIDERS_UPDATED].
30  */
31 sealed interface NetworkNameModel : Diffable<NetworkNameModel> {
32     val name: String
33 
34     /** The default name is read from [com.android.internal.R.string.lockscreen_carrier_default] */
35     data class Default(override val name: String) : NetworkNameModel {
logDiffsnull36         override fun logDiffs(prevVal: NetworkNameModel, row: TableRowLogger) {
37             if (prevVal !is Default || prevVal.name != name) {
38                 row.logChange(COL_NETWORK_NAME, "Default($name)")
39             }
40         }
41 
logFullnull42         override fun logFull(row: TableRowLogger) {
43             row.logChange(COL_NETWORK_NAME, "Default($name)")
44         }
45     }
46 
47     /**
48      * This name has been derived from telephony intents. see
49      * [android.telephony.TelephonyManager.ACTION_SERVICE_PROVIDERS_UPDATED]
50      */
51     data class IntentDerived(override val name: String) : NetworkNameModel {
logDiffsnull52         override fun logDiffs(prevVal: NetworkNameModel, row: TableRowLogger) {
53             if (prevVal !is IntentDerived || prevVal.name != name) {
54                 row.logChange(COL_NETWORK_NAME, "IntentDerived($name)")
55             }
56         }
57 
logFullnull58         override fun logFull(row: TableRowLogger) {
59             row.logChange(COL_NETWORK_NAME, "IntentDerived($name)")
60         }
61     }
62 
63     /** This name has been derived from SubscriptionModel. see [SubscriptionModel] */
64     data class SubscriptionDerived(override val name: String) : NetworkNameModel {
logDiffsnull65         override fun logDiffs(prevVal: NetworkNameModel, row: TableRowLogger) {
66             if (prevVal !is SubscriptionDerived || prevVal.name != name) {
67                 row.logChange(COL_NETWORK_NAME, "SubscriptionDerived($name)")
68             }
69         }
70 
logFullnull71         override fun logFull(row: TableRowLogger) {
72             row.logChange(COL_NETWORK_NAME, "SubscriptionDerived($name)")
73         }
74     }
75 
76     /**
77      * This name has been derived from the sim via
78      * [android.telephony.TelephonyManager.getSimOperatorName].
79      */
80     data class SimDerived(override val name: String) : NetworkNameModel {
logDiffsnull81         override fun logDiffs(prevVal: NetworkNameModel, row: TableRowLogger) {
82             if (prevVal !is SimDerived || prevVal.name != name) {
83                 row.logChange(COL_NETWORK_NAME, "SimDerived($name)")
84             }
85         }
86 
logFullnull87         override fun logFull(row: TableRowLogger) {
88             row.logChange(COL_NETWORK_NAME, "SimDerived($name)")
89         }
90     }
91 
92     companion object {
93         const val COL_NETWORK_NAME = "networkName"
94     }
95 }
96 
toNetworkNameModelnull97 fun Intent.toNetworkNameModel(separator: String): NetworkNameModel? {
98     val showSpn = getBooleanExtra(EXTRA_SHOW_SPN, false)
99     val spn = getStringExtra(EXTRA_DATA_SPN)
100     val showPlmn = getBooleanExtra(EXTRA_SHOW_PLMN, false)
101     val plmn = getStringExtra(EXTRA_PLMN)
102 
103     val str = StringBuilder()
104     val strData = StringBuilder()
105     if (showPlmn && plmn != null) {
106         str.append(plmn)
107         strData.append(plmn)
108     }
109     if (showSpn && spn != null) {
110         if (str.isNotEmpty()) {
111             str.append(separator)
112         }
113         str.append(spn)
114     }
115 
116     return if (str.isNotEmpty()) NetworkNameModel.IntentDerived(str.toString()) else null
117 }
118