Commit 2d74bbdd authored by Tiago Cunha's avatar Tiago Cunha Committed by GitHub

Merge pull request #182 from RocketChat/feature/image-keyboard

Initial support for image keyboard accepting only gif
parents 41f255cc b60c36a9
...@@ -3,11 +3,15 @@ package chat.rocket.android.fragment.chatroom; ...@@ -3,11 +3,15 @@ package chat.rocket.android.fragment.chatroom;
import android.Manifest; import android.Manifest;
import android.app.Activity; import android.app.Activity;
import android.content.Intent; import android.content.Intent;
import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar; import android.support.design.widget.Snackbar;
import android.support.v13.view.inputmethod.InputConnectionCompat;
import android.support.v13.view.inputmethod.InputContentInfoCompat;
import android.support.v4.app.DialogFragment; import android.support.v4.app.DialogFragment;
import android.support.v4.os.BuildCompat;
import android.support.v4.view.GravityCompat; import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout; import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SlidingPaneLayout; import android.support.v4.widget.SlidingPaneLayout;
...@@ -23,6 +27,7 @@ import java.lang.reflect.Field; ...@@ -23,6 +27,7 @@ import java.lang.reflect.Field;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import bolts.Task;
import chat.rocket.android.R; import chat.rocket.android.R;
import chat.rocket.android.api.MethodCallHelper; import chat.rocket.android.api.MethodCallHelper;
import chat.rocket.android.fragment.chatroom.dialog.FileUploadProgressDialogFragment; import chat.rocket.android.fragment.chatroom.dialog.FileUploadProgressDialogFragment;
...@@ -131,6 +136,7 @@ public class RoomFragment extends AbstractChatRoomFragment ...@@ -131,6 +136,7 @@ public class RoomFragment extends AbstractChatRoomFragment
.createObjectObserver(realm -> .createObjectObserver(realm ->
realm.where(LoadMessageProcedure.class).equalTo(LoadMessageProcedure.ID, roomId)) realm.where(LoadMessageProcedure.class).equalTo(LoadMessageProcedure.ID, roomId))
.setOnUpdateListener(this::onUpdateLoadMessageProcedure); .setOnUpdateListener(this::onUpdateLoadMessageProcedure);
if (savedInstanceState == null) { if (savedInstanceState == null) {
initialRequest(); initialRequest();
} }
...@@ -310,20 +316,8 @@ public class RoomFragment extends AbstractChatRoomFragment ...@@ -310,20 +316,8 @@ public class RoomFragment extends AbstractChatRoomFragment
(MessageFormLayout) rootView.findViewById(R.id.message_composer); (MessageFormLayout) rootView.findViewById(R.id.message_composer);
messageFormManager = messageFormManager =
new MessageFormManager(messageFormLayout, this::showExtraActionSelectionDialog); new MessageFormManager(messageFormLayout, this::showExtraActionSelectionDialog);
messageFormManager.setSendMessageCallback(messageText -> messageFormManager.setSendMessageCallback(this::sendMessage);
realmHelper.executeTransaction(realm -> messageFormLayout.setEditTextContentListener(this::onCommitContent);
realm.createOrUpdateObjectFromJson(Message.class, new JSONObject()
.put(Message.ID, UUID.randomUUID().toString())
.put(Message.SYNC_STATE, SyncState.NOT_SYNCED)
.put(Message.TIMESTAMP, System.currentTimeMillis())
.put(Message.ROOM_ID, roomId)
.put(Message.USER, new JSONObject()
.put(User.ID, userId))
.put(Message.MESSAGE, messageText)))
.onSuccess(_task -> {
scrollToLatestMessage();
return null;
}));
} }
@Override @Override
...@@ -337,8 +331,12 @@ public class RoomFragment extends AbstractChatRoomFragment ...@@ -337,8 +331,12 @@ public class RoomFragment extends AbstractChatRoomFragment
return; return;
} }
uploadFile(data.getData());
}
private void uploadFile(Uri uri) {
String uplId = new FileUploadHelper(getContext(), realmHelper) String uplId = new FileUploadHelper(getContext(), realmHelper)
.requestUploading(roomId, data.getData()); .requestUploading(roomId, uri);
if (!TextUtils.isEmpty(uplId)) { if (!TextUtils.isEmpty(uplId)) {
FileUploadProgressDialogFragment.create(hostname, roomId, uplId) FileUploadProgressDialogFragment.create(hostname, roomId, uplId)
.show(getFragmentManager(), FileUploadProgressDialogFragment.class.getSimpleName()); .show(getFragmentManager(), FileUploadProgressDialogFragment.class.getSimpleName());
...@@ -485,4 +483,58 @@ public class RoomFragment extends AbstractChatRoomFragment ...@@ -485,4 +483,58 @@ public class RoomFragment extends AbstractChatRoomFragment
protected void onExtraActionSelected(MessageExtraActionBehavior action) { protected void onExtraActionSelected(MessageExtraActionBehavior action) {
action.handleItemSelectedOnFragment(RoomFragment.this); action.handleItemSelectedOnFragment(RoomFragment.this);
} }
private boolean onCommitContent(InputContentInfoCompat inputContentInfo, int flags,
Bundle opts, String[] supportedMimeTypes) {
boolean supported = false;
for (final String mimeType : supportedMimeTypes) {
if (inputContentInfo.getDescription().hasMimeType(mimeType)) {
supported = true;
break;
}
}
if (!supported) {
return false;
}
if (BuildCompat.isAtLeastNMR1()
&& (flags & InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION) != 0) {
try {
inputContentInfo.requestPermission();
} catch (Exception e) {
return false;
}
}
Uri linkUri = inputContentInfo.getLinkUri();
if (linkUri == null) {
return false;
}
sendMessage(linkUri.toString());
try {
inputContentInfo.releasePermission();
} catch (Exception e) {
}
return true;
}
private Task<Void> sendMessage(String messageText) {
return realmHelper.executeTransaction(realm ->
realm.createOrUpdateObjectFromJson(Message.class, new JSONObject()
.put(Message.ID, UUID.randomUUID().toString())
.put(Message.SYNC_STATE, SyncState.NOT_SYNCED)
.put(Message.TIMESTAMP, System.currentTimeMillis())
.put(Message.ROOM_ID, roomId)
.put(Message.USER, new JSONObject()
.put(User.ID, userId))
.put(Message.MESSAGE, messageText)))
.onSuccess(_task -> {
scrollToLatestMessage();
return null;
});
}
} }
...@@ -11,6 +11,7 @@ ext { ...@@ -11,6 +11,7 @@ ext {
supportAnnotations = "com.android.support:support-annotations:$supportVersion" supportAnnotations = "com.android.support:support-annotations:$supportVersion"
supportRecyclerView = "com.android.support:recyclerview-v7:$supportVersion" supportRecyclerView = "com.android.support:recyclerview-v7:$supportVersion"
supportAppCompat = "com.android.support:appcompat-v7:$supportVersion" supportAppCompat = "com.android.support:appcompat-v7:$supportVersion"
supportV13 = "com.android.support:support-v13:$supportVersion"
supportDesign = "com.android.support:design:$supportVersion" supportDesign = "com.android.support:design:$supportVersion"
rxJava = 'io.reactivex:rxjava:1.2.3' rxJava = 'io.reactivex:rxjava:1.2.3'
......
...@@ -33,6 +33,7 @@ dependencies { ...@@ -33,6 +33,7 @@ dependencies {
testCompile 'junit:junit:4.12' testCompile 'junit:junit:4.12'
compile rootProject.ext.supportAnnotations compile rootProject.ext.supportAnnotations
compile rootProject.ext.supportAppCompat compile rootProject.ext.supportAppCompat
compile rootProject.ext.supportV13
compile rootProject.ext.supportDesign compile rootProject.ext.supportDesign
compile 'org.nibor.autolink:autolink:0.5.0' compile 'org.nibor.autolink:autolink:0.5.0'
compile rootProject.ext.textDrawable compile rootProject.ext.textDrawable
......
package chat.rocket.android.widget.message;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v13.view.inputmethod.EditorInfoCompat;
import android.support.v13.view.inputmethod.InputConnectionCompat;
import android.support.v13.view.inputmethod.InputContentInfoCompat;
import android.util.AttributeSet;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.widget.EditText;
public class ImageKeyboardEditText extends EditText {
private final String[] mimeTypes = {"image/gif"};
final InputConnectionCompat.OnCommitContentListener inputConnectionListener =
new InputConnectionCompat.OnCommitContentListener() {
@Override
public boolean onCommitContent(InputContentInfoCompat inputContentInfo, int flags,
Bundle opts) {
if (listener != null) {
return listener.onCommitContent(inputContentInfo, flags, opts, mimeTypes);
}
return false;
}
};
private OnCommitContentListener listener;
public ImageKeyboardEditText(Context context) {
super(context);
}
public ImageKeyboardEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ImageKeyboardEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public ImageKeyboardEditText(Context context, AttributeSet attrs, int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public InputConnection onCreateInputConnection(EditorInfo editorInfo) {
final InputConnection inputConnection = super.onCreateInputConnection(editorInfo);
EditorInfoCompat.setContentMimeTypes(editorInfo, mimeTypes);
return InputConnectionCompat
.createWrapper(inputConnection, editorInfo, inputConnectionListener);
}
public void setContentListener(OnCommitContentListener listener) {
this.listener = listener;
}
public interface OnCommitContentListener {
boolean onCommitContent(InputContentInfoCompat inputContentInfo, int flags,
Bundle opts, String[] supportedMimeTypes);
}
}
...@@ -3,6 +3,8 @@ package chat.rocket.android.widget.message; ...@@ -3,6 +3,8 @@ package chat.rocket.android.widget.message;
import android.annotation.TargetApi; import android.annotation.TargetApi;
import android.content.Context; import android.content.Context;
import android.os.Build; import android.os.Build;
import android.os.Bundle;
import android.support.v13.view.inputmethod.InputContentInfoCompat;
import android.text.Editable; import android.text.Editable;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.TextWatcher; import android.text.TextWatcher;
...@@ -10,7 +12,6 @@ import android.util.AttributeSet; ...@@ -10,7 +12,6 @@ import android.util.AttributeSet;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
...@@ -25,6 +26,7 @@ public class MessageFormLayout extends LinearLayout { ...@@ -25,6 +26,7 @@ public class MessageFormLayout extends LinearLayout {
private ExtraActionSelectionClickListener extraActionSelectionClickListener; private ExtraActionSelectionClickListener extraActionSelectionClickListener;
private SubmitTextListener submitTextListener; private SubmitTextListener submitTextListener;
private ImageKeyboardEditText.OnCommitContentListener listener;
public MessageFormLayout(Context context) { public MessageFormLayout(Context context) {
super(context); super(context);
...@@ -76,7 +78,9 @@ public class MessageFormLayout extends LinearLayout { ...@@ -76,7 +78,9 @@ public class MessageFormLayout extends LinearLayout {
btnSubmit.setScaleY(0); btnSubmit.setScaleY(0);
btnSubmit.setVisibility(GONE); btnSubmit.setVisibility(GONE);
((EditText) composer.findViewById(R.id.editor)).addTextChangedListener(new TextWatcher() { ImageKeyboardEditText editText = (ImageKeyboardEditText) composer.findViewById(R.id.editor);
editText.addTextChangedListener(new TextWatcher() {
@Override @Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { public void beforeTextChanged(CharSequence s, int start, int count, int after) {
} }
...@@ -97,6 +101,17 @@ public class MessageFormLayout extends LinearLayout { ...@@ -97,6 +101,17 @@ public class MessageFormLayout extends LinearLayout {
} }
}); });
editText.setContentListener(new ImageKeyboardEditText.OnCommitContentListener() {
@Override
public boolean onCommitContent(InputContentInfoCompat inputContentInfo, int flags,
Bundle opts, String[] supportedMimeTypes) {
if (listener != null) {
return listener.onCommitContent(inputContentInfo, flags, opts, supportedMimeTypes);
}
return false;
}
});
addView(composer); addView(composer);
} }
...@@ -123,8 +138,14 @@ public class MessageFormLayout extends LinearLayout { ...@@ -123,8 +138,14 @@ public class MessageFormLayout extends LinearLayout {
return getEditor().getText().toString().trim(); return getEditor().getText().toString().trim();
} }
public final void setText(CharSequence text) { public final void setText(final CharSequence text) {
getEditor().setText(text); final TextView editor = getEditor();
editor.post(new Runnable() {
@Override
public void run() {
editor.setText(text);
}
});
} }
public void setEnabled(boolean enabled) { public void setEnabled(boolean enabled) {
...@@ -132,6 +153,10 @@ public class MessageFormLayout extends LinearLayout { ...@@ -132,6 +153,10 @@ public class MessageFormLayout extends LinearLayout {
composer.findViewById(R.id.btn_submit).setEnabled(enabled); composer.findViewById(R.id.btn_submit).setEnabled(enabled);
} }
public void setEditTextContentListener(ImageKeyboardEditText.OnCommitContentListener listener) {
this.listener = listener;
}
private void animateHide(final View view) { private void animateHide(final View view) {
view.animate().scaleX(0).scaleY(0).setDuration(150).withEndAction(new Runnable() { view.animate().scaleX(0).scaleY(0).setDuration(150).withEndAction(new Runnable() {
@Override @Override
......
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
android:layout_height="32dp" android:layout_height="32dp"
android:layout_margin="8dp" /> android:layout_margin="8dp" />
<EditText <chat.rocket.android.widget.message.ImageKeyboardEditText
android:id="@+id/editor" android:id="@+id/editor"
android:layout_width="0px" android:layout_width="0px"
android:layout_height="wrap_content" android:layout_height="wrap_content"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment