[Mainline] Copy the method getSelectableSubscriptionInfoList from SubscriptionManager to Settings
Bug: 147206736 Test: make RunSettingsRoboTests -j ROBOTEST_FILTER=SubscriptionUtilTest make RunSettingsRoboTests -j ROBOTEST_FILTER=MobileDataSliceTest Change-Id: I6d7e72a656723c0620d579e1e223945e2a3588af
This commit is contained in:
@@ -32,7 +32,9 @@ import android.text.TextUtils;
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class SubscriptionUtil {
|
||||
private static final String TAG = "SubscriptionUtil";
|
||||
@@ -79,12 +81,7 @@ public class SubscriptionUtil {
|
||||
if (sAvailableResultsForTesting != null) {
|
||||
return sAvailableResultsForTesting;
|
||||
}
|
||||
final SubscriptionManager subMgr = context.getSystemService(SubscriptionManager.class);
|
||||
|
||||
final List<SubscriptionInfo> subscriptions =
|
||||
new ArrayList<>(emptyIfNull(subMgr.getSelectableSubscriptionInfoList()));
|
||||
|
||||
return subscriptions;
|
||||
return new ArrayList<>(emptyIfNull(getSelectableSubscriptionInfoList(context)));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -242,4 +239,70 @@ public class SubscriptionUtil {
|
||||
}
|
||||
return info.getSimSlotIndex();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of subscriptions that are available and visible to the user.
|
||||
*
|
||||
* @return list of user selectable subscriptions.
|
||||
*/
|
||||
public static List<SubscriptionInfo> getSelectableSubscriptionInfoList(Context context) {
|
||||
SubscriptionManager subManager = context.getSystemService(SubscriptionManager.class);
|
||||
List<SubscriptionInfo> availableList = subManager.getAvailableSubscriptionInfoList();
|
||||
if (availableList == null) {
|
||||
return null;
|
||||
} else {
|
||||
// Multiple subscriptions in a group should only have one representative.
|
||||
// It should be the current active primary subscription if any, or any
|
||||
// primary subscription.
|
||||
List<SubscriptionInfo> selectableList = new ArrayList<>();
|
||||
Map<ParcelUuid, SubscriptionInfo> groupMap = new HashMap<>();
|
||||
|
||||
for (SubscriptionInfo info : availableList) {
|
||||
// Opportunistic subscriptions are considered invisible
|
||||
// to users so they should never be returned.
|
||||
if (!isSubscriptionVisible(subManager, context, info)) continue;
|
||||
|
||||
ParcelUuid groupUuid = info.getGroupUuid();
|
||||
if (groupUuid == null) {
|
||||
// Doesn't belong to any group. Add in the list.
|
||||
selectableList.add(info);
|
||||
} else if (!groupMap.containsKey(groupUuid)
|
||||
|| (groupMap.get(groupUuid).getSimSlotIndex() == INVALID_SIM_SLOT_INDEX
|
||||
&& info.getSimSlotIndex() != INVALID_SIM_SLOT_INDEX)) {
|
||||
// If it belongs to a group that has never been recorded or it's the current
|
||||
// active subscription, add it in the list.
|
||||
selectableList.remove(groupMap.get(groupUuid));
|
||||
selectableList.add(info);
|
||||
groupMap.put(groupUuid, info);
|
||||
}
|
||||
|
||||
}
|
||||
return selectableList;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Whether a subscription is visible to API caller. If it's a bundled opportunistic
|
||||
* subscription, it should be hidden anywhere in Settings, dialer, status bar etc.
|
||||
* Exception is if caller owns carrier privilege, in which case they will
|
||||
* want to see their own hidden subscriptions.
|
||||
*
|
||||
* @param info the subscriptionInfo to check against.
|
||||
* @return true if this subscription should be visible to the API caller.
|
||||
*/
|
||||
private static boolean isSubscriptionVisible(
|
||||
SubscriptionManager subscriptionManager, Context context, SubscriptionInfo info) {
|
||||
if (info == null) return false;
|
||||
// If subscription is NOT grouped opportunistic subscription, it's visible.
|
||||
if (info.getGroupUuid() == null || !info.isOpportunistic()) return true;
|
||||
|
||||
// If the caller is the carrier app and owns the subscription, it should be visible
|
||||
// to the caller.
|
||||
TelephonyManager telephonyManager = context.getSystemService(TelephonyManager.class)
|
||||
.createForSubscriptionId(info.getSubscriptionId());
|
||||
boolean hasCarrierPrivilegePermission = telephonyManager.hasCarrierPrivileges()
|
||||
|| subscriptionManager.canManageSubscription(info);
|
||||
return hasCarrierPrivilegePermission;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user