/** * Copyright (C) 2016 Hyphenate Inc. 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.fanxin.huangfangyi.ui; import com.fanxin.huangfangyi.DemoHelper; import com.hyphenate.chat.EMClient; import com.fanxin.huangfangyi.R; import com.fanxin.easeui.widget.EaseAlertDialog; import android.app.ProgressDialog; import android.content.Context; 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; 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.em_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); } /** * search 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)) { new EaseAlertDialog(this, R.string.Please_enter_a_username).show(); return; } // TODO you can search the user from your app server here. //show the userame and add button if user exist searchedUserLayout.setVisibility(View.VISIBLE); nameText.setText(toAddUsername); } } /** * add contact * @param view */ public void addContact(View view){ if(EMClient.getInstance().getCurrentUser().equals(nameText.getText().toString())){ new EaseAlertDialog(this, R.string.not_add_myself).show(); return; } if(DemoHelper.getInstance().getContactList().containsKey(nameText.getText().toString())){ //let the user know the contact already in your contact list if(EMClient.getInstance().contactManager().getBlackListUsernames().contains(nameText.getText().toString())){ new EaseAlertDialog(this, R.string.user_already_in_contactlist).show(); return; } new EaseAlertDialog(this, R.string.This_user_is_already_your_friend).show(); 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 use a hardcode reason here, you need let user to input if you like String s = getResources().getString(R.string.Add_a_friend); EMClient.getInstance().contactManager().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(); } }