xref: /aosp_15_r20/frameworks/base/packages/SettingsLib/src/com/android/settingslib/MobileNetworkTypeIcons.kt (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
1 /*
<lambda>null2  * 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.settingslib
18 
19 import com.android.settingslib.mobile.TelephonyIcons.ICON_NAME_TO_ICON
20 
21 /**
22  * A utility class to fetch instances of [MobileNetworkTypeIcon] given a
23  * [SignalIcon.MobileIconGroup].
24  *
25  * Use [getNetworkTypeIcon] to fetch the instances.
26  */
27 class MobileNetworkTypeIcons {
28     companion object {
29         /**
30          * A map from a [SignalIcon.MobileIconGroup.name] to an instance of [MobileNetworkTypeIcon],
31          * which is the preferred class going forward.
32          */
33         private val MOBILE_NETWORK_TYPE_ICONS: Map<String, MobileNetworkTypeIcon>
34 
35         init {
36             // Build up the mapping from the old implementation to the new one.
37             val tempMap: MutableMap<String, MobileNetworkTypeIcon> = mutableMapOf()
38 
39             ICON_NAME_TO_ICON.forEach { (_, mobileIconGroup) ->
40                 tempMap[mobileIconGroup.name] = mobileIconGroup.toNetworkTypeIcon()
41             }
42 
43             MOBILE_NETWORK_TYPE_ICONS = tempMap
44         }
45 
46         /**
47          * A converter function between the old mobile network type icon implementation and the new
48          * one. Given an instance of the old class [mobileIconGroup], outputs an instance of the
49          * new class [MobileNetworkTypeIcon].
50          */
51         @JvmStatic
52         fun getNetworkTypeIcon(
53             mobileIconGroup: SignalIcon.MobileIconGroup
54         ): MobileNetworkTypeIcon {
55             return MOBILE_NETWORK_TYPE_ICONS[mobileIconGroup.name]
56                 ?: mobileIconGroup.toNetworkTypeIcon()
57         }
58 
59         private fun SignalIcon.MobileIconGroup.toNetworkTypeIcon(): MobileNetworkTypeIcon {
60             return MobileNetworkTypeIcon(
61                 name = this.name,
62                 iconResId = this.dataType,
63                 contentDescriptionResId = this.dataContentDescription
64             )
65         }
66     }
67 }
68