use of android.net.NetworkCapabilities in project android_frameworks_base by ResurrectionRemix.
the class ConnectivityServiceTest method testMobileDataAlwaysOn.
@SmallTest
public void testMobileDataAlwaysOn() throws Exception {
final TestNetworkCallback cellNetworkCallback = new TestNetworkCallback();
final NetworkRequest cellRequest = new NetworkRequest.Builder().addTransportType(TRANSPORT_CELLULAR).build();
mCm.registerNetworkCallback(cellRequest, cellNetworkCallback);
final HandlerThread handlerThread = new HandlerThread("MobileDataAlwaysOnFactory");
handlerThread.start();
NetworkCapabilities filter = new NetworkCapabilities().addTransportType(TRANSPORT_CELLULAR).addCapability(NET_CAPABILITY_INTERNET);
final MockNetworkFactory testFactory = new MockNetworkFactory(handlerThread.getLooper(), mServiceContext, "testFactory", filter);
testFactory.setScoreFilter(40);
// Register the factory and expect it to start looking for a network.
testFactory.expectAddRequests(1);
testFactory.register();
testFactory.waitForNetworkRequests(1);
assertTrue(testFactory.getMyStartRequested());
// Bring up wifi. The factory stops looking for a network.
mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
// Because the default request changes score twice.
testFactory.expectAddRequests(2);
mWiFiNetworkAgent.connect(true);
testFactory.waitForNetworkRequests(1);
assertFalse(testFactory.getMyStartRequested());
ContentResolver cr = mServiceContext.getContentResolver();
// Turn on mobile data always on. The factory starts looking again.
testFactory.expectAddRequests(1);
setMobileDataAlwaysOn(true);
testFactory.waitForNetworkRequests(2);
assertTrue(testFactory.getMyStartRequested());
// Bring up cell data and check that the factory stops looking.
assertEquals(1, mCm.getAllNetworks().length);
mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
// Because the cell request changes score twice.
testFactory.expectAddRequests(2);
mCellNetworkAgent.connect(true);
cellNetworkCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
testFactory.waitForNetworkRequests(2);
// Because the cell network outscores us.
assertFalse(testFactory.getMyStartRequested());
// Check that cell data stays up.
mService.waitForIdle();
verifyActiveNetwork(TRANSPORT_WIFI);
assertEquals(2, mCm.getAllNetworks().length);
// Turn off mobile data always on and expect the request to disappear...
testFactory.expectRemoveRequests(1);
setMobileDataAlwaysOn(false);
testFactory.waitForNetworkRequests(1);
// ... and cell data to be torn down.
cellNetworkCallback.expectCallback(CallbackState.LOST, mCellNetworkAgent);
assertEquals(1, mCm.getAllNetworks().length);
testFactory.unregister();
mCm.unregisterNetworkCallback(cellNetworkCallback);
handlerThread.quit();
}
use of android.net.NetworkCapabilities in project Resurrection_packages_apps_Settings by ResurrectionRemix.
the class WifiNoInternetDialog method onCreate.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Intent intent = getIntent();
if (intent == null || !isKnownAction(intent) || !"netId".equals(intent.getScheme())) {
Log.e(TAG, "Unexpected intent " + intent + ", exiting");
finish();
return;
}
mAction = intent.getAction();
try {
mNetwork = new Network(Integer.parseInt(intent.getData().getSchemeSpecificPart()));
} catch (NullPointerException | NumberFormatException e) {
mNetwork = null;
}
if (mNetwork == null) {
Log.e(TAG, "Can't determine network from '" + intent.getData() + "' , exiting");
finish();
return;
}
// TODO: add a registerNetworkCallback(Network network, NetworkCallback networkCallback) and
// simplify this.
final NetworkRequest request = new NetworkRequest.Builder().clearCapabilities().build();
mNetworkCallback = new NetworkCallback() {
@Override
public void onLost(Network network) {
// Close the dialog if the network disconnects.
if (mNetwork.equals(network)) {
Log.d(TAG, "Network " + mNetwork + " disconnected");
finish();
}
}
@Override
public void onCapabilitiesChanged(Network network, NetworkCapabilities nc) {
// Close the dialog if the network validates.
if (mNetwork.equals(network) && nc.hasCapability(NET_CAPABILITY_VALIDATED)) {
Log.d(TAG, "Network " + mNetwork + " validated");
finish();
}
}
};
mCM = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
mCM.registerNetworkCallback(request, mNetworkCallback);
final NetworkInfo ni = mCM.getNetworkInfo(mNetwork);
if (ni == null || !ni.isConnectedOrConnecting()) {
Log.d(TAG, "Network " + mNetwork + " is not connected: " + ni);
finish();
return;
}
mNetworkName = ni.getExtraInfo();
if (mNetworkName != null) {
// Remove double quotes
mNetworkName = mNetworkName.replaceAll("^\"|\"$", "");
}
createDialog();
}
use of android.net.NetworkCapabilities in project android_frameworks_base by crdroidandroid.
the class NetworkControllerBaseTest method setUp.
@Override
protected void setUp() throws Exception {
super.setUp();
mMockWm = mock(WifiManager.class);
mMockTm = mock(TelephonyManager.class);
mMockSm = mock(SubscriptionManager.class);
mMockCm = mock(ConnectivityManager.class);
mMockSubDefaults = mock(SubscriptionDefaults.class);
mNetCapabilities = new NetworkCapabilities();
when(mMockCm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE)).thenReturn(true);
when(mMockCm.getDefaultNetworkCapabilitiesForUser(0)).thenReturn(new NetworkCapabilities[] { mNetCapabilities });
mSignalStrength = mock(SignalStrength.class);
mServiceState = mock(ServiceState.class);
mConfig = new Config();
mConfig.hspaDataDistinguishable = true;
mCallbackHandler = mock(CallbackHandler.class);
mNetworkController = new NetworkControllerImpl(mContext, mMockCm, mMockTm, mMockWm, mMockSm, mConfig, Looper.getMainLooper(), mCallbackHandler, mock(AccessPointControllerImpl.class), mock(DataUsageController.class), mMockSubDefaults);
setupNetworkController();
// Trigger blank callbacks to always get the current state (some tests don't trigger
// changes from default state).
mNetworkController.addSignalCallback(mock(SignalCallback.class));
mNetworkController.addEmergencyListener(null);
}
use of android.net.NetworkCapabilities in project android_frameworks_base by crdroidandroid.
the class ConnectivityService method pendingRequestForNetwork.
@Override
public NetworkRequest pendingRequestForNetwork(NetworkCapabilities networkCapabilities, PendingIntent operation) {
checkNotNull(operation, "PendingIntent cannot be null.");
networkCapabilities = new NetworkCapabilities(networkCapabilities);
enforceNetworkRequestPermissions(networkCapabilities);
enforceMeteredApnPolicy(networkCapabilities);
ensureRequestableCapabilities(networkCapabilities);
NetworkRequest networkRequest = new NetworkRequest(networkCapabilities, TYPE_NONE, nextNetworkRequestId(), NetworkRequest.Type.REQUEST);
NetworkRequestInfo nri = new NetworkRequestInfo(networkRequest, operation);
if (DBG)
log("pendingRequest for " + nri);
mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_REQUEST_WITH_INTENT, nri));
return networkRequest;
}
use of android.net.NetworkCapabilities in project android_frameworks_base by crdroidandroid.
the class ConnectivityService method listenForNetwork.
@Override
public NetworkRequest listenForNetwork(NetworkCapabilities networkCapabilities, Messenger messenger, IBinder binder) {
if (!hasWifiNetworkListenPermission(networkCapabilities)) {
enforceAccessPermission();
}
NetworkCapabilities nc = new NetworkCapabilities(networkCapabilities);
if (!ConnectivityManager.checkChangePermission(mContext)) {
// Apps without the CHANGE_NETWORK_STATE permission can't use background networks, so
// make all their listens include NET_CAPABILITY_FOREGROUND. That way, they will get
// onLost and onAvailable callbacks when networks move in and out of the background.
// There is no need to do this for requests because an app without CHANGE_NETWORK_STATE
// can't request networks.
nc.addCapability(NET_CAPABILITY_FOREGROUND);
}
NetworkRequest networkRequest = new NetworkRequest(nc, TYPE_NONE, nextNetworkRequestId(), NetworkRequest.Type.LISTEN);
NetworkRequestInfo nri = new NetworkRequestInfo(messenger, networkRequest, binder);
if (VDBG)
log("listenForNetwork for " + nri);
mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_LISTENER, nri));
return networkRequest;
}
Aggregations