Merge "Support dynamically update IMSI" am: 05e92bb5ef am: 3fd314dbca am: bb9de525b2

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1418348

Change-Id: Id948a9453139cbf4e9afc75647152bdbd24cfe0a
This commit is contained in:
Junyu Lai
2020-10-13 04:19:38 +00:00
committed by Automerger Merge Worker

View File

@@ -30,6 +30,7 @@ import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
import android.util.Pair;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.CollectionUtils;
@@ -94,41 +95,43 @@ public class NetworkStatsSubscriptionsMonitor extends
// also needed to track CBRS.
final List<Integer> newSubs = getActiveSubIdList(mSubscriptionManager);
for (final int subId : newSubs) {
final RatTypeListener match = CollectionUtils.find(mRatListeners,
it -> it.mSubId == subId);
if (match != null) continue;
// Create listener for every newly added sub. Also store subscriberId into it to
// prevent binder call to telephony when querying RAT. If the subscriberId is empty
// for any reason, such as SIM PIN locked, skip registration.
// SubscriberId will be unavailable again if 1. modem crashed 2. reboot
// 3. re-insert SIM. If that happens, the listeners will be eventually synchronized
// with active sub list once all subscriberIds are ready.
// IMSI is needed for every newly added sub. Listener stores subscriberId into it to
// prevent binder call to telephony when querying RAT. Keep listener registration with empty
// IMSI is meaningless since the RAT type changed is ambiguous for multi-SIM if reported
// with empty IMSI. So filter the subs w/o a valid IMSI to prevent such registration.
final List<Pair<Integer, String>> filteredNewSubs =
CollectionUtils.mapNotNull(newSubs, subId -> {
final String subscriberId = mTeleManager.getSubscriberId(subId);
if (TextUtils.isEmpty(subscriberId)) {
Log.d(NetworkStatsService.TAG, "Empty subscriberId for newly added sub "
+ subId + ", skip listener registration");
return TextUtils.isEmpty(subscriberId) ? null : new Pair(subId, subscriberId);
});
for (final Pair<Integer, String> sub : filteredNewSubs) {
// Fully match listener with subId and IMSI, since in some rare cases, IMSI might be
// suddenly change regardless of subId, such as switch IMSI feature in modem side.
// If that happens, register new listener with new IMSI and remove old one later.
if (CollectionUtils.find(mRatListeners,
it -> it.equalsKey(sub.first, sub.second)) != null) {
continue;
}
final RatTypeListener listener =
new RatTypeListener(mExecutor, this, subId, subscriberId);
new RatTypeListener(mExecutor, this, sub.first, sub.second);
mRatListeners.add(listener);
// Register listener to the telephony manager that associated with specific sub.
mTeleManager.createForSubscriptionId(subId)
mTeleManager.createForSubscriptionId(sub.first)
.listen(listener, PhoneStateListener.LISTEN_SERVICE_STATE);
Log.d(NetworkStatsService.TAG, "RAT type listener registered for sub " + subId);
Log.d(NetworkStatsService.TAG, "RAT type listener registered for sub " + sub.first);
}
for (final RatTypeListener listener : new ArrayList<>(mRatListeners)) {
// If the new list contains the subId of the listener, keeps it.
final Integer match = CollectionUtils.find(newSubs, it -> it == listener.mSubId);
if (match != null) continue;
// If there is no subId and IMSI matched the listener, removes it.
if (CollectionUtils.find(filteredNewSubs,
it -> listener.equalsKey(it.first, it.second)) == null) {
handleRemoveRatTypeListener(listener);
}
}
}
@NonNull
private List<Integer> getActiveSubIdList(@NonNull SubscriptionManager subscriptionManager) {
@@ -232,5 +235,9 @@ public class NetworkStatsSubscriptionsMonitor extends
public int getSubId() {
return mSubId;
}
boolean equalsKey(int subId, @NonNull String subscriberId) {
return mSubId == subId && TextUtils.equals(mSubscriberId, subscriberId);
}
}
}