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.connectivity 18 19 import android.annotation.DrawableRes 20 import android.content.Context 21 import com.android.settingslib.SignalIcon.MobileIconGroup 22 import com.android.settingslib.mobile.MobileIconCarrierIdOverrides 23 import com.android.settingslib.mobile.MobileIconCarrierIdOverridesImpl 24 25 /** 26 * Cache for network type resource IDs. 27 * 28 * The default framework behavior is to have a statically defined icon per network type. See 29 * [MobileIconGroup] for the standard mapping. 30 * 31 * For the case of carrierId-defined overrides, we want to check [MobileIconCarrierIdOverrides] for 32 * an existing icon override, and cache the result of the operation 33 */ 34 class NetworkTypeResIdCache( 35 private val overrides: MobileIconCarrierIdOverrides = MobileIconCarrierIdOverridesImpl() 36 ) { 37 @DrawableRes private var cachedResId: Int = 0 38 private var lastCarrierId: Int? = null 39 private var lastIconGroup: MobileIconGroup? = null 40 private var isOverridden: Boolean = false 41 42 @DrawableRes getnull43 fun get(iconGroup: MobileIconGroup, carrierId: Int, context: Context): Int { 44 if (lastCarrierId != carrierId || lastIconGroup != iconGroup) { 45 lastCarrierId = carrierId 46 lastIconGroup = iconGroup 47 48 val maybeOverride = calculateOverriddenIcon(iconGroup, carrierId, context) 49 if (maybeOverride > 0) { 50 cachedResId = maybeOverride 51 isOverridden = true 52 } else { 53 cachedResId = iconGroup.dataType 54 isOverridden = false 55 } 56 } 57 58 return cachedResId 59 } 60 toStringnull61 override fun toString(): String { 62 return "networkTypeResIdCache={id=$cachedResId, isOverridden=$isOverridden}" 63 } 64 65 @DrawableRes calculateOverriddenIconnull66 private fun calculateOverriddenIcon( 67 iconGroup: MobileIconGroup, 68 carrierId: Int, 69 context: Context, 70 ): Int { 71 val name = iconGroup.name 72 if (!overrides.carrierIdEntryExists(carrierId)) { 73 return 0 74 } 75 76 return overrides.getOverrideFor(carrierId, name, context.resources) 77 } 78 } 79