Search in sources :

Example 1 with ChatInputView

use of cn.jiguang.imui.chatinput.ChatInputView in project aurora-imui by jpush.

the class ReactChatInputManager method createViewInstance.

@Override
protected ChatInputView createViewInstance(final ThemedReactContext reactContext) {
    mContext = reactContext;
    final SharedPreferences sp = reactContext.getSharedPreferences(AURORA_IMUI_SHARED_PREFERENCES, Context.MODE_PRIVATE);
    mSoftKeyboardHeight = sp.getInt(SOFT_KEYBOARD_HEIGHT, 0);
    if (!EventBus.getDefault().isRegistered(this)) {
        EventBus.getDefault().register(this);
    }
    final Activity activity = reactContext.getCurrentActivity();
    mChatInput = new ChatInputView(activity, null);
    final EditText editText = mChatInput.getInputView();
    editText.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(mChatInput.getId(), ON_TOUCH_EDIT_TEXT_EVENT, null);
                if (!mChatInput.isFocused()) {
                    EmoticonsKeyboardUtils.openSoftKeyboard(mChatInput.getInputView());
                }
                EventBus.getDefault().post(new ScrollEvent(true));
                int height = mChatInput.getSoftKeyboardHeight();
                if (mSoftKeyboardHeight != height && height != 0 && height < 1000) {
                    mSoftKeyboardHeight = mChatInput.getSoftKeyboardHeight();
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putInt(SOFT_KEYBOARD_HEIGHT, mSoftKeyboardHeight);
                    editor.commit();
                }
            }
            return false;
        }
    });
    editText.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {
            editText.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            mWidth = editText.getWidth();
        }
    });
    DisplayMetrics dm = new DisplayMetrics();
    reactContext.getCurrentActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
    mDensity = dm.density;
    mScreenWidth = dm.widthPixels;
    mMenuContainerHeight = (int) (mMenuContainerHeight / mDensity);
    editText.addTextChangedListener(new TextWatcher() {

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

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            WritableMap event = Arguments.createMap();
            double layoutHeight = calculateMenuHeight();
            mInitState = false;
            event.putDouble("height", layoutHeight);
            editText.setLayoutParams(new LinearLayout.LayoutParams(editText.getWidth(), (int) (mCurrentInputHeight)));
            reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(mChatInput.getId(), ON_INPUT_SIZE_CHANGED_EVENT, event);
        }
    });
    // Use default layout
    mChatInput.setMenuClickListener(new OnMenuClickListener() {

        @Override
        public boolean onSendTextMessage(CharSequence input) {
            if (input.length() == 0) {
                return false;
            }
            // WritableMap map = Arguments.createMap();
            // map.putDouble("height", mInitialChatInputHeight);
            // reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(mChatInput.getId(), ON_INPUT_SIZE_CHANGED_EVENT, map);
            WritableMap event = Arguments.createMap();
            event.putString("text", input.toString());
            reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(mChatInput.getId(), ON_SEND_TEXT_EVENT, event);
            return true;
        }

        @Override
        public void onSendFiles(List<FileItem> list) {
            if (list == null || list.isEmpty()) {
                return;
            }
            WritableMap event = Arguments.createMap();
            WritableArray array = new WritableNativeArray();
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            for (FileItem fileItem : list) {
                WritableMap map = new WritableNativeMap();
                if (fileItem.getType().ordinal() == 0) {
                    map.putString("mediaType", "image");
                    BitmapFactory.decodeFile(fileItem.getFilePath(), options);
                    map.putInt("width", options.outWidth);
                    map.putInt("height", options.outHeight);
                } else {
                    map.putString("mediaType", "video");
                    map.putInt("duration", (int) ((VideoItem) fileItem).getDuration());
                }
                map.putDouble("size", fileItem.getLongFileSize());
                map.putString("mediaPath", fileItem.getFilePath());
                array.pushMap(map);
            }
            event.putArray("mediaFiles", array);
            reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(mChatInput.getId(), ON_SEND_FILES_EVENT, event);
        }

        @Override
        public boolean switchToMicrophoneMode() {
            String[] perms = new String[] { Manifest.permission.RECORD_AUDIO, Manifest.permission.WRITE_EXTERNAL_STORAGE };
            if (!EasyPermissions.hasPermissions(activity, perms)) {
                EasyPermissions.requestPermissions(activity, activity.getResources().getString(R.string.rationale_record_voice), RC_RECORD_VOICE, perms);
            }
            reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(mChatInput.getId(), SWITCH_TO_MIC_EVENT, null);
            // If menu is visible, close menu.
            if (mLastClickId == 0 && mShowMenu) {
                mShowMenu = false;
                mChatInput.dismissMenuLayout();
                mChatInput.dismissRecordVoiceLayout();
                sendSizeChangedEvent(mInitialChatInputHeight + mLineExpend);
            } else if (mShowMenu) {
                mChatInput.showMenuLayout();
                mChatInput.showRecordVoiceLayout();
                mChatInput.requestLayout();
            } else {
                mShowMenu = true;
                // mChatInput.setPendingShowMenu(true);
                EmoticonsKeyboardUtils.closeSoftKeyboard(editText);
                sendSizeChangedEvent(calculateMenuHeight());
                new Handler().postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        mChatInput.showMenuLayout();
                        mChatInput.showRecordVoiceLayout();
                        mChatInput.requestLayout();
                    }
                }, 150);
            }
            mLastClickId = 0;
            return false;
        }

        @Override
        public boolean switchToGalleryMode() {
            String[] perms = new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE };
            if (!EasyPermissions.hasPermissions(activity, perms)) {
                EasyPermissions.requestPermissions(activity, activity.getResources().getString(R.string.rationale_photo), RC_SELECT_PHOTO, perms);
                return false;
            }
            reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(mChatInput.getId(), SWITCH_TO_GALLERY_EVENT, null);
            if (mLastClickId == 1 && mShowMenu) {
                mShowMenu = false;
                mChatInput.dismissMenuLayout();
                mChatInput.dismissPhotoLayout();
                sendSizeChangedEvent(mInitialChatInputHeight + mLineExpend);
            } else if (mShowMenu) {
                mChatInput.getSelectPhotoView().updateData();
                mChatInput.showMenuLayout();
                mChatInput.showSelectPhotoLayout();
                mChatInput.requestLayout();
            } else {
                mChatInput.getSelectPhotoView().updateData();
                mShowMenu = true;
                // mChatInput.setPendingShowMenu(true);
                EmoticonsKeyboardUtils.closeSoftKeyboard(editText);
                sendSizeChangedEvent(calculateMenuHeight());
                new Handler().postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        mChatInput.showMenuLayout();
                        mChatInput.showSelectPhotoLayout();
                        mChatInput.requestLayout();
                    }
                }, 150);
            }
            mLastClickId = 1;
            return false;
        }

        @Override
        public boolean switchToCameraMode() {
            String[] perms = new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO };
            if (!EasyPermissions.hasPermissions(activity, perms)) {
                EasyPermissions.requestPermissions(activity, activity.getResources().getString(R.string.rationale_camera), RC_CAMERA, perms);
                return false;
            }
            reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(mChatInput.getId(), SWITCH_TO_CAMERA_EVENT, null);
            if (mLastClickId == 2 && mShowMenu) {
                mShowMenu = false;
                mChatInput.dismissMenuLayout();
                mChatInput.dismissCameraLayout();
                sendSizeChangedEvent(mInitialChatInputHeight + mLineExpend);
            } else if (mShowMenu) {
                mChatInput.initCamera();
                mChatInput.showMenuLayout();
                mChatInput.showCameraLayout();
                mChatInput.requestLayout();
            } else {
                mShowMenu = true;
                // mChatInput.setPendingShowMenu(true);
                mChatInput.initCamera();
                EmoticonsKeyboardUtils.closeSoftKeyboard(editText);
                new Handler().postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        mChatInput.showMenuLayout();
                        mChatInput.showCameraLayout();
                        sendSizeChangedEvent(calculateMenuHeight());
                        mChatInput.requestLayout();
                    }
                }, 100);
            }
            mLastClickId = 2;
            return false;
        }

        @Override
        public boolean switchToEmojiMode() {
            reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(mChatInput.getId(), SWITCH_TO_EMOJI_EVENT, null);
            if (mLastClickId == 3 && mShowMenu) {
                mShowMenu = false;
                mChatInput.dismissMenuLayout();
                mChatInput.dismissEmojiLayout();
                sendSizeChangedEvent(mInitialChatInputHeight + mLineExpend);
            } else if (mShowMenu) {
                mChatInput.showMenuLayout();
                mChatInput.showEmojiLayout();
                mChatInput.requestLayout();
            } else {
                mShowMenu = true;
                // mChatInput.setPendingShowMenu(true);
                EmoticonsKeyboardUtils.closeSoftKeyboard(editText);
                sendSizeChangedEvent(calculateMenuHeight());
                new Handler().postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        mChatInput.showMenuLayout();
                        mChatInput.showEmojiLayout();
                        mChatInput.requestLayout();
                    }
                }, 150);
            }
            mLastClickId = 3;
            return false;
        }
    });
    mChatInput.setOnCameraCallbackListener(new OnCameraCallbackListener() {

        @Override
        public void onTakePictureCompleted(String photoPath) {
            if (mLastPhotoPath.equals(photoPath)) {
                return;
            }
            mLastPhotoPath = photoPath;
            mContext.runOnUiQueueThread(new Runnable() {

                @Override
                public void run() {
                    if (mChatInput.isFullScreen()) {
                        mChatInput.dismissCameraLayout();
                    }
                    mChatInput.dismissMenuLayout();
                }
            });
            WritableMap event = Arguments.createMap();
            event.putString("mediaPath", photoPath);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(photoPath, options);
            event.putInt("width", options.outWidth);
            event.putInt("height", options.outHeight);
            File file = new File(photoPath);
            event.putDouble("size", file.length());
            reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(mChatInput.getId(), TAKE_PICTURE_EVENT, event);
        }

        @Override
        public void onStartVideoRecord() {
            reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(mChatInput.getId(), START_RECORD_VIDEO_EVENT, null);
        }

        @Override
        public void onFinishVideoRecord(String videoPath) {
            if (videoPath != null) {
                WritableMap event = Arguments.createMap();
                event.putString("mediaPath", videoPath);
                MediaPlayer mediaPlayer = MediaPlayer.create(reactContext, Uri.parse(videoPath));
                // Millisecond to second.
                int duration = mediaPlayer.getDuration() / 1000;
                mediaPlayer.release();
                File file = new File(videoPath);
                event.putDouble("size", file.length());
                event.putInt("duration", duration);
                reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(mChatInput.getId(), FINISH_RECORD_VIDEO_EVENT, event);
            }
        }

        @Override
        public void onCancelVideoRecord() {
            reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(mChatInput.getId(), CANCEL_RECORD_VIDEO_EVENT, null);
        }
    });
    mChatInput.setRecordVoiceListener(new RecordVoiceListener() {

        @Override
        public void onStartRecord() {
            reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(mChatInput.getId(), START_RECORD_VOICE_EVENT, null);
            File rootDir = reactContext.getFilesDir();
            String fileDir = rootDir.getAbsolutePath() + "/voice";
            mChatInput.getRecordVoiceButton().setVoiceFilePath(fileDir, DateFormat.format("yyyy_MMdd_hhmmss", Calendar.getInstance(Locale.CHINA)) + "");
        }

        @Override
        public void onFinishRecord(File voiceFile, int duration) {
            WritableMap event = Arguments.createMap();
            event.putString("mediaPath", voiceFile.getAbsolutePath());
            event.putInt("duration", duration);
            event.putDouble("size", voiceFile.length());
            reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(mChatInput.getId(), FINISH_RECORD_VOICE_EVENT, event);
        }

        @Override
        public void onCancelRecord() {
            reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(mChatInput.getId(), CANCEL_RECORD_VOICE_EVENT, null);
        }

        @Override
        public void onPreviewCancel() {
        }

        @Override
        public void onPreviewSend() {
            sendSizeChangedEvent(mInitialChatInputHeight + mLineExpend);
        }
    });
    mChatInput.setCameraControllerListener(new CameraControllerListener() {

        @Override
        public void onFullScreenClick() {
            reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(mChatInput.getId(), ON_FULL_SCREEN_EVENT, null);
        }

        @Override
        public void onRecoverScreenClick() {
            mShowMenu = false;
            reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(mChatInput.getId(), ON_RECOVER_SCREEN_EVENT, null);
            WritableMap map = Arguments.createMap();
            Log.e(REACT_CHAT_INPUT, "send onSizeChangedEvent to js, height: " + mInitialChatInputHeight + mLineExpend);
            map.putDouble("height", mInitialChatInputHeight + mLineExpend);
            reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(mChatInput.getId(), ON_INPUT_SIZE_CHANGED_EVENT, map);
        }

        @Override
        public void onCloseCameraClick() {
            mShowMenu = false;
            reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(mChatInput.getId(), CLOSE_CAMERA_EVENT, null);
        }

        @Override
        public void onSwitchCameraModeClick(boolean isRecordVideoMode) {
            WritableMap map = Arguments.createMap();
            map.putBoolean("isRecordVideoMode", isRecordVideoMode);
            reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(mChatInput.getId(), SWITCH_CAMERA_MODE_EVENT, map);
        }
    });
    mChatInput.getSelectAlbumBtn().setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(mChatInput.getId(), ON_CLICK_SELECT_ALBUM_EVENT, null);
        }
    });
    mChatInput.getEmojiContainer().getEmoticonsFuncView().addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
            Log.e(REACT_CHAT_INPUT, "EmotionPage Position" + position);
            mChatInput.requestLayout();
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    });
    return mChatInput;
}
Also used : CameraControllerListener(cn.jiguang.imui.chatinput.listener.CameraControllerListener) WritableNativeMap(com.facebook.react.bridge.WritableNativeMap) Activity(android.app.Activity) DisplayMetrics(android.util.DisplayMetrics) WritableNativeArray(com.facebook.react.bridge.WritableNativeArray) OnMenuClickListener(cn.jiguang.imui.chatinput.listener.OnMenuClickListener) TextWatcher(android.text.TextWatcher) Editable(android.text.Editable) BitmapFactory(android.graphics.BitmapFactory) ViewTreeObserver(android.view.ViewTreeObserver) EditText(android.widget.EditText) OnCameraCallbackListener(cn.jiguang.imui.chatinput.listener.OnCameraCallbackListener) ChatInputView(cn.jiguang.imui.chatinput.ChatInputView) WritableMap(com.facebook.react.bridge.WritableMap) SharedPreferences(android.content.SharedPreferences) WritableArray(com.facebook.react.bridge.WritableArray) Handler(android.os.Handler) RecordVoiceListener(cn.jiguang.imui.chatinput.listener.RecordVoiceListener) ChatInputView(cn.jiguang.imui.chatinput.ChatInputView) View(android.view.View) ViewPager(android.support.v4.view.ViewPager) MotionEvent(android.view.MotionEvent) FileItem(cn.jiguang.imui.chatinput.model.FileItem) ScrollEvent(cn.jiguang.imui.messagelist.event.ScrollEvent) File(java.io.File) MediaPlayer(android.media.MediaPlayer)

Example 2 with ChatInputView

use of cn.jiguang.imui.chatinput.ChatInputView in project aurora-imui by jpush.

the class MessageListActivity method onTouch.

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    switch(motionEvent.getAction()) {
        case MotionEvent.ACTION_DOWN:
            ChatInputView chatInputView = mChatView.getChatInputView();
            if (chatInputView.getMenuState() == View.VISIBLE) {
                chatInputView.dismissMenuLayout();
            }
            mChatView.setMsgListHeight(true);
            try {
                View v = getCurrentFocus();
                if (mImm != null && v != null) {
                    mImm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                    mWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
                    view.clearFocus();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            break;
        case MotionEvent.ACTION_UP:
            view.performClick();
            break;
    }
    return false;
}
Also used : ChatInputView(cn.jiguang.imui.chatinput.ChatInputView) ImageView(android.widget.ImageView) ChatView(imui.jiguang.cn.imuisample.views.ChatView) ChatInputView(cn.jiguang.imui.chatinput.ChatInputView) View(android.view.View) GlideException(com.bumptech.glide.load.engine.GlideException)

Aggregations

View (android.view.View)2 ChatInputView (cn.jiguang.imui.chatinput.ChatInputView)2 Activity (android.app.Activity)1 SharedPreferences (android.content.SharedPreferences)1 BitmapFactory (android.graphics.BitmapFactory)1 MediaPlayer (android.media.MediaPlayer)1 Handler (android.os.Handler)1 ViewPager (android.support.v4.view.ViewPager)1 Editable (android.text.Editable)1 TextWatcher (android.text.TextWatcher)1 DisplayMetrics (android.util.DisplayMetrics)1 MotionEvent (android.view.MotionEvent)1 ViewTreeObserver (android.view.ViewTreeObserver)1 EditText (android.widget.EditText)1 ImageView (android.widget.ImageView)1 CameraControllerListener (cn.jiguang.imui.chatinput.listener.CameraControllerListener)1 OnCameraCallbackListener (cn.jiguang.imui.chatinput.listener.OnCameraCallbackListener)1 OnMenuClickListener (cn.jiguang.imui.chatinput.listener.OnMenuClickListener)1 RecordVoiceListener (cn.jiguang.imui.chatinput.listener.RecordVoiceListener)1 FileItem (cn.jiguang.imui.chatinput.model.FileItem)1