I've built an app that once set as device owner turns around and sets the global proxy settings using the setRecommendedGlobalProxy() function like this:
String pacUrl = "http://my-proxy-server.com:65011/proxy.pac?a=ZGtyZWl89xyy1qAZW15";
Uri pacUrl = Uri.parse(proxyPacUrl);
ProxyInfo newProxy = ProxyInfo.buildPacProxy(pacUrl);
try {
Log.i("ADMIN", "Setting global proxy to " + newProxy.toString());
mDPM.setRecommendedGlobalProxy(adminComponent, newProxy);
} catch (Exception e) {
Log.w("ADMIN", "Caught exception while setting global proxy: " + e.toString());
}
It all works great until I restart the phone and then the global proxy settings are gone. If I check for the global proxy settings via ConnectivityManager.getDefaultProxy(), it returns the proxy pac url that the app set earlier but even so, no network traffic is being proxified.
Interestingly enough, if I go to Settings -> Google -> Security -> Verify Apps, disable "Scan device for security threats", reboot the device, clear the global proxy settings and then reset the global proxy settings they will work until I re-enable "Scan device for security threats" and restart it. This makes no sense to me because it clearly states that I will be notified if any security risks are found and I sure haven't seen any notifications yet.
A few things worth mentioning:
I've tested on various devices and can reproduce the issue on all of them.
Properly signing the apk and then installing it doesn't fix the issue.
Once the global proxy settings have been broke, I have to first clear and then reset them before they'll properly work again.
Any suggestions or possible ideas as to what I'm doing wrong would be greatly welcomed!
Short Answer
Use buildDirectProxy(string hostname, int port)
Long Answer
If you use buildDirectProxy(string hostname, int port) the global proxy does not break or randomly quit working - it's rock solid.
But if you revert to a proxy PAC URL it breaks.
The reason for the unstable global proxy PAC URL is because the proxy port is set to -1, which eventually causes a crash. Here's a log excerpt.
02-12 16:20:57.723 2018 3270 E AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
02-12 16:20:57.723 2018 3270 E AndroidRuntime: Process: com.mydeviceowner.package, PID: 2019
02-12 16:20:57.723 2018 3270 E AndroidRuntime: java.lang.IllegalArgumentException: port out of range:-1
Android 11 introduced a way to set the port when setting a global pac URL so it's likely that this issue will be resolved in future releases.