Search in sources :

Example 26 with Network

use of android.net.Network in project android_frameworks_base by AOSPA.

the class ConnectivityService method getDefaultNetworkCapabilitiesForUser.

@Override
public NetworkCapabilities[] getDefaultNetworkCapabilitiesForUser(int userId) {
    // The basic principle is: if an app's traffic could possibly go over a
    // network, without the app doing anything multinetwork-specific,
    // (hence, by "default"), then include that network's capabilities in
    // the array.
    //
    // In the normal case, app traffic only goes over the system's default
    // network connection, so that's the only network returned.
    //
    // With a VPN in force, some app traffic may go into the VPN, and thus
    // over whatever underlying networks the VPN specifies, while other app
    // traffic may go over the system default network (e.g.: a split-tunnel
    // VPN, or an app disallowed by the VPN), so the set of networks
    // returned includes the VPN's underlying networks and the system
    // default.
    enforceAccessPermission();
    HashMap<Network, NetworkCapabilities> result = new HashMap<Network, NetworkCapabilities>();
    NetworkAgentInfo nai = getDefaultNetwork();
    NetworkCapabilities nc = getNetworkCapabilitiesInternal(nai);
    if (nc != null) {
        result.put(nai.network, nc);
    }
    if (!mLockdownEnabled) {
        synchronized (mVpns) {
            Vpn vpn = mVpns.get(userId);
            if (vpn != null) {
                Network[] networks = vpn.getUnderlyingNetworks();
                if (networks != null) {
                    for (Network network : networks) {
                        nai = getNetworkAgentInfoForNetwork(network);
                        nc = getNetworkCapabilitiesInternal(nai);
                        if (nc != null) {
                            result.put(network, nc);
                        }
                    }
                }
            }
        }
    }
    NetworkCapabilities[] out = new NetworkCapabilities[result.size()];
    out = result.values().toArray(out);
    return out;
}
Also used : HashMap(java.util.HashMap) Vpn(com.android.server.connectivity.Vpn) NetworkAgentInfo(com.android.server.connectivity.NetworkAgentInfo) Network(android.net.Network) NetworkCapabilities(android.net.NetworkCapabilities)

Example 27 with Network

use of android.net.Network in project android_frameworks_base by AOSPA.

the class NetworkTest method testZeroIsObviousForDebugging.

@SmallTest
public void testZeroIsObviousForDebugging() {
    Network zero = new Network(0);
    assertEquals(0, zero.hashCode());
    assertEquals(0, zero.getNetworkHandle());
    assertEquals("0", zero.toString());
}
Also used : Network(android.net.Network) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 28 with Network

use of android.net.Network in project android_frameworks_base by AOSPA.

the class ConnectivityService method getAllNetworkState.

@Override
public NetworkState[] getAllNetworkState() {
    // Require internal since we're handing out IMSI details
    enforceConnectivityInternalPermission();
    final ArrayList<NetworkState> result = Lists.newArrayList();
    for (Network network : getAllNetworks()) {
        final NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network);
        if (nai != null) {
            result.add(nai.getNetworkState());
        }
    }
    return result.toArray(new NetworkState[result.size()]);
}
Also used : NetworkAgentInfo(com.android.server.connectivity.NetworkAgentInfo) Network(android.net.Network) NetworkState(android.net.NetworkState)

Example 29 with Network

use of android.net.Network in project android_frameworks_base by AOSPA.

the class CaptivePortalLoginActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mCm = ConnectivityManager.from(this);
    mNetwork = getIntent().getParcelableExtra(ConnectivityManager.EXTRA_NETWORK);
    mCaptivePortal = getIntent().getParcelableExtra(ConnectivityManager.EXTRA_CAPTIVE_PORTAL);
    mUserAgent = getIntent().getParcelableExtra(ConnectivityManager.EXTRA_CAPTIVE_PORTAL_USER_AGENT);
    mUrl = getUrl();
    if (mUrl == null) {
        // getUrl() failed to parse the url provided in the intent: bail out in a way that
        // at least provides network access.
        done(Result.WANTED_AS_IS);
        return;
    }
    if (DBG) {
        Log.d(TAG, String.format("onCreate for %s", mUrl.toString()));
    }
    // Also initializes proxy system properties.
    mCm.bindProcessToNetwork(mNetwork);
    // Proxy system properties must be initialized before setContentView is called because
    // setContentView initializes the WebView logic which in turn reads the system properties.
    setContentView(R.layout.activity_captive_portal_login);
    getActionBar().setDisplayShowHomeEnabled(false);
    // Exit app if Network disappears.
    final NetworkCapabilities networkCapabilities = mCm.getNetworkCapabilities(mNetwork);
    if (networkCapabilities == null) {
        finishAndRemoveTask();
        return;
    }
    mNetworkCallback = new NetworkCallback() {

        @Override
        public void onLost(Network lostNetwork) {
            if (mNetwork.equals(lostNetwork))
                done(Result.UNWANTED);
        }
    };
    final NetworkRequest.Builder builder = new NetworkRequest.Builder();
    for (int transportType : networkCapabilities.getTransportTypes()) {
        builder.addTransportType(transportType);
    }
    mCm.registerNetworkCallback(builder.build(), mNetworkCallback);
    final WebView myWebView = (WebView) findViewById(R.id.webview);
    myWebView.clearCache(true);
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
    mWebViewClient = new MyWebViewClient();
    myWebView.setWebViewClient(mWebViewClient);
    myWebView.setWebChromeClient(new MyWebChromeClient());
    // Start initial page load so WebView finishes loading proxy settings.
    // Actual load of mUrl is initiated by MyWebViewClient.
    myWebView.loadData("", "text/html", null);
}
Also used : WebSettings(android.webkit.WebSettings) Network(android.net.Network) NetworkRequest(android.net.NetworkRequest) WebView(android.webkit.WebView) NetworkCapabilities(android.net.NetworkCapabilities) NetworkCallback(android.net.ConnectivityManager.NetworkCallback)

Example 30 with Network

use of android.net.Network in project NetGuard by M66B.

the class Util method getDefaultDNS.

public static List<String> getDefaultDNS(Context context) {
    String dns1 = null;
    String dns2 = null;
    if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.N) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        Network an = cm.getActiveNetwork();
        if (an != null) {
            LinkProperties lp = cm.getLinkProperties(an);
            if (lp != null) {
                List<InetAddress> dns = lp.getDnsServers();
                if (dns != null) {
                    if (dns.size() > 0)
                        dns1 = dns.get(0).getHostAddress();
                    if (dns.size() > 1)
                        dns2 = dns.get(1).getHostAddress();
                    for (InetAddress d : dns) Log.i(TAG, "DNS from LP: " + d.getHostAddress());
                }
            }
        }
    } else {
        dns1 = jni_getprop("net.dns1");
        dns2 = jni_getprop("net.dns2");
    }
    List<String> listDns = new ArrayList<>();
    listDns.add(TextUtils.isEmpty(dns1) ? "8.8.8.8" : dns1);
    listDns.add(TextUtils.isEmpty(dns2) ? "8.8.4.4" : dns2);
    return listDns;
}
Also used : ConnectivityManager(android.net.ConnectivityManager) Network(android.net.Network) ArrayList(java.util.ArrayList) LinkProperties(android.net.LinkProperties) InetAddress(java.net.InetAddress)

Aggregations

Network (android.net.Network)90 SmallTest (android.test.suitebuilder.annotation.SmallTest)28 NetworkAgentInfo (com.android.server.connectivity.NetworkAgentInfo)25 NetworkCapabilities (android.net.NetworkCapabilities)22 NetworkInfo (android.net.NetworkInfo)17 LinkProperties (android.net.LinkProperties)16 NetworkRequest (android.net.NetworkRequest)16 NetworkCallback (android.net.ConnectivityManager.NetworkCallback)11 ConnectivityManager (android.net.ConnectivityManager)9 InetAddress (java.net.InetAddress)6 Test (org.junit.Test)6 Nullable (android.annotation.Nullable)5 NetworkMisc (android.net.NetworkMisc)5 NetworkState (android.net.NetworkState)5 WifiConfiguration (android.net.wifi.WifiConfiguration)5 WifiInfo (android.net.wifi.WifiInfo)5 ConditionVariable (android.os.ConditionVariable)5 WebSettings (android.webkit.WebSettings)5 HomeSP (com.android.hotspot2.pps.HomeSP)5 LegacyVpnInfo (com.android.internal.net.LegacyVpnInfo)5