Răsfoiți Sursa

Show notification if app is not in foreground

Signed-off-by: Mario Danic <mario@lovelyhq.com>
Mario Danic 6 ani în urmă
părinte
comite
aed03980af

+ 2 - 2
app/build.gradle

@@ -79,8 +79,8 @@ dependencies {
     implementation fileTree(dir: 'libs', include: ['*.jar'])
     implementation "com.android.support:appcompat-v7:${supportLibraryVersion}"
     implementation "com.android.support:design:${supportLibraryVersion}"
-    implementation 'com.android.support.constraint:constraint-layout:1.1.1'
-
+    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
+    implementation "android.arch.lifecycle:extensions:1.1.1"
 
     implementation 'com.android.support:multidex:1.0.3'
 

+ 17 - 1
app/src/main/java/com/nextcloud/talk/application/NextcloudTalkApplication.java

@@ -20,6 +20,10 @@
  */
 package com.nextcloud.talk.application;
 
+import android.arch.lifecycle.Lifecycle;
+import android.arch.lifecycle.LifecycleObserver;
+import android.arch.lifecycle.OnLifecycleEvent;
+import android.arch.lifecycle.ProcessLifecycleOwner;
 import android.content.Context;
 import android.os.Build;
 import android.support.multidex.MultiDex;
@@ -41,6 +45,7 @@ import com.nextcloud.talk.utils.ClosedInterfaceImpl;
 import com.nextcloud.talk.utils.DeviceUtils;
 import com.nextcloud.talk.utils.DisplayUtils;
 import com.nextcloud.talk.utils.database.user.UserModule;
+import com.nextcloud.talk.utils.singletons.ApplicationWideStateHolder;
 import com.nextcloud.talk.webrtc.MagicWebRTCUtils;
 import com.squareup.leakcanary.LeakCanary;
 import com.squareup.leakcanary.RefWatcher;
@@ -69,7 +74,7 @@ import autodagger.AutoInjector;
 
 @Singleton
 @AutoInjector(NextcloudTalkApplication.class)
-public class NextcloudTalkApplication extends MultiDexApplication {
+public class NextcloudTalkApplication extends MultiDexApplication implements LifecycleObserver {
     private static final String TAG = NextcloudTalkApplication.class.getSimpleName();
 
     //region Public variables
@@ -112,6 +117,7 @@ public class NextcloudTalkApplication extends MultiDexApplication {
     @Override
     public void onCreate() {
         super.onCreate();
+        ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
 
         JobManager.create(this).addJobCreator(new MagicJobCreator());
 
@@ -188,4 +194,14 @@ public class NextcloudTalkApplication extends MultiDexApplication {
         MultiDex.install(this);
     }
     //endregion
+
+    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
+    private void onAppBackgrounded() {
+        ApplicationWideStateHolder.getInstance().setInForeground(false);
+    }
+
+    @OnLifecycleEvent(Lifecycle.Event.ON_START)
+    private void onAppForegrounded() {
+        ApplicationWideStateHolder.getInstance().setInForeground(true);
+    }
 }

+ 1 - 1
app/src/main/java/com/nextcloud/talk/controllers/AccountVerificationController.java

@@ -46,7 +46,7 @@ import com.nextcloud.talk.models.json.generic.Status;
 import com.nextcloud.talk.models.json.rooms.RoomsOverall;
 import com.nextcloud.talk.models.json.userprofile.UserProfileOverall;
 import com.nextcloud.talk.utils.ApiUtils;
-import com.nextcloud.talk.utils.ApplicationWideMessageHolder;
+import com.nextcloud.talk.utils.singletons.ApplicationWideMessageHolder;
 import com.nextcloud.talk.utils.bundle.BundleKeys;
 import com.nextcloud.talk.utils.database.user.UserUtils;
 import com.nextcloud.talk.utils.preferences.AppPreferences;

+ 1 - 1
app/src/main/java/com/nextcloud/talk/controllers/CallController.java

@@ -70,7 +70,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.singletons.ApplicationWideCurrentRoomHolder;
 import com.nextcloud.talk.utils.animations.PulseAnimation;
 import com.nextcloud.talk.utils.bundle.BundleKeys;
 import com.nextcloud.talk.utils.database.user.UserUtils;

+ 1 - 1
app/src/main/java/com/nextcloud/talk/controllers/ChatController.java

@@ -75,7 +75,7 @@ import com.nextcloud.talk.models.json.rooms.RoomOverall;
 import com.nextcloud.talk.models.json.rooms.RoomsOverall;
 import com.nextcloud.talk.presenters.MentionAutocompletePresenter;
 import com.nextcloud.talk.utils.ApiUtils;
-import com.nextcloud.talk.utils.ApplicationWideCurrentRoomHolder;
+import com.nextcloud.talk.utils.singletons.ApplicationWideCurrentRoomHolder;
 import com.nextcloud.talk.utils.KeyboardUtils;
 import com.nextcloud.talk.utils.bundle.BundleKeys;
 import com.nextcloud.talk.utils.database.user.UserUtils;

+ 1 - 1
app/src/main/java/com/nextcloud/talk/controllers/ServerSelectionController.java

@@ -45,7 +45,7 @@ import com.nextcloud.talk.application.NextcloudTalkApplication;
 import com.nextcloud.talk.controllers.base.BaseController;
 import com.nextcloud.talk.utils.AccountUtils;
 import com.nextcloud.talk.utils.ApiUtils;
-import com.nextcloud.talk.utils.ApplicationWideMessageHolder;
+import com.nextcloud.talk.utils.singletons.ApplicationWideMessageHolder;
 import com.nextcloud.talk.utils.bundle.BundleKeys;
 import com.nextcloud.talk.utils.database.user.UserUtils;
 import com.nextcloud.talk.utils.preferences.AppPreferences;

+ 1 - 1
app/src/main/java/com/nextcloud/talk/controllers/SettingsController.java

@@ -54,7 +54,7 @@ import com.nextcloud.talk.jobs.AccountRemovalJob;
 import com.nextcloud.talk.models.RingtoneSettings;
 import com.nextcloud.talk.models.database.UserEntity;
 import com.nextcloud.talk.utils.ApiUtils;
-import com.nextcloud.talk.utils.ApplicationWideMessageHolder;
+import com.nextcloud.talk.utils.singletons.ApplicationWideMessageHolder;
 import com.nextcloud.talk.utils.bundle.BundleKeys;
 import com.nextcloud.talk.utils.database.user.UserUtils;
 import com.nextcloud.talk.utils.glide.GlideApp;

+ 1 - 1
app/src/main/java/com/nextcloud/talk/controllers/WebViewLoginController.java

@@ -49,7 +49,7 @@ import com.nextcloud.talk.controllers.base.BaseController;
 import com.nextcloud.talk.events.CertificateEvent;
 import com.nextcloud.talk.models.LoginData;
 import com.nextcloud.talk.models.database.UserEntity;
-import com.nextcloud.talk.utils.ApplicationWideMessageHolder;
+import com.nextcloud.talk.utils.singletons.ApplicationWideMessageHolder;
 import com.nextcloud.talk.utils.bundle.BundleKeys;
 import com.nextcloud.talk.utils.database.user.UserUtils;
 import com.nextcloud.talk.utils.preferences.AppPreferences;

+ 1 - 1
app/src/main/java/com/nextcloud/talk/controllers/bottomsheet/EntryMenuController.java

@@ -41,7 +41,7 @@ import com.nextcloud.talk.application.NextcloudTalkApplication;
 import com.nextcloud.talk.controllers.base.BaseController;
 import com.nextcloud.talk.events.BottomSheetLockEvent;
 import com.nextcloud.talk.models.json.rooms.Room;
-import com.nextcloud.talk.utils.ApplicationWideMessageHolder;
+import com.nextcloud.talk.utils.singletons.ApplicationWideMessageHolder;
 import com.nextcloud.talk.utils.ShareUtils;
 import com.nextcloud.talk.utils.bundle.BundleKeys;
 import com.nextcloud.talk.utils.database.user.UserUtils;

+ 1 - 1
app/src/main/java/com/nextcloud/talk/controllers/bottomsheet/OperationsMenuController.java

@@ -56,7 +56,7 @@ import com.nextcloud.talk.models.json.participants.AddParticipantOverall;
 import com.nextcloud.talk.models.json.rooms.Room;
 import com.nextcloud.talk.models.json.rooms.RoomOverall;
 import com.nextcloud.talk.utils.ApiUtils;
-import com.nextcloud.talk.utils.ApplicationWideMessageHolder;
+import com.nextcloud.talk.utils.singletons.ApplicationWideMessageHolder;
 import com.nextcloud.talk.utils.DisplayUtils;
 import com.nextcloud.talk.utils.bundle.BundleKeys;
 import com.nextcloud.talk.utils.database.user.UserUtils;

+ 5 - 2
app/src/main/java/com/nextcloud/talk/jobs/NotificationJob.java

@@ -46,12 +46,13 @@ import com.nextcloud.talk.application.NextcloudTalkApplication;
 import com.nextcloud.talk.models.RingtoneSettings;
 import com.nextcloud.talk.models.SignatureVerification;
 import com.nextcloud.talk.models.json.push.DecryptedPushMessage;
-import com.nextcloud.talk.utils.ApplicationWideCurrentRoomHolder;
+import com.nextcloud.talk.utils.singletons.ApplicationWideCurrentRoomHolder;
 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 com.nextcloud.talk.utils.preferences.AppPreferences;
+import com.nextcloud.talk.utils.singletons.ApplicationWideStateHolder;
 
 import org.parceler.Parcels;
 
@@ -117,7 +118,9 @@ public class NotificationJob extends Job {
 
                         boolean shouldShowNotification = decryptedPushMessage.getApp().equals("spreed") &&
                                 !decryptedPushMessage.getType().equals("room") &&
-                                (!isInTheSameRoomAsNotification || decryptedPushMessage.getType().equals("call"));
+                                (!isInTheSameRoomAsNotification ||
+                                        !ApplicationWideStateHolder.getInstance().isInForeground() ||
+                                        decryptedPushMessage.getType().equals("call"));
 
                         if (shouldShowNotification) {
                             int smallIcon = 0;

+ 1 - 1
app/src/main/java/com/nextcloud/talk/utils/ApplicationWideCurrentRoomHolder.java → app/src/main/java/com/nextcloud/talk/utils/singletons/ApplicationWideCurrentRoomHolder.java

@@ -18,7 +18,7 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-package com.nextcloud.talk.utils;
+package com.nextcloud.talk.utils.singletons;
 
 import com.nextcloud.talk.models.database.UserEntity;
 

+ 2 - 2
app/src/main/java/com/nextcloud/talk/utils/ApplicationWideMessageHolder.java → app/src/main/java/com/nextcloud/talk/utils/singletons/ApplicationWideMessageHolder.java

@@ -2,7 +2,7 @@
  * Nextcloud Talk application
  *
  * @author Mario Danic
- * Copyright (C) 2017 Mario Danic <mario@lovelyhq.com>
+ * 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
@@ -18,7 +18,7 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-package com.nextcloud.talk.utils;
+package com.nextcloud.talk.utils.singletons;
 
 import android.support.annotation.Nullable;
 

+ 38 - 0
app/src/main/java/com/nextcloud/talk/utils/singletons/ApplicationWideStateHolder.java

@@ -0,0 +1,38 @@
+/*
+ * 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.singletons;
+
+public class ApplicationWideStateHolder {
+    private boolean isInForeground;
+    private static final ApplicationWideStateHolder holder = new ApplicationWideStateHolder();
+
+    public static ApplicationWideStateHolder getInstance() {
+        return holder;
+    }
+
+    public boolean isInForeground() {
+        return isInForeground;
+    }
+
+    public void setInForeground(boolean inForeground) {
+        isInForeground = inForeground;
+    }
+}