com.easemob.qixin.activity.ContactlistFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.easemob.qixin.activity.ContactlistFragment.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.easemob.qixin.activity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Hashtable;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Pair;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;

import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMConversation;
import com.easemob.qixin.Constant;
import com.easemob.qixin.DemoApplication;
import com.easemob.qixin.R;
import com.easemob.qixin.adapter.ContactAdapter;
import com.easemob.qixin.parse.QXUser;
import com.easemob.qixin.widget.Sidebar;

/**
 * ?
 * 
 */
public class ContactlistFragment extends Fragment implements OnClickListener {
    public static final String TAG = "ContactlistFragment";
    private ContactAdapter adapter;
    private ListView listView;
    private boolean hidden;
    private Sidebar sidebar;
    private InputMethodManager inputMethodManager;
    ImageButton clearSearch;
    EditText query;
    Handler handler = new Handler();
    private List<QXUser> list;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_contact_list, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // T??home???appcrash
        if (savedInstanceState != null && savedInstanceState.getBoolean("isConflict", false))
            return;
        inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        listView = (ListView) getView().findViewById(R.id.list);
        sidebar = (Sidebar) getView().findViewById(R.id.sidebar);
        sidebar.setListView(listView);
        View huanxinView = getView().findViewById(R.id.huanxin_contact_list);
        View groupView = getView().findViewById(R.id.group_contact_list);
        huanxinView.setOnClickListener(this);
        groupView.setOnClickListener(this);

        // ?
        query = (EditText) getView().findViewById(R.id.query);
        query.setHint(R.string.search);
        clearSearch = (ImageButton) getView().findViewById(R.id.search_clear);
        query.addTextChangedListener(new TextWatcher() {
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                adapter.getFilter().filter(s);
                if (s.length() > 0) {
                    clearSearch.setVisibility(View.VISIBLE);
                } else {
                    clearSearch.setVisibility(View.INVISIBLE);

                }
            }

            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            public void afterTextChanged(Editable s) {
            }
        });
        clearSearch.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                query.getText().clear();
                hideSoftKeyboard();
            }
        });

        list = new ArrayList<QXUser>();

        // adapter
        adapter = new ContactAdapter(getActivity(), R.layout.row_contact, loadConversationsWithRecentChat());
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                startActivity(new Intent(getActivity(), PersonalInfoActivity.class).putExtra("userId",
                        adapter.getItem(position).getHXid()));
            }
        });
        listView.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // ??
                if (getActivity().getWindow()
                        .getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
                    if (getActivity().getCurrentFocus() != null)
                        inputMethodManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(),
                                InputMethodManager.HIDE_NOT_ALWAYS);
                }
                return false;
            }
        });

        registerForContextMenu(listView);

    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);

    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        return super.onContextItemSelected(item);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        this.hidden = hidden;
        if (!hidden) {
            refresh();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!hidden) {
            refresh();
        }
    }

    // ui
    public void refresh() {
        try {
            // ??
            getActivity().runOnUiThread(new Runnable() {
                public void run() {
                    loadConversationsWithRecentChat();
                    adapter.notifyDataSetChanged();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    void hideSoftKeyboard() {
        if (getActivity().getWindow()
                .getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
            if (getActivity().getCurrentFocus() != null)
                inputMethodManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(),
                        InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (((MainActivity) getActivity()).isConflict) {
            outState.putBoolean("isConflict", true);
        } else if (((MainActivity) getActivity()).getCurrentAccountRemoved()) {
            outState.putBoolean(Constant.ACCOUNT_REMOVED, true);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.huanxin_contact_list:
            getActivity().startActivity(new Intent(getActivity(), ContactsActivity.class));
            break;
        case R.id.group_contact_list:
            getActivity().startActivity(new Intent(getActivity(), GroupsActivity.class));
            break;
        default:
            break;
        }
    }

    /**
     * ??
     * 
     * @param context
     * @return +
     */
    private List<QXUser> loadConversationsWithRecentChat() {

        Hashtable<String, EMConversation> conversations = EMChatManager.getInstance().getAllConversations();
        // messages size0conversation
        /**
         * ??lastMsgTime?? ??Collection.sort
         * ??ConversationSort????? ???
         */
        List<Pair<Long, String>> sortList = new ArrayList<Pair<Long, String>>();
        synchronized (conversations) {
            for (EMConversation conversation : conversations.values()) {
                if (conversation.getAllMessages().size() != 0) {
                    sortList.add(new Pair<Long, String>(conversation.getLastMessage().getMsgTime(),
                            conversation.getUserName()));
                }
            }
        }
        try {
            // Internal is TimSort algorithm, has bug
            sortConversationByLastChatTime(sortList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        list.clear();
        for (Pair<Long, String> sortItem : sortList) {
            for (QXUser user : DemoApplication.getInstance().getAllUsers()) {
                if (user.getHXid().equals(sortItem.second)) {
                    list.add(user);
                }
            }
        }
        return list;
    }

    /**
     * ?????
     * 
     * @param usernames
     */
    private void sortConversationByLastChatTime(List<Pair<Long, String>> conversationList) {
        Collections.sort(conversationList, new Comparator<Pair<Long, String>>() {
            @Override
            public int compare(final Pair<Long, String> con1, final Pair<Long, String> con2) {

                if (con1.first == con2.first) {
                    return 0;
                } else if (con2.first > con1.first) {
                    return 1;
                } else {
                    return -1;
                }
            }

        });
    }
}