[Settings] Change the way in NetworkProviderCallsSmsController for getting the subscription info from room db part2
Bug: 236919685 Test: atest NetworkProviderCallsSmsControllerTest Change-Id: Ibbfa1fed15ea4fa38cff38d78855326e3d74fe0e
This commit is contained in:
@@ -21,13 +21,13 @@ import static androidx.lifecycle.Lifecycle.Event;
|
||||
import android.content.Context;
|
||||
import android.os.UserManager;
|
||||
import android.telephony.ServiceState;
|
||||
import android.telephony.SubscriptionInfo;
|
||||
import android.telephony.SubscriptionManager;
|
||||
import android.telephony.TelephonyManager;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.lifecycle.LifecycleObserver;
|
||||
import androidx.lifecycle.LifecycleOwner;
|
||||
import androidx.lifecycle.OnLifecycleEvent;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
@@ -37,50 +37,55 @@ import com.android.settingslib.RestrictedPreference;
|
||||
import com.android.settingslib.Utils;
|
||||
import com.android.settingslib.core.AbstractPreferenceController;
|
||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||
import com.android.settingslib.mobile.dataservice.MobileNetworkInfoEntity;
|
||||
import com.android.settingslib.mobile.dataservice.SubscriptionInfoEntity;
|
||||
import com.android.settingslib.mobile.dataservice.UiccInfoEntity;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class NetworkProviderCallsSmsController extends AbstractPreferenceController implements
|
||||
SubscriptionsChangeListener.SubscriptionsChangeListenerClient, LifecycleObserver {
|
||||
LifecycleObserver, MobileNetworkRepository.MobileNetworkCallback {
|
||||
|
||||
private static final String TAG = "NetworkProviderCallsSmsController";
|
||||
private static final String KEY = "calls_and_sms";
|
||||
private static final String RTL_MARK = "\u200F";
|
||||
|
||||
private UserManager mUserManager;
|
||||
private SubscriptionManager mSubscriptionManager;
|
||||
private SubscriptionsChangeListener mSubscriptionsChangeListener;
|
||||
private TelephonyManager mTelephonyManager;
|
||||
private RestrictedPreference mPreference;
|
||||
private boolean mIsRtlMode;
|
||||
private LifecycleOwner mLifecycleOwner;
|
||||
private MobileNetworkRepository mMobileNetworkRepository;
|
||||
private List<SubscriptionInfoEntity> mSubInfoEntityList;
|
||||
|
||||
/**
|
||||
* The summary text and click behavior of the "Calls & SMS" item on the
|
||||
* Network & internet page.
|
||||
*/
|
||||
public NetworkProviderCallsSmsController(Context context, Lifecycle lifecycle) {
|
||||
public NetworkProviderCallsSmsController(Context context, Lifecycle lifecycle,
|
||||
LifecycleOwner lifecycleOwner) {
|
||||
super(context);
|
||||
|
||||
mUserManager = context.getSystemService(UserManager.class);
|
||||
mSubscriptionManager = context.getSystemService(SubscriptionManager.class);
|
||||
mTelephonyManager = mContext.getSystemService(TelephonyManager.class);
|
||||
mIsRtlMode = context.getResources().getConfiguration().getLayoutDirection()
|
||||
== View.LAYOUT_DIRECTION_RTL;
|
||||
mLifecycleOwner = lifecycleOwner;
|
||||
mMobileNetworkRepository = new MobileNetworkRepository(context, this);
|
||||
if (lifecycle != null) {
|
||||
mSubscriptionsChangeListener = new SubscriptionsChangeListener(context, this);
|
||||
lifecycle.addObserver(this);
|
||||
}
|
||||
}
|
||||
|
||||
@OnLifecycleEvent(Event.ON_RESUME)
|
||||
public void onResume() {
|
||||
mSubscriptionsChangeListener.start();
|
||||
mMobileNetworkRepository.addRegister(mLifecycleOwner);
|
||||
update();
|
||||
}
|
||||
|
||||
@OnLifecycleEvent(Event.ON_PAUSE)
|
||||
public void onPause() {
|
||||
mSubscriptionsChangeListener.stop();
|
||||
mMobileNetworkRepository.removeRegister();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -91,27 +96,24 @@ public class NetworkProviderCallsSmsController extends AbstractPreferenceControl
|
||||
|
||||
@Override
|
||||
public CharSequence getSummary() {
|
||||
final List<SubscriptionInfo> subs = SubscriptionUtil.getActiveSubscriptions(
|
||||
mSubscriptionManager);
|
||||
|
||||
if (subs.isEmpty()) {
|
||||
List<SubscriptionInfoEntity> list = getSubscriptionInfoList();
|
||||
if (list == null || list .isEmpty()) {
|
||||
return setSummaryResId(R.string.calls_sms_no_sim);
|
||||
} else {
|
||||
final StringBuilder summary = new StringBuilder();
|
||||
for (SubscriptionInfo subInfo : subs) {
|
||||
int subsSize = subs.size();
|
||||
int subId = subInfo.getSubscriptionId();
|
||||
final CharSequence displayName = SubscriptionUtil.getUniqueSubscriptionDisplayName(
|
||||
subInfo, mContext);
|
||||
for (SubscriptionInfoEntity subInfo : list) {
|
||||
int subsSize = list.size();
|
||||
int subId = Integer.parseInt(subInfo.subId);
|
||||
final CharSequence displayName = subInfo.uniqueName;
|
||||
|
||||
// Set displayName as summary if there is only one valid SIM.
|
||||
if (subsSize == 1
|
||||
&& SubscriptionManager.isValidSubscriptionId(subId)
|
||||
&& list.get(0).isValidSubscription
|
||||
&& isInService(subId)) {
|
||||
return displayName;
|
||||
}
|
||||
|
||||
CharSequence status = getPreferredStatus(subsSize, subId);
|
||||
CharSequence status = getPreferredStatus(subInfo, subsSize, subId);
|
||||
if (status.toString().isEmpty()) {
|
||||
// If there are 2 or more SIMs and one of these has no preferred status,
|
||||
// set only its displayName as summary.
|
||||
@@ -123,7 +125,7 @@ public class NetworkProviderCallsSmsController extends AbstractPreferenceControl
|
||||
.append(")");
|
||||
}
|
||||
// Do not add ", " for the last subscription.
|
||||
if (subInfo != subs.get(subs.size() - 1)) {
|
||||
if (!subInfo.equals(list.get(list.size() - 1))) {
|
||||
summary.append(", ");
|
||||
}
|
||||
|
||||
@@ -136,12 +138,13 @@ public class NetworkProviderCallsSmsController extends AbstractPreferenceControl
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
protected CharSequence getPreferredStatus(int subsSize, int subId) {
|
||||
protected CharSequence getPreferredStatus(SubscriptionInfoEntity subInfo, int subsSize,
|
||||
int subId) {
|
||||
String status = "";
|
||||
boolean isDataPreferred = subId == getDefaultVoiceSubscriptionId();
|
||||
boolean isSmsPreferred = subId == getDefaultSmsSubscriptionId();
|
||||
boolean isDataPreferred = subInfo.isDefaultVoiceSubscription;
|
||||
boolean isSmsPreferred = subInfo.isDefaultSmsSubscription;
|
||||
|
||||
if (!SubscriptionManager.isValidSubscriptionId(subId) || !isInService(subId)) {
|
||||
if (!subInfo.isValidSubscription || !isInService(subId)) {
|
||||
status = setSummaryResId(subsSize > 1 ? R.string.calls_sms_unavailable :
|
||||
R.string.calls_sms_temp_unavailable);
|
||||
} else {
|
||||
@@ -161,13 +164,8 @@ public class NetworkProviderCallsSmsController extends AbstractPreferenceControl
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
protected int getDefaultVoiceSubscriptionId() {
|
||||
return SubscriptionManager.getDefaultVoiceSubscriptionId();
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
protected int getDefaultSmsSubscriptionId() {
|
||||
return SubscriptionManager.getDefaultSmsSubscriptionId();
|
||||
protected List<SubscriptionInfoEntity> getSubscriptionInfoList() {
|
||||
return mSubInfoEntityList;
|
||||
}
|
||||
|
||||
private void update() {
|
||||
@@ -178,9 +176,7 @@ public class NetworkProviderCallsSmsController extends AbstractPreferenceControl
|
||||
mPreference.setOnPreferenceClickListener(null);
|
||||
mPreference.setFragment(null);
|
||||
|
||||
final List<SubscriptionInfo> subs = SubscriptionUtil.getActiveSubscriptions(
|
||||
mSubscriptionManager);
|
||||
if (subs.isEmpty()) {
|
||||
if (mSubInfoEntityList == null || mSubInfoEntityList.isEmpty()) {
|
||||
mPreference.setEnabled(false);
|
||||
} else {
|
||||
mPreference.setEnabled(true);
|
||||
@@ -213,16 +209,34 @@ public class NetworkProviderCallsSmsController extends AbstractPreferenceControl
|
||||
update();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSubscriptionsChanged() {
|
||||
refreshSummary(mPreference);
|
||||
update();
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
protected boolean isInService(int subId) {
|
||||
ServiceState serviceState =
|
||||
mTelephonyManager.createForSubscriptionId(subId).getServiceState();
|
||||
return Utils.isInService(serviceState);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAvailableSubInfoChanged(List<SubscriptionInfoEntity> subInfoEntityList) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActiveSubInfoChanged(List<SubscriptionInfoEntity> activeSubInfoList) {
|
||||
if ((mSubInfoEntityList != null &&
|
||||
(activeSubInfoList.isEmpty() || !activeSubInfoList.equals(mSubInfoEntityList)))
|
||||
|| (!activeSubInfoList.isEmpty() && mSubInfoEntityList == null)) {
|
||||
Log.d(TAG, "subInfo list from framework is changed, update the subInfo entity list.");
|
||||
mSubInfoEntityList = activeSubInfoList;
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAllUiccInfoChanged(List<UiccInfoEntity> uiccInfoEntityList) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAllMobileNetworkInfoChanged(
|
||||
List<MobileNetworkInfoEntity> mobileNetworkInfoEntityList) {
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user