Android Open Source - twawm2 State Machine






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;
/*  w w  w  .j  av a2 s .c  o m*/
import android.content.Context;

import com.appspot.afnf4199ga.twawm.app.BackgroundService;
import com.appspot.afnf4199ga.twawm.app.BackgroundService.ConnectivityState;
import com.appspot.afnf4199ga.twawm.app.DefaultWidgetProvider;
import com.appspot.afnf4199ga.twawm.app.UIAct;
import com.appspot.afnf4199ga.twawm.router.RouterInfo;
import com.appspot.afnf4199ga.twawm.router.RouterInfo.COM_TYPE;
import com.appspot.afnf4199ga.utils.AndroidUtils;
import com.appspot.afnf4199ga.utils.Logger;
import com.appspot.afnf4199ga.utils.MyStringUtlis;

import net.afnf.and.twawm2.R;

import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

public class StateMachine {

    // STATE
    private STATE current = null;
    private STATE prevState1 = null;

    // ??
    private NETWORK_STATE netState;
    private int antennaLevel;
    private int batteryLevel;
    private int apConnFailedCount;
    private long lockTextUntil;
    private int notifyImageId;
    private String triggerName;
    private int wdImageId;
    private String wdText;
    private long lockRouterSwitchUntil;
    private COM_TYPE comState;
    private COM_TYPE comSetting;
    private Boolean wifiSpotEnabled;

    // ????
    protected final int RATE_SIZE = 5;
    protected double currentRate = -1;
    protected double averageRate = -1;
    protected List<Double> pastRates = new ArrayList<Double>();
    protected long startTime = -1;
    protected int startBattLevel = -1;
    protected long prevUpdateTime = -1;
    private static final int BATT_SAVE_THRESHOLD = 15 * 60 * 1000 + 1; // 15?
    private static final double BATT_RATE_THRESHOLD_MIN = 4; // 4%/h ? 25h
    private static final double BATT_RATE_THRESHOLD_MAX = 25; // 25%/h ? 4h

    public static enum STATE {
        /** online */
        ONLINE,
        /** offline */
        OFFLINE,
        /** notWM */
        NOT_WM,
        /** AP???? */
        AP_NOT_FOUND,
        /** wifi?? */
        WIFI_DISABLED,
        /** BT?????? */
        BT_DISABLE_WAIT,
        /** BT?????? */
        BT_ENABLE_WAIT,
        /** BT?????? */
        BT_CONN_WAIT,
        /** wifi?????? */
        WIFI_ENABLE_WAIT,
        /** supplicant?????? */
        SUPPLICANT_ENABLE_WAIT,
        /** online???? */
        ONLINE_WAIT,
        /** ?????wifi?????? */
        RECCONECT_WIFI_DISABLE_WAIT,
        /** ?????????? */
        STANDBY_WAIT,
        /** wifi?????? */
        WIFI_DISABLE_WAIT;

        public static STATE ordinalOf(int ordinal) {
            Iterator<STATE> ite = EnumSet.allOf(STATE.class).iterator();
            while (ite.hasNext()) {
                STATE e = ite.next();
                if (e.ordinal() == ordinal) {
                    return e;
                }
            }
            return null;
        }
    }

    public enum NETWORK_STATE {
        AP_NOT_FOUND, NOT_WM_ROUTER, OFFLINE, ONLINE
    }

    public static enum TRIGGER {
        /** ?? */
        BOOT,
        /** ?????? + wifi ON */
        BUTTON_BT_RESUME,
        /** wifi ON */
        BUTTON_WIFI_ON,
        /** wifi OFF */
        BUTTON_WIFI_OFF,
        /** BT???? */
        BT_DISABLED,
        /** BT???? */
        BT_ENABLED,
        /** BT???? */
        BT_CONNECTED,
        /** BC?W_Enabled */
        BC_WIFI_ENABLED,
        /** BC?S_Completed */
        BC_SUPPLICANT_COMPLETE,
        /** online */
        ONLINE,
        /** offline */
        OFFLINE,
        /** notWM */
        NOT_WM,
        /** AP???? */
        AP_NOT_FOUND,
        /** watchdog?????? */
        WATCHDOG_TIMEOUT,
        /** ??????????? */
        BUTTON_STANDBY,
        /** ????????? */
        STANDBY_OK,
        /** ???????? */
        STANDBY_NG,
        /** supplicant?? */
        BC_SUPPLICANT_DISCONNECTED,
        /** BC?W_disanabling */
        BC_WIFI_DISABLING,
        /** BC?W_disanabled */
        BC_WIFI_DISABLED;

        public static TRIGGER ordinalOf(int ordinal) {
            Iterator<TRIGGER> ite = EnumSet.allOf(TRIGGER.class).iterator();
            while (ite.hasNext()) {
                TRIGGER e = ite.next();
                if (e.ordinal() == ordinal) {
                    return e;
                }
            }
            return null;
        }
    }

    public static final String[] STATE_ARRAY = {
            "ay-d-----0123-c--a-4",
            "ay-d-----0123-c--a-4",
            "ay-d----a0123------4",
            "-5-d----a0123------4",
            "-y8----9------------",
            "----6-8-----------d-",
            "-----78-----------d-",
            "------8-----------d-",
            "-------9----------d-",
            "--------a----z----d-",
            "---------012zz-----4",
            "-------------4-----8",
            "-------------3-x3--4",
            "-------------4-----4"};

    public static final String[] ACTION_ARRAY = {
            "CY-D-----WW##-S--C-A",
            "CY-D-----WW##-S--C-A",
            "CY-D----CWW##------A",
            "-M-D----CWW##------A",
            "-YE----P------------",
            "----N-E-----------#-",
            "-----BE-----------#-",
            "------E-----------#-",
            "-------P----------#-",
            "--------C----Z----#-",
            "---------WW#ZZ-----A",
            "-------------D-----E",
            "-------------#-X#--A",
            "-------------#-----A"};

    public static final int[] ANNTENA_LEVEL_BY_STATE = {
            //
            0, // online??????????????????
            -1, // offline??????????????????
            -2, // notWM??????????????????
            -2, // AP??????????????????????
            -2, // wifi??
            -2, // BT??????
            -2, // BT??????
            -2, // BT??????
            -2, // wifi??????
            -2, // supplicant??????
            -2, // online????
            -2, // ?????wifi??????
            -1, // ??????????
            -2 // wifi??????
    };

    public static final int[] WD_TEXT_ARRAY_BY_STATE = {
            //
            R.string.online, // online
            R.string.offline, // offline
            R.string.not_wm, // notWM
            R.string.no_ap, // AP????
            R.string.wifi_off, // wifi??
            R.string.processing1, // BT??????
            R.string.processing1, // BT??????
            R.string.processing1, // BT??????
            R.string.processing2, // wifi??????
            R.string.processing3, // supplicant??????
            R.string.processing4, // online????
            R.string.processing, // ?????wifi??????
            R.string.processing, // ??????????
            R.string.processing // wifi??????
    };

    public static final int[] WD_TEXT_ARRAY_BY_TRIGGER = {
            //
            -1, // ?? 
            -1, // ?????? + wifi ON
            -1, // wifi ON
            -1, // wifi OFF
            -1, // BT????
            -1, // BT????
            -1, // BT????
            -1, // BC?W_Enabled
            -1, // BC?S_Completed
            -1, // online
            -1, // offline
            R.string.not_wm, // notWM
            -1, // AP????????????????????????????????
            R.string.watchdog_timeout, // watchdog??????
            -1, // ???????????
            R.string.standby_ok, // ?????????
            R.string.standby_ng, // ????????
            -1, // "BC?S_disconected
            -1, // BC?W_disanabling
            -1 // BC?W_disanabled
    };

    public static final int[] NOTIF_TEXT_ARRAY_BY_TRIGGER = {
            //
            -1, // ??
            -1, // ?????? + wifi ON
            -1, // wifi ON
            -1, // wifi OFF
            -1, // BT????
            -1, // BT????
            -1, // BT????
            -1, // BC?W_Enabled
            -1, // BC?S_Completed
            -1, // online
            -1, // offline
            R.string.not_wm_long, // notWM
            -1, // AP????????????????????????????????
            R.string.watchdog_timeout_long, // watchdog??????
            -1, // ???????????
            R.string.standby_ok_long, // ?????????
            R.string.standby_ng_long, // ????????
            -1, // "BC?S_disconected
            -1, // BC?W_disanabling
            -1 // BC?W_disanabled
    };

    public void init(boolean wifiEnable) {

        BackgroundService service = BackgroundService.getInstance();
        if (service == null) {
            Logger.e("service is null on StateMachine.init");
            return;
        }

        if (wifiEnable == false) {
            setState(STATE.WIFI_DISABLED);
        }
        else {
            ConnectivityState connectivityState = service.getConnectivityState();
            if (connectivityState == ConnectivityState.NONE) {
                setState(STATE.AP_NOT_FOUND); // SUPWAIT?????????????watchdog??????????????
            }
            else {
                setState(STATE.OFFLINE);
            }
        }

        this.apConnFailedCount = 0;
        this.antennaLevel = -2;
        this.batteryLevel = -1;
        this.lockTextUntil = -1;
        this.notifyImageId = R.drawable.ntficon_wimax_gray_batt_na;
        this.triggerName = service.getString(R.string.processing);
        this.wdImageId = R.drawable.icon_wimax_gray_batt_na;
        this.wdText = service.getString(wifiEnable ? R.string.processing : R.string.wifi_off);
        this.lockRouterSwitchUntil = -1;
        this.comState = COM_TYPE.NA;
        this.comSetting = COM_TYPE.NA;
        this.wifiSpotEnabled = null;
    }

    private static HashMap<Character, Integer> stateCharMap = new HashMap<Character, Integer>();

    static {
        stateCharMap.put('0', 0);
        stateCharMap.put('1', 1);
        stateCharMap.put('2', 2);
        stateCharMap.put('3', 3);
        stateCharMap.put('4', 4);
        stateCharMap.put('5', 5);
        stateCharMap.put('6', 6);
        stateCharMap.put('7', 7);
        stateCharMap.put('8', 8);
        stateCharMap.put('9', 9);
        stateCharMap.put('a', 10);
        stateCharMap.put('b', 11);
        stateCharMap.put('c', 12);
        stateCharMap.put('d', 13);
    }

    public void perform(TRIGGER trigger) {
        BackgroundService service = BackgroundService.getInstance();
        if (service == null) {
            Logger.e("service is null on StateMachine.perform");
            return;
        }

        int currentInt = getState().ordinal();
        int triggerInt = trigger.ordinal();
        triggerName = trigger.name();
        char action = ACTION_ARRAY[currentInt].charAt(triggerInt);
        char nextState = STATE_ARRAY[currentInt].charAt(triggerInt);
        int wdTextInt = WD_TEXT_ARRAY_BY_TRIGGER[triggerInt];

        int notifyTextInt = NOTIF_TEXT_ARRAY_BY_TRIGGER[triggerInt];
        String notifyText = null;
        if (notifyTextInt != -1) {
            notifyText = service.getString(notifyTextInt);
        }

        Integer nextStateInt = null;

        // ???????????
        if (trigger == TRIGGER.BUTTON_BT_RESUME && nextState == 'y' && action == 'Y') {

            // BT???
            String bluetoothAddress = Const.getPrefBluetoothAddress(service);
            if (BluetoothHelper.isValidBluetoothAddress(bluetoothAddress)) {
                nextStateInt = STATE.BT_DISABLE_WAIT.ordinal(); // BT??????
                action = 'M';
            }
            // Wifi???
            else {
                nextStateInt = STATE.WIFI_ENABLE_WAIT.ordinal(); // wifi??????
                action = 'E';
            }
        }
        else if ((trigger == TRIGGER.WATCHDOG_TIMEOUT || trigger == TRIGGER.AP_NOT_FOUND) && nextState == 'z' && action == 'Z') {

            // ????
            Logger.i("apConnFailedCount = " + apConnFailedCount);
            if (apConnFailedCount++ < Const.getPrefApConnRetryLimit(service)) {
                nextStateInt = STATE.RECCONECT_WIFI_DISABLE_WAIT.ordinal(); // ?????wifi??????
                action = 'D';

                // ????????WATCHDOG_TIMEOUT???wdtext??????????????????
                if (trigger == TRIGGER.WATCHDOG_TIMEOUT) {
                    wdTextInt = -1;
                }
            }

            // ???????
            else {
                apConnFailedCount = 0;

                // ?????????????
                wdTextInt = R.string.no_ap;
                notifyText = service.getString(R.string.ap_not_found_long);

                // ????????????????wifi???????????
                if (Const.getPrefDisableWifiWhenApConnFailed(service)) {
                    nextStateInt = STATE.WIFI_DISABLE_WAIT.ordinal(); // wifi??????
                    action = 'D';
                }
                else {
                    nextStateInt = STATE.AP_NOT_FOUND.ordinal(); // AP????
                    action = '#';
                }
            }
        }
        else if (trigger == TRIGGER.STANDBY_OK && nextState == 'x' && action == 'X') {

            // ?????????????WiFi?????
            if (Const.isPrefActionAfterSuspendWifiOFF(service)) {
                nextStateInt = STATE.WIFI_DISABLE_WAIT.ordinal(); // wifi??????
                action = 'D';
                // WiFi????????????????????????
            }
            // ????
            else {
                nextStateInt = STATE.AP_NOT_FOUND.ordinal(); // AP????
                action = '#';

                // ?????????????
                if (Const.isPrefActionAfterSuspendStopService(service)) {
                    service.stopServiceWithDelay();
                }
            }
        }

        Logger.i("StateMachine perform : " + currentInt + "," + triggerInt + "," + action + ","
                + (nextStateInt != null ? String.valueOf(nextStateInt) : nextState));

        if (nextStateInt == null) {
            nextStateInt = stateCharMap.get(nextState);
            if (nextStateInt == null) {
                return;
            }
        }

        if (action != '-') {

            // ??????????WifiOFF
            boolean stanbyToWifioff = prevState1 == STATE.STANDBY_WAIT && nextStateInt == STATE.WIFI_DISABLED.ordinal();

            // ????
            setState(STATE.ordinalOf(nextStateInt));

            // ????????????????????????????????????
            if (isOnlineCheckableState() == false) {
                antennaLevel = ANNTENA_LEVEL_BY_STATE[nextStateInt];
                notifyImageId = IconSelector.selectNotifyIcon(antennaLevel, batteryLevel, netState, comState);
                wdImageId = IconSelector.selectWdIcon(antennaLevel, batteryLevel, netState, comState);
            }

            // ?????
            if (wdTextInt != -1) {
                lockText();
                wdText = service.getString(wdTextInt);
                // ??
                service.postNotify(notifyImageId, notifyText);
            }
            // ????
            else if (isTextLocked() == false) {
                // online??????????????????????????????????????????????????????????
                if (getState() != STATE.ONLINE) {
                    wdText = service.getString(WD_TEXT_ARRAY_BY_STATE[nextStateInt]);
                }
            }

            // UI??
            reflesh(stanbyToWifioff);

            switch (action) {
                case 'E':
                    service.enableWifi();
                    break;

                case 'C':
                    service.checkOnline();
                    break;

                case 'W':
                    service.checkOnlineWithDelay();
                    break;

                case 'D':
                    service.disableWifi();
                    break;

                case 'S':
                    service.standby();
                    break;

                case 'P':
                    service.waitSupplicantComplete();
                    break;

                case 'A':
                    service.wifiDisabled();
                    break;

                case 'M':
                    service.disableBT();
                    break;

                case 'N':
                    service.enableBT();
                    break;

                case 'B':
                    service.bluetoothResume();
                    break;

                case '#':
                    // do nothing
                    break;
            }
        }
    }

    public void reflesh(boolean donotUpdateWidget) {
        BackgroundService service = BackgroundService.getInstance();
        if (service == null) {
            Logger.e("service is null on StateMachine.reflesh");
            return;
        }

        if (donotUpdateWidget == false) {
            DefaultWidgetProvider.update(service);
        }

        UIAct.postActivityInfo(wdImageId, wdText, triggerName, getState().name());
        boolean wifiEnabled = AndroidUtils.isWifiEnabled(service.getWifi());
        boolean suppCompleted = wifiEnabled && service.isSupplicantCompleted();
        UIAct.postActivityButton(isStableState(), isStableState(), wifiEnabled, suppCompleted, service.getEcoCharge(),
                comSetting, wifiSpotEnabled);
    }

    public void setStateToWifiRestart() {
        BackgroundService service = BackgroundService.getInstance();
        if (service == null) {
            Logger.e("service is null on StateMachine.setStateToWifiRestart");
            return;
        }

        batteryLevel = -1;
        apConnFailedCount = 0;
        setState(STATE.RECCONECT_WIFI_DISABLE_WAIT);
        wdText = service.getString(R.string.processing);
        wdImageId = R.drawable.icon_wimax_gray_batt_na;
        notifyImageId = R.drawable.ntficon_wimax_gray_batt_na;

        comState = COM_TYPE.NA;
        comSetting = COM_TYPE.NA;
        wifiSpotEnabled = null;

        service.disableWifi();
        reflesh(false);
    }

    public void onOnlineCheckFinished(boolean inetReachable, boolean routerReachable, RouterInfo routerInfo) {

        // ??????????????????????????????????????????
        if (isOnlineCheckAcceptableState() == false) {
            Logger.i("onOnlineCheckFinished skipped");
            return;
        }

        BackgroundService service = BackgroundService.getInstance();
        if (service == null) {
            Logger.e("service is null on StateMachine.onOnlineCheckFinished");
            return;
        }

        boolean prevOnline = netState == NETWORK_STATE.ONLINE;

        // antennaLevel??
        antennaLevel = -2;
        if (routerInfo != null) {
            antennaLevel = routerInfo.antennaLevel;
        }

        // ???????Wi-Fi???????
        comState = COM_TYPE.NA;
        if (routerInfo != null) {
            if (routerInfo.comState != COM_TYPE.NA) {
                comState = routerInfo.comState;
            }
            if (routerInfo.nad) {
                if (routerInfo.comSetting != COM_TYPE.NA) {
                    comSetting = routerInfo.comSetting;
                }
                if (routerInfo.wifiSpotEnabled != null) {
                    wifiSpotEnabled = routerInfo.wifiSpotEnabled;
                }
            }
        }

        // ConnectivityState.COMPLETE_WIFI??????AP????
        ConnectivityState connectivityState = service.getConnectivityState();
        if (connectivityState != ConnectivityState.COMPLETE_WIFI) {
            netState = NETWORK_STATE.AP_NOT_FOUND;
        }
        else {
            if (routerReachable == false) {
                netState = NETWORK_STATE.NOT_WM_ROUTER;
            }
            else {
                // inetReachable????????????1???????????????????????????
                if (inetReachable && antennaLevel >= 1) {
                    netState = NETWORK_STATE.ONLINE;
                }
                else {
                    netState = NETWORK_STATE.OFFLINE;
                }
            }
        }

        boolean becomeOnline = false;
        if (prevOnline == false && netState == NETWORK_STATE.ONLINE) {
            becomeOnline = true;
        }

        // online???
        TRIGGER nextTrigger;
        String notifyText;
        if (netState == NETWORK_STATE.AP_NOT_FOUND) {
            notifyText = service.getString(R.string.no_ap);
            nextTrigger = TRIGGER.AP_NOT_FOUND;
        }
        else {
            apConnFailedCount = 0;

            if (netState == NETWORK_STATE.NOT_WM_ROUTER) {
                notifyText = service.getString(R.string.not_wm);
                nextTrigger = TRIGGER.NOT_WM;
            }
            else if (netState == NETWORK_STATE.OFFLINE) {
                notifyText = service.getString(R.string.offline);
                nextTrigger = TRIGGER.OFFLINE;
            }
            else { // netState == NETWORK_STATE.ONLINE
                notifyText = service.getString(R.string.online);
                nextTrigger = TRIGGER.ONLINE;
            }
        }

        // ??????
        long now = System.currentTimeMillis();
        String remain = "";
        if (routerInfo != null && routerInfo.charging == false) {
            updateBattCalc(routerInfo.battery, now, service);
            remain = createRemainText(routerInfo.battery, averageRate);
        }
        else {
            stopBattCalc(service, now);
        }

        if (routerInfo != null) {
            // N/A???????????????
            if (routerInfo.comState != COM_TYPE.NA) {
                notifyText += "(" + routerInfo.comState.toString() + ")";
            }
        }

        // ????????wdText???
        batteryLevel = -1;
        String battNotifyText = "";
        if (isTextLocked() == false) {
            if (routerInfo == null) {
                wdText = "batt=N/A";
                battNotifyText = wdText;
            }
            else {
                batteryLevel = routerInfo.battery;
                if (MyStringUtlis.isEmpty(remain)) {
                    wdText = "batt=" + routerInfo.getBatteryText();
                    battNotifyText = wdText;
                }
                else {
                    wdText = routerInfo.getBatteryText() + remain;
                    battNotifyText = "batt=" + wdText;
                }
            }
        }
        notifyText += ", " + battNotifyText;

        // ????????
        int max = 6; // WM???????max6
        if (routerInfo != null && routerInfo.nad) {
            if (comState == COM_TYPE.HIGH_SPEED) {
                max = 4;
            }
            if (comState == COM_TYPE.NO_LIMIT || comState == COM_TYPE.WIFI_SPOT) {
                max = 5;
            }
        }
        if (0 <= antennaLevel && antennaLevel <= 6) {
            notifyText += ", ant=" + antennaLevel + "/" + max;
        }
        else {
            notifyText += ", ant=N/A";
        }

        // ???????
        notifyImageId = IconSelector.selectNotifyIcon(antennaLevel, batteryLevel, netState, comState);
        wdImageId = IconSelector.selectWdIcon(antennaLevel, batteryLevel, netState, comState);

        // ????????????????????
        service.onOnlineCheckComplete(nextTrigger, becomeOnline);

        // ????????????????????????????????????perform??????????????????????OFF???noAP??????????????????????????????????????????????
        if (isTextLocked() == false) {
            service.postNotify(notifyImageId, notifyText);
        }

        // UI??
        reflesh(false);
    }

    protected static String createRemainText(int battery, double rate) {

        if (rate == 0) {
            return "";
        }

        double remain = (double) battery / rate;
        String remainText = "(";
        if (remain >= 100) {
            remainText += "99h)";
        }
        else if (remain >= 10) {
            remainText += (int) remain + "h)";
        }
        else if (remain >= 1.6) { // 96
            remainText += (Math.round(remain * 10.0) / 10.0) + "h)";
        }
        else if (remain > 0) {
            remainText += (int) Math.round(remain * 60.0) + "m)";
        }
        else {
            remainText = "";
        }

        return remainText;
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////

    public synchronized void startBattCalc(Context context) {
        currentRate = -1;
        averageRate = -1;
        startTime = -1;
        startBattLevel = -1;
        prevUpdateTime = -1;

        // ??
        if (context != null) {
            pastRates = Const.getPrefBattRates(context);
        }

        // averageRate??
        updateAverage(null);
    }

    protected synchronized void updateBattCalc(int battery, long now, Context context) {

        if (startBattLevel == -1) {
            startBattCalc(context);
            startBattLevel = battery;
        }
        else {
            if (startTime == -1 && startBattLevel > battery) {
                startTime = now;
                startBattLevel = battery;
                prevUpdateTime = now;
            }
            else if (startTime != -1) {

                long deltaTimeMs = now - startTime;
                int deltaBattLevel = startBattLevel - battery;
                if (deltaTimeMs > 0 && deltaBattLevel > 0) {
                    currentRate = (double) deltaBattLevel / deltaTimeMs * 1000 * 60 * 60;

                    // averageRate??
                    updateAverage(currentRate);

                    Logger.v("startBattLevel=" + startBattLevel + ", currentRate=" + MyStringUtlis.round3(currentRate)
                            + ", averageRate=" + MyStringUtlis.round3(averageRate) + ", pastRates=" + pastRates.size());

                    // ??????????15?????????????????????
                    if (now - prevUpdateTime > BATT_SAVE_THRESHOLD) {
                        savePastRates(context, now);
                    }
                }
            }
        }
    }

    public synchronized void stopBattCalc(Context context) {
        stopBattCalc(context, System.currentTimeMillis());
    }

    public synchronized void stopBattCalc(Context context, long now) {

        if (startTime != -1) {
            savePastRates(context, now);
        }

        currentRate = -1;
        averageRate = -1;
        startTime = -1;
        startBattLevel = -1;
        prevUpdateTime = -1;
    }

    protected void savePastRates(Context context, long now) {

        if (BATT_RATE_THRESHOLD_MIN <= currentRate && currentRate <= BATT_RATE_THRESHOLD_MAX) {
            prevUpdateTime = now;

            int size = pastRates.size();
            if (size >= RATE_SIZE) {
                pastRates = pastRates.subList(size - RATE_SIZE + 1, size);
            }
            pastRates.add(currentRate);

            // ???
            if (context != null) {
                Const.updatePrefBattRates(context, pastRates);
            }
        }
    }

    protected void updateAverage(Double currentRate) {
        int size = pastRates.size();
        averageRate = 0;
        if (currentRate != null && BATT_RATE_THRESHOLD_MIN <= currentRate.doubleValue()
                && currentRate.doubleValue() <= BATT_RATE_THRESHOLD_MAX) {
            size++;
            averageRate = currentRate;
        }
        if (size > 0) {
            // averageRate??
            for (Double d : pastRates) {
                averageRate += d;
            }
            averageRate /= size;
        }
    }

    public static void resetBatt(Context context) {
        BackgroundService service = BackgroundService.getInstance();
        if (service != null) {
            StateMachine sm = service.getStateMachine();
            sm.pastRates = new ArrayList<Double>();
            sm.startTime = -1; // save????????????????????????????-1???????
            sm.stopBattCalc(context);
        }
        Const.updatePrefBattRates(context, null);
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////

    public void resetTextLock() {
        lockTextUntil = -1;
    }

    public void lockText() {
        lockTextUntil = System.currentTimeMillis() + Const.WD_TEXT_LOCK_TIME_MS;
    }

    public boolean isTextLocked() {
        return lockTextUntil > System.currentTimeMillis();
    }

    public void resetRouterSwitchLock() {
        lockRouterSwitchUntil = -1;
    }

    public void lockRouterSwitch() {
        lockRouterSwitchUntil = System.currentTimeMillis() + Const.ROUTER_SWITCH_LOCK_TIME_MS;
    }

    public boolean isRouterSwitchLocked() {
        return lockRouterSwitchUntil > System.currentTimeMillis();
    }

    public int getWdImageId() {
        return wdImageId;
    }

    public String getWdText() {
        return wdText;
    }

    public boolean isSupplicantEnableWaitState() {
        return current == STATE.NOT_WM || current == STATE.AP_NOT_FOUND || current == STATE.SUPPLICANT_ENABLE_WAIT;
    }

    public boolean isStandbyWaitState() {
        return current == STATE.STANDBY_WAIT;
    }

    public boolean isStableState() {
        return current == STATE.ONLINE || current == STATE.OFFLINE || current == STATE.NOT_WM || current == STATE.AP_NOT_FOUND
                || current == STATE.WIFI_DISABLED;
    }

    public boolean isOnlineCheckableState() {
        return current == STATE.ONLINE || current == STATE.OFFLINE;
    }

    private boolean isOnlineCheckAcceptableState() {
        return current == STATE.ONLINE || current == STATE.OFFLINE || current == STATE.NOT_WM || current == STATE.AP_NOT_FOUND
                || current == STATE.ONLINE_WAIT;
    }

    public boolean isWmReachableState() {
        return current == STATE.ONLINE || current == STATE.OFFLINE || current == STATE.ONLINE_WAIT;
    }

    private void setState(STATE state) {
        prevState1 = current;
        current = state;
    }

    public STATE getState() {
        return current;
    }

    public boolean isOnline() {
        return netState == NETWORK_STATE.ONLINE; // NETWORK_STATE.NOT_WM_ROUTER???????????????????? 
    }

    public void setNetStateToNoAP() {
        netState = NETWORK_STATE.AP_NOT_FOUND;
    }

    public void setOfflineTemporarily() {
        netState = NETWORK_STATE.OFFLINE;
        batteryLevel = -1;
        current = STATE.OFFLINE;
        wdImageId = R.drawable.icon_wimax_white_batt_na;
        comState = COM_TYPE.NA;
        comSetting = COM_TYPE.NA;
        wifiSpotEnabled = null;

        // UI??
        reflesh(false);
    }

    public boolean isComSettingHS() {
        return comSetting == COM_TYPE.HIGH_SPEED;
    }

    public boolean isWifiSpotEnabled() {
        return wifiSpotEnabled != null && wifiSpotEnabled == true;
    }

    //  public int getNotifyImageId() {
    //    return notifyImageId;
    //  }
    //  public String getNotifyText() {
    //    return notifyText;
    //  }
}




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