qualifiedNetworkTypes) {
int[] qualifiedNetworkTypesArray =
qualifiedNetworkTypes.stream().mapToInt(i->i).toArray();
mHandler.obtainMessage(QNS_UPDATE_QUALIFIED_NETWORKS, mSlotIndex, apnTypes,
qualifiedNetworkTypesArray).sendToTarget();
}
/**
* Request to make a clean initial connection instead of handover to a transport type mapped
* to the {@code qualifiedNetworkType} for the {@code apnTypes}. This will update the
* preferred network type like {@link #updateQualifiedNetworkTypes(int, List)}, however if
* the data network for the {@code apnTypes} is not in the state {@link TelephonyManager
* #DATA_CONNECTED} or it's already connected on the transport type mapped to the
* qualified network type, forced reconnection will be ignored.
*
* This will tear down current data network even though target transport type mapped to
* the {@code qualifiedNetworkType} is not available, and the data network will be connected
* to the transport type when it becomes available.
*
*
This is one shot request and does not mean further handover is not allowed to the
* qualified network type for this APN type.
*
* @param apnTypes APN type(s) of the qualified networks. This must be a bitmask combination
* of {@link ApnType}. The same qualified networks will be applicable to all APN types
* specified here.
* @param qualifiedNetworkType Access network types which are qualified for data connection
* setup for {@link ApnType}. Empty list means QNS has no suggestion to the frameworks, and
* for that APN type frameworks will route the corresponding network requests to
* {@link AccessNetworkConstants#TRANSPORT_TYPE_WWAN}.
*
*
If one of the element is invalid, for example, {@link AccessNetworkType#UNKNOWN},
* then this operation becomes a no-op.
*
* @hide
*/
public final void reconnectQualifiedNetworkType(@ApnType int apnTypes,
@AccessNetworkConstants.RadioAccessNetworkType int qualifiedNetworkType) {
mHandler.obtainMessage(QNS_RECONNECT_QUALIFIED_NETWORK, mSlotIndex, apnTypes,
new Integer(qualifiedNetworkType)).sendToTarget();
}
private void onUpdateQualifiedNetworkTypes(
@ApnType int apnTypes, int[] qualifiedNetworkTypes) {
mQualifiedNetworkTypesList.put(apnTypes, qualifiedNetworkTypes);
if (mCallback != null) {
try {
mCallback.onQualifiedNetworkTypesChanged(apnTypes, qualifiedNetworkTypes);
} catch (RemoteException e) {
loge("Failed to call onQualifiedNetworksChanged. " + e);
}
}
}
private void onReconnectQualifiedNetworkType(@ApnType int apnTypes,
@AccessNetworkConstants.RadioAccessNetworkType int qualifiedNetworkType) {
if (mCallback != null) {
try {
mCallback.onReconnectQualifiedNetworkType(apnTypes, qualifiedNetworkType);
} catch (RemoteException e) {
loge("Failed to call onReconnectQualifiedNetworkType. " + e);
}
}
}
/**
* The framework calls this method when the throttle status of an APN changes.
*
* This method is meant to be overridden.
*
* @param statuses the statuses that have changed
*/
public void reportThrottleStatusChanged(@NonNull List statuses) {
Log.d(TAG, "reportThrottleStatusChanged: statuses size=" + statuses.size());
}
/**
* The framework calls this method when the preferred transport type used to set up
* emergency data network is changed.
*
* This method is meant to be overridden.
*
* @param transportType transport type changed to be preferred
*/
public void reportEmergencyDataNetworkPreferredTransportChanged(
@AccessNetworkConstants.TransportType int transportType) {
Log.d(TAG, "reportEmergencyDataNetworkPreferredTransportChanged: "
+ AccessNetworkConstants.transportTypeToString(transportType));
}
/**
* Request network validation to the connected data network for given a network capability.
*
* This network validation can only be performed when a data network is in connected
* state, and will not be triggered if the data network does not support network validation
* feature or network validation is not in connected state.
*
*
See {@link DataServiceCallback.ResultCode} for the type of response that indicates
* whether the request was successfully submitted or had an error.
*
*
If network validation is requested, monitor network validation status in {@link
* PreciseDataConnectionState#getNetworkValidationStatus()}.
*
* @param networkCapability A network capability. (Note that only APN-type capabilities are
* supported.
* @param executor executor The callback executor that responds whether the request has been
* successfully submitted or not.
* @param resultCodeCallback A callback to determine whether the request was successfully
* submitted or not.
*/
@FlaggedApi(Flags.FLAG_NETWORK_VALIDATION)
public void requestNetworkValidation(
@NetCapability int networkCapability,
@NonNull @CallbackExecutor Executor executor,
@NonNull @DataServiceCallback.ResultCode Consumer resultCodeCallback) {
Objects.requireNonNull(executor, "executor cannot be null");
Objects.requireNonNull(resultCodeCallback, "resultCodeCallback cannot be null");
if (!sFeatureFlag.networkValidation()) {
loge("networkValidation feature is disabled");
executor.execute(
() ->
resultCodeCallback.accept(
DataServiceCallback.RESULT_ERROR_UNSUPPORTED));
return;
}
IIntegerConsumer callback = new IIntegerConsumer.Stub() {
@Override
public void accept(int result) {
executor.execute(() -> resultCodeCallback.accept(result));
}
};
// Move to the internal handler and process it.
mHandler.obtainMessage(
QNS_REQUEST_NETWORK_VALIDATION,
mSlotIndex,
0,
new NetworkValidationRequestData(networkCapability, callback))
.sendToTarget();
}
/** Process a network validation request on the internal handler. */
private void onRequestNetworkValidation(NetworkValidationRequestData data) {
try {
log("onRequestNetworkValidation");
// Callback to request a network validation.
mCallback.onNetworkValidationRequested(data.mNetworkCapability, data.mCallback);
} catch (RemoteException | NullPointerException e) {
loge("Failed to call onRequestNetworkValidation. " + e);
FunctionalUtils.ignoreRemoteException(data.mCallback::accept)
.accept(DataServiceCallback.RESULT_ERROR_UNSUPPORTED);
}
}
/**
* Called when the qualified networks provider is removed. The extended class should
* implement this method to perform cleanup works.
*/
@Override
public abstract void close();
}
private class QualifiedNetworksServiceHandler extends Handler {
QualifiedNetworksServiceHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message message) {
IQualifiedNetworksServiceCallback callback;
final int slotIndex = message.arg1;
NetworkAvailabilityProvider provider = mProviders.get(slotIndex);
switch (message.what) {
case QNS_CREATE_NETWORK_AVAILABILITY_PROVIDER:
if (mProviders.get(slotIndex) != null) {
loge("Network availability provider for slot " + slotIndex
+ " already existed.");
return;
}
provider = onCreateNetworkAvailabilityProvider(slotIndex);
if (provider != null) {
mProviders.put(slotIndex, provider);
callback = (IQualifiedNetworksServiceCallback) message.obj;
provider.registerForQualifiedNetworkTypesChanged(callback);
} else {
loge("Failed to create network availability provider. slot index = "
+ slotIndex);
}
break;
case QNS_APN_THROTTLE_STATUS_CHANGED:
if (provider != null) {
List statuses = (List) message.obj;
provider.reportThrottleStatusChanged(statuses);
}
break;
case QNS_EMERGENCY_DATA_NETWORK_PREFERRED_TRANSPORT_CHANGED:
if (provider != null) {
int transportType = (int) message.arg2;
provider.reportEmergencyDataNetworkPreferredTransportChanged(transportType);
}
break;
case QNS_REMOVE_NETWORK_AVAILABILITY_PROVIDER:
if (provider != null) {
provider.close();
mProviders.remove(slotIndex);
}
break;
case QNS_REMOVE_ALL_NETWORK_AVAILABILITY_PROVIDERS:
for (int i = 0; i < mProviders.size(); i++) {
provider = mProviders.get(i);
if (provider != null) {
provider.close();
}
}
mProviders.clear();
break;
case QNS_UPDATE_QUALIFIED_NETWORKS:
if (provider == null) break;
provider.onUpdateQualifiedNetworkTypes(message.arg2, (int[]) message.obj);
break;
case QNS_REQUEST_NETWORK_VALIDATION:
if (provider == null) break;
provider.onRequestNetworkValidation((NetworkValidationRequestData) message.obj);
break;
case QNS_RECONNECT_QUALIFIED_NETWORK:
if (provider == null) break;
provider.onReconnectQualifiedNetworkType(message.arg2, (Integer) message.obj);
break;
}
}
}
/**
* Default constructor.
*/
public QualifiedNetworksService() {
mHandlerThread = new HandlerThread(TAG);
mHandlerThread.start();
mHandler = new QualifiedNetworksServiceHandler(mHandlerThread.getLooper());
log("Qualified networks service created");
}
/**
* Create the instance of {@link NetworkAvailabilityProvider}. Vendor qualified network service
* must override this method to facilitate the creation of {@link NetworkAvailabilityProvider}
* instances. The system will call this method after binding the qualified networks service for
* each active SIM slot index.
*
* @param slotIndex SIM slot index the qualified networks service associated with.
* @return Qualified networks service instance
*/
@NonNull
public abstract NetworkAvailabilityProvider onCreateNetworkAvailabilityProvider(int slotIndex);
/** @hide */
@Override
public IBinder onBind(Intent intent) {
if (intent == null || !QUALIFIED_NETWORKS_SERVICE_INTERFACE.equals(intent.getAction())) {
loge("Unexpected intent " + intent);
return null;
}
return mBinder;
}
/** @hide */
@Override
public boolean onUnbind(Intent intent) {
mHandler.obtainMessage(QNS_REMOVE_ALL_NETWORK_AVAILABILITY_PROVIDERS).sendToTarget();
return false;
}
/** @hide */
@Override
public void onDestroy() {
mHandlerThread.quit();
}
/**
* A wrapper around IQualifiedNetworksService that forwards calls to implementations of
* {@link QualifiedNetworksService}.
*/
private class IQualifiedNetworksServiceWrapper extends IQualifiedNetworksService.Stub {
@Override
public void createNetworkAvailabilityProvider(int slotIndex,
IQualifiedNetworksServiceCallback callback) {
mHandler.obtainMessage(QNS_CREATE_NETWORK_AVAILABILITY_PROVIDER, slotIndex, 0,
callback).sendToTarget();
}
@Override
public void removeNetworkAvailabilityProvider(int slotIndex) {
mHandler.obtainMessage(QNS_REMOVE_NETWORK_AVAILABILITY_PROVIDER, slotIndex, 0)
.sendToTarget();
}
@Override
public void reportThrottleStatusChanged(int slotIndex,
List statuses) {
mHandler.obtainMessage(QNS_APN_THROTTLE_STATUS_CHANGED, slotIndex, 0, statuses)
.sendToTarget();
}
@Override
public void reportEmergencyDataNetworkPreferredTransportChanged(int slotIndex,
@AccessNetworkConstants.TransportType int transportType) {
mHandler.obtainMessage(
QNS_EMERGENCY_DATA_NETWORK_PREFERRED_TRANSPORT_CHANGED,
slotIndex, transportType).sendToTarget();
}
}
private static final class NetworkValidationRequestData {
final @NetCapability int mNetworkCapability;
final IIntegerConsumer mCallback;
private NetworkValidationRequestData(@NetCapability int networkCapability,
@NonNull IIntegerConsumer callback) {
mNetworkCapability = networkCapability;
mCallback = callback;
}
}
private void log(String s) {
Rlog.d(TAG, s);
}
private void loge(String s) {
Rlog.e(TAG, s);
}
}