/**
* 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.easemob.chatuidemo.activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
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.applib.utils.HXPreferenceUtils;
import com.easemob.chat.EMContactManager;
import com.easemob.chatuidemo.DemoApplication;
import com.sxit.dreamiya.R;
public class AddContactActivity extends BaseActivity{
private EditText editText;
private LinearLayout searchedUserLayout;
private TextView nameText,mTextView;
private Button searchBtn;
private ImageView avatar;
private InputMethodManager inputMethodManager;
private String toAddUsername;
private ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_contact);
mTextView = (TextView) findViewById(R.id.add_list_friends);
editText = (EditText) findViewById(R.id.edit_note);
String strAdd = getResources().getString(R.string.add_friend);
mTextView.setText(strAdd);
String strUserName = getResources().getString(R.string.user_name);
editText.setHint(strUserName);
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);
}
/**
* 鏌ユ壘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)) {
String st = getResources().getString(R.string.Please_enter_a_username);
startActivity(new Intent(this, AlertDialog.class).putExtra("msg", st));
return;
}
// TODO 浠庢湇鍔″櫒鑾峰彇姝ontact,濡傛灉涓嶅瓨鍦ㄦ彁绀轰笉瀛樺湪姝ょ敤鎴�
//鏈嶅姟鍣ㄥ瓨鍦ㄦ鐢ㄦ埛锛屾樉绀烘鐢ㄦ埛鍜屾坊鍔犳寜閽�
searchedUserLayout.setVisibility(View.VISIBLE);
nameText.setText(toAddUsername);
}
}
/**
* 娣诲姞contact
* @param view
*/
public void addContact(View view){
if(DemoApplication.getInstance().getUserName().equals(nameText.getText().toString())){
String str = getString(R.string.not_add_myself);
startActivity(new Intent(this, AlertDialog.class).putExtra("msg", str));
return;
}
if(DemoApplication.getInstance().getContactList().containsKey(nameText.getText().toString())){
//鎻愮ず宸插湪濂藉弸鍒楄〃涓紝鏃犻渶娣诲姞
if(EMContactManager.getInstance().getBlackListUsernames().contains(nameText.getText().toString())){
startActivity(new Intent(this, AlertDialog.class).putExtra("msg", "姝ょ敤鎴峰凡鏄綘濂藉弸(琚媺榛戠姸鎬�)锛屼粠榛戝悕鍗曞垪琛ㄤ腑绉诲嚭鍗冲彲"));
return;
}
String strin = getString(R.string.This_user_is_already_your_friend);
startActivity(new Intent(this, AlertDialog.class).putExtra("msg", strin));
return;
}
progressDialog = new ProgressDialog(this);
String stri = getResources().getString(R.string.Is_sending_a_request);
progressDialog.setMessage(stri);
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
new Thread(new Runnable() {
public void run() {
try {
//demo鍐欐浜嗕釜reason锛屽疄闄呭簲璇ヨ鐢ㄦ埛鎵嬪姩濉叆
String s = getResources().getString(R.string.Add_a_friend);
EMContactManager.getInstance().addContact(toAddUsername, s);
runOnUiThread(new Runnable() {
public void run() {
progressDialog.dismiss();
String s1 = getResources().getString(R.string.send_successful);
Toast.makeText(getApplicationContext(), s1, 1).show();
}
});
} catch (final Exception e) {
runOnUiThread(new Runnable() {
public void run() {
progressDialog.dismiss();
String s2 = getResources().getString(R.string.Request_add_buddy_failure);
Toast.makeText(getApplicationContext(), s2 + e.getMessage(), 1).show();
}
});
}
}
}).start();
}
public void back(View v) {
finish();
}
}