Android Open Source - twawm2 Eco Mode Control






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.router;
/*from w  w w .  ja v  a 2  s .  c  o m*/
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.appspot.afnf4199ga.twawm.Const;
import com.appspot.afnf4199ga.twawm.app.BackgroundService;
import com.appspot.afnf4199ga.twawm.router.MyHttpClient.AuthType;
import com.appspot.afnf4199ga.utils.Logger;
import com.appspot.afnf4199ga.utils.MyStringUtlis;

public class EcoModeControl extends Thread {

    public static void changeEcoMode(Boolean nextEcoCharge) {
        new EcoModeControl(nextEcoCharge).start();
    }

    protected Boolean currentEcoCharge = null;
    protected List<NameValuePair> currentValues;
    protected Boolean nextEcoCharge;

    public EcoModeControl(Boolean nextEcoCharge) {
        this.nextEcoCharge = nextEcoCharge;
    }

    public void run() {
        Logger.v("EcoModeControl started, next=" + nextEcoCharge);

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

        MyHttpClient httpClient = null;
        try {
            // ????IP?????????
            String routerIpAddr = MyHttpClient.getRouterIpAddr(new InetLookupWrappter(), Const.getPrefApIpAddr(service));
            if (routerIpAddr == null || routerIpAddr == MyHttpClient.NOT_SITE_LOCAL_ADDR) {
                Logger.w("EcoModeControl getRouterIpAddr failed");
                service.onEcoModeControlFinished(null);
                return;
            }

            // httpClient???
            httpClient = MyHttpClient.createClient(service);

            // ??????????
            {
                HttpGet method = new HttpGet("http://" + routerIpAddr + Const.ROUTER_URL_ECO_MAIN);
                if (request(httpClient, method) == false) {
                    service.onEcoModeControlFinished(null);
                    return;
                }
            }
            Logger.v("EcoModeControl current=" + currentEcoCharge);

            // ??????????????????????????????????????????????????????
            if (nextEcoCharge == null || currentEcoCharge == null || currentEcoCharge == nextEcoCharge) {
                service.onEcoModeControlFinished(currentEcoCharge);
                return;
            }

            // ????
            {
                if (nextEcoCharge) {
                    currentValues.add(new BasicNameValuePair("ECO_CHARGE", "0"));
                }

                HttpPost method = new HttpPost("http://" + routerIpAddr + Const.ROUTER_URL_ECO_POST);
                method.setEntity(new UrlEncodedFormEntity(currentValues, "UTF-8"));
                if (request(httpClient, method) == false) {
                    service.onEcoModeControlFinished(null);
                    return;
                }
            }
            Logger.v("EcoModeControl changed=" + currentEcoCharge);

            service.onEcoModeControlFinished(currentEcoCharge);
        }
        catch (Throwable e) {
            Logger.w("EcoModeControl error", e);
            service.onEcoModeControlFinished(null);
        }
        finally {
            MyHttpClient.close(httpClient);
        }
    }

    protected boolean request(MyHttpClient httpClient, HttpRequestBase method) throws Exception {

        HttpResponse response = httpClient.executeWithAuth(method, AuthType.DEFAULT);
        boolean success = false;
        int statusCode = -1;

        if (response != null && response.getStatusLine() != null) {
            statusCode = response.getStatusLine().getStatusCode();
            HttpEntity entity = response.getEntity();

            // ????
            if (entity != null && statusCode == HttpStatus.SC_OK) {
                String content = EntityUtils.toString(entity, RouterControlByHttp.getPageCharset());
                entity.consumeContent();
                success = parseContent(content);
            }
        }

        if (success == false) {
            Logger.w("EcoModeControl " + statusCode);
            MyHttpClient.discardContent(response);
        }

        return success;
    }

    protected boolean parseContent(String content) {

        currentEcoCharge = null;
        currentValues = new ArrayList<NameValuePair>();

        if (content == null) {
            Logger.w("EcoModeControl failed, content is empty");
            return false;
        }
        else if (content.indexOf("??????????????????") != -1) {
            Logger.w("EcoModeControl failed, not initialized");
            return false;
        }
        else {

            try {
                Document doc = Jsoup.parse(content);

                // input
                {
                    Elements inputs = doc.select("#eco_mode_main input");
                    if (inputs != null) {
                        Iterator<Element> iterator = inputs.iterator();
                        while (iterator.hasNext()) {
                            Element e = (Element) iterator.next();
                            String name = MyStringUtlis.normalize(e.attr("name"));

                            if (MyStringUtlis.eqauls("ECO_CHARGE", name)) {
                                name = null;
                                currentEcoCharge = MyStringUtlis.eqauls("checked", e.attr("checked"));
                            }
                            else if (MyStringUtlis.eqauls("submit", e.attr("type"))
                                    || MyStringUtlis.eqauls("button", e.attr("type"))) {
                                name = null;
                            }
                            else if (MyStringUtlis.eqauls("checkbox", e.attr("type"))
                                    && MyStringUtlis.eqauls("checked", e.attr("checked")) == false) {
                                name = null;
                            }

                            if (MyStringUtlis.isEmpty(name) == false) {
                                String value = MyStringUtlis.normalize(e.attr("value"));
                                //Logger.i(name + ":" + value);
                                currentValues.add(new BasicNameValuePair(name, value));
                            }
                        }
                    }
                }

                // select
                currentValues.addAll(RouterControlByHttp.getPulldownValues(doc, "#eco_mode_main select"));

                return true;
            }
            catch (Throwable e) {
                Logger.w("EcoModeControl parsing failed", e);
            }
        }

        return false;
    }
}




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