Remove the cached display name

If the display name is changed, then the Settings should remove the
cached display name.

Bug: 296157273
Test: [pass] atest SubscriptionUtilTest  [pass]Build and manual test
Change-Id: I3b1297ddddf9f9051dd16523b97fc27255cf3923
This commit is contained in:
SongFerngWang
2023-09-19 01:51:31 +08:00
committed by SongFerng Wang
parent d43d6f259a
commit d21f14a002
2 changed files with 133 additions and 21 deletions

View File

@@ -18,7 +18,6 @@ package com.android.settings.network;
import static android.telephony.SubscriptionManager.INVALID_SIM_SLOT_INDEX;
import static android.telephony.UiccSlotInfo.CARD_STATE_INFO_PRESENT;
import static com.android.internal.util.CollectionUtils.emptyIfNull;
import android.annotation.Nullable;
@@ -56,6 +55,8 @@ import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -66,6 +67,9 @@ public class SubscriptionUtil {
static final String SUB_ID = "sub_id";
@VisibleForTesting
static final String KEY_UNIQUE_SUBSCRIPTION_DISPLAYNAME = "unique_subscription_displayName";
private static final String REGEX_DISPLAY_NAME_PREFIXES = "^";
private static final String REGEX_DISPLAY_NAME_SUFFIXES = "\\s[0-9]+";
private static List<SubscriptionInfo> sAvailableResultsForTesting;
private static List<SubscriptionInfo> sActiveResultsForTesting;
@@ -281,8 +285,8 @@ public class SubscriptionUtil {
String displayName = i.getDisplayName().toString();
info.originalName =
TextUtils.equals(displayName, PROFILE_GENERIC_DISPLAY_NAME)
? context.getResources().getString(R.string.sim_card)
: displayName.trim();
? context.getResources().getString(R.string.sim_card)
: displayName.trim();
return info;
});
@@ -298,12 +302,17 @@ public class SubscriptionUtil {
// If a display name is duplicate, append the final 4 digits of the phone number.
// Creates a mapping of Subscription id to original display name + phone number display name
final Supplier<Stream<DisplayInfo>> uniqueInfos = () -> originalInfos.get().map(info -> {
int infoSubId = info.subscriptionInfo.getSubscriptionId();
String cachedDisplayName = getDisplayNameFromSharedPreference(
context, info.subscriptionInfo.getSubscriptionId());
if (!TextUtils.isEmpty(cachedDisplayName)) {
Log.d(TAG, "use cached display name : " + cachedDisplayName);
context, infoSubId);
if (isValidCachedDisplayName(cachedDisplayName, info.originalName.toString())) {
Log.d(TAG, "use cached display name : for subId : " + infoSubId
+ "cached display name : " + cachedDisplayName);
info.uniqueName = cachedDisplayName;
return info;
} else {
Log.d(TAG, "remove cached display name : " + infoSubId);
removeItemFromDisplayNameSharedPreference(context, infoSubId);
}
if (duplicateOriginalNames.contains(info.originalName)) {
@@ -320,9 +329,8 @@ public class SubscriptionUtil {
} else {
info.uniqueName = info.originalName + " " + lastFourDigits;
Log.d(TAG, "Cache display name [" + info.uniqueName + "] for sub id "
+ info.subscriptionInfo.getSubscriptionId());
saveDisplayNameToSharedPreference(
context, info.subscriptionInfo.getSubscriptionId(), info.uniqueName);
+ infoSubId);
saveDisplayNameToSharedPreference(context, infoSubId, info.uniqueName);
}
} else {
info.uniqueName = info.originalName;
@@ -404,10 +412,27 @@ public class SubscriptionUtil {
.apply();
}
private static void removeItemFromDisplayNameSharedPreference(Context context, int subId) {
getDisplayNameSharedPreferenceEditor(context)
.remove(SUB_ID + subId)
.commit();
}
private static String getDisplayNameFromSharedPreference(Context context, int subid) {
return getDisplayNameSharedPreferences(context).getString(SUB_ID + subid, "");
}
@VisibleForTesting
static boolean isValidCachedDisplayName(String cachedDisplayName, String originalName) {
if (TextUtils.isEmpty(cachedDisplayName) || TextUtils.isEmpty(originalName)) {
return false;
}
String regex = REGEX_DISPLAY_NAME_PREFIXES + originalName + REGEX_DISPLAY_NAME_SUFFIXES;
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(cachedDisplayName);
return matcher.matches();
}
public static String getDisplayName(SubscriptionInfo info) {
final CharSequence name = info.getDisplayName();
if (name != null) {