Merge "Improve doc for CSM#setGlobalProxy"

This commit is contained in:
Treehugger Robot
2023-05-23 04:04:45 +00:00
committed by Gerrit Code Review

View File

@@ -28,6 +28,7 @@ import android.annotation.NonNull;
import android.annotation.Nullable; import android.annotation.Nullable;
import android.annotation.SystemApi; import android.annotation.SystemApi;
import android.content.Context; import android.content.Context;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager.MultipathPreference; import android.net.ConnectivityManager.MultipathPreference;
import android.os.Binder; import android.os.Binder;
import android.os.Build; import android.os.Build;
@@ -36,6 +37,7 @@ import android.os.UserHandle;
import android.provider.Settings; import android.provider.Settings;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.ArraySet; import android.util.ArraySet;
import android.util.Log;
import android.util.Range; import android.util.Range;
import com.android.net.module.util.ConnectivitySettingsUtils; import com.android.net.module.util.ConnectivitySettingsUtils;
@@ -55,6 +57,7 @@ import java.util.StringJoiner;
*/ */
@SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
public class ConnectivitySettingsManager { public class ConnectivitySettingsManager {
private static final String TAG = ConnectivitySettingsManager.class.getSimpleName();
private ConnectivitySettingsManager() {} private ConnectivitySettingsManager() {}
@@ -696,10 +699,20 @@ public class ConnectivitySettingsManager {
/** /**
* Set global http proxy settings from given {@link ProxyInfo}. * Set global http proxy settings from given {@link ProxyInfo}.
* *
* <p class="note">
* While a {@link ProxyInfo} for a PAC proxy can be specified, not all devices support
* PAC proxies. In particular, smaller devices like watches often do not have the capabilities
* necessary to interpret the PAC file. In such cases, calling this API with a PAC proxy
* results in undefined behavior, including possibly breaking networking for applications.
* You can test for this by checking for the presence of {@link PackageManager.FEATURE_WEBVIEW}.
* </p>
*
* @param context The {@link Context} to set the setting. * @param context The {@link Context} to set the setting.
* @param proxyInfo The {@link ProxyInfo} for global http proxy settings which build from * @param proxyInfo The {@link ProxyInfo} for global http proxy settings which build from
* {@link ProxyInfo#buildPacProxy(Uri)} or * {@link ProxyInfo#buildPacProxy(Uri)} or
* {@link ProxyInfo#buildDirectProxy(String, int, List)} * {@link ProxyInfo#buildDirectProxy(String, int, List)}
* @throws UnsupportedOperationException if |proxyInfo| codes for a PAC proxy but the system
* does not support PAC proxies.
*/ */
public static void setGlobalProxy(@NonNull Context context, @NonNull ProxyInfo proxyInfo) { public static void setGlobalProxy(@NonNull Context context, @NonNull ProxyInfo proxyInfo) {
final String host = proxyInfo.getHost(); final String host = proxyInfo.getHost();
@@ -707,6 +720,14 @@ public class ConnectivitySettingsManager {
final String exclusionList = proxyInfo.getExclusionListAsString(); final String exclusionList = proxyInfo.getExclusionListAsString();
final String pacFileUrl = proxyInfo.getPacFileUrl().toString(); final String pacFileUrl = proxyInfo.getPacFileUrl().toString();
if (!TextUtils.isEmpty(pacFileUrl)) {
final PackageManager pm = context.getPackageManager();
if (null != pm && !pm.hasSystemFeature(PackageManager.FEATURE_WEBVIEW)) {
Log.wtf(TAG, "PAC proxy can't be installed on a device without FEATURE_WEBVIEW");
}
}
if (TextUtils.isEmpty(pacFileUrl)) { if (TextUtils.isEmpty(pacFileUrl)) {
Settings.Global.putString(context.getContentResolver(), GLOBAL_HTTP_PROXY_HOST, host); Settings.Global.putString(context.getContentResolver(), GLOBAL_HTTP_PROXY_HOST, host);
Settings.Global.putInt(context.getContentResolver(), GLOBAL_HTTP_PROXY_PORT, port); Settings.Global.putInt(context.getContentResolver(), GLOBAL_HTTP_PROXY_PORT, port);