Merge "Remove the cached display name" into udc-qpr-dev am: 26a1866b8d

Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/Settings/+/24793547

Change-Id: I0c006b215c02acda9a44e717165249fbbd8f353a
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
SongFerng Wang
2023-09-19 12:22:25 +00:00
committed by Automerger Merge Worker
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) {