Merge "Guard the usage of PacProxyManager when it is not supported by the device."

This commit is contained in:
Abdelrahman Ibrahim 2023-06-20 16:35:56 +00:00 • committed by Gerrit Code Review
commit 76f61bb2e8
2 changed files with 18 additions and 7 deletions

View file

@ -86,6 +86,7 @@ public class ProxyTracker {
private final Handler mConnectivityServiceHandler;
@Nullable
private final PacProxyManager mPacProxyManager;
private class PacProxyInstalledListener implements PacProxyManager.PacProxyInstalledListener {
@ -109,9 +110,11 @@ public class ProxyTracker {
mConnectivityServiceHandler = connectivityServiceInternalHandler;
mPacProxyManager = context.getSystemService(PacProxyManager.class);
PacProxyInstalledListener listener = new PacProxyInstalledListener(pacChangedEvent);
mPacProxyManager.addPacProxyInstalledListener(
if (mPacProxyManager != null) {
PacProxyInstalledListener listener = new PacProxyInstalledListener(pacChangedEvent);
mPacProxyManager.addPacProxyInstalledListener(
mConnectivityServiceHandler::post, listener);
}
}
// Convert empty ProxyInfo's to null as null-checks are used to determine if proxies are present
@ -205,7 +208,7 @@ public class ProxyTracker {
mGlobalProxy = proxyProperties;
}
if (!TextUtils.isEmpty(pacFileUrl)) {
if (!TextUtils.isEmpty(pacFileUrl) && mPacProxyManager != null) {
mConnectivityServiceHandler.post(
() -> mPacProxyManager.setCurrentProxyScriptUrl(proxyProperties));
}
@ -251,7 +254,10 @@ public class ProxyTracker {
final ProxyInfo defaultProxy = getDefaultProxy();
final ProxyInfo proxyInfo = null != defaultProxy ?
defaultProxy : ProxyInfo.buildDirectProxy("", 0, Collections.emptyList());
mPacProxyManager.setCurrentProxyScriptUrl(proxyInfo);
if (mPacProxyManager != null) {
mPacProxyManager.setCurrentProxyScriptUrl(proxyInfo);
}
if (!shouldSendBroadcast(proxyInfo)) {
return;

View file

@ -44,12 +44,15 @@ import android.util.Range;
import androidx.test.InstrumentationRegistry;
import com.android.compatibility.common.util.RequiredFeatureRule;
import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo;
import com.android.testutils.DevSdkIgnoreRunner;
import com.android.testutils.TestHttpServer;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -76,6 +79,11 @@ public final class PacProxyManagerTest {
private ServerSocket mServerSocket;
private Instrumentation mInstrumentation;
// Devices without WebView/JavaScript cannot support PAC proxies.
@Rule
public RequiredFeatureRule mRequiredWebviewFeatureRule =
new RequiredFeatureRule(PackageManager.FEATURE_WEBVIEW);
private static final String PAC_FILE = "function FindProxyForURL(url, host)"
+ "{"
+ " return \"PROXY 192.168.0.1:9091\";"
@ -152,9 +160,6 @@ public final class PacProxyManagerTest {
@AppModeFull(reason = "Instant apps can't bind sockets to localhost for a test proxy server")
@Test
public void testSetCurrentProxyScriptUrl() throws Exception {
// Devices without WebView/JavaScript cannot support PAC proxies
assumeTrue(mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WEBVIEW));
// Register a PacProxyInstalledListener
final TestPacProxyInstalledListener listener = new TestPacProxyInstalledListener();
final Executor executor = (Runnable r) -> r.run();