Merge "Avoid unnecessary Html.fromHtml()" into main

This commit is contained in:
Chaohui Wang
2023-11-10 01:10:15 +00:00
committed by Android (Google) Code Review
7 changed files with 116 additions and 60 deletions

View File

@@ -0,0 +1,38 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.network
import android.content.Context
import android.telephony.SubscriptionManager
import android.text.Html
import com.android.settingslib.SignalIcon
fun SignalIcon.MobileIconGroup.getSummaryForSub(context: Context, subId: Int): String =
when (dataContentDescription) {
0 -> ""
else -> {
SubscriptionManager.getResourcesForSubId(context, subId)
.getString(dataContentDescription)
}
}
fun String.maybeToHtml(): CharSequence = when {
contains(HTML_TAG) -> Html.fromHtml(this, Html.FROM_HTML_MODE_LEGACY)
else -> this
}
private const val HTML_TAG = "</"

View File

@@ -15,6 +15,7 @@
*/
package com.android.settings.network;
import static com.android.settings.network.MobileIconGroupExtKt.maybeToHtml;
import static com.android.settings.network.telephony.MobileNetworkUtils.NO_CELL_DATA_TYPE_ICON;
import android.app.PendingIntent;
@@ -31,7 +32,6 @@ import android.telephony.SignalStrength;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
@@ -176,7 +176,7 @@ public class ProviderModelSliceHelper {
protected ListBuilder.RowBuilder createCarrierRow(String networkTypeDescription) {
final String title = getMobileTitle();
final String summary = getMobileSummary(networkTypeDescription);
final CharSequence summary = getMobileSummary(networkTypeDescription);
Drawable drawable = mContext.getDrawable(
R.drawable.ic_signal_strength_zero_bar_no_internet);
try {
@@ -195,7 +195,7 @@ public class ProviderModelSliceHelper {
.setTitleItem(levelIcon, ListBuilder.ICON_IMAGE)
.addEndItem(toggleAction)
.setPrimaryAction(primaryAction)
.setSubtitle(Html.fromHtml(summary, Html.FROM_HTML_MODE_LEGACY));
.setSubtitle(summary);
return rowBuilder;
}
@@ -255,7 +255,7 @@ public class ProviderModelSliceHelper {
return drawable;
}
private String getMobileSummary(String networkTypeDescription) {
private CharSequence getMobileSummary(String networkTypeDescription) {
if (!isMobileDataEnabled()) {
return mContext.getString(R.string.mobile_data_off_summary);
}
@@ -268,7 +268,7 @@ public class ProviderModelSliceHelper {
mContext.getString(R.string.mobile_data_connection_active),
networkTypeDescription);
}
return summary;
return maybeToHtml(summary);
}
protected String getMobileTitle() {

View File

@@ -19,6 +19,8 @@ package com.android.settings.network;
import static androidx.lifecycle.Lifecycle.Event.ON_PAUSE;
import static androidx.lifecycle.Lifecycle.Event.ON_RESUME;
import static com.android.settings.network.MobileIconGroupExtKt.getSummaryForSub;
import static com.android.settings.network.MobileIconGroupExtKt.maybeToHtml;
import static com.android.settings.network.telephony.MobileNetworkUtils.NO_CELL_DATA_TYPE_ICON;
import static com.android.settingslib.mobile.MobileMappings.getIconKey;
import static com.android.settingslib.mobile.MobileMappings.mapIconSets;
@@ -39,7 +41,6 @@ import android.telephony.SubscriptionManager;
import android.telephony.TelephonyCallback;
import android.telephony.TelephonyDisplayInfo;
import android.telephony.TelephonyManager;
import android.text.Html;
import android.util.ArraySet;
import android.util.Log;
@@ -289,18 +290,19 @@ public class SubscriptionsPreferenceController extends AbstractPreferenceControl
String result = mSubsPrefCtrlInjector.getNetworkType(mContext, mConfig,
mTelephonyDisplayInfo, subId, isCarrierNetworkActive, mCarrierNetworkChangeMode);
if (mSubsPrefCtrlInjector.isActiveCellularNetwork(mContext) || isCarrierNetworkActive) {
String connectionState = mContext.getString(isDds
? R.string.mobile_data_connection_active
: R.string.mobile_data_temp_connection_active);
if (result.isEmpty()) {
result = mContext.getString(isDds ? R.string.mobile_data_connection_active
: R.string.mobile_data_temp_connection_active);
return connectionState;
} else {
result = mContext.getString(R.string.preference_summary_default_combination,
mContext.getString(isDds ? R.string.mobile_data_connection_active
: R.string.mobile_data_temp_connection_active), result);
result = mContext.getString(
R.string.preference_summary_default_combination, connectionState, result);
}
} else if (!isDataInService) {
result = mContext.getString(R.string.mobile_data_no_connection);
return mContext.getString(R.string.mobile_data_no_connection);
}
return Html.fromHtml(result, Html.FROM_HTML_MODE_LEGACY);
return maybeToHtml(result);
}
@VisibleForTesting
@@ -579,10 +581,7 @@ public class SubscriptionsPreferenceController extends AbstractPreferenceControl
return "";
}
int resId = iconGroup.dataContentDescription;
return resId != 0
? SubscriptionManager.getResourcesForSubId(context, subId).getString(resId)
: "";
return getSummaryForSub(iconGroup, context, subId);
}
/**

View File

@@ -17,6 +17,7 @@
package com.android.settings.network.telephony;
import static com.android.settings.network.InternetUpdater.INTERNET_ETHERNET;
import static com.android.settings.network.MobileIconGroupExtKt.getSummaryForSub;
import static com.android.settingslib.mobile.MobileMappings.getIconKey;
import static com.android.settingslib.mobile.MobileMappings.mapIconSets;
@@ -284,19 +285,14 @@ public class NetworkProviderWorker extends WifiScanWorker implements
private String updateNetworkTypeName(Context context, Config config,
TelephonyDisplayInfo telephonyDisplayInfo, int subId) {
String iconKey = getIconKey(telephonyDisplayInfo);
int resId = mapIconSets(config).get(iconKey).dataContentDescription;
if (mWifiPickerTrackerHelper != null
&& mWifiPickerTrackerHelper.isCarrierNetworkActive()) {
MobileIconGroup carrierMergedWifiIconGroup = TelephonyIcons.CARRIER_MERGED_WIFI;
resId = carrierMergedWifiIconGroup.dataContentDescription;
return resId != 0
? SubscriptionManager.getResourcesForSubId(context, subId)
.getString(resId) : "";
return getSummaryForSub(carrierMergedWifiIconGroup, context, subId);
}
return resId != 0
? SubscriptionManager.getResourcesForSubId(context, subId).getString(resId) : "";
String iconKey = getIconKey(telephonyDisplayInfo);
return getSummaryForSub(mapIconSets(config).get(iconKey), context, subId);
}
@VisibleForTesting