Android Open Source - twawm2 Online Checker






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;
//from  ww w .ja  v a  2s .c om
import java.net.InetAddress;

import com.appspot.afnf4199ga.twawm.app.BackgroundService;
import com.appspot.afnf4199ga.twawm.app.BackgroundService.ConnectivityState;
import com.appspot.afnf4199ga.twawm.router.RouterControl;
import com.appspot.afnf4199ga.twawm.router.RouterControlByHttp;
import com.appspot.afnf4199ga.twawm.router.RouterInfo;
import com.appspot.afnf4199ga.utils.AndroidUtils;
import com.appspot.afnf4199ga.utils.Logger;

public class OnlineChecker extends Thread {

    private boolean executing = false;
    private long delay_ms;
    private Boolean inetReachable;
    private Boolean routerReachable;
    private RouterInfo routerInfo;

    public OnlineChecker(long delay_ms) {
        this.delay_ms = delay_ms;
    }

    @Override
    public void run() {
        try {
            executing = true;
            Thread.sleep(delay_ms);

            final BackgroundService service = BackgroundService.getInstance();
            if (service == null) {
                Logger.e("service is null on OnlineChecker.run");
                return;
            }

            // ????????????????????????????wakelock???????????????
            if (service.isScreenOn() || service.isWakeLocked()) {

                // ???????????????
                ConnectivityState connectivityState = service.getConnectivityState();
                if (connectivityState == ConnectivityState.NONE) {
                    Logger.w("OnlineChecker ConnectivityState.NONE");
                    service.getStateMachine().onOnlineCheckFinished(false, false, null);
                }
                else {
                    // ??????????
                    if (connectivityState == ConnectivityState.COMPLETE_WIFI) {
                        RouterControl.execFetchInfo(this);
                    }
                    else {
                        onRouterInfoFetched(-1, null);
                    }

                    // Watchdoc??
                    new Thread(new Runnable() {
                        public void run() {
                            AndroidUtils.sleep(Const.getPrefOnlineCheckDnsTimeoutMs(service));
                            onCompleteParticialy(TYPE.WATCHDOG);
                        }
                    }).start();

                    // ??????????
                    String hostname = HostnameList.getNext();
                    try {
                        InetAddress address = InetAddress.getByName(hostname);
                        inetReachable = false;
                        // Watchdoc????????????????????????executing == false???????
                        if (executing == false) {
                            Logger.i("dns lookup timeout, name=" + hostname);
                        }
                        else if (address != null) {
                            inetReachable = true;
                        }
                    }
                    catch (Throwable e) {
                        inetReachable = false;
                        Logger.i("dns lookup failed, name=" + hostname);
                    }

                    // ????????????????????
                    onCompleteParticialy(TYPE.DNS);
                }
            }
            else {
                Logger.i("screenOff, OnlineCheck skipped");
            }
        }
        catch (InterruptedException e) {
            // interrupt?????????????
        }
        catch (Throwable e) {
            // do nothing
            Logger.e("OnlineChecker error", e);
        }
    }

    enum TYPE {
        ROUTER, DNS, WATCHDOG
    }

    private synchronized void onCompleteParticialy(TYPE type) {
        //Logger.v("OnlineChecker onCompleteParticialy, type=" + type + ", exe=" + executing);

        // ???????????????
        if (type == TYPE.WATCHDOG && inetReachable == null) {
            inetReachable = false;
        }

        // ???????????????
        if (routerReachable != null && inetReachable != null) {

            // ?????????????????????????????executing==false???????
            if (executing) {
                executing = false;
                Logger.i("OnlineChecker finished, inet=" + inetReachable + ", router=" + routerReachable);

                BackgroundService service = BackgroundService.getInstance();
                if (service == null) {
                    Logger.e("service is null on OnlineChecker.onCompleteParticialy");
                    return;
                }
                service.getStateMachine().onOnlineCheckFinished(inetReachable, routerReachable, routerInfo);
            }
        }
    }

    public void onRouterInfoFetched(int ret, RouterInfo newRouterInfo) {
        routerReachable = ret == RouterControlByHttp.CTRL_OK;
        routerInfo = newRouterInfo;
        onCompleteParticialy(TYPE.ROUTER);
    }

    public boolean isExecuting() {
        return executing;
    }

    public void stopThread() {
        if (executing) {
            executing = false;
            interrupt();
        }
    }
}




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