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