From e6825275ba99ba64c2649a281d6fe3735228d130 Mon Sep 17 00:00:00 2001 From: Mario Danic Date: Mon, 28 May 2018 12:54:27 +0200 Subject: [PATCH] Guest chat support + other things Signed-off-by: Mario Danic --- .../talk/activities/CallActivity.java | 33 ++++++++++++++- .../talk/controllers/ChatController.java | 41 ++++++++++++++++++- .../bottomsheet/EntryMenuController.java | 3 ++ .../bottomsheet/OperationsMenuController.java | 41 +++++++++++++++---- .../talk/utils/bundle/BundleKeys.java | 1 + 5 files changed, 107 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/activities/CallActivity.java b/app/src/main/java/com/nextcloud/talk/activities/CallActivity.java index a7dd7ed75..0dab635ad 100644 --- a/app/src/main/java/com/nextcloud/talk/activities/CallActivity.java +++ b/app/src/main/java/com/nextcloud/talk/activities/CallActivity.java @@ -1257,6 +1257,33 @@ public class CallActivity extends AppCompatActivity { } } + private void leaveRoom() { + ncApi.leaveRoom(credentials, ApiUtils.getRoom(baseUrl, roomToken)) + .subscribeOn(Schedulers.newThread()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(new Observer() { + @Override + public void onSubscribe(Disposable d) { + + } + + @Override + public void onNext(GenericOverall genericOverall) { + finish(); + } + + @Override + public void onError(Throwable e) { + + } + + @Override + public void onComplete() { + + } + }); + } + private void hangupNetworkCalls() { ncApi.leaveCall(credentials, ApiUtils.getUrlForCall(baseUrl, roomToken)) .subscribeOn(Schedulers.newThread()) @@ -1269,7 +1296,11 @@ public class CallActivity extends AppCompatActivity { @Override public void onNext(GenericOverall genericOverall) { - finish(); + if (isMultiSession) { + finish(); + } else { + leaveRoom(); + } } @Override diff --git a/app/src/main/java/com/nextcloud/talk/controllers/ChatController.java b/app/src/main/java/com/nextcloud/talk/controllers/ChatController.java index abee711f2..b5a21185c 100644 --- a/app/src/main/java/com/nextcloud/talk/controllers/ChatController.java +++ b/app/src/main/java/com/nextcloud/talk/controllers/ChatController.java @@ -92,6 +92,7 @@ import com.webianks.library.PopupBubble; import org.parceler.Parcels; import java.io.IOException; +import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; @@ -142,11 +143,14 @@ public class ChatController extends BaseController implements MessagesListAdapte private int globalLastKnownPastMessageId = -1; private MessagesListAdapter adapter; + private String myFirstMessage; + private Autocomplete mentionAutocomplete; private LinearLayoutManager layoutManager; private boolean lookingIntoFuture = false; private int newMessagesCount = 0; + private String senderId; /* TODO: @@ -166,6 +170,7 @@ public class ChatController extends BaseController implements MessagesListAdapte this.currentCall = Parcels.unwrap(args.getParcelable(BundleKeys.KEY_ACTIVE_CONVERSATION)); } this.baseUrl = args.getString(BundleKeys.KEY_MODIFIED_BASE_URL, ""); + this.roomPassword = args.getString(BundleKeys.KEY_CONVERSATION_PASSWORD, ""); } @Override @@ -180,6 +185,12 @@ public class ChatController extends BaseController implements MessagesListAdapte boolean adapterWasNull = false; + if (conversationUser != null && conversationUser.getUserId() != null) { + senderId = conversationUser.getUserId(); + } else { + senderId = "-1"; + } + if (adapter == null) { try { @@ -196,7 +207,7 @@ public class ChatController extends BaseController implements MessagesListAdapte holdersConfig.setOutcoming(MagicOutcomingTextMessageViewHolder.class, R.layout.item_custom_outcoming_text_message); - adapter = new MessagesListAdapter<>(conversationUser.getUserId(), holdersConfig, new ImageLoader() { + adapter = new MessagesListAdapter<>(senderId, holdersConfig, new ImageLoader() { @Override public void loadImage(ImageView imageView, String url) { GlideApp.with(NextcloudTalkApplication.getSharedApplication().getApplicationContext()) @@ -297,6 +308,7 @@ public class ChatController extends BaseController implements MessagesListAdapte if (conversationUser == null) { conversationUser = new UserEntity(); conversationUser.setDisplayName(currentUser.getDisplayName()); + conversationUser.setBaseUrl(baseUrl); } joinRoomWithPassword(); } @@ -371,7 +383,7 @@ public class ChatController extends BaseController implements MessagesListAdapte private void joinRoomWithPassword() { String password = ""; - if (TextUtils.isEmpty(roomPassword)) { + if (!TextUtils.isEmpty(roomPassword)) { password = roomPassword; } @@ -421,6 +433,20 @@ public class ChatController extends BaseController implements MessagesListAdapte } } + private void setSenderId(String guestSenderId) { + if (senderId.equals("-1")) { + try { + final Field senderId = adapter.getClass().getDeclaredField("senderId"); + senderId.setAccessible(true); + senderId.set(adapter, guestSenderId); + } catch (NoSuchFieldException e) { + Log.e(TAG, "Failed to set sender id"); + } catch (IllegalAccessException e) { + Log.e(TAG, "Failed to access and set field"); + } + } + } + private void sendMessage(String message) { Map fieldMap = new HashMap<>(); fieldMap.put("message", message); @@ -439,6 +465,10 @@ public class ChatController extends BaseController implements MessagesListAdapte @Override public void onNext(GenericOverall genericOverall) { + if (senderId.equals("-1") && TextUtils.isEmpty(myFirstMessage)) { + myFirstMessage = message; + } + if (popupBubble.isShown()) { popupBubble.hide(); } @@ -590,6 +620,13 @@ public class ChatController extends BaseController implements MessagesListAdapte } else { for (int i = 0; i < chatMessageList.size(); i++) { chatMessageList.get(i).setBaseUrl(conversationUser.getBaseUrl()); + if (senderId.equals("-1") && !TextUtils.isEmpty(myFirstMessage)) { + ChatMessage chatMessage = chatMessageList.get(i); + if (chatMessage.getActorType().equals("guests") && + chatMessage.getActorDisplayName().equals(conversationUser.getDisplayName())) { + setSenderId(chatMessage.getActorId()); + } + } boolean shouldScroll = layoutManager.findFirstVisibleItemPosition() == 0 || adapter.getItemCount() == 0; diff --git a/app/src/main/java/com/nextcloud/talk/controllers/bottomsheet/EntryMenuController.java b/app/src/main/java/com/nextcloud/talk/controllers/bottomsheet/EntryMenuController.java index 9c7009468..8258c2ff9 100644 --- a/app/src/main/java/com/nextcloud/talk/controllers/bottomsheet/EntryMenuController.java +++ b/app/src/main/java/com/nextcloud/talk/controllers/bottomsheet/EntryMenuController.java @@ -131,6 +131,9 @@ public class EntryMenuController extends BaseController { bundle.putString(BundleKeys.KEY_CALL_URL, callUrl); bundle.putString(BundleKeys.KEY_CONVERSATION_PASSWORD, editText.getText().toString()); bundle.putInt(BundleKeys.KEY_OPERATION_CODE, operationCode); + if(originalBundle.containsKey(BundleKeys.KEY_SPREED_CAPABILITIES)) { + bundle.putParcelable(BundleKeys.KEY_SPREED_CAPABILITIES, originalBundle.getParcelable(BundleKeys.KEY_SPREED_CAPABILITIES)); + } getRouter().pushController(RouterTransaction.with(new OperationsMenuController(bundle)) .pushChangeHandler(new HorizontalChangeHandler()) .popChangeHandler(new HorizontalChangeHandler())); diff --git a/app/src/main/java/com/nextcloud/talk/controllers/bottomsheet/OperationsMenuController.java b/app/src/main/java/com/nextcloud/talk/controllers/bottomsheet/OperationsMenuController.java index e257e4284..1635245f1 100644 --- a/app/src/main/java/com/nextcloud/talk/controllers/bottomsheet/OperationsMenuController.java +++ b/app/src/main/java/com/nextcloud/talk/controllers/bottomsheet/OperationsMenuController.java @@ -25,6 +25,7 @@ import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.text.TextUtils; import android.view.LayoutInflater; import android.view.View; @@ -63,6 +64,7 @@ import org.greenrobot.eventbus.EventBus; import org.parceler.Parcels; import java.util.ArrayList; +import java.util.List; import javax.inject.Inject; @@ -117,6 +119,7 @@ public class OperationsMenuController extends BaseController { private Room.RoomType conversationType; private ArrayList invitedUsers = new ArrayList<>(); + private List spreedCapabilities; private String credentials; public OperationsMenuController(Bundle args) { @@ -136,6 +139,10 @@ public class OperationsMenuController extends BaseController { if (args.containsKey(BundleKeys.KEY_CONVERSATION_TYPE)) { this.conversationType = Parcels.unwrap(args.getParcelable(BundleKeys.KEY_CONVERSATION_TYPE)); } + + if (args.containsKey(BundleKeys.KEY_SPREED_CAPABILITIES)) { + this.spreedCapabilities = Parcels.unwrap(args.getParcelable(BundleKeys.KEY_SPREED_CAPABILITIES)); + } } @Override @@ -435,12 +442,17 @@ public class OperationsMenuController extends BaseController { Bundle bundle = new Bundle(); bundle.putParcelable(BundleKeys.KEY_ROOM, Parcels.wrap(room)); bundle.putString(BundleKeys.KEY_CALL_URL, callUrl); + bundle.putParcelable(BundleKeys.KEY_SPREED_CAPABILITIES, + Parcels.wrap(capabilitiesOverall.getOcs().getData().getCapabilities() + .getSpreedCapability().getFeatures())); bundle.putInt(BundleKeys.KEY_OPERATION_CODE, 99); getRouter().pushController(RouterTransaction.with(new EntryMenuController(bundle)) .pushChangeHandler(new HorizontalChangeHandler()) .popChangeHandler(new HorizontalChangeHandler())); } else { - initiateConversation(true); + initiateConversation(false, capabilitiesOverall.getOcs().getData() + .getCapabilities().getSpreedCapability() + .getFeatures()); } } else if (capabilitiesOverall.getOcs().getData() .getCapabilities().getSpreedCapability() != null && @@ -503,7 +515,7 @@ public class OperationsMenuController extends BaseController { } if (localInvitedUsers.size() == 0) { - initiateConversation(false); + initiateConversation(false, null); } dispose(); } @@ -514,18 +526,29 @@ public class OperationsMenuController extends BaseController { } } - private void initiateConversation(boolean dismissView) { - if (currentUser.hasSpreedCapabilityWithName("chat-v2")) { + private void initiateConversation(boolean dismissView, @Nullable List spreedCapabilities) { + Bundle bundle = new Bundle(); + boolean hasChatCapability; + boolean isGuest = false; + + if (baseUrl != null && !baseUrl.equals(currentUser.getBaseUrl())) { + bundle.putString(BundleKeys.KEY_MODIFIED_BASE_URL, baseUrl); + hasChatCapability = spreedCapabilities != null && spreedCapabilities.contains("chat-v2"); + isGuest = true; + } else { + hasChatCapability = currentUser.hasSpreedCapabilityWithName("chat-v2"); + } + + + if (hasChatCapability) { eventBus.post(new BottomSheetLockEvent(true, 0, true, true, dismissView)); Intent conversationIntent = new Intent(getActivity(), CallActivity.class); - Bundle bundle = new Bundle(); bundle.putString(BundleKeys.KEY_ROOM_TOKEN, room.getToken()); bundle.putString(BundleKeys.KEY_CONVERSATION_NAME, room.getDisplayName()); - bundle.putParcelable(BundleKeys.KEY_USER_ENTITY, Parcels.wrap(currentUser)); - if (baseUrl != null && !baseUrl.equals(currentUser.getBaseUrl())) { - bundle.putString(BundleKeys.KEY_MODIFIED_BASE_URL, baseUrl); + if (!isGuest) { + bundle.putParcelable(BundleKeys.KEY_USER_ENTITY, Parcels.wrap(currentUser)); } bundle.putParcelable(BundleKeys.KEY_ACTIVE_CONVERSATION, Parcels.wrap(call)); @@ -582,7 +605,7 @@ public class OperationsMenuController extends BaseController { } else { CallOverall callOverall = (CallOverall) o; call = callOverall.getOcs().getData(); - initiateConversation(true); + initiateConversation(true, spreedCapabilities); } } diff --git a/app/src/main/java/com/nextcloud/talk/utils/bundle/BundleKeys.java b/app/src/main/java/com/nextcloud/talk/utils/bundle/BundleKeys.java index 044a277e8..37f19ee75 100644 --- a/app/src/main/java/com/nextcloud/talk/utils/bundle/BundleKeys.java +++ b/app/src/main/java/com/nextcloud/talk/utils/bundle/BundleKeys.java @@ -47,4 +47,5 @@ public class BundleKeys { public static final String KEY_CONVERSATION_NAME = "KEY_CONVERSATION_NAME"; public static final String KEY_CALL_VOICE_ONLY = "KEY_CALL_VOICE_ONLY"; public static final String KEY_ACTIVE_CONVERSATION = "KEY_ACTIVE_CONVERSATION"; + public static final String KEY_SPREED_CAPABILITIES = "KEY_SPREED_CAPABILITIES"; }