com.gizwits.gizdataaccesssdkdemo.activitys.MainActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.gizwits.gizdataaccesssdkdemo.activitys.MainActivity.java

Source

/**
 * Project Name:GizDataAccessSDKDemo
 * File Name:MainActivity.java
 * Package Name:com.gizwits.gizdataaccesssdkdemo
 * Date:2015-1-12 15:30:22
 * Copyright (c) 2014~2015 Xtreme Programming Group, Inc.
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package com.gizwits.gizdataaccesssdkdemo.activitys;

import java.util.Calendar;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Paint.Join;
import android.os.Bundle;
import android.provider.Settings.Secure;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.gizwits.gizdataaccess.GizDataAccess;
import com.gizwits.gizdataaccess.GizDataAccessLogin;
import com.gizwits.gizdataaccess.GizDataAccessSource;
import com.gizwits.gizdataaccess.entity.GizDataAccessErrorCode;
import com.gizwits.gizdataaccess.listener.GizDataAccessLoginListener;
import com.gizwits.gizdataaccess.listener.GizDataAccessSourceListener;
import com.gizwits.gizdataaccesssdkdemo.Constant;
import com.gizwits.gizdataaccesssdkdemo.R;
import com.gizwits.gizdataaccesssdkdemo.R.id;
import com.gizwits.gizdataaccesssdkdemo.R.layout;
import com.gizwits.gizdataaccesssdkdemo.utils.DateUtils;
import com.gizwits.gizdataaccesssdkdemo.utils.LoginType;
import com.gizwits.gizdataaccesssdkdemo.utils.NetworkUtils;
import com.gizwits.gizdataaccesssdkdemo.utils.RegexUtils;

// TODO: Auto-generated Javadoc
/**
 * 
 * ClassName: Class MainActivity. <br/>
 * <br/>
 * date: 2015-1-12 15:30:22 <br/>
 * 
 * @author Lien
 */
public class MainActivity extends Activity implements OnClickListener {

    /** The tv version. */
    TextView tvVersion;

    /** The tv terminal. */
    TextView tvTerminal;

    /** The btn save. */
    Button btnSave;

    /** The btn load. */
    Button btnLoad;

    /** The btn clean. */
    Button btnClean;

    /** The btn start load. */
    Button btnStartLoad;

    /** The buffer. */
    StringBuffer buffer;

    /** The dialog. */
    Dialog dialog;

    /** The et sec start. */
    EditText etYearStart, etMonStart, etDayStart, etHourStart, etMinStart, etSecStart;

    /** The et sec end. */
    EditText etYearEnd, etMonEnd, etDayEnd, etHourEnd, etMinEnd, etSecEnd;

    /** The et skip. */
    EditText etLimit, etSkip;

    /** The load start time. */
    long loadStartTime;

    /** The load end time. */
    long loadEndTime;

    private GizDataAccessSourceListener accessSourceListener = new GizDataAccessSourceListener() {
        /*
         * (non-Javadoc)
         * 
         * @see com.gizwits.gizdataaccess.listener.GizDataAccessSourceListener#
         * didLoadData (com.gizwits.gizdataaccess.GizDataAccessSource,
         * org.json.JSONArray,
         * com.gizwits.gizdataaccess.entity.GizDataAccessErrorCode,
         * java.lang.String)
         */
        @Override
        public void didLoadData(GizDataAccessSource arg0, JSONArray jsonArray, GizDataAccessErrorCode result,
                String message) {
            if (result.getResult() == 0) {
                if (jsonArray != null) {
                    buffer.append(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        try {
                            JSONObject jsonObject = jsonArray.getJSONObject(i);
                            buffer.append("attrs:" + jsonObject.get("attrs").toString() + "\n");
                            buffer.append("ts:" + DateUtils.getDateToString(jsonObject.getLong("ts")) + "\n");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                } else {
                    buffer.append(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
                    buffer.append("?");

                }
            } else {
                buffer.append(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
                buffer.append("?" + message);
            }
            tvTerminal.setText(buffer.toString());
        }

    };

    /*
     * (non-Javadoc)
     * 
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.main);
        tvTerminal = (TextView) findViewById(R.id.tvTerminal);
        btnLoad = (Button) findViewById(R.id.btnLoad);
        btnSave = (Button) findViewById(R.id.btnSave);
        btnClean = (Button) findViewById(R.id.btnClean);
        tvVersion = (TextView) findViewById(R.id.tvVersion);
        buffer = new StringBuffer();
        tvTerminal.setText(buffer.toString());
        btnLoad.setOnClickListener(this);
        btnSave.setOnClickListener(this);
        btnClean.setOnClickListener(this);
        dialog = dateTimePicKDialog();
        tvVersion.setText("SDK?:" + GizDataAccess.getVersion());
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.view.View.OnClickListener#onClick(android.view.View)
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btnLoad:

            if (NetworkUtils.isNetworkConnected(MainActivity.this)) {
                dialog.show();
            } else {
                Toast.makeText(MainActivity.this, "", Toast.LENGTH_SHORT).show();
            }
            break;
        case R.id.btnSave:
            Intent intent = new Intent(MainActivity.this, SaveActivity.class);
            startActivity(intent);
            break;
        case R.id.btnClean:
            buffer.setLength(0);
            tvTerminal.setText(buffer.toString());
            break;
        }

    }

    /**
     * Load data.
     */
    private void loadData() {
        String strYearStart = etYearStart.getText().toString();
        String strMonStart = etMonStart.getText().toString();
        String strDayStart = etDayStart.getText().toString();
        String strHourStart = etHourStart.getText().toString();
        String strMinStart = etMinStart.getText().toString();
        String strSecStart = etSecStart.getText().toString();
        String strYearEnd = etYearEnd.getText().toString();
        String strMonEnd = etMonEnd.getText().toString();
        String strDayEnd = etDayEnd.getText().toString();
        String strHourEnd = etHourEnd.getText().toString();
        String strMinEnd = etMinEnd.getText().toString();
        String strSecEnd = etSecEnd.getText().toString();
        int limit = Integer.valueOf(isEmpty(etLimit.getText().toString()) ? "20" : etLimit.getText().toString());
        int skip = Integer.valueOf(isEmpty(etSkip.getText().toString()) ? "0" : etSkip.getText().toString());

        if (isEmpty(strYearStart) || isEmpty(strMonStart) || isEmpty(strDayStart) || isEmpty(strHourStart)
                || isEmpty(strMinStart) || isEmpty(strSecStart)) {
            Toast.makeText(MainActivity.this, "", Toast.LENGTH_SHORT).show();
        } else if (isEmpty(strYearEnd) || isEmpty(strMonEnd) || isEmpty(strDayEnd) || isEmpty(strHourEnd)
                || isEmpty(strMinEnd) || isEmpty(strSecEnd)) {
            Toast.makeText(MainActivity.this, "?", Toast.LENGTH_SHORT).show();
        } else {
            loadStartTime = DateUtils.getStringToDate(
                    strYearStart + strMonStart + strDayStart + strHourStart + strMinStart + strSecStart + "0000");
            loadEndTime = DateUtils.getStringToDate(
                    strYearEnd + strMonEnd + strDayEnd + strHourEnd + strMinEnd + strSecEnd + "0000");
            Log.i("loadData", "timeStartlong=" + loadStartTime);
            Log.i("loadData", "timeStartstr=" + DateUtils.getDateToString(loadStartTime));
            Log.i("loadData", "timeEndlong=" + loadEndTime);
            Log.i("loadData", "timeEndstr=" + DateUtils.getDateToString(loadEndTime));
            new GizDataAccessSource(accessSourceListener).loadData(Constant.TOKEN, Constant.PRODUCTKEY,
                    Constant.DEVICE_SN, loadStartTime, loadEndTime, limit, skip);
            if (dialog != null && dialog.isShowing()) {
                dialog.dismiss();
            }
        }

    }

    /**
     * .
     * 
     * @return the dialog
     */
    public Dialog dateTimePicKDialog() {
        DisplayMetrics dm = new DisplayMetrics();
        dm = this.getResources().getDisplayMetrics();
        int screenWidth = dm.widthPixels;
        int screenHeight = dm.heightPixels;
        LinearLayout dateTimeLayout = (LinearLayout) this.getLayoutInflater().inflate(R.layout.dialog_time, null);

        etLimit = (EditText) dateTimeLayout.findViewById(R.id.etLimit);
        etSkip = (EditText) dateTimeLayout.findViewById(R.id.etSkip);
        etYearStart = (EditText) dateTimeLayout.findViewById(R.id.etYearStart);
        etYearEnd = (EditText) dateTimeLayout.findViewById(R.id.etYearEnd);
        etMonStart = (EditText) dateTimeLayout.findViewById(R.id.etMonStart);
        etMonEnd = (EditText) dateTimeLayout.findViewById(R.id.etMonEnd);
        etDayStart = (EditText) dateTimeLayout.findViewById(R.id.etDayStart);
        etDayEnd = (EditText) dateTimeLayout.findViewById(R.id.etDayEnd);
        etHourStart = (EditText) dateTimeLayout.findViewById(R.id.etHourStart);
        etHourEnd = (EditText) dateTimeLayout.findViewById(R.id.etHourEnd);
        etMinStart = (EditText) dateTimeLayout.findViewById(R.id.etMinStart);
        etMinEnd = (EditText) dateTimeLayout.findViewById(R.id.etMinEnd);
        etSecStart = (EditText) dateTimeLayout.findViewById(R.id.etSecStart);
        etSecEnd = (EditText) dateTimeLayout.findViewById(R.id.etSecEnd);
        btnStartLoad = (Button) dateTimeLayout.findViewById(R.id.btnStartLoad);
        btnStartLoad.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                loadData();

            }
        });

        dialog = new Dialog(this);
        WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        params.width = (int) (screenWidth * 0.8);
        params.height = screenHeight / 5;
        params.width = (int) (screenWidth * 0.8);
        params.height = (int) (screenHeight * 0.8);
        dialog.getWindow().setAttributes(params);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setContentView(dateTimeLayout);

        return dialog;
    }

    /**
     * ?null.
     * 
     * @param str
     *            
     * @return boolean true or false
     */
    private static boolean isEmpty(String str) {
        return RegexUtils.isEmpty(str);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.action_changePsw:
            if (Constant.loginType != LoginType.LoginReal) {
                Toast.makeText(MainActivity.this, "?????", Toast.LENGTH_SHORT).show();
                ;
            } else {
                Intent intent = new Intent(MainActivity.this, ChangePswActivity.class);
                startActivity(intent);
            }

            return true;
        case R.id.action_logout:
            onBackPressed();
            return true;
        case R.id.action_change2phone:
            if (Constant.loginType == LoginType.LoginReal) {
                Intent intent2 = new Intent(MainActivity.this, ChangePhoneActivity.class);
                startActivity(intent2);
            } else {
                Toast.makeText(MainActivity.this, "????", Toast.LENGTH_SHORT)
                        .show();
            }
            return true;
        case R.id.action_change2mail:
            if (Constant.loginType == LoginType.LoginReal) {
                Intent intent3 = new Intent(MainActivity.this, ChangeMailActivity.class);
                startActivity(intent3);
            } else {
                Toast.makeText(MainActivity.this, "????", Toast.LENGTH_SHORT)
                        .show();
            }
            return true;
        case R.id.action_anonychange2phone:
            if (Constant.loginType == LoginType.loginAnonymous) {
                Intent intent4 = new Intent(MainActivity.this, TransPhoneActivity.class);
                startActivity(intent4);
            } else {
                Toast.makeText(MainActivity.this, "????", Toast.LENGTH_SHORT)
                        .show();
            }
            return true;
        case R.id.action_anonychange2mail:
            if (Constant.loginType == LoginType.loginAnonymous) {
                Intent intent5 = new Intent(MainActivity.this, TransMailActivity.class);
                startActivity(intent5);
            } else {
                Toast.makeText(MainActivity.this, "????", Toast.LENGTH_SHORT)
                        .show();
            }
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onBackPressed() {
        finish();
    }
}