im.afterclass.android.fragment.CommunicationFragment.java Source code

Java tutorial

Introduction

Here is the source code for im.afterclass.android.fragment.CommunicationFragment.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 im.afterclass.android.fragment;

import im.afterclass.android.Constant;
import im.afterclass.android.DemoApplication;
import im.afterclass.android.activity.GroupsActivity;
import im.afterclass.android.activity.LoginActivity;
import im.afterclass.android.db.InviteMessgeDao;
import im.afterclass.android.db.UserDao;
import im.afterclass.android.domain.InviteMessage;
import im.afterclass.android.domain.User;
import im.afterclass.android.domain.InviteMessage.InviteMesageStatus;
import im.afterclass.android.fragment.ChatHistoryFragment;
import im.afterclass.android.fragment.ContactlistFragment;
import im.afterclass.android.utils.CommonUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.SimpleOnPageChangeListener;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;

import com.easemob.chat.ConnectionListener;
import com.easemob.chat.EMChat;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMContactListener;
import com.easemob.chat.EMContactManager;
import com.easemob.chat.EMConversation;
import com.easemob.chat.EMGroupManager;
import com.easemob.chat.EMMessage;
import com.easemob.chat.EMMessage.ChatType;
import com.easemob.chat.EMMessage.Type;
import com.easemob.chat.EMNotifier;
import com.easemob.chat.GroupChangeListener;
import com.easemob.chat.TextMessageBody;
import im.afterclass.android.R;
import com.easemob.util.HanziToPinyin;

public class CommunicationFragment extends Fragment implements OnClickListener, AnimationListener {

    protected static final String TAG = "MainActivity";
    public static Activity main;
    // ?textview
    private TextView unreadLabel;
    // textview
    private TextView unreadAddressLable;

    private Button[] mTabs;
    private ViewPager pager;
    private FragmentPagerAdapter adapter;
    private SimpleOnPageChangeListener changeListener;
    private ContactlistFragment contactListFragment;
    private ChatHistoryFragment chatHistoryFragment;
    private Fragment[] fragments;
    private int index;
    // ?fragmentindex
    private int currentTabIndex;
    private NewMessageBroadcastReceiver msgReceiver;
    // ?
    private boolean isConflict = false;

    private TextView mSearchText;
    private int mSortMode = -1;
    private int width;
    private int height;

    Animation animPushbottomin;

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

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        //mSearchText = new TextView(this);
        //setContentView(mSearchText);
        //getActivity().setTheme(android.R.style.Theme_WithActionBar);
        //setContentView(R.layout.activity_main);

        main = getActivity();

        chatHistoryFragment = new ChatHistoryFragment();
        contactListFragment = new ContactlistFragment();
        fragments = new Fragment[] { chatHistoryFragment, contactListFragment };

        initView();

        inviteMessgeDao = new InviteMessgeDao(getActivity());
        userDao = new UserDao(getActivity());
        // fragment
        /*getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, chatHistoryFragment)
        .add(R.id.fragment_container, contactListFragment).hide(contactListFragment).show(chatHistoryFragment).commit();*/

        // ?BroadcastReceiver
        msgReceiver = new NewMessageBroadcastReceiver();
        IntentFilter intentFilter = new IntentFilter(EMChatManager.getInstance().getNewMessageBroadcastAction());
        intentFilter.setPriority(3);
        getActivity().registerReceiver(msgReceiver, intentFilter);

        // ack?BroadcastReceiver
        IntentFilter ackMessageIntentFilter = new IntentFilter(
                EMChatManager.getInstance().getAckMessageBroadcastAction());
        ackMessageIntentFilter.setPriority(3);
        getActivity().registerReceiver(ackMessageReceiver, ackMessageIntentFilter);

        // setContactListener???
        EMContactManager.getInstance().setContactListener(new MyContactListener());
        // ??listener
        EMChatManager.getInstance().addConnectionListener(new MyConnectionListener());
        // ?listener
        EMGroupManager.getInstance().addGroupChangeListener(new MyGroupChangeListener());
        // sdkUI ??receiverlistener, ??broadcast
        EMChat.getInstance().setAppInited();
    }

    /**
     * ?
     */
    private void initView() {
        unreadLabel = (TextView) getView().findViewById(R.id.unread_msg_number);
        unreadAddressLable = (TextView) getView().findViewById(R.id.unread_address_number);
        pager = (ViewPager) getView().findViewById(R.id.viewPager);
        adapter = new FragmentPagerAdapter(getActivity().getSupportFragmentManager()) {

            public int getCount() {
                return fragments.length;
            }

            public Fragment getItem(int position) {
                return fragments[position];
            }
        };
        changeListener = new SimpleOnPageChangeListener() {
            public void onPageSelected(int position) {
                mTabs[currentTabIndex].setSelected(false);
                mTabs[position].setSelected(true);
                currentTabIndex = position;
            }
        };
        pager.setAdapter(adapter);
        pager.setCurrentItem(0);
        pager.setOnPageChangeListener(changeListener);
        mTabs = new Button[2];
        mTabs[0] = (Button) getView().findViewById(R.id.btn_conversation);
        mTabs[1] = (Button) getView().findViewById(R.id.btn_address_list);
        mTabs[0].setOnClickListener(this);
        mTabs[1].setOnClickListener(this);
        // tab?
        mTabs[0].setSelected(true);

        // load the animation
        animPushbottomin = AnimationUtils.loadAnimation(getActivity().getApplicationContext(),
                R.anim.push_bottom_in);

        // set animation listener
        animPushbottomin.setAnimationListener(this);
    }

    /*@Override
    public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        
       default:
          return super.onOptionsItemSelected(item);
    }
    }
        
    public void onSort(MenuItem item) {
    mSortMode = item.getItemId();
    Intent intent;
    switch (mSortMode) {
       case R.id.action_add_person:
          intent = new Intent(getActivity(),AddFriendActivity.class);
         startActivity(intent);
         getActivity().overridePendingTransition(R.anim.slide_in_from_left,0);
         break;
       case R.id.action_add_group:
          intent = new Intent(getActivity(),AddFriendActivity.class);
         startActivity(intent);
         break;
       case R.id.action_sort_scan:
          intent = new Intent(getActivity(),AddFriendActivity.class);
         startActivity(intent);
         break;
       default:
          // Request a call to onPrepareOptionsMenu so we can change the sort icon
          getActivity().invalidateOptionsMenu();
    }
        
        
    }
        
    public boolean onQueryTextChange(String newText) {
    newText = newText.isEmpty() ? "" : "Query so far: " + newText;
    mSearchText.setText(newText);
    return true;
    }
        
    public boolean onQueryTextSubmit(String query) {
    Toast.makeText(getActivity(), "Searching for: " + query + "...", Toast.LENGTH_SHORT).show();
    return true;
    }*/

    /*private void showTopPopupWindow(View v){
       if(!isOpen){
       LayoutInflater inflater = LayoutInflater.from(this);
       View view = inflater.inflate(R.layout.vertical_popupwindow, null);
       ListView list = (ListView) view.findViewById(R.id.morelist);
       list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, getData()));
       mPopupWindow = new PopupWindow(view);
       list.setOnItemClickListener(new OnItemClickListener() {
        
     @Override
     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
           long arg3) {
        // TODO Auto-generated method stub
        switch(arg2){
        case 0:
           break;
        case 1:
           break;
        case 2:
           Intent intent = new Intent(MainActivity.this,SettingsActivity.class);
           startActivity(intent);
           break;
        }
        mPopupWindow.dismiss();
     }
       });
       list.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
       mPopupWindow.setWidth(list.getMeasuredWidth());
       mPopupWindow.setHeight((list.getMeasuredHeight()+20)*3);
       mPopupWindow.setTouchable(true);
       mPopupWindow.setFocusable(true);
       mPopupWindow.setOutsideTouchable(true);
       mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
       mPopupWindow.showAsDropDown(v);
       }else{
     mPopupWindow.dismiss();
       }
    }*/

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.btn_conversation:
            index = 0;
            pager.setCurrentItem(0);
            break;
        case R.id.btn_address_list:
            index = 1;
            pager.setCurrentItem(1);
            break;
        }
        /*if (currentTabIndex != index) {
           FragmentTransaction trx = getSupportFragmentManager().beginTransaction();
           trx.hide(fragments[currentTabIndex]);
           if (!fragments[index].isAdded()) {
        trx.add(R.id.fragment_container, fragments[index]);
           }
           trx.show(fragments[index]).commit();
        }*/
        mTabs[currentTabIndex].setSelected(false);
        // ?tab?
        mTabs[index].setSelected(true);
        currentTabIndex = index;
    }

    /**
     * button
     * 
     * @param view
     */
    public void onTabClicked(View view) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // 
        try {
            getActivity().unregisterReceiver(msgReceiver);
        } catch (Exception e) {
        }
        try {
            getActivity().unregisterReceiver(ackMessageReceiver);
        } catch (Exception e) {
        }

        if (conflictBuilder != null) {
            conflictBuilder.create().dismiss();
            conflictBuilder = null;
        }

    }

    /**
     * ?
     */
    public void updateUnreadLabel() {
        int count = getUnreadMsgCountTotal();
        if (count > 0) {
            unreadLabel.setText(String.valueOf(count));
            unreadLabel.setVisibility(View.VISIBLE);
        } else {
            unreadLabel.setVisibility(View.INVISIBLE);
        }
    }

    /**
     * ??
     */
    public void updateUnreadAddressLable() {
        getActivity().runOnUiThread(new Runnable() {
            public void run() {
                int count = getUnreadAddressCountTotal();
                if (count > 0) {
                    unreadAddressLable.setText(String.valueOf(count));
                    unreadAddressLable.setVisibility(View.VISIBLE);
                } else {
                    unreadAddressLable.setVisibility(View.INVISIBLE);
                }
            }
        });

    }

    /**
     * ???
     * 
     * @return
     */
    public int getUnreadAddressCountTotal() {
        int unreadAddressCountTotal = 0;
        if (DemoApplication.getInstance().getContactList().get(Constant.NEW_FRIENDS_USERNAME) != null)
            unreadAddressCountTotal = DemoApplication.getInstance().getContactList()
                    .get(Constant.NEW_FRIENDS_USERNAME).getUnreadMsgCount();
        return unreadAddressCountTotal;
    }

    /**
     * ??
     * 
     * @return
     */
    public int getUnreadMsgCountTotal() {
        int unreadMsgCountTotal = 0;
        unreadMsgCountTotal = EMChatManager.getInstance().getUnreadMsgsCount();
        return unreadMsgCountTotal;
    }

    /**
     * ?
     * 
     * 
     */
    private class NewMessageBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            // ?id
            String msgId = intent.getStringExtra("msgid");
            // message?db?id?mesage
            // EMMessage message =
            // EMChatManager.getInstance().getMessage(msgId);

            // bottom bar?
            updateUnreadLabel();
            if (currentTabIndex == 0) {
                // ??????
                if (chatHistoryFragment != null) {
                    chatHistoryFragment.refresh();
                }
            }
            // ?ChatActivity
            abortBroadcast();
        }
    }

    /**
     * ?BroadcastReceiver
     */
    private BroadcastReceiver ackMessageReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String msgid = intent.getStringExtra("msgid");
            String from = intent.getStringExtra("from");
            EMConversation conversation = EMChatManager.getInstance().getConversation(from);
            if (conversation != null) {
                // message
                EMMessage msg = conversation.getMessage(msgid);
                if (msg != null) {
                    msg.isAcked = true;
                }
            }
            abortBroadcast();
        }
    };

    private InviteMessgeDao inviteMessgeDao;
    private UserDao userDao;

    /***
     * ??listener
     * 
     */
    private class MyContactListener implements EMContactListener {

        @Override
        public void onContactAdded(List<String> usernameList) {
            // ??
            Map<String, User> localUsers = DemoApplication.getInstance().getContactList();
            Map<String, User> toAddUsers = new HashMap<String, User>();
            for (String username : usernameList) {
                User user = new User();
                user.setUsername(username);
                String headerName = null;
                if (!TextUtils.isEmpty(user.getNick())) {
                    headerName = user.getNick();
                } else {
                    headerName = user.getUsername();
                }
                if (username.equals(Constant.NEW_FRIENDS_USERNAME)) {
                    user.setHeader("");
                } else if (Character.isDigit(headerName.charAt(0))) {
                    user.setHeader("#");
                } else {
                    user.setHeader(HanziToPinyin.getInstance().get(headerName.substring(0, 1)).get(0).target
                            .substring(0, 1).toUpperCase());
                    char header = user.getHeader().toLowerCase().charAt(0);
                    if (header < 'a' || header > 'z') {
                        user.setHeader("#");
                    }
                }
                // bug??added
                if (!localUsers.containsKey(username)) {
                    userDao.saveContact(user);
                }
                toAddUsers.put(username, user);
            }
            localUsers.putAll(toAddUsers);
            // ui
            if (currentTabIndex == 1)
                contactListFragment.refresh();

        }

        @Override
        public void onContactDeleted(List<String> usernameList) {
            // 
            Map<String, User> localUsers = DemoApplication.getInstance().getContactList();
            for (String username : usernameList) {
                localUsers.remove(username);
                userDao.deleteContact(username);
                inviteMessgeDao.deleteMessage(username);
            }
            // ui
            if (currentTabIndex == 1)
                contactListFragment.refresh();
            updateUnreadLabel();

        }

        @Override
        public void onContactInvited(String username, String reason) {
            // ???(???)???????????
            List<InviteMessage> msgs = inviteMessgeDao.getMessagesList();
            for (InviteMessage inviteMessage : msgs) {
                if (inviteMessage.getFrom().equals(username)) {
                    return;
                }
            }
            // ?javabean
            InviteMessage msg = new InviteMessage();
            msg.setFrom(username);
            msg.setTime(System.currentTimeMillis());
            msg.setReason(reason);
            Log.d(TAG, username + "?,reason: " + reason);
            // status
            msg.setStatus(InviteMesageStatus.BEINVITEED);
            notifyNewIviteMessage(msg);

        }

        @Override
        public void onContactAgreed(String username) {
            List<InviteMessage> msgs = inviteMessgeDao.getMessagesList();
            for (InviteMessage inviteMessage : msgs) {
                if (inviteMessage.getFrom().equals(username)) {
                    return;
                }
            }
            // ?javabean
            InviteMessage msg = new InviteMessage();
            msg.setFrom(username);
            msg.setTime(System.currentTimeMillis());
            Log.d(TAG, username + "???");
            msg.setStatus(InviteMesageStatus.BEAGREED);
            notifyNewIviteMessage(msg);

        }

        @Override
        public void onContactRefused(String username) {
            //???,demo

        }

    }

    protected void notifyNewIviteMessage(InviteMessage msg) {
        // ?msg
        inviteMessgeDao.saveMessage(msg);
        // 1
        User user = DemoApplication.getInstance().getContactList().get(Constant.NEW_FRIENDS_USERNAME);
        user.setUnreadMsgCount(user.getUnreadMsgCount() + 1);
        // ???
        EMNotifier.getInstance(getActivity().getApplicationContext()).notifyOnNewMsg();

        // bottom bar?
        updateUnreadAddressLable();
        // ??ui
        if (currentTabIndex == 1)
            contactListFragment.refresh();
    }

    /**
     * ?listener
     *
     */
    private class MyConnectionListener implements ConnectionListener {

        @Override
        public void onConnected() {
            Log.i("Connect!!!", "Connect Successfully!!!");
            //chatHistoryFragment.errorItem.setVisibility(View.GONE);
        }

        @Override
        public void onDisConnected(String errorString) {
            if (errorString != null && errorString.contains("conflict")) {
                // ??dialog
                showConflictDialog();
            } else {
                chatHistoryFragment.errorItem.setVisibility(View.VISIBLE);
                chatHistoryFragment.errorText.setText("???");
            }
        }

        @Override
        public void onReConnected() {
            chatHistoryFragment.errorItem.setVisibility(View.GONE);
        }

        @Override
        public void onReConnecting() {
        }

        @Override
        public void onConnecting(String progress) {
        }

    }

    /**
     * MyGroupChangeListener
     */
    private class MyGroupChangeListener implements GroupChangeListener {

        @Override
        public void onInvitationReceived(String groupId, String groupName, String inviter, String reason) {
            // 
            EMMessage msg = EMMessage.createReceiveMessage(Type.TXT);
            msg.setChatType(ChatType.GroupChat);
            msg.setFrom(inviter);
            msg.setTo(groupId);
            msg.setMsgId(UUID.randomUUID().toString());
            msg.addBody(new TextMessageBody(inviter + "?"));
            // ??
            EMChatManager.getInstance().saveMessage(msg);
            // ???
            EMNotifier.getInstance(getActivity().getApplicationContext()).notifyOnNewMsg();

            getActivity().runOnUiThread(new Runnable() {
                public void run() {
                    updateUnreadLabel();
                    // ui
                    if (currentTabIndex == 0)
                        chatHistoryFragment.refresh();
                    if (CommonUtils.getTopActivity(getActivity()).equals(GroupsActivity.class.getName())) {
                        GroupsActivity.instance.onResume();
                    }
                }
            });

        }

        @Override
        public void onInvitationAccpted(String groupId, String inviter, String reason) {

        }

        @Override
        public void onInvitationDeclined(String groupId, String invitee, String reason) {

        }

        @Override
        public void onUserRemoved(String groupId, String groupName) {
            // ??Tdemo?
            // ui
            getActivity().runOnUiThread(new Runnable() {
                public void run() {
                    try {
                        updateUnreadLabel();
                        if (currentTabIndex == 0)
                            chatHistoryFragment.refresh();
                        if (CommonUtils.getTopActivity(getActivity()).equals(GroupsActivity.class.getName())) {
                            GroupsActivity.instance.onResume();
                        }
                    } catch (Exception e) {
                        Log.e("###", "refresh exception " + e.getMessage());
                    }

                }
            });
        }

        @Override
        public void onGroupDestroy(String groupId, String groupName) {
            // 
            // ??,demo?
            // ui
            getActivity().runOnUiThread(new Runnable() {
                public void run() {
                    updateUnreadLabel();
                    if (currentTabIndex == 0)
                        chatHistoryFragment.refresh();
                    if (CommonUtils.getTopActivity(getActivity()).equals(GroupsActivity.class.getName())) {
                        GroupsActivity.instance.onResume();
                    }
                }
            });

        }

    }

    @Override
    public void onResume() {
        super.onResume();
        if (!isConflict) {
            updateUnreadLabel();
            updateUnreadAddressLable();
        }

    }

    /*@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {  
      moveTaskToBack(false);  
        return true;  
    }  
    return super.onKeyDown(keyCode, event);  
    }*/

    private android.app.AlertDialog.Builder conflictBuilder;

    /**
     * ??dialog
     */
    private void showConflictDialog() {

        DemoApplication.getInstance().logout();

        if (!getActivity().isFinishing()) {
            // clear up global variables
            try {
                if (conflictBuilder == null)
                    conflictBuilder = new android.app.AlertDialog.Builder(getActivity());
                conflictBuilder.setTitle("");
                conflictBuilder.setMessage(R.string.connect_conflict);
                conflictBuilder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        conflictBuilder = null;
                        getActivity().finish();
                        startActivity(new Intent(getActivity(), LoginActivity.class));
                    }
                });
                conflictBuilder.setCancelable(false);
                conflictBuilder.create().show();
                isConflict = true;
            } catch (Exception e) {
                Log.e("###", "---------color conflictBuilder error" + e.getMessage());
            }

        }

    }

    @Override
    public void onAnimationEnd(Animation animation) {
        // Take any action after completing the animation

        // check for fade in animation
        if (animation == animPushbottomin) {
            //         Toast.makeText(getApplicationContext(), "Animation Stopped",
            //                Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public void onAnimationRepeat(Animation animation) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onAnimationStart(Animation animation) {
        // TODO Auto-generated method stub

    }

    /*class  ItemClickListener implements OnItemClickListener  {  
       public void onItemClick(AdapterView<?> arg0,//The AdapterView where the click happened   
                                 View arg1,//The view within the AdapterView that was clicked  
                                 int arg2,//The position of the view in the adapter  
                                 long arg3//The row id of the item that was clicked  
                                 ) {  
        //arg2=arg3  
        HashMap<String, Object> item=(HashMap<String, Object>) arg0.getItemAtPosition(arg2);  
        //ItemItemText  
            
        SharedPreferences.Editor editor = sp.edit();
        String themelist = sp.getString("themelist", "*?**?*?**?***?**?**KTV*****?");
       themearray = themelist.split(Pattern.quote("*"));
    //    editor.putInt("theme_type", arg2);
        editor.putString("theme_type", themearray[arg2]);
        editor.commit();
        getActivity().setTitle((String)item.get("ItemText"));  
        Intent intent = new Intent(getActivity(),CreateActivity.class);
       mPopupWindow.dismiss();
       startActivity(intent);
       getActivity().finish();
       getActivity().overridePendingTransition(R.anim.head_in,R.anim.head_out);
       }
    }*/

}