use of android.net.ConnectivityManager.NetworkCallback in project android_frameworks_base by crdroidandroid.
the class ConnectivityServiceTest method testNetworkRequestMaximum.
@SmallTest
public void testNetworkRequestMaximum() {
final int MAX_REQUESTS = 100;
// Test that the limit is enforced when MAX_REQUESTS simultaneous requests are added.
NetworkRequest networkRequest = new NetworkRequest.Builder().build();
ArrayList<NetworkCallback> networkCallbacks = new ArrayList<NetworkCallback>();
try {
for (int i = 0; i < MAX_REQUESTS; i++) {
NetworkCallback networkCallback = new NetworkCallback();
mCm.requestNetwork(networkRequest, networkCallback);
networkCallbacks.add(networkCallback);
}
fail("Registering " + MAX_REQUESTS + " NetworkRequests did not throw exception");
} catch (IllegalArgumentException expected) {
}
for (NetworkCallback networkCallback : networkCallbacks) {
mCm.unregisterNetworkCallback(networkCallback);
}
networkCallbacks.clear();
try {
for (int i = 0; i < MAX_REQUESTS; i++) {
NetworkCallback networkCallback = new NetworkCallback();
mCm.registerNetworkCallback(networkRequest, networkCallback);
networkCallbacks.add(networkCallback);
}
fail("Registering " + MAX_REQUESTS + " NetworkCallbacks did not throw exception");
} catch (IllegalArgumentException expected) {
}
for (NetworkCallback networkCallback : networkCallbacks) {
mCm.unregisterNetworkCallback(networkCallback);
}
networkCallbacks.clear();
ArrayList<PendingIntent> pendingIntents = new ArrayList<PendingIntent>();
try {
for (int i = 0; i < MAX_REQUESTS + 1; i++) {
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, new Intent("a" + i), 0);
mCm.requestNetwork(networkRequest, pendingIntent);
pendingIntents.add(pendingIntent);
}
fail("Registering " + MAX_REQUESTS + " PendingIntent NetworkRequests did not throw exception");
} catch (IllegalArgumentException expected) {
}
for (PendingIntent pendingIntent : pendingIntents) {
mCm.unregisterNetworkCallback(pendingIntent);
}
pendingIntents.clear();
try {
for (int i = 0; i < MAX_REQUESTS + 1; i++) {
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, new Intent("a" + i), 0);
mCm.registerNetworkCallback(networkRequest, pendingIntent);
pendingIntents.add(pendingIntent);
}
fail("Registering " + MAX_REQUESTS + " PendingIntent NetworkCallbacks did not throw exception");
} catch (IllegalArgumentException expected) {
}
for (PendingIntent pendingIntent : pendingIntents) {
mCm.unregisterNetworkCallback(pendingIntent);
}
pendingIntents.clear();
mService.waitForIdle(5000);
// Test that the limit is not hit when MAX_REQUESTS requests are added and removed.
for (int i = 0; i < MAX_REQUESTS; i++) {
NetworkCallback networkCallback = new NetworkCallback();
mCm.requestNetwork(networkRequest, networkCallback);
mCm.unregisterNetworkCallback(networkCallback);
}
mService.waitForIdle();
for (int i = 0; i < MAX_REQUESTS; i++) {
NetworkCallback networkCallback = new NetworkCallback();
mCm.registerNetworkCallback(networkRequest, networkCallback);
mCm.unregisterNetworkCallback(networkCallback);
}
mService.waitForIdle();
for (int i = 0; i < MAX_REQUESTS; i++) {
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, new Intent("b" + i), 0);
mCm.requestNetwork(networkRequest, pendingIntent);
mCm.unregisterNetworkCallback(pendingIntent);
}
mService.waitForIdle();
for (int i = 0; i < MAX_REQUESTS; i++) {
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, new Intent("c" + i), 0);
mCm.registerNetworkCallback(networkRequest, pendingIntent);
mCm.unregisterNetworkCallback(pendingIntent);
}
}
use of android.net.ConnectivityManager.NetworkCallback in project android_packages_apps_Settings by omnirom.
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.ConnectivityManager.NetworkCallback in project android_packages_apps_Settings by SudaMod.
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.ConnectivityManager.NetworkCallback in project android_packages_apps_Settings by DirtyUnicorns.
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.ConnectivityManager.NetworkCallback in project android_packages_apps_Settings by crdroidandroid.
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();
}
Aggregations