• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * 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.network.telephony
18 
19 import android.telephony.CellIdentity
20 import android.telephony.CellInfo
21 import android.text.BidiFormatter
22 import android.text.TextDirectionHeuristics
23 
24 /**
25  * Add static Utility functions to get information from the CellInfo object.
26  * TODO: Modify [CellInfo] for simplify those functions
27  */
28 object CellInfoUtil {
29 
30     /**
31      * Returns the title of the network obtained in the manual search.
32      *
33      * By the following order,
34      * 1. Long Name if not null/empty
35      * 2. Short Name if not null/empty
36      * 3. OperatorNumeric (MCCMNC) string
37      */
38     @JvmStatic
39     fun CellIdentity.getNetworkTitle(): String? {
40         operatorAlphaLong?.takeIf { it.isNotBlank() }?.let { return it.toString() }
41         operatorAlphaShort?.takeIf { it.isNotBlank() }?.let { return it.toString() }
42         val operatorNumeric = getOperatorNumeric() ?: return null
43         val bidiFormatter = BidiFormatter.getInstance()
44         return bidiFormatter.unicodeWrap(operatorNumeric, TextDirectionHeuristics.LTR)
45     }
46 
47     /**
48      * Convert a list of cellInfos to readable string without sensitive info.
49      */
50     @JvmStatic
51     fun cellInfoListToString(cellInfos: List<CellInfo>): String =
52         cellInfos.joinToString(System.lineSeparator()) { cellInfo -> cellInfo.readableString() }
53 
54     /**
55      * Convert [CellInfo] to a readable string without sensitive info.
56      */
57     private fun CellInfo.readableString(): String = buildString {
58         append("{CellType = ${this@readableString::class.simpleName}, ")
59         append("isRegistered = $isRegistered, ")
60         append(cellIdentity.readableString())
61         append("}")
62     }
63 
64     private fun CellIdentity.readableString(): String = buildString {
65         append("mcc = $mccString, ")
66         append("mnc = $mncString, ")
67         append("alphaL = $operatorAlphaLong, ")
68         append("alphaS = $operatorAlphaShort")
69     }
70 
71     /**
72      * Returns the MccMnc.
73      */
74     @JvmStatic
75     fun CellIdentity.getOperatorNumeric(): String? {
76         val mcc = mccString
77         val mnc = mncString
78         return if (mcc == null || mnc == null) null else mcc + mnc
79     }
80 }
81