Add signal strength icons to multi-SIM header

When a device is in DSDS mode with multiple SIMs, we show a header on
the Network & internet page with a "connectivity overview", with an
entry for Wi-Fi (if connected) and each active SIM. The icon for Wi-Fi
shows signal strength, but the icons for the active SIMs were just
static; this CL fixes that. It introduces a class for listening to
signal strength changes on a set of subscriptions, and moves some
existing code we had for displaying signal strength (when manually
picking networks for GSM) in NetworkOperatorPreference into a utility
method that can be shared.

Bug: 128855095
Test: make RunSettingsRoboTests
Change-Id: I668cafe1e9f9c3651b1f33783c8538ad9c2732b5
This commit is contained in:
Antony Sargent
2019-05-22 14:50:36 -07:00
parent fb28f92f71
commit 223c179cd3
7 changed files with 613 additions and 137 deletions

View File

@@ -19,16 +19,22 @@ 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.telephony.MobileNetworkUtils.NO_CELL_DATA_TYPE_ICON;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.provider.Settings;
import android.telephony.SignalStrength;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.util.ArraySet;
import androidx.annotation.VisibleForTesting;
import androidx.collection.ArrayMap;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
@@ -40,9 +46,14 @@ import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.network.telephony.DataConnectivityListener;
import com.android.settings.network.telephony.MobileNetworkActivity;
import com.android.settings.network.telephony.MobileNetworkUtils;
import com.android.settings.network.telephony.SignalStrengthListener;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.net.SignalStrengthUtil;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
/**
* This manages a set of Preferences it places into a PreferenceGroup owned by some parent
@@ -51,7 +62,8 @@ import java.util.Map;
*/
public class SubscriptionsPreferenceController extends AbstractPreferenceController implements
LifecycleObserver, SubscriptionsChangeListener.SubscriptionsChangeListenerClient,
MobileDataEnabledListener.Client, DataConnectivityListener.Client {
MobileDataEnabledListener.Client, DataConnectivityListener.Client,
SignalStrengthListener.Callback {
private static final String TAG = "SubscriptionsPrefCntrlr";
private UpdateListener mUpdateListener;
@@ -62,6 +74,8 @@ public class SubscriptionsPreferenceController extends AbstractPreferenceControl
private SubscriptionsChangeListener mSubscriptionsListener;
private MobileDataEnabledListener mDataEnabledListener;
private DataConnectivityListener mConnectivityListener;
private SignalStrengthListener mSignalStrengthListener;
// Map of subscription id to Preference
private Map<Integer, Preference> mSubscriptionPreferences;
@@ -102,6 +116,7 @@ public class SubscriptionsPreferenceController extends AbstractPreferenceControl
mSubscriptionsListener = new SubscriptionsChangeListener(context, this);
mDataEnabledListener = new MobileDataEnabledListener(context, this);
mConnectivityListener = new DataConnectivityListener(context, this);
mSignalStrengthListener = new SignalStrengthListener(context, this);
lifecycle.addObserver(this);
}
@@ -110,6 +125,7 @@ public class SubscriptionsPreferenceController extends AbstractPreferenceControl
mSubscriptionsListener.start();
mDataEnabledListener.start(SubscriptionManager.getDefaultDataSubscriptionId());
mConnectivityListener.start();
mSignalStrengthListener.resume();
update();
}
@@ -118,6 +134,7 @@ public class SubscriptionsPreferenceController extends AbstractPreferenceControl
mSubscriptionsListener.stop();
mDataEnabledListener.stop();
mConnectivityListener.stop();
mSignalStrengthListener.pause();
}
@Override
@@ -136,6 +153,7 @@ public class SubscriptionsPreferenceController extends AbstractPreferenceControl
mPreferenceGroup.removePreference(pref);
}
mSubscriptionPreferences.clear();
mSignalStrengthListener.updateSubscriptionIds(Collections.emptySet());
mUpdateListener.onChildrenUpdated();
return;
}
@@ -144,16 +162,20 @@ public class SubscriptionsPreferenceController extends AbstractPreferenceControl
mSubscriptionPreferences = new ArrayMap<>();
int order = mStartOrder;
final Set<Integer> activeSubIds = new ArraySet<>();
final int dataDefaultSubId = SubscriptionManager.getDefaultDataSubscriptionId();
for (SubscriptionInfo info : SubscriptionUtil.getActiveSubscriptions(mManager)) {
final int subId = info.getSubscriptionId();
activeSubIds.add(subId);
Preference pref = existingPrefs.remove(subId);
if (pref == null) {
pref = new Preference(mPreferenceGroup.getContext());
mPreferenceGroup.addPreference(pref);
}
pref.setTitle(info.getDisplayName());
pref.setSummary(getSummary(subId));
pref.setIcon(R.drawable.ic_network_cell);
final boolean isDefaultForData = (subId == dataDefaultSubId);
pref.setSummary(getSummary(subId, isDefaultForData));
setIcon(pref, subId, isDefaultForData);
pref.setOrder(order++);
pref.setOnPreferenceClickListener(clickedPref -> {
@@ -165,6 +187,7 @@ public class SubscriptionsPreferenceController extends AbstractPreferenceControl
mSubscriptionPreferences.put(subId, pref);
}
mSignalStrengthListener.updateSubscriptionIds(activeSubIds);
// Remove any old preferences that no longer map to a subscription.
for (Preference pref : existingPrefs.values()) {
@@ -173,6 +196,32 @@ public class SubscriptionsPreferenceController extends AbstractPreferenceControl
mUpdateListener.onChildrenUpdated();
}
@VisibleForTesting
boolean shouldInflateSignalStrength(int subId) {
return SignalStrengthUtil.shouldInflateSignalStrength(mContext, subId);
}
@VisibleForTesting
void setIcon(Preference pref, int subId, boolean isDefaultForData) {
final TelephonyManager mgr = mContext.getSystemService(
TelephonyManager.class).createForSubscriptionId(subId);
final SignalStrength strength = mgr.getSignalStrength();
int level = (strength == null) ? 0 : strength.getLevel();
int numLevels = SignalStrength.NUM_SIGNAL_STRENGTH_BINS;
if (shouldInflateSignalStrength(subId)) {
level += 1;
numLevels += 1;
}
final boolean showCutOut = !isDefaultForData || !mgr.isDataEnabled();
pref.setIcon(getIcon(level, numLevels, showCutOut));
}
@VisibleForTesting
Drawable getIcon(int level, int numLevels, boolean cutOut) {
return MobileNetworkUtils.getSignalStrengthIcon(mContext, level, numLevels,
NO_CELL_DATA_TYPE_ICON, cutOut);
}
private boolean activeNetworkIsCellular() {
final Network activeNetwork = mConnectivityManager.getActiveNetwork();
if (activeNetwork == null) {
@@ -197,10 +246,9 @@ public class SubscriptionsPreferenceController extends AbstractPreferenceControl
*
* If a subscription isn't the default for anything, we just say it is available.
*/
protected String getSummary(int subId) {
protected String getSummary(int subId, boolean isDefaultForData) {
final int callsDefaultSubId = SubscriptionManager.getDefaultVoiceSubscriptionId();
final int smsDefaultSubId = SubscriptionManager.getDefaultSmsSubscriptionId();
final int dataDefaultSubId = SubscriptionManager.getDefaultDataSubscriptionId();
String line1 = null;
if (subId == callsDefaultSubId && subId == smsDefaultSubId) {
@@ -212,10 +260,10 @@ public class SubscriptionsPreferenceController extends AbstractPreferenceControl
}
String line2 = null;
if (subId == dataDefaultSubId) {
if (isDefaultForData) {
final TelephonyManager telMgrForSub = mContext.getSystemService(
TelephonyManager.class).createForSubscriptionId(subId);
boolean dataEnabled = telMgrForSub.isDataEnabled();
final boolean dataEnabled = telMgrForSub.isDataEnabled();
if (dataEnabled && activeNetworkIsCellular()) {
line2 = mContext.getString(R.string.mobile_data_active);
} else if (!dataEnabled) {
@@ -277,4 +325,9 @@ public class SubscriptionsPreferenceController extends AbstractPreferenceControl
public void onDataConnectivityChange() {
update();
}
@Override
public void onSignalStrengthChanged() {
update();
}
}