Android Open Source - twawm2 Network Switcher






From Project

Back to project page twawm2.

License

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.

Java Source Code

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();
                }
            }
        }
    }

}




Java Source Code List

com.appspot.afnf4199ga.twawm.BluetoothHelper.java
com.appspot.afnf4199ga.twawm.Const.java
com.appspot.afnf4199ga.twawm.Const.java
com.appspot.afnf4199ga.twawm.HostnameListTest.java
com.appspot.afnf4199ga.twawm.HostnameList.java
com.appspot.afnf4199ga.twawm.IconSelectorTest.java
com.appspot.afnf4199ga.twawm.IconSelector.java
com.appspot.afnf4199ga.twawm.OnlineChecker.java
com.appspot.afnf4199ga.twawm.StateMachineTest.java
com.appspot.afnf4199ga.twawm.StateMachine.java
com.appspot.afnf4199ga.twawm.TwawmUtils.java
com.appspot.afnf4199ga.twawm.app.BackgroundServiceTest.java
com.appspot.afnf4199ga.twawm.app.BackgroundService.java
com.appspot.afnf4199ga.twawm.app.DefaultWidgetProvider.java
com.appspot.afnf4199ga.twawm.app.InfoActivity.java
com.appspot.afnf4199ga.twawm.app.InitialConfigurationWizardActivity.java
com.appspot.afnf4199ga.twawm.app.LogSendActivity.java
com.appspot.afnf4199ga.twawm.app.MainActivity.java
com.appspot.afnf4199ga.twawm.app.MainApp.java
com.appspot.afnf4199ga.twawm.app.MyPreferenceActivity.java
com.appspot.afnf4199ga.twawm.app.NetworkSwitcher.java
com.appspot.afnf4199ga.twawm.app.StaticIntentListener.java
com.appspot.afnf4199ga.twawm.app.UIAct.java
com.appspot.afnf4199ga.twawm.ctl.CustomizeActionsActivityTest.java
com.appspot.afnf4199ga.twawm.ctl.CustomizeActionsActivity.java
com.appspot.afnf4199ga.twawm.ctl.CwacTouchListView.java
com.appspot.afnf4199ga.twawm.ctl.ListItem.java
com.appspot.afnf4199ga.twawm.router.EcoModeControlTest.java
com.appspot.afnf4199ga.twawm.router.EcoModeControl.java
com.appspot.afnf4199ga.twawm.router.InetLookupWrappter.java
com.appspot.afnf4199ga.twawm.router.InetLookupWrappter.java
com.appspot.afnf4199ga.twawm.router.MyHttpClientTest.java
com.appspot.afnf4199ga.twawm.router.MyHttpClient.java
com.appspot.afnf4199ga.twawm.router.MyHttpClient.java
com.appspot.afnf4199ga.twawm.router.RouterControlByHttpTest.java
com.appspot.afnf4199ga.twawm.router.RouterControlByHttp.java
com.appspot.afnf4199ga.twawm.router.RouterControlByHttp.java
com.appspot.afnf4199ga.twawm.router.RouterControl.java
com.appspot.afnf4199ga.twawm.router.RouterControl.java
com.appspot.afnf4199ga.twawm.router.RouterInfo.java
com.appspot.afnf4199ga.twawm.router.RouterInfo.java
com.appspot.afnf4199ga.utils.AndroidUtilsTest.java
com.appspot.afnf4199ga.utils.AndroidUtils.java
com.appspot.afnf4199ga.utils.AndroidUtils.java
com.appspot.afnf4199ga.utils.Logger.java
com.appspot.afnf4199ga.utils.Logger.java
com.appspot.afnf4199ga.utils.MyStringUtlisTest.java
com.appspot.afnf4199ga.utils.MyStringUtlis.java
com.appspot.afnf4199ga.utils.MyStringUtlis.java
com.appspot.afnf4199ga.utils.MyTestUtils.java
com.appspot.afnf4199ga.utils.MyUncaughtExceptionHandler.java
com.appspot.afnf4199ga.utils.MyUncaughtExceptionHandler.java
com.appspot.afnf4199ga.wmgraph.app.FetchThread.java
com.appspot.afnf4199ga.wmgraph.app.InetLookupThread.java
com.appspot.afnf4199ga.wmgraph.app.InfoActivity.java
com.appspot.afnf4199ga.wmgraph.app.MainActivity.java
com.appspot.afnf4199ga.wmgraph.app.MyPreferenceActivity.java
com.appspot.afnf4199ga.wmgraph.app.UIAct.java
net.afnf.and.twawm2.DexmakerInstrumentationTestCase.java
net.afnf.and.twawm2.MyInstrumentationTestRunner.java