Fix creating ConnectivityUtil to work on any platform
After this CL, ConnectivityUtil for default subId is used again on pre
N because TelephonyManager has createForSubscriptionId() for a specific
subId from N.
Nevertheless, listen() uses the default subId on PhoneStateListener
instead of the subId on the manager still even N. On O and beyond,
ConnectivityUtil works correctly on multi-sim devices.
Revert "Fix missing computeIfAbsent() method in L_MR1 and M"
This reverts commit f0ccb76d8d.
Reason for revert: Manage ConnectivityUtil instances for subIds at least
on N instead.
Test: Manual
Signed-off-by: Taesu Lee <taesu82.lee@samsung.com>
Change-Id: I2f08928a6798a2ce275c5c75569ad379999d274c
This commit is contained in:
@@ -66,11 +66,11 @@ public class DataModelImpl extends DataModel {
|
||||
private final DatabaseHelper mDatabaseHelper;
|
||||
private final SyncManager mSyncManager;
|
||||
|
||||
// Cached ConnectivityUtil instance for Pre-L_MR1
|
||||
private static ConnectivityUtil sConnectivityUtilInstanceCachePreLMR1 = null;
|
||||
// Cached ConnectivityUtil subId->instance for L_MR1 and beyond
|
||||
// Cached ConnectivityUtil instance for Pre-N.
|
||||
private static ConnectivityUtil sConnectivityUtilInstanceCachePreN = null;
|
||||
// Cached ConnectivityUtil subId->instance for N and beyond
|
||||
private static final ConcurrentHashMap<Integer, ConnectivityUtil>
|
||||
sConnectivityUtilInstanceCacheLMR1 = new ConcurrentHashMap<>();
|
||||
sConnectivityUtilInstanceCacheN = new ConcurrentHashMap<>();
|
||||
|
||||
public DataModelImpl(final Context context) {
|
||||
super();
|
||||
@@ -79,10 +79,10 @@ public class DataModelImpl extends DataModel {
|
||||
mDataModelWorker = new BackgroundWorker();
|
||||
mDatabaseHelper = DatabaseHelper.getInstance(context);
|
||||
mSyncManager = new SyncManager();
|
||||
if (OsUtil.isAtLeastL_MR1()) {
|
||||
createConnectivityUtilForLMR1();
|
||||
if (OsUtil.isAtLeastN()) {
|
||||
createConnectivityUtilForEachActiveSubscription();
|
||||
} else {
|
||||
sConnectivityUtilInstanceCachePreLMR1 = new ConnectivityUtil(context);
|
||||
sConnectivityUtilInstanceCachePreN = new ConnectivityUtil(context);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -236,13 +236,15 @@ public class DataModelImpl extends DataModel {
|
||||
// gracefully
|
||||
MmsConfig.loadAsync();
|
||||
ParticipantRefresh.refreshSelfParticipants();
|
||||
createConnectivityUtilForLMR1();
|
||||
if (OsUtil.isAtLeastN()) {
|
||||
createConnectivityUtilForEachActiveSubscription();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void createConnectivityUtilForLMR1() {
|
||||
private void createConnectivityUtilForEachActiveSubscription() {
|
||||
PhoneUtils.forEachActiveSubscription(new PhoneUtils.SubscriptionRunnable() {
|
||||
@Override
|
||||
public void runForSubscription(int subId) {
|
||||
@@ -250,20 +252,17 @@ public class DataModelImpl extends DataModel {
|
||||
if (subId <= ParticipantData.DEFAULT_SELF_SUB_ID) {
|
||||
subId = PhoneUtils.getDefault().getDefaultSmsSubscriptionId();
|
||||
}
|
||||
|
||||
if (!sConnectivityUtilInstanceCacheLMR1.containsKey(subId)) {
|
||||
sConnectivityUtilInstanceCacheLMR1.put(
|
||||
subId, new ConnectivityUtil(mContext, subId));
|
||||
}
|
||||
sConnectivityUtilInstanceCacheN.computeIfAbsent(
|
||||
subId, key -> new ConnectivityUtil(mContext, key));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static ConnectivityUtil getConnectivityUtil(final int subId) {
|
||||
if (OsUtil.isAtLeastL_MR1()) {
|
||||
return sConnectivityUtilInstanceCacheLMR1.get(subId);
|
||||
if (OsUtil.isAtLeastN()) {
|
||||
return sConnectivityUtilInstanceCacheN.get(subId);
|
||||
} else {
|
||||
return sConnectivityUtilInstanceCachePreLMR1;
|
||||
return sConnectivityUtilInstanceCachePreN;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,16 @@ import android.telephony.TelephonyManager;
|
||||
|
||||
import com.android.messaging.datamodel.data.ParticipantData;
|
||||
|
||||
/**
|
||||
* ConnectivityUtil listens to the network service state changes.
|
||||
*
|
||||
* On N and beyond, This class instance can be created via ConnectivityUtil(context, subId), use
|
||||
* ConnectivityUtil(context) for others.
|
||||
*
|
||||
* Note that TelephonyManager has createForSubscriptionId() for a specific subId from N but listen()
|
||||
* does not use the subId on the manager, and uses the default subId on PhoneStateListener. From O,
|
||||
* the manager uses its' own subId in listen().
|
||||
*/
|
||||
public class ConnectivityUtil {
|
||||
// Assume not connected until informed differently
|
||||
private volatile int mCurrentServiceState = ServiceState.STATE_POWER_OFF;
|
||||
@@ -40,6 +50,7 @@ public class ConnectivityUtil {
|
||||
}
|
||||
|
||||
public ConnectivityUtil(final Context context, final int subId) {
|
||||
Assert.isTrue(OsUtil.isAtLeastN());
|
||||
mTelephonyManager =
|
||||
((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE))
|
||||
.createForSubscriptionId(subId);
|
||||
|
||||
Reference in New Issue
Block a user