com.app.sample.chatting.activity.chat.ChatActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.app.sample.chatting.activity.chat.ChatActivity.java

Source

/*
 * Copyright (c) 2015, .
 *
 * 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.app.sample.chatting.activity.chat;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.ListView;

import com.app.sample.chatting.MyApplication;
import com.app.sample.chatting.R;
import com.app.sample.chatting.adapter.chat.ChatAdapter;
import com.app.sample.chatting.bean.Emojicon;
import com.app.sample.chatting.bean.Faceicon;
import com.app.sample.chatting.bean.MessageChat;
import com.app.sample.chatting.data.Constant;
import com.app.sample.chatting.data.Tools;
import com.app.sample.chatting.data.emoji.DisplayRules;
import com.app.sample.chatting.event.chat.ChatPersonMessageEvent;
import com.app.sample.chatting.model.Friend;
import com.app.sample.chatting.service.XMPPConnectionService;
import com.app.sample.chatting.util.SaveUtil;
import com.app.sample.chatting.widget.KJChatKeyboard;

import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.chat.Chat;
import org.jivesoftware.smack.chat.ChatManager;
import org.kymjs.kjframe.KJActivity;
import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.FileUtils;
import org.kymjs.kjframe.utils.KJLoger;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;
import greendao.NeoChatHistory;

/**
 * ??
 */
public class ChatActivity extends KJActivity {

    public static final int REQUEST_CODE_GETIMAGE_BYSDCARD = 0x1;
    private static final int TAKE_PICTURE = 101;
    public static String KEY_FRIEND = "com.app.sample.chatting.FRIEND";
    public static String KEY_SNIPPET = "com.app.sample.chatting.SNIPPET";
    public final Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    public static String chatwithWho = "";
    private View parent_view;

    public Uri outputFileUri;
    @BindView(R.id.iv_takedPic)
    ImageView ivTakedPic;

    // give preparation animation activity transition
    public static void navigate(AppCompatActivity activity, View transitionImage, Friend obj, String snippet) {
        Intent intent = new Intent(activity, ChatActivity.class);
        intent.putExtra(KEY_FRIEND, obj);
        intent.putExtra(KEY_SNIPPET, snippet);
        ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(activity,
                transitionImage, KEY_FRIEND);
        SaveUtil.getDelete(obj.getUserId());
        ActivityCompat.startActivity(activity, intent, options.toBundle());
    }

    private KJChatKeyboard box;
    private ListView mRealListView;
    private ActionBar actionBar;
    private Friend friend;
    List<MessageChat> datas = new ArrayList<MessageChat>();
    private ChatAdapter adapter;
    private Chat chatOfFriend;

    @Override
    public void setRootView() {
        setContentView(R.layout.activity_chat);
        parent_view = findViewById(android.R.id.content);

        // animation transition
        ViewCompat.setTransitionName(parent_view, KEY_FRIEND);
        // initialize conversation data
        Intent intent = getIntent();
        friend = (Friend) intent.getExtras().getSerializable(KEY_FRIEND);
        String snippet = intent.getStringExtra(KEY_SNIPPET);
        chatOfFriend = createChat(friend.getUserId());
        chatwithWho = friend.getUserId();
        initToolbar();
        // for system bar in lollipop
        Tools.systemBarLolipop(this);
    }

    @Override
    public void initWidget() {
        super.initWidget();
        box = (KJChatKeyboard) findViewById(R.id.chat_msg_input_box);
        mRealListView = (ListView) findViewById(R.id.chat_listview);

        mRealListView.setSelector(android.R.color.transparent);
        initMessageInputToolBox();
        initListView();
    }

    public void initToolbar() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeButtonEnabled(true);
        actionBar.setTitle(
                !TextUtils.isEmpty(friend.getName()) ? friend.getName() : friend.getUserId().split("@")[0]);
        toolbar.setTitleTextColor(getResources().getColor(R.color.white));
    }

    private void initMessageInputToolBox() {
        box.setOnOperationListener(new OnOperationListener() {
            @Override
            public void send(String content) {
                try {
                    if (chatOfFriend == null) {
                        MyApplication.showToast("");
                        return;
                    }

                    chatOfFriend.sendMessage(content);

                } catch (SmackException.NotConnectedException e) {
                    e.printStackTrace();
                    MyApplication.showToast("??");
                }
                NeoChatHistory hzChatHistory = new NeoChatHistory(null, Constant.getMyOpenfireId(), chatwithWho,
                        System.currentTimeMillis(), 1, content, true);
                //Long id, String myJID, String friendJID, Long time, Integer sendState, String body

                MessageChat message = new MessageChat(MessageChat.MSG_TYPE_TEXT, MessageChat.MSG_STATE_SUCCESS,
                        "Tom", "avatar", "Jerry", "avatar", content, true, true,
                        transferLongToDate(hzChatHistory.getTime()));
                datas.add(message);
                adapter.refresh(datas);
                mRealListView.setSelection(adapter.getCount() - 1);
                SaveUtil.saveChatHistoryMessage(hzChatHistory);
                //                createReplayMsg(message);
            }

            @Override
            public void selectedFace(Faceicon content) {
                MessageChat message = new MessageChat(MessageChat.MSG_TYPE_FACE, MessageChat.MSG_STATE_SUCCESS,
                        "Tom", "avatar", "Jerry", "avatar", content.getPath(), true, true, new Date());
                datas.add(message);
                adapter.refresh(datas);
                //                createReplayMsg(message);
            }

            @Override
            public void selectedEmoji(Emojicon emoji) {
                box.getEditTextBox().append(emoji.getValue());
            }

            @Override
            public void selectedBackSpace(Emojicon back) {
                DisplayRules.backspace(box.getEditTextBox());
            }

            @Override
            public void selectedFunction(int index) {
                switch (index) {
                case 0:
                    goToAlbum();
                    break;
                case 1:
                    goToCamera();
                    break;
                }
            }
        });

        List<String> faceCagegory = new ArrayList<>();
        //        File faceList = FileUtils.getSaveFolder("chat");
        File faceList = new File("");
        if (faceList.isDirectory()) {
            File[] faceFolderArray = faceList.listFiles();
            for (File folder : faceFolderArray) {
                if (!folder.isHidden()) {
                    faceCagegory.add(folder.getAbsolutePath());
                }
            }
        }

        box.setFaceData(faceCagegory);
        mRealListView.setOnTouchListener(getOnTouchListener());
    }

    private void initListView() {
        List<NeoChatHistory> historyList = SaveUtil.selectUser(chatwithWho, 0, 0, 0);
        if (historyList.size() > 0)
            for (int i = 0; i < historyList.size(); i++) {
                MessageChat message = new MessageChat(MessageChat.MSG_TYPE_TEXT, MessageChat.MSG_STATE_SUCCESS,
                        "Tom", "avatar", "Jerry", "avatar", historyList.get(i).getBody(),
                        historyList.get(i).getIsSend(), true, transferLongToDate(historyList.get(i).getTime()));
                datas.add(message);
            }

        //        byte[] emoji = new byte[]{
        //                (byte) 0xF0, (byte) 0x9F, (byte) 0x98, (byte) 0x81
        //        };
        //        MessageChat message = new MessageChat(MessageChat.MSG_TYPE_TEXT,
        //                MessageChat.MSG_STATE_SUCCESS, "\ue415", "avatar", "Jerry", "avatar",
        //                new String(emoji), false, true, new Date(System.currentTimeMillis()
        //                - (1000 * 60 * 60 * 24) * 8));
        //        MessageChat message1 = new MessageChat(MessageChat.MSG_TYPE_TEXT,
        //                MessageChat.MSG_STATE_SUCCESS, "Tom", "avatar", "Jerry", "avatar",
        //                "??:http://www.kymjs.com?http?https?svn?ftp",
        //                true, true, new Date(System.currentTimeMillis() - (1000 * 60 * 60 * 24) * 8));
        //        MessageChat message2 = new MessageChat(MessageChat.MSG_TYPE_PHOTO,
        //                MessageChat.MSG_STATE_SUCCESS, "Tom", "avatar", "Jerry", "avatar",
        //                "http://static.oschina.net/uploads/space/2015/0611/103706_rpPc_1157342.png",
        //                false, true, new Date(
        //                System.currentTimeMillis() - (1000 * 60 * 60 * 24) * 7));
        //        MessageChat message6 = new MessageChat(MessageChat.MSG_TYPE_TEXT,
        //                MessageChat.MSG_STATE_FAIL, "Tom", "avatar", "Jerry", "avatar",
        //                "test send fail", true, false, new Date(
        //                System.currentTimeMillis() - (1000 * 60 * 60 * 24) * 6));
        //        MessageChat message7 = new MessageChat(MessageChat.MSG_TYPE_TEXT,
        //                MessageChat.MSG_STATE_SENDING, "Tom", "avatar", "Jerry", "avatar",
        //                "<a href=\"http://kymjs.com\"></a>?", true, true, new Date(System.currentTimeMillis()
        //                - (1000 * 60 * 60 * 24) * 6));
        //
        //        datas.add(message);
        //        datas.add(message1);
        //        datas.add(message2);
        //        datas.add(message6);
        //        datas.add(message7);

        adapter = new ChatAdapter(this, datas, getOnChatItemClickListener());
        mRealListView.setAdapter(adapter);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mRealListView.setSelection(adapter.getCount() - 1);
            }
        }, 450);
        mRealListView.requestFocus();
    }

    private void createReplayMsg(MessageChat message) {
        final MessageChat reMessage = new MessageChat(message.getType(), MessageChat.MSG_STATE_SUCCESS, "Tom",
                "avatar", "Jerry", "avatar",
                message.getType() == MessageChat.MSG_TYPE_TEXT ? ":" + message.getContent()
                        : message.getContent(),
                false, true, new Date());
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000 * (new Random().nextInt(3) + 1));
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            datas.add(reMessage);
                            adapter.refresh(datas);
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && box.isShow()) {
            box.hideLayout();
            return true;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }

    /**
     * ?
     */
    private void goToAlbum() {
        Intent intent;
        if (Build.VERSION.SDK_INT < 19) {
            intent = new Intent();
            intent.setAction(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");
            startActivityForResult(Intent.createChooser(intent, ""), REQUEST_CODE_GETIMAGE_BYSDCARD);
        } else {
            intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            intent.setType("image/*");
            startActivityForResult(Intent.createChooser(intent, ""), REQUEST_CODE_GETIMAGE_BYSDCARD);
        }

    }

    private void goToCamera() {

        File file = null;
        try {
            file = new File(
                    Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "temp.jpg");

        } catch (Exception e) {
            e.printStackTrace();
        }
        Uri uri = Uri.fromFile(file);
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        this.startActivityForResult(intent, TAKE_PICTURE);

        /*//
        File file = new File(Environment.getExternalStorageDirectory(), "temp.jpg");
        outputFileUri = Uri.fromFile(file);
        //?intent
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        //???
        startActivityForResult(intent, TAKE_PICTURE);*/
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == TAKE_PICTURE) {
            if (resultCode != Activity.RESULT_OK) {
                //TODO ????

                File bb = new File(
                        Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "temp.jpg");

                Intent i = new Intent("com.android.camera.action.CROP");
                i.setType("image/*");

                //i.putExtra("data", bb);
                i.setDataAndType(Uri.fromFile(bb), "image/jpeg");

                i.putExtra("crop", "true");

                i.putExtra("aspectX", 1);

                i.putExtra("aspectY", 1);

                i.putExtra("outputX", 500);

                i.putExtra("outputY", 500);

                i.putExtra("return-data", true);

                this.startActivityForResult(i, 7);

                /*if (data != null) {
                //??
                    
                if (data.hasExtra("data")) {
                    Bitmap tempPic = data.getParcelableExtra("data");
                    ivTakedPic.setVisibility(View.VISIBLE);
                    ivTakedPic.setImageBitmap(tempPic);
                } else {
                    //?Uri
                    int width = ivTakedPic.getWidth();
                    int height = ivTakedPic.getHeight();
                    BitmapFactory.Options factoryOption = new BitmapFactory.Options();
                    factoryOption.inJustDecodeBounds = true;
                    BitmapFactory.decodeFile(outputFileUri.getPath(),factoryOption);
                    int imageWidth = factoryOption.outWidth;
                    int imageHeight = factoryOption.outHeight;
                    //?
                    int scaleFactor = Math.min(imageWidth/width,imageHeight/height);
                    //????
                    factoryOption.inJustDecodeBounds = false;
                    factoryOption.inSampleSize = scaleFactor;
                    factoryOption.inPurgeable = true;
                    
                    Bitmap bitmap = BitmapFactory.decodeFile(outputFileUri.getPath(),factoryOption);
                    ivTakedPic.setImageBitmap(bitmap);
                }
                }*/
                return;
            }
        }
        if (requestCode == REQUEST_CODE_GETIMAGE_BYSDCARD) {
            Uri dataUri = data.getData();
            if (dataUri != null) {
                File file = FileUtils.uri2File(aty, dataUri);
                MessageChat message = new MessageChat(MessageChat.MSG_TYPE_PHOTO, MessageChat.MSG_STATE_SUCCESS,
                        "Tom", "avatar", "Jerry", "avatar", file.getAbsolutePath(), true, true, new Date());
                datas.add(message);
                adapter.refresh(datas);
            }
        }
    }

    /**
     * ??
     *
     * @return ???
     */
    private View.OnTouchListener getOnTouchListener() {
        return new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                box.hideLayout();
                box.hideKeyboard(aty);
                return false;
            }
        };
    }

    /**
     * @return ??
     */
    private OnChatItemClickListener getOnChatItemClickListener() {
        return new OnChatItemClickListener() {
            @Override
            public void onPhotoClick(int position) {
                KJLoger.debug(datas.get(position).getContent() + "");
                ViewInject.toast(aty, datas.get(position).getContent() + "");
            }

            @Override
            public void onTextClick(int position) {
            }

            @Override
            public void onFaceClick(int position) {
            }
        };
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    /**
     * ??
     */
    public interface OnChatItemClickListener {
        void onPhotoClick(int position);

        void onTextClick(int position);

        void onFaceClick(int position);
    }

    /**
     * ?
     */
    public Chat createChat(String jid) {
        if (isLoginSucceed()) {
            ChatManager chatManager = ChatManager.getInstanceFor(getmConnection());
            return chatManager.createChat(jid);
        } else {
            MyApplication.showToast("??");
            return null;
        }
    }

    private boolean isLoginSucceed() {
        return XMPPConnectionService.isConnected();
    }

    private AbstractXMPPConnection getmConnection() {
        return XMPPConnectionService.getmConnection();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_chat_details, menu);
        return true;
    }

    /**
     * Handle click on action bar
     **/
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case android.R.id.home:
            onBackPressed();
            return true;

        case R.id.action_sample:
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    public void onEventMainThread(ChatPersonMessageEvent event) {

        if (event.getMessage().getFrom().split("@")[0].equals(chatwithWho.split("@")[0])) {
            NeoChatHistory hzChatHistory = null;
            //text?
            //Long id, String myJID, String friendJID, Long time, Integer sendState, String body
            //            MessageChat msg = new MessageChat(MessageChat.MSG_TYPE_TEXT, MessageChat.MSG_STATE_SUCCESS, "Tom",
            //                    "avatar", "Jerry", "avatar", "41234", false,
            //                    true, transferLongToDate(hzChatHistory.getTime()));
            MessageChat message1 = new MessageChat(MessageChat.MSG_TYPE_TEXT, MessageChat.MSG_STATE_SUCCESS, "Tom",
                    "avatar", "Jerry", "avatar", event.getMessage().getBody(), false, true,
                    new Date(System.currentTimeMillis() - (1000 * 60 * 60 * 24) * 8));
            datas.add(message1);
            adapter.refresh(datas);
            mRealListView.setSelection(adapter.getCount() - 1);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        chatwithWho = "";
    }

    @Override
    public void onStop() {
        EventBus.getDefault().unregister(this);
        super.onStop();
    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    /**
     * ?
     */
    private Date transferLongToDate(Long millSec) {
        Date date = new Date(millSec);
        return date;
    }
}