浏览代码

Improve notifications while in conversation

Signed-off-by: Mario Danic <mario@lovelyhq.com>
Mario Danic 6 年之前
父节点
当前提交
31d2d29a1f

+ 16 - 0
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();

+ 1 - 0
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))) {

+ 11 - 2
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());

+ 2 - 0
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")) {

+ 1 - 0
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));
 

+ 18 - 2
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()));
 

+ 65 - 0
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 <mario@lovelyhq.com>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+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;
+    }
+}

+ 1 - 0
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";
 }