类android.net.ProxyInfo源码实例Demo

下面列出了怎么用android.net.ProxyInfo的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: android_9.0.0_r45   文件: PacManager.java
/**
 * Updates the PAC Manager with current Proxy information. This is called by
 * the ConnectivityService directly before a broadcast takes place to allow
 * the PacManager to indicate that the broadcast should not be sent and the
 * PacManager will trigger a new broadcast when it is ready.
 *
 * @param proxy Proxy information that is about to be broadcast.
 * @return Returns true when the broadcast should not be sent
 */
public synchronized boolean setCurrentProxyScriptUrl(ProxyInfo proxy) {
    if (!Uri.EMPTY.equals(proxy.getPacFileUrl())) {
        if (proxy.getPacFileUrl().equals(mPacUrl) && (proxy.getPort() > 0)) {
            // Allow to send broadcast, nothing to do.
            return false;
        }
        mPacUrl = proxy.getPacFileUrl();
        mCurrentDelay = DELAY_1;
        mHasSentBroadcast = false;
        mHasDownloaded = false;
        getAlarmManager().cancel(mPacRefreshIntent);
        bind();
        return true;
    } else {
        getAlarmManager().cancel(mPacRefreshIntent);
        synchronized (mProxyLock) {
            mPacUrl = Uri.EMPTY;
            mCurrentPac = null;
            if (mProxyService != null) {
                try {
                    mProxyService.stopPacSystem();
                } catch (RemoteException e) {
                    Log.w(TAG, "Failed to stop PAC service", e);
                } finally {
                    unbind();
                }
            }
        }
        return false;
    }
}
 
源代码2 项目: android_9.0.0_r45   文件: PacManager.java
private synchronized void sendProxyIfNeeded() {
    if (!mHasDownloaded || (mLastPort == -1)) {
        return;
    }
    if (!mHasSentBroadcast) {
        sendPacBroadcast(new ProxyInfo(mPacUrl, mLastPort));
        mHasSentBroadcast = true;
    }
}
 
源代码3 项目: android_9.0.0_r45   文件: NetworkMonitor.java
/**
 * Do a DNS resolution and URL fetch on a known web server to see if we get the data we expect.
 * @return a CaptivePortalProbeResult inferred from the HTTP response.
 */
private CaptivePortalProbeResult sendDnsAndHttpProbes(ProxyInfo proxy, URL url, int probeType) {
    // Pre-resolve the captive portal server host so we can log it.
    // Only do this if HttpURLConnection is about to, to avoid any potentially
    // unnecessary resolution.
    final String host = (proxy != null) ? proxy.getHost() : url.getHost();
    sendDnsProbe(host);
    return sendHttpProbe(url, probeType, null);
}
 
源代码4 项目: WiFiProxySwitcher   文件: WiFiProxyUtil.java
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private boolean unSetWiFiProxySettings5() {
    try {
        WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiConfiguration config = getCurrentWifiConfiguration(manager);
        Class proxySettings = Class.forName("android.net.IpConfiguration$ProxySettings");

        Class[] setProxyParams = new Class[2];
        setProxyParams[0] = proxySettings;
        setProxyParams[1] = ProxyInfo.class;

        Method setProxy = WifiConfiguration.class.getDeclaredMethod("setProxy", setProxyParams);
        setProxy.setAccessible(true);

        Object[] methodParams = new Object[2];
        methodParams[0] = Enum.valueOf(proxySettings, "NONE");
        methodParams[1] = null;

        setProxy.invoke(config, methodParams);

        manager.updateNetwork(config);
        manager.disconnect();
        manager.reconnect();

        ToastUtil.showToast(context, "取消Proxy设置成功");
        return true;
    } catch (Exception e) {
        ToastUtil.showToast(context, "取消Proxy设置失败");
        return false;
    }
}
 
源代码5 项目: android_9.0.0_r45   文件: PacManager.java
private void sendPacBroadcast(ProxyInfo proxy) {
    mConnectivityHandler.sendMessage(mConnectivityHandler.obtainMessage(mProxyMessage, proxy));
}
 
源代码6 项目: android_9.0.0_r45   文件: NetworkMonitor.java
@VisibleForTesting
protected CaptivePortalProbeResult isCaptivePortal() {
    if (!mIsCaptivePortalCheckEnabled) {
        validationLog("Validation disabled.");
        return CaptivePortalProbeResult.SUCCESS;
    }

    URL pacUrl = null;
    URL httpsUrl = mCaptivePortalHttpsUrl;
    URL httpUrl = mCaptivePortalHttpUrl;

    // On networks with a PAC instead of fetching a URL that should result in a 204
    // response, we instead simply fetch the PAC script.  This is done for a few reasons:
    // 1. At present our PAC code does not yet handle multiple PACs on multiple networks
    //    until something like https://android-review.googlesource.com/#/c/115180/ lands.
    //    Network.openConnection() will ignore network-specific PACs and instead fetch
    //    using NO_PROXY.  If a PAC is in place, the only fetch we know will succeed with
    //    NO_PROXY is the fetch of the PAC itself.
    // 2. To proxy the generate_204 fetch through a PAC would require a number of things
    //    happen before the fetch can commence, namely:
    //        a) the PAC script be fetched
    //        b) a PAC script resolver service be fired up and resolve the captive portal
    //           server.
    //    Network validation could be delayed until these prerequisities are satisifed or
    //    could simply be left to race them.  Neither is an optimal solution.
    // 3. PAC scripts are sometimes used to block or restrict Internet access and may in
    //    fact block fetching of the generate_204 URL which would lead to false negative
    //    results for network validation.
    final ProxyInfo proxyInfo = mNetworkAgentInfo.linkProperties.getHttpProxy();
    if (proxyInfo != null && !Uri.EMPTY.equals(proxyInfo.getPacFileUrl())) {
        pacUrl = makeURL(proxyInfo.getPacFileUrl().toString());
        if (pacUrl == null) {
            return CaptivePortalProbeResult.FAILED;
        }
    }

    if ((pacUrl == null) && (httpUrl == null || httpsUrl == null)) {
        return CaptivePortalProbeResult.FAILED;
    }

    long startTime = SystemClock.elapsedRealtime();

    final CaptivePortalProbeResult result;
    if (pacUrl != null) {
        result = sendDnsAndHttpProbes(null, pacUrl, ValidationProbeEvent.PROBE_PAC);
    } else if (mUseHttps) {
        result = sendParallelHttpProbes(proxyInfo, httpsUrl, httpUrl);
    } else {
        result = sendDnsAndHttpProbes(proxyInfo, httpUrl, ValidationProbeEvent.PROBE_HTTP);
    }

    long endTime = SystemClock.elapsedRealtime();

    sendNetworkConditionsBroadcast(true /* response received */,
            result.isPortal() /* isCaptivePortal */,
            startTime, endTime);

    return result;
}
 
源代码7 项目: WiFiProxySwitcher   文件: WiFiProxyUtil.java
/**
 * 高于 Android 5.0 系统使用
 */
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private boolean setWiFiProxySettings5(String ip, int port) {
    try {
        WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiConfiguration config = getCurrentWifiConfiguration(manager);
        Class proxySettings = Class.forName("android.net.IpConfiguration$ProxySettings");

        Class[] setProxyParams = new Class[2];
        setProxyParams[0] = proxySettings;
        setProxyParams[1] = ProxyInfo.class;

        Method setProxy = WifiConfiguration.class.getDeclaredMethod("setProxy", setProxyParams);
        setProxy.setAccessible(true);

        ProxyInfo proxyInfo = ProxyInfo.buildDirectProxy(ip, port);

        Object[] methodParams = new Object[2];
        methodParams[0] = Enum.valueOf(proxySettings, "STATIC");
        methodParams[1] = proxyInfo;

        setProxy.invoke(config, methodParams);

        manager.updateNetwork(config);

        String result = getCurrentWifiConfiguration(manager).toString();
        String key = "Proxy settings: ";
        int start = result.indexOf(key) + key.length();
        if (result.substring(start, start + 4).equals("NONE")) {
            throw new RuntimeException("Can't update the Network, you should have the right WifiConfiguration");
        }

        manager.disconnect();
        manager.reconnect();

        ToastUtil.showToast(context, "保存Proxy设置成功");
        return true;
    } catch (Exception e) {
        ToastUtil.showToast(context, "保存Proxy设置失败");
        return false;
    }
}
 
/**
 * Shows a dialog that asks the user for a host and port, then sets the recommended global proxy
 * to these values.
 */
private void showSetGlobalHttpProxyDialog() {
    if (getActivity() == null || getActivity().isFinishing()) {
        return;
    }

    final View dialogView = getActivity().getLayoutInflater().inflate(
            R.layout.proxy_config_dialog, null);
    final EditText hostEditText = (EditText) dialogView.findViewById(R.id.proxy_host);
    final EditText portEditText = (EditText) dialogView.findViewById(R.id.proxy_port);
    final String host = System.getProperty("http.proxyHost");
    if (!TextUtils.isEmpty(host)) {
        hostEditText.setText(host);
        portEditText.setText(System.getProperty("http.proxyPort"));
    }

    new AlertDialog.Builder(getActivity())
            .setTitle(R.string.set_global_http_proxy)
            .setView(dialogView)
            .setPositiveButton(android.R.string.ok, (dialogInterface, i) -> {
                final String hostString = hostEditText.getText().toString();
                if (hostString.isEmpty()) {
                    showToast(R.string.no_host);
                    return;
                }
                final String portString = portEditText.getText().toString();
                if (portString.isEmpty()) {
                    showToast(R.string.no_port);
                    return;
                }
                final int port = Integer.parseInt(portString);
                if (port > 65535) {
                    showToast(R.string.port_out_of_range);
                    return;
                }
                mDevicePolicyManager.setRecommendedGlobalProxy(mAdminComponentName,
                        ProxyInfo.buildDirectProxy(hostString, port));
            })
            .setNegativeButton(android.R.string.cancel, null)
            .show();
}
 
 类所在包
 类方法
 同包方法