com.ccxt.whl.activity.AddContactActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.ccxt.whl.activity.AddContactActivity.java

Source

/**
 * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ccxt.whl.activity;

import java.util.Map;

import org.apache.http.Header;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.chat.EMContactManager;
import com.ccxt.whl.Constant;
import com.ccxt.whl.DemoApplication;
import com.ccxt.whl.R;
import com.ccxt.whl.utils.CommonUtils;
import com.ccxt.whl.utils.HttpRestClient;
import com.ccxt.whl.utils.ImageOptions;
import com.ccxt.whl.utils.JsonToMapList;
import com.ccxt.whl.utils.PreferenceUtils;
import com.loopj.android.http.BaseJsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.ImageLoader;

public class AddContactActivity extends BaseActivity {
    private EditText editText;
    private LinearLayout searchedUserLayout;
    private TextView nameText;
    private Button searchBtn;
    private ImageView avatar;
    private InputMethodManager inputMethodManager;
    private String toAddUsername;
    private ProgressDialog progressDialog;

    private BaseJsonHttpResponseHandler responseHandler;
    private String sendaddname;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_contact);

        editText = (EditText) findViewById(R.id.edit_note);
        searchedUserLayout = (LinearLayout) findViewById(R.id.ll_user);
        nameText = (TextView) findViewById(R.id.name);
        searchBtn = (Button) findViewById(R.id.search);
        avatar = (ImageView) findViewById(R.id.avatar);
        inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

        /*************************http?***********************/
        responseHandler = new BaseJsonHttpResponseHandler() {

            @Override
            public void onSuccess(int statusCode, Header[] headers, String rawJsonResponse, Object response) {
                // TODO Auto-generated method stub 
                Log.d("setting_qes" + rawJsonResponse);
                progressDialog.dismiss();
                if (CommonUtils.isNullOrEmpty(rawJsonResponse)) {
                    Toast.makeText(AddContactActivity.this, "?,?", 0).show();
                    return;
                }

                Map<String, Object> lm = JsonToMapList.getMap(rawJsonResponse);
                if (lm.get("status").toString() != null && lm.get("status").toString().equals("yes")) {
                    Toast.makeText(AddContactActivity.this, "?", 0).show();
                    Log.d("log message==" + lm.get("message").toString());
                    if (!CommonUtils.isNullOrEmpty(lm.get("result").toString())) {
                        Map<String, Object> lmres = JsonToMapList.getMap(lm.get("result").toString());
                        String nickname = lmres.get("nickname").toString();
                        //String age = lmres.get("age").toString();
                        //String sex = lmres.get("sex").toString();
                        String headurl = lmres.get("headurl").toString();
                        sendaddname = lmres.get("user").toString();

                        if (CommonUtils.isNullOrEmpty(sendaddname)) {
                            Toast.makeText(AddContactActivity.this, "?", 0).show();
                            return;
                        }

                        if (!CommonUtils.isNullOrEmpty(nickname)) {
                            //tv_user_nicheng.setText(nickname);
                            searchedUserLayout.setVisibility(View.VISIBLE);
                            nameText.setText(nickname);
                            PreferenceUtils.getInstance(AddContactActivity.this).setSettingUserNickName(nickname);
                        } else if (!CommonUtils.isNullOrEmpty(headurl)) {
                            //tv_user_xingbie.setText(sex);
                            //?
                            ImageLoader.getInstance().displayImage(headurl, avatar, ImageOptions.getOptions());

                        }

                    }
                } else {
                    if (!CommonUtils.isNullOrEmpty(lm.get("message").toString()))
                        Toast.makeText(AddContactActivity.this, lm.get("message").toString(), 0).show();
                    return;
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, String rawJsonData,
                    Object errorResponse) {
                // TODO Auto-generated method stub
                progressDialog.dismiss();
                Toast.makeText(AddContactActivity.this, ",?", 0).show();
                return;
            }

            @Override
            protected Object parseResponse(String rawJsonData, boolean isFailure) throws Throwable {
                // TODO Auto-generated method stub
                return null;
            }

        };
    }

    /**
     * contact
     * @param v
     */
    public void searchContact(View v) {
        final String name = editText.getText().toString();
        String saveText = searchBtn.getText().toString();

        if (getString(R.string.button_search).equals(saveText)) {
            toAddUsername = name;
            if (TextUtils.isEmpty(name)) {
                startActivity(new Intent(this, AlertDialog.class).putExtra("msg", "??"));
                return;
            }

            RequestParams params = new RequestParams();
            params.add("email", toAddUsername);
            HttpRestClient.get(Constant.USER_URL_E, params, responseHandler);
            progressDialog = new ProgressDialog(this);
            progressDialog.setMessage("??...");
            progressDialog.setCanceledOnTouchOutside(false);
            progressDialog.show();
            // TODO ??contact,????
            //?
            searchedUserLayout.setVisibility(View.VISIBLE);
            nameText.setText(toAddUsername);

        }
    }

    /**
     *  contact
     * @param view
     */
    public void addContact(View view) {
        /*String sendstr = data.getStringExtra("edittext");
        if(CommonUtils.isNullOrEmpty(sendstr)){
           sendstr = "";
        }
        //addUserToBlacklist(deleteMsg.getFrom());
        addContact(deleteMsg.getFrom(),sendstr);
        */
        //if(DemoApplication.getInstance().getUserName().equals(nameText.getText().toString())){
        if (DemoApplication.getInstance().getUser().equals(nameText.getText().toString())) {
            startActivity(new Intent(this, AlertDialog.class).putExtra("msg", "?"));
            return;
        }

        if (DemoApplication.getInstance().getContactList().containsKey(nameText.getText().toString())) {
            startActivity(new Intent(this, AlertDialog.class).putExtra("msg", "?"));
            return;
        }

        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("??...");
        progressDialog.setCanceledOnTouchOutside(false);
        progressDialog.show();

        new Thread(new Runnable() {
            public void run() {

                try {
                    //demoreason
                    EMContactManager.getInstance().addContact(toAddUsername, "?");
                    runOnUiThread(new Runnable() {
                        public void run() {
                            progressDialog.dismiss();
                            Toast.makeText(getApplicationContext(), "???,?", 1)
                                    .show();
                        }
                    });
                } catch (final Exception e) {
                    runOnUiThread(new Runnable() {
                        public void run() {
                            progressDialog.dismiss();
                            Toast.makeText(getApplicationContext(), "?:" + e.getMessage(), 1)
                                    .show();
                        }
                    });
                }
            }
        }).start();
    }

    public void back(View v) {
        finish();
    }
}