From 31d2d29a1ff2f40e8c3ea47c758f71e3d04ec958 Mon Sep 17 00:00:00 2001 From: Mario Danic Date: Mon, 28 May 2018 20:44:11 +0200 Subject: [PATCH] Improve notifications while in conversation Signed-off-by: Mario Danic --- .../talk/activities/CallActivity.java | 16 +++++ .../talk/controllers/CallsListController.java | 1 + .../talk/controllers/ChatController.java | 13 +++- .../talk/controllers/ContactsController.java | 2 + .../bottomsheet/OperationsMenuController.java | 1 + .../nextcloud/talk/jobs/NotificationJob.java | 20 +++++- .../ApplicationWideCurrentRoomHolder.java | 65 +++++++++++++++++++ .../talk/utils/bundle/BundleKeys.java | 1 + 8 files changed, 115 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/com/nextcloud/talk/utils/ApplicationWideCurrentRoomHolder.java 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 8a59c824f..b8a26d5b4 100644 --- a/app/src/main/java/com/nextcloud/talk/activities/CallActivity.java +++ b/app/src/main/java/com/nextcloud/talk/activities/CallActivity.java @@ -74,6 +74,7 @@ import com.nextcloud.talk.models.json.signaling.SignalingOverall; import com.nextcloud.talk.models.json.signaling.settings.IceServer; import com.nextcloud.talk.models.json.signaling.settings.SignalingSettingsOverall; import com.nextcloud.talk.utils.ApiUtils; +import com.nextcloud.talk.utils.ApplicationWideCurrentRoomHolder; import com.nextcloud.talk.utils.animations.PulseAnimation; import com.nextcloud.talk.utils.bundle.BundleKeys; import com.nextcloud.talk.utils.database.user.UserUtils; @@ -223,6 +224,7 @@ public class CallActivity extends AppCompatActivity { private View.OnClickListener videoOnClickListener; private String baseUrl; + private String roomId; private SpotlightView spotlightView; @@ -259,6 +261,7 @@ public class CallActivity extends AppCompatActivity { .setRepeatCount(PulseAnimation.INFINITE) .setRepeatMode(PulseAnimation.REVERSE); + roomId = getIntent().getExtras().getString(BundleKeys.KEY_ROOM_ID, ""); roomToken = getIntent().getExtras().getString(BundleKeys.KEY_ROOM_TOKEN, ""); userEntity = Parcels.unwrap(getIntent().getExtras().getParcelable(BundleKeys.KEY_USER_ENTITY)); @@ -930,8 +933,13 @@ public class CallActivity extends AppCompatActivity { public void onNext(GenericOverall genericOverall) { inCall = true; + // start pinging the call if (!hasChatSupport) { + ApplicationWideCurrentRoomHolder.getInstance().setCurrentRoomId(roomId); + ApplicationWideCurrentRoomHolder.getInstance().setInCall(true); + ApplicationWideCurrentRoomHolder.getInstance().setUserInRoom(userEntity); + ncApi.pingCall(credentials, ApiUtils.getUrlForCallPing(baseUrl, roomToken)) .subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread()) @@ -959,6 +967,8 @@ public class CallActivity extends AppCompatActivity { dispose(pingDisposable); } }); + } else { + ApplicationWideCurrentRoomHolder.getInstance().setInCall(true); } // Start pulling signaling messages @@ -1416,6 +1426,12 @@ public class CallActivity extends AppCompatActivity { if (inCall) { hangup(false); } + + if (hasChatSupport) { + ApplicationWideCurrentRoomHolder.getInstance().setInCall(false); + } else { + ApplicationWideCurrentRoomHolder.getInstance().clear(); + } //this.unregisterReceiver(networkBroadcastReceier); rootEglBase.release(); super.onDestroy(); diff --git a/app/src/main/java/com/nextcloud/talk/controllers/CallsListController.java b/app/src/main/java/com/nextcloud/talk/controllers/CallsListController.java index 48823a5ec..03568d025 100644 --- a/app/src/main/java/com/nextcloud/talk/controllers/CallsListController.java +++ b/app/src/main/java/com/nextcloud/talk/controllers/CallsListController.java @@ -494,6 +494,7 @@ public class CallsListController extends BaseController implements SearchView.On Room room = callItem.getModel(); Bundle bundle = new Bundle(); bundle.putString(BundleKeys.KEY_ROOM_TOKEN, callItem.getModel().getToken()); + bundle.putString(BundleKeys.KEY_ROOM_ID, callItem.getModel().getRoomId()); if (room.hasPassword && (room.participantType.equals(Participant.ParticipantType.GUEST) || room.participantType.equals(Participant.ParticipantType.USER_FOLLOWING_LINK))) { 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 de17493e6..f117467f9 100644 --- a/app/src/main/java/com/nextcloud/talk/controllers/ChatController.java +++ b/app/src/main/java/com/nextcloud/talk/controllers/ChatController.java @@ -56,6 +56,7 @@ import com.bumptech.glide.load.resource.bitmap.CircleCrop; import com.bumptech.glide.request.RequestListener; import com.bumptech.glide.request.RequestOptions; import com.bumptech.glide.request.target.Target; +import com.nextcloud.talk.utils.ApplicationWideCurrentRoomHolder; import com.nextcloud.talk.R; import com.nextcloud.talk.activities.CallActivity; import com.nextcloud.talk.adapters.messages.MagicIncomingTextMessageViewHolder; @@ -153,7 +154,7 @@ public class ChatController extends BaseController implements MessagesListAdapte private int newMessagesCount = 0; private Boolean startCallFromNotification; - + private String roomId; /* TODO: - check push notifications @@ -171,6 +172,12 @@ public class ChatController extends BaseController implements MessagesListAdapte this.conversationUser = currentUser; } + this.roomId = args.getString(BundleKeys.KEY_ROOM_ID, ""); + + ApplicationWideCurrentRoomHolder.getInstance().setCurrentRoomId(roomId); + ApplicationWideCurrentRoomHolder.getInstance().setInCall(false); + ApplicationWideCurrentRoomHolder.getInstance().setUserInRoom(conversationUser); + this.roomToken = args.getString(BundleKeys.KEY_ROOM_TOKEN, ""); if (args.containsKey(BundleKeys.KEY_ACTIVE_CONVERSATION)) { @@ -211,7 +218,7 @@ public class ChatController extends BaseController implements MessagesListAdapte @Override public void onNext(RoomsOverall roomsOverall) { for (Room room : roomsOverall.getOcs().getData()) { - if (roomToken.equals(room.getRoomId())) { + if (roomId.equals(room.getRoomId())) { roomToken = room.getToken(); conversationName = room.getDisplayName(); setTitle(); @@ -393,6 +400,7 @@ public class ChatController extends BaseController implements MessagesListAdapte public void onDestroy() { inChat = false; dispose(); + ApplicationWideCurrentRoomHolder.getInstance().clear(); super.onDestroy(); } @@ -745,6 +753,7 @@ public class ChatController extends BaseController implements MessagesListAdapte if (currentCall != null && !TextUtils.isEmpty(currentCall.getSessionId())) { Bundle bundle = new Bundle(); bundle.putString(BundleKeys.KEY_ROOM_TOKEN, roomToken); + bundle.putString(BundleKeys.KEY_ROOM_ID, roomId); bundle.putParcelable(BundleKeys.KEY_USER_ENTITY, Parcels.wrap(conversationUser)); bundle.putString(BundleKeys.KEY_CONVERSATION_PASSWORD, roomPassword); bundle.putString(BundleKeys.KEY_CALL_SESSION, currentCall.getSessionId()); diff --git a/app/src/main/java/com/nextcloud/talk/controllers/ContactsController.java b/app/src/main/java/com/nextcloud/talk/controllers/ContactsController.java index 0575d458c..4e66da802 100644 --- a/app/src/main/java/com/nextcloud/talk/controllers/ContactsController.java +++ b/app/src/main/java/com/nextcloud/talk/controllers/ContactsController.java @@ -296,6 +296,7 @@ public class ContactsController extends BaseController implements SearchView.OnQ Intent conversationIntent = new Intent(getActivity(), CallActivity.class); Bundle bundle = new Bundle(); bundle.putString(BundleKeys.KEY_ROOM_TOKEN, roomOverall.getOcs().getData().getToken()); + bundle.putString(BundleKeys.KEY_ROOM_ID, roomOverall.getOcs().getData().getRoomId()); if (currentUser.hasSpreedCapabilityWithName("chat-v2")) { bundle.putString(BundleKeys.KEY_CONVERSATION_NAME, @@ -826,6 +827,7 @@ public class ContactsController extends BaseController implements SearchView.OnQ Intent conversationIntent = new Intent(getActivity(), CallActivity.class); Bundle bundle = new Bundle(); bundle.putString(BundleKeys.KEY_ROOM_TOKEN, roomOverall.getOcs().getData().getToken()); + bundle.putString(BundleKeys.KEY_ROOM_ID, roomOverall.getOcs().getData().getRoomId()); conversationIntent.putExtras(bundle); if (currentUser.hasSpreedCapabilityWithName("chat-v2")) { 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 d709b8092..7db3d452f 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 @@ -544,6 +544,7 @@ public class OperationsMenuController extends BaseController { Intent conversationIntent = new Intent(getActivity(), CallActivity.class); bundle.putString(BundleKeys.KEY_ROOM_TOKEN, room.getToken()); + bundle.putString(BundleKeys.KEY_ROOM_ID, room.getToken()); bundle.putString(BundleKeys.KEY_CONVERSATION_NAME, room.getDisplayName()); bundle.putParcelable(BundleKeys.KEY_ACTIVE_CONVERSATION, Parcels.wrap(call)); diff --git a/app/src/main/java/com/nextcloud/talk/jobs/NotificationJob.java b/app/src/main/java/com/nextcloud/talk/jobs/NotificationJob.java index f352850f9..f16f2d076 100644 --- a/app/src/main/java/com/nextcloud/talk/jobs/NotificationJob.java +++ b/app/src/main/java/com/nextcloud/talk/jobs/NotificationJob.java @@ -39,6 +39,7 @@ import android.util.Log; import com.bluelinelabs.logansquare.LoganSquare; import com.evernote.android.job.Job; import com.evernote.android.job.util.support.PersistableBundleCompat; +import com.nextcloud.talk.utils.ApplicationWideCurrentRoomHolder; import com.nextcloud.talk.R; import com.nextcloud.talk.activities.CallActivity; import com.nextcloud.talk.activities.MainActivity; @@ -48,6 +49,7 @@ import com.nextcloud.talk.models.json.push.DecryptedPushMessage; import com.nextcloud.talk.utils.NotificationUtils; import com.nextcloud.talk.utils.PushUtils; import com.nextcloud.talk.utils.bundle.BundleKeys; +import com.nextcloud.talk.utils.database.user.UserUtils; import org.parceler.Parcels; @@ -59,6 +61,7 @@ import java.util.zip.CRC32; import javax.crypto.Cipher; import javax.crypto.NoSuchPaddingException; +import javax.inject.Inject; import autodagger.AutoInjector; @@ -66,9 +69,14 @@ import autodagger.AutoInjector; public class NotificationJob extends Job { public static final String TAG = "NotificationJob"; + @Inject + UserUtils userUtils; + @NonNull @Override protected Result onRunJob(Params params) { + NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this); + Context context = getContext(); PersistableBundleCompat persistableBundleCompat = getParams().getExtras(); String subject = persistableBundleCompat.getString(BundleKeys.KEY_NOTIFICATION_SUBJECT, ""); @@ -96,7 +104,15 @@ public class NotificationJob extends Job { boolean hasChatSupport = signatureVerification.getUserEntity().hasSpreedCapabilityWithName ("chat-v2"); - if (decryptedPushMessage.getApp().equals("spreed")) { + boolean isInTheSameRoomAsNotification = ApplicationWideCurrentRoomHolder.getInstance(). + getCurrentRoomId().equals(decryptedPushMessage.getId()) && + signatureVerification.getUserEntity().equals(ApplicationWideCurrentRoomHolder + .getInstance().getUserInRoom()); + + boolean shouldShowNotification = decryptedPushMessage.getApp().equals("spreed") && + !(isInTheSameRoomAsNotification && !decryptedPushMessage.getType().equals("call")); + + if (shouldShowNotification) { int smallIcon; Bitmap largeIcon; String category = ""; @@ -115,7 +131,7 @@ public class NotificationJob extends Job { bundle.putBoolean(BundleKeys.KEY_FROM_NOTIFICATION_START_CALL, true); } - bundle.putString(BundleKeys.KEY_ROOM_TOKEN, decryptedPushMessage.getId()); + bundle.putString(BundleKeys.KEY_ROOM_ID, decryptedPushMessage.getId()); bundle.putParcelable(BundleKeys.KEY_USER_ENTITY, Parcels.wrap(signatureVerification .getUserEntity())); diff --git a/app/src/main/java/com/nextcloud/talk/utils/ApplicationWideCurrentRoomHolder.java b/app/src/main/java/com/nextcloud/talk/utils/ApplicationWideCurrentRoomHolder.java new file mode 100644 index 000000000..e3cd42689 --- /dev/null +++ b/app/src/main/java/com/nextcloud/talk/utils/ApplicationWideCurrentRoomHolder.java @@ -0,0 +1,65 @@ +/* + * Nextcloud Talk application + * + * @author Mario Danic + * Copyright (C) 2017-2018 Mario Danic + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.nextcloud.talk.utils; + +import com.nextcloud.talk.models.database.UserEntity; + +public class ApplicationWideCurrentRoomHolder { + private String currentRoomId = ""; + private UserEntity userInRoom = new UserEntity(); + private boolean inCall = false; + + private static final ApplicationWideCurrentRoomHolder holder = new ApplicationWideCurrentRoomHolder(); + + public static ApplicationWideCurrentRoomHolder getInstance() { + return holder; + } + + public void clear() { + currentRoomId = ""; + userInRoom = new UserEntity(); + inCall = false; + } + + public String getCurrentRoomId() { + return currentRoomId; + } + + public void setCurrentRoomId(String currentRoomId) { + this.currentRoomId = currentRoomId; + } + + public UserEntity getUserInRoom() { + return userInRoom; + } + + public void setUserInRoom(UserEntity userInRoom) { + this.userInRoom = userInRoom; + } + + public boolean isInCall() { + return inCall; + } + + public void setInCall(boolean inCall) { + this.inCall = inCall; + } +} 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 67e4fc459..e6bc19d88 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 @@ -49,4 +49,5 @@ public class BundleKeys { public static final String KEY_ACTIVE_CONVERSATION = "KEY_ACTIVE_CONVERSATION"; public static final String KEY_SPREED_CAPABILITIES = "KEY_SPREED_CAPABILITIES"; public static final String KEY_FROM_NOTIFICATION_START_CALL = "KEY_FROM_NOTIFICATION_START_CALL"; + public static final String KEY_ROOM_ID = "KEY_ROOM_ID"; }