com.zlkj.dingdangwuyou.activity.CompanyInfoActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.zlkj.dingdangwuyou.activity.CompanyInfoActivity.java

Source

package com.zlkj.dingdangwuyou.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.zlkj.dingdangwuyou.R;
import com.zlkj.dingdangwuyou.base.BaseActivity;
import com.zlkj.dingdangwuyou.entity.User;
import com.zlkj.dingdangwuyou.net.MyHttpClient;
import com.zlkj.dingdangwuyou.net.Url;
import com.zlkj.dingdangwuyou.utils.AppTool;
import com.zlkj.dingdangwuyou.utils.Const;
import com.zlkj.dingdangwuyou.utils.GsonUtil;
import com.zlkj.dingdangwuyou.utils.ImageUtil;
import com.zlkj.dingdangwuyou.utils.UserUtil;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * ???
 * Created by btx on 2016/11/21.
 */

public class CompanyInfoActivity extends BaseActivity {

    @BindView(R.id.txtTitle)
    TextView txtTitle;
    @BindView(R.id.txtRight)
    TextView txtRight;
    @BindView(R.id.txtNameCom)
    EditText txtNameCom;
    @BindView(R.id.txtLegalPerson)
    EditText txtLegalPerson;
    @BindView(R.id.txtCompanyAdd)
    EditText txtCompanyAdd;
    @BindView(R.id.txtRegisterAdd)
    EditText txtRegisterAdd;
    @BindView(R.id.txtType)
    EditText txtType;
    @BindView(R.id.txtIndustry)
    EditText txtIndustry;
    @BindView(R.id.txtIntro)
    EditText txtIntro;

    @BindView(R.id.imgViLicense)
    ImageView imgViLicense;
    @BindView(R.id.imgViLegalPerson)
    ImageView imgViLegalPerson;
    @BindView(R.id.imgViLegalPersonID)
    ImageView imgViLegalPersonID;

    private String id = ""; //?id

    private File cameraFile; // ??(?)

    private int choosePosition; //?? 1??2?3?

    private int uploadSize = 300; //?????KB

    private File licenseFile; //??
    private File legalPersonFile; //?
    private File legalPersonIDFile; //??

    private List<File> uploadList; //?(?)

    @Override
    protected int getContentViewId() {
        return R.layout.activity_company_info;
    }

    @Override
    protected void initData() {
        txtTitle.setText("??");
        txtRight.setText("?");
        txtRight.setVisibility(View.VISIBLE);

        uploadList = new ArrayList<File>();

        id = UserUtil.getUserInfo().getId();
        getCompanyInfo();
    }

    /**
     * ???
     */
    private void getCompanyInfo() {
        RequestParams params = new RequestParams();
        params.put("id", id);
        MyHttpClient.getInstance().post(Url.URL_GET_COM_INFO, params, new AsyncHttpResponseHandler() {
            @Override
            public void onStart() {
                super.onStart();
                pDialog.show();
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                String jsonStr = new String(responseBody);
                try {
                    JSONArray jsonArr = new JSONArray(jsonStr);
                    if (jsonArr.length() > 0) {
                        User user = GsonUtil.getEntity(jsonArr.getJSONObject(0).toString(), User.class);
                        setData(user);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                Toast.makeText(context, getResources().getString(R.string.request_fail), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                pDialog.dismiss();
            }
        });
    }

    /**
     * ???
     */
    private void saveCompanyInfo() {
        if (TextUtils.isEmpty(txtNameCom.getText().toString().trim())) {
            Toast.makeText(context, "???", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(txtLegalPerson.getText().toString().trim())) {
            Toast.makeText(context, "?", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(txtCompanyAdd.getText().toString().trim())) {
            Toast.makeText(context, "??", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(txtRegisterAdd.getText().toString().trim())) {
            Toast.makeText(context, "?", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(txtType.getText().toString().trim())) {
            Toast.makeText(context, "?", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(txtIndustry.getText().toString().trim())) {
            Toast.makeText(context, "", Toast.LENGTH_SHORT).show();
            return;
        }

        RequestParams params = new RequestParams();
        params.put("id", id);
        params.put("cpname", txtNameCom.getText().toString().trim());
        params.put("cpfaren", txtLegalPerson.getText().toString().trim());
        params.put("cpadress", txtCompanyAdd.getText().toString().trim());
        params.put("registadress", txtRegisterAdd.getText().toString().trim());
        params.put("cptype", txtType.getText().toString().trim());
        params.put("industry", txtIndustry.getText().toString().trim());
        params.put("cpcontent", txtIntro.getText().toString().trim());

        if (licenseFile != null && licenseFile.exists()) {
            try {
                params.put("file1", licenseFile);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
        if (legalPersonFile != null && legalPersonFile.exists()) {
            try {
                params.put("file2", legalPersonFile);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
        if (legalPersonIDFile != null && legalPersonIDFile.exists()) {
            try {
                params.put("file3", legalPersonIDFile);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

        MyHttpClient.getInstance().post(Url.URL_MODIFY_COM_INFO, params, new AsyncHttpResponseHandler() {
            @Override
            public void onStart() {
                super.onStart();
                pDialog.setMessage("???....");
                pDialog.show();
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                Toast.makeText(context, "?", Toast.LENGTH_SHORT).show();
                setResult(Const.RESULT_CODE_SAVE_SUCCEED);
                finish();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                Toast.makeText(context, getResources().getString(R.string.request_fail), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                pDialog.dismiss();
            }
        });
    }

    /**
     * ?
     */
    private void setData(User user) {
        txtNameCom.setText(user.getCpname());
        txtLegalPerson.setText(user.getCpfaren());
        txtCompanyAdd.setText(user.getCpadress());
        txtRegisterAdd.setText(user.getRegistadress());
        txtType.setText(user.getCptype());
        txtIndustry.setText(user.getIndustry());
        txtIntro.setText(user.getCpcontent());

        txtNameCom.setSelection(user.getCpname().length());

        //?
        if (!TextUtils.isEmpty(user.getImg_yyzz())) {
            Glide.with(context).load(Url.HOST + user.getImg_yyzz()).placeholder(R.mipmap.image_loading)
                    .error(R.mipmap.image_error).into(imgViLicense);
        }

        //
        if (!TextUtils.isEmpty(user.getImg_frjqzp())) {
            Glide.with(context).load(Url.HOST + user.getImg_frjqzp()).placeholder(R.mipmap.image_loading)
                    .error(R.mipmap.image_error).into(imgViLegalPerson);
        }

        //???
        if (!TextUtils.isEmpty(user.getImg_frsfz())) {
            Glide.with(context).load(Url.HOST + user.getImg_frsfz()).placeholder(R.mipmap.image_loading)
                    .error(R.mipmap.image_error).into(imgViLegalPersonID);
        }
    }

    /**
     * ?
     */
    private void showAvatarOption() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        String[] items = new String[] { "?", "" };
        builder.setTitle("").setItems(items, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent();
                switch (which) {
                case 0: // ?
                    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                        String curTime = AppTool.dateFormat(System.currentTimeMillis(), "yyyyMMddHHmmss");
                        // ???
                        File imagePath = new File(Environment.getExternalStorageDirectory() + Const.APP_IMAGE_PATH);
                        if (!imagePath.exists()) {
                            imagePath.mkdirs();
                        }
                        cameraFile = new File(imagePath.getPath(), curTime + ".jpg");

                        intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
                        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(cameraFile));
                        startActivityForResult(intent, Const.REQUEST_CODE_CAMERA);

                    } else {
                        Toast.makeText(context, "???", Toast.LENGTH_SHORT)
                                .show();
                    }
                    break;

                case 1: // 
                    intent.setAction(Intent.ACTION_PICK);
                    intent.setType("image/*");
                    startActivityForResult(intent, Const.REQUEST_CODE_GALLERY);
                    break;

                default:
                    break;
                }
            }
        });

        builder.show();
    }

    /**
     * ?
     * @param imgPath 
     * @return ?
     */
    private boolean compressAndSave(String imgPath) {
        Map<String, Object> map = ImageUtil.compressImage(imgPath, uploadSize);
        if (map == null) {
            Toast.makeText(context, "", Toast.LENGTH_SHORT).show();
            return false;
        }

        String currTime = AppTool.dateFormat(System.currentTimeMillis(), "yyyyMMddHHmmss");
        File file = new File(Environment.getExternalStorageDirectory() + Const.APP_IMAGE_PATH, currTime + ".jpg");

        switch (choosePosition) {
        case 1:
            licenseFile = file;
            break;
        case 2:
            legalPersonFile = file;
            break;
        case 3:
            legalPersonIDFile = file;
            break;
        default:
            break;
        }

        return ImageUtil.saveBitmap((Bitmap) map.get(Const.KEY_BITMAP), (int) map.get(Const.KEY_QUALITY), file);
    }

    /**
     * 
     */
    private void refreshImage(String imgPath) {
        if (compressAndSave(imgPath)) {
            switch (choosePosition) {
            case 1:
                Glide.with(context).load(licenseFile).into(imgViLicense);
                uploadList.add(licenseFile);
                break;
            case 2:
                Glide.with(context).load(legalPersonFile).into(imgViLegalPerson);
                uploadList.add(legalPersonFile);
                break;
            case 3:
                Glide.with(context).load(legalPersonIDFile).into(imgViLegalPersonID);
                uploadList.add(legalPersonIDFile);
                break;
            default:
                break;
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Const.REQUEST_CODE_CAMERA) { // ?
            if (cameraFile.exists()) {
                refreshImage(cameraFile.getPath());
            }

        } else if (requestCode == Const.REQUEST_CODE_GALLERY) { // 
            if (data != null) {
                Uri imgUri = data.getData();
                String imgPath = ImageUtil.getImageAbsolutePath(this, imgUri);
                if (!TextUtils.isEmpty(imgPath) && new File(imgPath).exists()) {
                    refreshImage(imgPath);
                }
            }
        }
    }

    @OnClick({ R.id.imgViBack, R.id.txtRight, R.id.imgViLicense, R.id.imgViLegalPerson, R.id.imgViLegalPersonID })
    public void onClick(View view) {
        Intent intent = new Intent(context, ImageBrowseActivity.class);
        switch (view.getId()) {
        case R.id.imgViBack:
            finish();
            break;
        case R.id.txtRight:
            saveCompanyInfo();
            break;
        case R.id.imgViLicense:
            choosePosition = 1;
            showAvatarOption();
            break;
        case R.id.imgViLegalPerson:
            choosePosition = 2;
            showAvatarOption();
            break;
        case R.id.imgViLegalPersonID:
            choosePosition = 3;
            showAvatarOption();
            break;
        default:
            break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //?
        for (File file : uploadList) {
            if (file != null && file.exists()) {
                file.delete();
            }
        }
    }

}