net.evecom.androidecssp.activity.WelcomeActivity.java Source code

Java tutorial

Introduction

Here is the source code for net.evecom.androidecssp.activity.WelcomeActivity.java

Source

/*
 * Copyright (c) 2005, 2014, EVECOM Technology Co.,Ltd. All rights reserved.
 * EVECOM PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * 
 */
package net.evecom.androidecssp.activity;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import net.evecom.androidecssp.R;
import net.evecom.androidecssp.base.BaseActivity;
import net.evecom.androidecssp.base.BaseModel;
import net.evecom.androidecssp.base.ICallback;
import net.evecom.androidecssp.bean.SysDictBean;
import net.evecom.androidecssp.util.ShareUtil;
import net.evecom.androidecssp.util.UiUtil;
import net.evecom.androidecssp.util.entryption.EncryptUtil;
import net.tsz.afinal.FinalDb;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

import com.tianditu.android.maps.GeoPoint;
import com.tianditu.android.maps.MapView;
import com.tianditu.android.maps.MyLocationOverlay;

/**
 * 
 *   WelcomeActivity
 * @author Mars zhang
 * @created 2015-11-12 10:16:20
 */
public class WelcomeActivity extends BaseActivity {
    /** EditText */
    private EditText userNmaeEditText;
    /** EditText */
    private EditText passwordEditText;
    /**  */
    private CheckBox jzmmCheckBox;
    /**  */
    private CheckBox zddrCheckBox;
    /** SharedPreferences */
    private SharedPreferences passnameSp;
    /** login */
    private ProgressDialog loginProgressDialog = null;
    /** loginResult  **/
    private String loginResult = "";
    /** dictResult  **/
    private String dictResult = "";
    /**   **/
    private Boolean islogining = false;
    /**  */
    private Boolean isNeedGpsSet = false;
    /**  */
    MyLocationOverlay mMyLocation = null;
    /**    */
    private Location currentLocation = null;
    /**  */
    LocationManager locationManager = null;
    /**  */
    FinalDb db = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome_at);
        passnameSp = this.getSharedPreferences("PASSNAME", 0);

        initdata();

        askForOpenGPS();

        initView();

    }

    /**
     * app
     */
    private void initdata() {
        db = FinalDb.create(this);
        List<SysDictBean> list = db.findAll(SysDictBean.class);
        //
        if (null != list && list.size() > 0) {
            return;
        }
        db.deleteAll(SysDictBean.class);
        //

        getDictSave2SqlData();
    }

    /**
     * 
     */
    private void getDictSave2SqlData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    dictResult = connServerForResultPost("jfs/mobile/androidIndex/gitSysDicts", null);
                } catch (ClientProtocolException e) {
                    Log.e("mars", "" + e.getMessage());
                } catch (IOException e) {
                    Log.e("mars", "" + e.getMessage());
                } finally {
                    try {
                        putString2Map(dictResult);
                    } catch (JSONException e) {
                        Log.e("mars", "json" + e.getMessage());
                    }
                }
            }
        }).start();
    }

    /**
     * strmap
     * @param val
     * @return
     * @throws JSONException 
     */
    private void putString2Map(String val) throws JSONException {
        if (null == val || val.length() < 1) {
            return;
        }
        long s = System.currentTimeMillis();
        JSONObject jsonObject = new JSONObject(val);
        Iterator<String> dickKeyIterator = jsonObject.keys();
        while (dickKeyIterator.hasNext()) {
            String key = dickKeyIterator.next();
            SysDictBean dictBean = new SysDictBean();
            dictBean.setIdkey(key);
            dictBean.setValue(jsonObject.get(key).toString());
            db.save(dictBean);
            //         dictBean=null;
        }
        Log.v("mars", (System.currentTimeMillis() - s) / 1000 + "");
    }

    /**
     * gps
     */
    private void askForOpenGPS() {
        boolean gpsEnabled = Settings.Secure.isLocationProviderEnabled(getContentResolver(),
                LocationManager.GPS_PROVIDER);
        if (!gpsEnabled) {
            isNeedGpsSet = true;
            toast(",GPS,!", 1);
            Intent intent = new Intent(Settings.ACTION_SETTINGS);
            startActivityForResult(intent, 0);
        }
    }

    /**
     * GIS
     */
    private void manageGis() {
        //   
        mMyLocation = new MyOverlay(this, null);
        //
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            toast("", 0);
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, mMyLocation);
        } else if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 1, mMyLocation);
            toast("", 0);
        } else {
            toast("", 0);
        }
    }

    /**
     * 
     */
    class MyOverlay extends MyLocationOverlay {
        public MyOverlay(Context context, MapView mapView) {
            super(context, mapView);
        }

        /*
         * ""
         */
        protected boolean dispatchTap() {
            return true;
        }

        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            super.onLocationChanged(location);
            if (location != null) {
                currentLocation = location;
                SharedPreferences sp = getApplicationContext().getSharedPreferences("GPS", MODE_PRIVATE);
                // 
                Editor editor = sp.edit();
                editor.putString("latitude", "" + location.getLatitude());
                editor.putString("longitude", "" + location.getLongitude());
                editor.commit();
                Log.v("", location.getLatitude() + "-----" + location.getLongitude());
                toast(location.getLatitude() + "-----" + location.getLongitude(), 0);
            }
            GeoPoint point = mMyLocation.getMyLocation();
        }

    }

    /**
     *  <br>byid <br>
     */
    private void initView() {
        findbyId();
        iflogin();
    }

    /**
     * byid
     */
    private void findbyId() {
        userNmaeEditText = (EditText) findViewById(R.id.welcome_user_edit);
        passwordEditText = (EditText) findViewById(R.id.welcome_password_edit);
        jzmmCheckBox = (CheckBox) findViewById(R.id.welcom_checkbox_jzmm);
        zddrCheckBox = (CheckBox) findViewById(R.id.welcom_checkbox_zddr);
        listener();
    }

    /**
     * 
     */
    private void listener() {
        //
        jzmmCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                Editor editor = passnameSp.edit();
                editor.putString("rembernp", "0");
                if (isChecked) {
                    editor.putString("rembernp", "1");
                } else {
                    editor.putString("rembernp", "0");
                }
                editor.commit();
            }
        });
        //
        zddrCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                Editor editor = passnameSp.edit();
                editor.putString("autologin", "0");
                if (isChecked) {
                    editor.putString("autologin", "1");
                } else {
                    editor.putString("autologin", "0");
                }
                editor.commit();
            }
        });
    }

    private void iflogin() {
        String autologin = ShareUtil.getString(getApplicationContext(), "PASSNAME", "autologin", "0");
        String rembernp = ShareUtil.getString(getApplicationContext(), "PASSNAME", "rembernp", "0");
        String username = ShareUtil.getString(getApplicationContext(), "PASSNAME", "username", "");
        String password = ShareUtil.getString(getApplicationContext(), "PASSNAME", "password", "");
        //EditText  
        userNmaeEditText.setText(username);
        passwordEditText.setText(password);
        //
        if (autologin.equals("1")) {
            zddrCheckBox.setChecked(true);
        } else {
            zddrCheckBox.setChecked(false);
        }
        if (rembernp.equals("1")) {
            jzmmCheckBox.setChecked(true);
        } else {
            jzmmCheckBox.setChecked(false);
        }
        //
        if (autologin.equals("1") && rembernp.equals("1") && !isNeedGpsSet) {
            Log.v("mars", "");
            loginsubmit(username, password);
        }
    }

    /**
     * 
     */
    private void loginsubmit(final String username, final String password) {
        if (islogining) {
            return;
        }

        /**  */
        final Editor editor = passnameSp.edit();
        editor.putString("username", username);
        editor.putString("password", password);
        editor.commit();

        //
        loginProgressDialog = ProgressDialog.show(this, "", "...");
        loginProgressDialog.setCancelable(true);

        //
        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.v("mars", EncryptUtil.getInstance().AESencode(password.trim()));
                try {
                    //  
                    Message loginMessage = new Message();
                    HashMap<String, String> hashMap = new HashMap<String, String>();
                    hashMap.put("pwd", password.trim());
                    loginResult = connServerForResultPost("jfs/ecssp/mobile/accessCtr/login", hashMap);

                    // 
                    if (loginResult.length() > 0) {
                        try {
                            BaseModel resultObj = getObjInfo(loginResult);
                            Boolean success = resultObj.get("sys_success");
                            if (null != success && success) {
                                loginMessage.what = MESSAGETYPE_01;// 
                                BaseModel organization = getObjInfo(resultObj.get("organization").toString());
                                BaseModel userdata = getObjInfo(resultObj.get("userdata").toString());
                                BaseModel userInfo = getObjInfo(resultObj.get("userInfo").toString());
                                String code = resultObj.get("code").toString();
                                //--
                                String username = userNmaeEditText.getText().toString();
                                editor.putString("username", userdata.getStr("loginname"));
                                editor.putString("userid", userdata.getStr("id"));
                                editor.putString("usernameCN", userInfo.getStr("name"));
                                editor.putString("sex", userInfo.get("sex") + "");
                                editor.putString("mobile_In_clound", userInfo.get("mobile") + "");
                                editor.putString("orgid", organization.getStr("id"));
                                editor.putString("orgname", organization.getStr("name"));
                                editor.putString("code", code);
                                editor.commit();
                            } else {
                                loginMessage.what = MESSAGETYPE_02;// 
                            }
                        } catch (JSONException e) {
                            loginMessage.what = MESSAGETYPE_02;// 
                        }
                    } else {
                        loginMessage.what = MESSAGETYPE_03;//  
                    }
                    loginRequestHandler.sendMessage(loginMessage);

                } catch (ClientProtocolException e) {
                } catch (IOException e) {
                }
            }
        }).start();
    }

    /**
     * GUI
     */
    private Handler loginRequestHandler = new Handler() {
        //handlerMessage  
        @Override
        public void handleMessage(Message msg) {
            final Editor editor = passnameSp.edit();
            switch (msg.what) {
            case MESSAGETYPE_01:
                //
                if (null != loginProgressDialog) {
                    loginProgressDialog.dismiss();
                }

                Intent intent = new Intent();
                intent.setClass(WelcomeActivity.this, MainMenuActivity.class);
                startActivity(intent);
                WelcomeActivity.this.finish();
                Log.v("mars", "");
                break;
            case MESSAGETYPE_02:
                //
                if (null != loginProgressDialog) {
                    loginProgressDialog.dismiss();
                }
                DialogToast(loginResult, new ICallback() {
                    @Override
                    public Object execute() {
                        loginResult = "";
                        return null;
                    }
                });
                Log.v("mars", " ");
                break;
            case MESSAGETYPE_03:
                //
                if (null != loginProgressDialog) {
                    loginProgressDialog.dismiss();
                }
                DialogToast(",", new ICallback() {
                    @Override
                    public Object execute() {
                        loginResult = "";
                        //                  toast("ok", 1);
                        return null;
                    }
                });
                Log.v("mars", "  ");
                break;
            default:
                break;
            }
        }

    };

    /**
     * 
     * @param view
     */
    public void welcomelogin(View view) {
        String username = userNmaeEditText.getText().toString();
        String password = passwordEditText.getText().toString();
        loginsubmit(username, password);

        /*Intent intent=new Intent(getApplicationContext(), TDTLocation222.class);
        startActivity(intent);*/
    }

    @Override
    protected void onPause() {
        if (null != mMyLocation) {
            locationManager.removeUpdates(mMyLocation);
        }
        super.onPause();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        switch (requestCode) {
        case 0:
            manageGis();
            break;

        default:
            break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    public void test(View view) {
        toast(UiUtil.getResolution(getWindowManager()), 1);
    }
}