Skip to content

Commit 970337a

Browse files
authored
Re-add preference to force websocket to remain on in background (#2169)
* feat: Add pref to enable websocket service in bg - This adds a flag in the advanced options to force the websocket to remain active when the app is not in the foreground. This is designed to enable devices without FCM to always receive notifications - The check we were using to determine if FCM was present wasn't accurate, since it only checks for Google Play Services, and not for FCM specifically. The code thus didn't work on devices with play services but with FCM disabled. The decision was made to fall back to a preference again. - For more information, see: wireapp/wire-android#2100
1 parent 4fc3c26 commit 970337a

File tree

5 files changed

+35
-14
lines changed

5 files changed

+35
-14
lines changed

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ ext {
2222
playServicesVersion = '15.0.1'
2323
audioVersion = System.getenv("AUDIO_VERSION") ?: '1.209.0@aar'
2424
stethoVersion = '1.5.0'
25-
zMessagingVersion = "141.0.2280"
25+
zMessagingVersion = "141.0.2281"
2626
paging_version = "1.0.0"
2727

2828
avsVersion = '4.9.12@aar'
@@ -36,7 +36,7 @@ ext {
3636
customAvsName = 'avs'
3737
customAvsGroup = 'com.wearezeta.avs'
3838

39-
zMessagingDevVersion = System.getenv("LOCAL_ZMESSAGING_VERSION") ?: ((System.getenv("ZMESSAGING_VERSION") ?: zMessagingVersion) + '-DEV')
39+
zMessagingDevVersion = System.getenv("LOCAL_ZMESSAGING_VERSION") ?: ((System.getenv("ZMESSAGING_VERSION") ?: zMessagingVersion + '-DEV'))
4040
zMessagingReleaseVersion = System.getenv("LOCAL_ZMESSAGING_VERSION") ?: ((System.getenv("ZMESSAGING_VERSION") ?: zMessagingVersion) + '@aar')
4141

4242
}

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@
504504
<string name="debug_report__title">Wire debug report - %1$s</string>
505505
<string name="debug_report__body">Please describe the steps required to reproduce the issue, provide screenshots if possible and send this email.\n\n</string>
506506
<string name="pref_advanced_ws_foreground_title">Maintain connection to server</string>
507-
<string name="pref_advanced_ws_foreground_summary">We have detected that Google Play Services are not installed. Wire will try to keep a connection with the server open to receive notifications while the application is not in the foreground.
507+
<string name="pref_advanced_ws_foreground_summary">Wire will try to keep a connection with the server open to receive notifications while the application is not in the foreground.
508508
Turning this setting off may improve battery performance, but you will no longer receive notifications when Wire is in the background. This applies to all accounts on this device.</string>
509509

510510
<!-- Support -->

app/src/main/scala/com/waz/services/websocket/WebSocketService.scala

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import com.waz.zclient._
3636
import com.waz.zclient.Intents.RichIntent
3737
import com.waz.zclient.log.LogUI._
3838

39-
class WebSocketController(implicit inj: Injector) extends Injectable {
39+
class WebSocketController(implicit inj: Injector) extends Injectable with DerivedLogTag {
4040
private lazy val global = inject[GlobalModule]
4141
private lazy val accounts = inject[AccountsService]
4242

@@ -49,12 +49,12 @@ class WebSocketController(implicit inj: Injector) extends Injectable {
4949
lazy val accountWebsocketStates: Signal[(Set[ZMessaging], Set[ZMessaging])] =
5050
for {
5151
cloudPushAvailable <- cloudPushAvailable
52-
uiActive <- global.lifecycle.uiActive
5352
wsForegroundEnabled <- global.prefs(WsForegroundKey).signal
53+
_ = verbose(l"wsForeground enabled: $wsForegroundEnabled")
5454
accs <- accounts.zmsInstances
5555
accsInFG <- Signal.sequence(accs.map(_.selfUserId).map(id => accounts.accountState(id).map(st => id -> st)).toSeq : _*).map(_.toMap)
5656
(zmsWithWSActive, zmsWithWSInactive) =
57-
accs.partition(zms => accsInFG(zms.selfUserId) == InForeground || (!cloudPushAvailable && (uiActive || wsForegroundEnabled)))
57+
accs.partition(zms => accsInFG(zms.selfUserId) == InForeground || wsForegroundEnabled || !cloudPushAvailable)
5858
} yield (zmsWithWSActive, zmsWithWSInactive)
5959

6060
lazy val serviceInForeground: Signal[Boolean] =
@@ -65,15 +65,21 @@ class WebSocketController(implicit inj: Injector) extends Injectable {
6565

6666
lazy val notificationTitleRes: Signal[Option[Int]] =
6767
serviceInForeground.flatMap {
68-
case true => global.network.isOnline.flatMap {
68+
case true =>
69+
verbose(l"Service in foreground")
70+
global.network.isOnline.flatMap {
6971
//checks to see if there are any accounts that haven't yet established a web socket
7072
case true => accounts.zmsInstances.flatMap(zs => Signal.sequence(zs.map(_.wsPushService.connected).toSeq: _ *).map(_.exists(!identity(_)))).map {
71-
case true => Option(R.string.ws_foreground_notification_connecting_title)
72-
case _ => Option(R.string.ws_foreground_notification_connected_title)
73+
case true => verbose(l"connecting");Option(R.string.ws_foreground_notification_connecting_title)
74+
case _ => verbose(l"connected");Option(R.string.ws_foreground_notification_connected_title)
7375
}
74-
case _ => Signal.const(Option(R.string.ws_foreground_notification_no_internet_title))
76+
case _ =>
77+
verbose(l"Returning no notification titles despite service in foreground")
78+
Signal.const(Option(R.string.ws_foreground_notification_no_internet_title))
7579
}
76-
case _ => Signal.const(Option.empty[Int])
80+
case _ =>
81+
verbose(l"Returning no notification titles")
82+
Signal.const(Option.empty[Int])
7783
}
7884
}
7985

app/src/main/scala/com/waz/zclient/WireApplication.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,21 @@ class WireApplication extends MultiDexApplication with WireContext with Injectab
419419
inject[ThemeController]
420420
inject[PreferencesController]
421421
Future(clearOldVideoFiles(getApplicationContext))(Threading.Background)
422+
Future(checkForPlayServices(prefs, googleApi))(Threading.Background)
423+
}
424+
425+
private def checkForPlayServices(prefs: GlobalPreferences, googleApi: GoogleApi): Unit = {
426+
val gps = prefs(GlobalPreferences.CheckedForPlayServices)
427+
gps.signal.head.collect {
428+
case false =>
429+
verbose(l"never checked for play services")
430+
googleApi.isGooglePlayServicesAvailable.head.foreach { gpsAvailable =>
431+
for {
432+
_ <- prefs(GlobalPreferences.WsForegroundKey) := !gpsAvailable
433+
_ <- gps := true
434+
} yield ()
435+
}
436+
}
422437
}
423438

424439
override def onTerminate(): Unit = {

app/src/main/scala/com/waz/zclient/preferences/pages/AdvancedView.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@ import android.widget.{LinearLayout, TextView, Toast}
2626
import com.waz.content.GlobalPreferences.WsForegroundKey
2727
import com.waz.log.BasicLogging.LogTag.DerivedLogTag
2828
import com.waz.log.LogsService
29-
import com.waz.service.{FCMNotificationStatsService, ZMessaging}
29+
import com.waz.service.{FCMNotificationStatsService, GlobalModule, ZMessaging}
3030
import com.waz.threading.{CancellableFuture, Threading}
3131
import com.waz.utils.events.Signal
3232
import com.waz.utils.returning
33-
import com.waz.utils.wrappers.GoogleApi
3433
import com.waz.zclient.preferences.views.{SwitchPreference, TextButton}
3534
import com.waz.zclient.utils.ContextUtils._
3635
import com.waz.zclient.utils.{BackStackKey, DebugUtils}
@@ -93,7 +92,8 @@ class AdvancedViewImpl(context: Context, attrs: AttributeSet, style: Int)
9392
statsDisplay.setVisible(BuildConfig.DEVELOPER_FEATURES_ENABLED)
9493

9594
val webSocketForegroundServiceSwitch = returning(findById[SwitchPreference](R.id.preferences_websocket_service)) { v =>
96-
inject[GoogleApi].isGooglePlayServicesAvailable.map(if (_) View.GONE else View.VISIBLE).onUi(v.setVisibility)
95+
v.setVisible(true)
96+
inject[GlobalModule].prefs(WsForegroundKey).signal.onUi(v.setChecked(_))
9797
v.setPreference(WsForegroundKey, global = true)
9898
}
9999

0 commit comments

Comments
 (0)