mSettingsObserver =
new
SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY);
mSettingsObserver.observe(mContext);
private
static
class
SettingsObserver
extends
ContentObserver {
private
int
mWhat;
private
Handler mHandler;
SettingsObserver(Handler handler,
int
what) {
super
(handler);
mHandler = handler;
mWhat = what;
}
void
observe(Context context) {
ContentResolver resolver = context.getContentResolver();
resolver.registerContentObserver(Settings.Global.getUriFor(
Settings.Global.HTTP_PROXY),
false
,
this
);
}
@Override
public
void
onChange(
boolean
selfChange) {
mHandler.obtainMessage(mWhat).sendToTarget();
}
}
case
EVENT_APPLY_GLOBAL_HTTP_PROXY: {
handleDeprecatedGlobalHttpProxy();
break
;
}
private
void
handleDeprecatedGlobalHttpProxy() {
String proxy = Settings.Global.getString(mContext.getContentResolver(),
Settings.Global.HTTP_PROXY);
if
(!TextUtils.isEmpty(proxy)) {
String data[] = proxy.split(
":"
);
if
(data.length ==
0
) {
return
;
}
String proxyHost = data[
0
];
int
proxyPort =
8080
;
if
(data.length >
1
) {
try
{
proxyPort = Integer.parseInt(data[
1
]);
}
catch
(NumberFormatException e) {
return
;
}
}
ProxyProperties p =
new
ProxyProperties(data[
0
], proxyPort,
""
);
setGlobalProxy(p);
}
}
public
void
setGlobalProxy(ProxyProperties proxyProperties) {
enforceConnectivityInternalPermission();
synchronized
(mProxyLock) {
if
(proxyProperties == mGlobalProxy)
return
;
if
(proxyProperties !=
null
&& proxyProperties.equals(mGlobalProxy))
return
;
if
(mGlobalProxy !=
null
&& mGlobalProxy.equals(proxyProperties))
return
;
String host =
""
;
int
port =
0
;
String exclList =
""
;
String pacFileUrl =
""
;
if
(proxyProperties !=
null
&& (!TextUtils.isEmpty(proxyProperties.getHost()) ||
!TextUtils.isEmpty(proxyProperties.getPacFileUrl()))) {
if
(!proxyProperties.isValid()) {
if
(DBG)
log(
"Invalid proxy properties, ignoring: "
+ proxyProperties.toString());
return
;
}
mGlobalProxy =
new
ProxyProperties(proxyProperties);
host = mGlobalProxy.getHost();
port = mGlobalProxy.getPort();
exclList = mGlobalProxy.getExclusionList();
if
(proxyProperties.getPacFileUrl() !=
null
) {
pacFileUrl = proxyProperties.getPacFileUrl();
}
}
else
{
mGlobalProxy =
null
;
}
ContentResolver res = mContext.getContentResolver();
final
long
token = Binder.clearCallingIdentity();
try
{
Settings.Global.putString(res, Settings.Global.GLOBAL_HTTP_PROXY_HOST, host);
Settings.Global.putInt(res, Settings.Global.GLOBAL_HTTP_PROXY_PORT, port);
Settings.Global.putString(res, Settings.Global.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
exclList);
Settings.Global.putString(res, Settings.Global.GLOBAL_HTTP_PROXY_PAC, pacFileUrl);
}
finally
{
Binder.restoreCallingIdentity(token);
}
}
if
(mGlobalProxy ==
null
) {
proxyProperties = mDefaultProxy;
}
sendProxyBroadcast(proxyProperties);
}
private
void
sendProxyBroadcast(ProxyProperties proxy) {
if
(proxy ==
null
) proxy =
new
ProxyProperties(
""
,
0
,
""
);
if
(mPacManager.setCurrentProxyScriptUrl(proxy))
return
;
if
(DBG) log(
"sending Proxy Broadcast for "
+ proxy);
Intent intent =
new
Intent(Proxy.PROXY_CHANGE_ACTION);
intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
final
long
ident = Binder.clearCallingIdentity();
try
{
mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL);
}
finally
{
Binder.restoreCallingIdentity(ident);
}
}