3.0之前版本code:
Java代碼
private void setWifiProxyConfiguration(ProxyConfiguration proxyconfiguration) {
// if isnot SSID ,return out
if (proxyconfiguration.getProxyScope() != ProxyConfiguration.ProxyScope.SSID){
return;
}
mWifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
// add a WifiConfig
List<WifiConfiguration> wifiConfiguredNetworks = mWifiManager
.getConfiguredNetworks();
for (final WifiConfiguration w : wifiConfiguredNetworks){
Log.i(TAG, “”+w.SSID+”:”+proxyconfiguration.getNetworkId());
if (w.SSID.equals(“””+proxyconfiguration.getNetworkId()+”””)){
mLinkProperties.clear();
Log.i(TAG, “w.SSID.equals(proxyconfiguration.getNetworkId()):”
+ wifiConfiguredNetworks.indexOf(w));
mWifiConfiguration = wifiConfiguredNetworks.get(wifiConfiguredNetworks.indexOf(w));
ProxyProperties proxyproperties = new ProxyProperties(
proxyconfiguration.getProxyURL(),
proxyconfiguration.getProxyPort(),
proxyconfiguration.getmUrlExclusionList());
mLinkProperties.setHttpProxy(proxyproperties);
mWifiConfiguration.linkProperties = new LinkProperties(mLinkProperties);
mWifiConfiguration.proxySettings = ProxySettings.STATIC;
Log.i(TAG, “mWifiConfiguration.linkProperties.setHttpProxy(proxyproperties);”+ mWifiConfiguration);
mWifiManager.saveNetwork(mWifiConfiguration);
}
}
}
附3.0 Demo
3.1時源碼有所改動加入瞭async code為:
Java代碼
private class WifiServiceHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED:
if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) {
Log.e(TAG, “Success to establish AsyncChannel connection”);
//AsyncChannel in msg.obj
} else {
//AsyncChannel set up failure, ignore
Log.e(TAG, “Failed to establish AsyncChannel connection”);
}
break;
default:
//Ignore
break;
}
}
WifiServiceHandler(Looper looper){
super(looper);
}
}
// Standard priority background threads.
final HandlerThread mHandlerThread = new HandlerThread(“DivideService”,Process.THREAD_PRIORITY_BACKGROUND);
final WifiServiceHandler mHandler;
// run a message loop for a thread.
// Threads do not have a message loop associated with them by default; so create one
mHandlerThread.start();
mHandler = new WifiServiceHandler(mHandlerThread.getLooper());
public void setProxyConfiguration(ProxyConfiguration[] proxyConfigurations) {
if (mContext.checkCallingOrSelfPermission(ENTERPROID_PERMISSION) != PackageManager.PERMISSION_GRANTED) {
if(DEBUG_LOADERS) Log.e(TAG, “setProxyConfiguration() no permission”);
return;
}
if (proxyConfigurations != null) {
if(DEBUG_LOADERS) Log.d(TAG, “setProxyConfiguration()”);
mProxyConfgs = new ProxyConfiguration[proxyConfigurations.length];
for (int i = 0; i < mProxyConfgs.length; i++) {
ProxyConfiguration pc = proxyConfigurations[i];
// TODO: Invoke the framework api existed in the Android 3.0
switch (pc.getProxyScope()) {
case SSID: {
setWifiProxyConfiguration(pc);
}
break;
default:
break;
}
}
} else {
mProxyConfgs = null;
}
}
private void setWifiProxyConfiguration(ProxyConfiguration[] proxyconfiguration) {
for (ProxyConfiguration pcf : proxyconfiguration) {
setWifiProxyConfiguration(pcf);
}
}
private void setWifiProxyConfiguration(ProxyConfiguration proxyconfiguration) {
if(DEBUG_LOADERS) Log.d(TAG, “setWifiProxyConfiguration()”);
// if isnot SSID ,return out
if (proxyconfiguration.getProxyScope() != ProxyConfiguration.ProxyScope.SSID) {
if(DEBUG_LOADERS) Log.d(TAG, “proxyconfiguration.getProxyScope() != ProxyConfiguration.ProxyScope.SSID”);
return;
}
WifiManager wm = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
wm.asyncConnect(mContext, mHandler);// DELL, deanye@cienet.com.cn, 5/16/2011, ENTERPROID-111
// add a WifiConfig
List<WifiConfiguration> wifiConfiguredNetworks = wm.getConfiguredNetworks();
for (final WifiConfiguration w : wifiConfiguredNetworks) {
if(DEBUG_LOADERS) Log.i(TAG, “” + w.SSID + “:” + proxyconfiguration.getNetworkId());
if (w.SSID.equals(“”” + proxyconfiguration.getNetworkId() + “””)) {
if(DEBUG_LOADERS) Log.i(TAG, “w.SSID.equals(proxyconfiguration.getNetworkId()):”
+ wifiConfiguredNetworks.indexOf(w));
WifiConfiguration wc = wifiConfiguredNetworks
.get(wifiConfiguredNetworks.indexOf(w));
ProxyProperties proxyproperties = new ProxyProperties(proxyconfiguration
.getProxyURL(), proxyconfiguration.getProxyPort(), proxyconfiguration
.getUrlExclusionList());
LinkProperties linkProperties = new LinkProperties();
linkProperties.clear();
linkProperties.setHttpProxy(proxyproperties);
wc.linkProperties = new LinkProperties(linkProperties);
wc.proxySettings = ProxySettings.STATIC;
if(DEBUG_LOADERS) Log.i(TAG, “mWifiConfiguration.linkProperties.setHttpProxy(proxyproperties);” + wc);
wm.saveNetwork(wc);
}
}
}
靜默安裝的code:
Java代碼
public void installDividePackage(Uri uri, String packageName) {
if (mContext.checkCallingOrSelfPermission(ENTERPROID_PERMISSION) != PackageManager.PERMISSION_GRANTED) {
Log.e(TAG, “installDividePackage() no permission”);
return;
}
PackageUriPair packageUriPair = new PackageUriPair();
packageUriPair.packageName = packageName;
packageUriPair.uri = uri;
Message msg = Message.obtain();
msg.obj = packageUriPair;
mInstallDivideHandler.sendMessage(msg);
}
private class PackageUriPair {
public String packageName;
public Uri uri;
}
private class DividePackageInstallObserver extends IPackageInstallObserver.Stub {
public void packageInstalled(String packageName, int returnCode) {
if (returnCode == PackageManager.INSTALL_SUCCEEDED) {
// Start Divide Activation again
Intent intent = new Intent();
intent.setComponent(new ComponentName(ETP_CORE_PKG_NAME, ETP_CORE_ACTIVATY_NAME));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
} else if (returnCode == PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE) {
Log.i(TAG, “no enough storage”);
} else {
Log.i(TAG, “unexpected error”);
}
}
}
private Handler mInstallDivideHandler = new Handler() {
public void handleMessage(Message msg) {
PackageUriPair packageUriPair = (PackageUriPair) msg.obj;
String packageName = packageUriPair.packageName;
Uri uri = packageUriPair.uri;
PackageManager pm = mContext.getPackageManager();
pm.installPackage(uri, new DividePackageInstallObserver(),
PackageManager.INSTALL_REPLACE_EXISTING, packageName);
}
};