Android Open Source - twawm2 Initial Configuration Wizard Activity






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;
/*  ww w.  j  ava2  s  .  c o  m*/
import java.util.EnumSet;
import java.util.Iterator;

import net.afnf.and.twawm2.R;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.appspot.afnf4199ga.twawm.Const;
import com.appspot.afnf4199ga.twawm.router.RouterControlByHttp;
import com.appspot.afnf4199ga.twawm.router.RouterControlByHttp.CTRL;
import com.appspot.afnf4199ga.twawm.router.RouterInfo;
import com.appspot.afnf4199ga.utils.Logger;

public class InitialConfigurationWizardActivity extends Activity {

    private ProgressDialog progressDialog;
    private PAGE page = PAGE.INIT;
    private boolean infoBtn = false;

    enum PAGE {
        INIT, CONNECTION, INPUT_ROUTER_IP, PASSWORD_NOT_INIT, INPUT_PASSWORD1, INPUT_PASSWORD2, INVALID_PASSWORD, FIN;

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

    public void onCancel(View view) {
        if (page == PAGE.INIT) {
            Const.updatePrefStartWizardAutomatically(this, false);
        }

        MainActivity.setInitWizardDisplayed(false);
        finish();
    }

    public void onSubButton(View view) {

        switch (page) {

        case CONNECTION:
            Intent it1 = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS);
            startActivity(it1);
            break;

        case PASSWORD_NOT_INIT:
            // ??????
            Intent it2 = new Intent(Intent.ACTION_VIEW, Uri.parse("http://" + Const.ROUTER_HOSTNAME
                    + (RouterControlByHttp.isNad() ? Const.ROUTER_URL_INFO_IDXCT : "/")));
            startActivity(it2);
            break;

        default:
            // do nothing
            break;
        }
    }

    public static void startWizard(Context context) {
        Intent intent = new Intent(context, InitialConfigurationWizardActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onPause() {
        //Logger.v("WizardActivity onPause");
        super.onPause();

        // ?????????????????????
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
            progressDialog = null;
        }
    }

    @Override
    protected void onDestroy() {
        //Logger.v("WizardActivity onDestroy");
        super.onDestroy();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        //Logger.v("WizardActivity onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wizard);

        Button nextButton = (Button) findViewById(R.id.wizardNextButton);
        nextButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Context context = InitialConfigurationWizardActivity.this;

                // ???????????????????????????
                if (page == PAGE.FIN) {
                    // ?????????????
                    BackgroundService service = BackgroundService.getInstance();
                    if (service != null) {
                        service.startOnlineCheck(0);
                    }
                    // ???????
                    MainActivity.setInitWizardDisplayed(false);
                    finish();
                    return;
                }
                else {

                    // ???????????IP???
                    EditText editText = (EditText) findViewById(R.id.wizardEditText);
                    if (editText != null) {
                        if (editText.getText() != null) {
                            String editTextStr = editText.getText().toString();
                            switch (page) {
                            case INPUT_ROUTER_IP:
                                Const.updatePrefApIpAddr(context, editTextStr);
                                break;
                            case INPUT_PASSWORD1:
                            case INPUT_PASSWORD2:
                            case INVALID_PASSWORD:
                                Const.updatePrefRouterControlPassword(context, editTextStr);
                                break;
                            default:
                                break;
                            }
                        }
                    }

                    // WiFi????????????????
                    int nextInt;
                    if (isSupplicantCompleted() == false) {
                        nextInt = PAGE.CONNECTION.ordinal();

                        // ?????????
                        nextActivity(nextInt);
                    }

                    // WiFi????????????????
                    else {

                        // ProgressDialog??
                        progressDialog = new ProgressDialog(context);
                        progressDialog.setMessage(getString(R.string.wizard_dialog_loading));
                        progressDialog.setCancelable(false);
                        progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.cancel),
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                        progressDialog = null;
                                    }
                                });
                        progressDialog.show();

                        // ?????????????
                        new CheckRouterThread().start();
                    }
                }
            }
        });
    }

    @Override
    protected void onStart() {
        //Logger.v("WizardActivity onStart");
        super.onStart();
        MainActivity.setInitWizardDisplayed(true);

        // ??????????
        Intent intent = getIntent();
        int pageInt = intent.getIntExtra(Const.INTENT_EX_ACTION_SELECT, PAGE.INIT.ordinal());
        page = PAGE.ordinalOf(pageInt);
        Logger.i("InitialConfigurationWizard, page=" + page);

        int progress = 0;
        int mainTextId = R.string.wizard_text_init;
        boolean dispEditText = false;
        String editTextValue = null;
        int subButtonTextId = -1;
        int nextButtonTextId = -1;
        int cancelButtonTextId = R.string.cancel;

        switch (page) {
        case INIT:
            infoBtn = false;
            cancelButtonTextId = R.string.wizard_button_dont_start_wizard_automatically;
            break;

        case CONNECTION:
            mainTextId = R.string.wizard_text_connection;
            subButtonTextId = R.string.wizard_button_wifi;
            progress = 20;
            break;

        case INPUT_ROUTER_IP:
            mainTextId = R.string.wizard_text_input_router_ip;
            dispEditText = true;
            editTextValue = Const.getPrefApIpAddr(this);
            progress = 40;
            break;

        case PASSWORD_NOT_INIT:
            mainTextId = R.string.wizard_text_password_not_init;
            subButtonTextId = R.string.wizard_button_router;
            progress = 50;
            break;

        case INPUT_PASSWORD1:
            mainTextId = R.string.wizard_text_input_password1;
            dispEditText = true;
            progress = 60;
            break;

        case INPUT_PASSWORD2:
            mainTextId = R.string.wizard_text_input_password2;
            dispEditText = true;
            progress = 80;
            break;

        case INVALID_PASSWORD:
            mainTextId = R.string.wizard_text_invalid_password;
            dispEditText = true;
            progress = 90;
            break;

        case FIN:
            mainTextId = R.string.wizard_text_fin;
            cancelButtonTextId = -1;
            progress = 100;
            nextButtonTextId = R.string.wizard_button_fin;
            break;

        default:
            break;
        }

        // ????
        TextView progressText = (TextView) findViewById(R.id.wizardProgressText);
        progressText.setText(progress + "%");
        ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
        progressBar.setProgress(progress);

        // ???????
        TextView mainText = (TextView) findViewById(R.id.wizardMainText);
        mainText.setText(getString(mainTextId));

        // ??
        EditText editText = (EditText) findViewById(R.id.wizardEditText);
        if (dispEditText == false) {
            editText.setVisibility(View.INVISIBLE);
        }
        else if (editTextValue != null) {
            editText.setText(editTextValue);
        }

        // ?????
        Button subButton = (Button) findViewById(R.id.wizardSubButton);
        if (subButtonTextId != -1) {
            subButton.setText(getString(subButtonTextId));
        }
        else {
            subButton.setVisibility(View.INVISIBLE);
        }

        // ????
        if (nextButtonTextId != -1) {
            Button nextButton = (Button) findViewById(R.id.wizardNextButton);
            nextButton.setText(getString(nextButtonTextId));
        }

        // ????????
        Button cancelButton = (Button) findViewById(R.id.wizardCancelButton);
        if (cancelButtonTextId != -1) {
            cancelButton.setText(getString(cancelButtonTextId));
        }
        else {
            cancelButton.setVisibility(View.INVISIBLE);
        }
    }

    private void nextActivity(int nextInt) {

        // ?????????
        Intent intent = new Intent(this, InitialConfigurationWizardActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra(Const.INTENT_EX_ACTION_SELECT, nextInt);
        startActivity(intent);
    }

    private class CheckRouterThread extends Thread {
        @Override
        public void run() {
            int nextInt = checkRouter();
            if (progressDialog != null) {
                progressDialog.dismiss();
                progressDialog = null;
                // ?????????
                nextActivity(nextInt);
            }
        }
    }

    private boolean isSupplicantCompleted() {
        WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        return wifi.isWifiEnabled() && BackgroundService.isSupplicantCompleted(wifi);
    }

    private int checkRouter() {
        RouterInfo routerInfo = new RouterInfo();

        // ???????????
        CTRL ctrl = infoBtn ? CTRL.GET_INFO_FORCE_RMTMAIN : CTRL.GET_INFO_FORCE_INFOBTN;
        int ret = RouterControlByHttp.exec(this, ctrl, routerInfo);
        Logger.i("checkRouter, ret=" + ret + ", stby=" + routerInfo.hasStandbyButton);

        // ??
        if (ret == RouterControlByHttp.CTRL_OK) {
            // ???????????
            if (routerInfo.hasStandbyButton) {
                return PAGE.FIN.ordinal();
            }
            // RMTMAIN?????????????????????????INFOBTN???????????
            else if (infoBtn) {
                infoBtn = false;
                return checkRouter();
            }
        }

        // ????????
        if (ret == RouterControlByHttp.CTRL_PASS_NOT_INITIALIZED) {
            return PAGE.PASSWORD_NOT_INIT.ordinal();
        }
        // ?????
        else if (ret == RouterControlByHttp.CTRL_UNAUTHORIZED) {
            if (page == PAGE.INPUT_PASSWORD1 || page == PAGE.INPUT_PASSWORD2 || page == PAGE.INVALID_PASSWORD) {
                return PAGE.INVALID_PASSWORD.ordinal();
            }
            else if (page == PAGE.PASSWORD_NOT_INIT) {
                return PAGE.INPUT_PASSWORD1.ordinal();
            }
            else {
                return PAGE.INPUT_PASSWORD2.ordinal();
            }
        }
        // ??????
        else {
            // ????IP??
            return PAGE.INPUT_ROUTER_IP.ordinal();
        }
    }
}




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