Back to project page twawm2.
The source code is released under:
Copyright (c) 2014, afnf All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistr...
If you think the Android project twawm2 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.appspot.afnf4199ga.twawm.app; /*from ww w. j a v a2s .c om*/ import java.util.HashSet; import java.util.List; import java.util.Set; import android.content.Context; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiManager; import com.appspot.afnf4199ga.twawm.Const; import com.appspot.afnf4199ga.utils.AndroidUtils; import com.appspot.afnf4199ga.utils.Logger; import com.appspot.afnf4199ga.utils.MyStringUtlis; public class NetworkSwitcher extends Thread { private static Set<String> disabledNetworks = new HashSet<String>(); private static long btFinTime = -1; private static NetworkSwitcher instance = null; private long delay = 0; /** * ???? */ public static void init() { reset(); // BT?????? btFinTime = System.currentTimeMillis(); } /** * ???????????? * * @param context */ public static void reEnableWithDelay(Context context) { if (disabledNetworks.size() >= 1) { instance = new NetworkSwitcher(); instance.delay = calcWindow(context); // ?????? instance.start(); } } /** * ???????????? * * @param context */ public static void reEnableWithoutD() { if (disabledNetworks.size() >= 1) { if (instance != null) { instance.interrupt(); } // ?????? instance = new NetworkSwitcher(); instance.run(); } } /** * ???? */ public static void reset() { btFinTime = -1; disabledNetworks.clear(); if (instance != null) { instance.interrupt(); instance = null; } } @Override public void run() { reEnableNetwork(); } /** * AP?????????????????????????????????20??90????????????? * * @param context * @return */ public static long calcWindow(Context context) { long delay = (Const.getPrefApConnRetryLimit(context) + 1) * Const.getPrefApConnTimeoutMs(context); return Math.max(20000, Math.min(delay, 90000)); } /** * ???AP?????? * * @param service */ public static void disableNetwork(BackgroundService service) { disabledNetworks.clear(); // BT???????????????????????AP????????????????? long diff = System.currentTimeMillis() - btFinTime; Logger.v("waitSupplicantComplete diff=" + diff + ", disableOtherApAfterResume=" + Const.getPrefDisableOtherApAfterResume(service) + ", wmssid=" + Const.getPrefLastTargetSSID(service)); if (diff <= calcWindow(service) && Const.getPrefDisableOtherApAfterResume(service)) { btFinTime = -1; // WM SSID??????? String confssid = Const.getPrefLastTargetSSID(service); if (MyStringUtlis.isEmpty(confssid) == false) { // ????????????????? List<WifiConfiguration> configuredNetworks = service.getWifi().getConfiguredNetworks(); if (configuredNetworks != null && configuredNetworks.size() >= 1) { for (WifiConfiguration wc : configuredNetworks) { // SSID?????????????????? // ?WifiConfiguration#SSID????????????????????????? String wcssid = MyStringUtlis.trimQuote(wc.SSID); if (MyStringUtlis.eqauls(confssid, wcssid) == false) { Logger.v("NetworkSwitcher disableNetwork=" + wcssid); // ??????????? service.getWifi().disableNetwork(wc.networkId); // SSID??????????????? disabledNetworks.add(wcssid); } } } } } // ??????????????????? NetworkSwitcher.reEnableWithDelay(service); } /** * ???????????? */ private void reEnableNetwork() { // ???? boolean interrupt = AndroidUtils.sleep(delay); // interrupt?????????????????????? if (interrupt == false) { synchronized (NetworkSwitcher.class) { if (disabledNetworks.size() >= 1) { BackgroundService service = BackgroundService.getInstance(); if (service == null) { Logger.e("service is null on NetworkSwitcher.run#1"); return; } WifiManager wifi = service.getWifi(); // ????????????????? List<WifiConfiguration> configuredNetworks = wifi.getConfiguredNetworks(); if (configuredNetworks != null && configuredNetworks.size() >= 1) { for (WifiConfiguration wc : configuredNetworks) { // ????????????????????SSID???????????????????? // ?WifiConfiguration#SSID????????????????????????? String wcssid = MyStringUtlis.trimQuote(wc.SSID); if (disabledNetworks.contains(wcssid)) { Logger.v("NetworkSwitcher enableNetwork=" + wcssid); // ??????????? wifi.enableNetwork(wc.networkId, false); } } } disabledNetworks.clear(); } } } } }