Skip to content

Commit b5db4cb

Browse files
fix: When the app is upgraded the received intent crashed the app (#2139)
1 parent c58c023 commit b5db4cb

File tree

1 file changed

+30
-21
lines changed

1 file changed

+30
-21
lines changed

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

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -86,31 +86,40 @@ class OnBootAndUpdateBroadcastReceiver extends BroadcastReceiver with DerivedLog
8686
implicit lazy val injector: Injector =
8787
context.getApplicationContext.asInstanceOf[WireApplication].module
8888

89-
private lazy val controller =
90-
injector.binding[WebSocketController].getOrElse(throw new Exception(s"Failed to load WebSocketController")).apply()
91-
92-
private lazy val accounts =
93-
injector.binding[AccountsService].getOrElse(throw new Exception(s"Failed to load AccountsService")).apply()
94-
9589
override def onReceive(context: Context, intent: Intent): Unit = {
9690
this.context = context
9791
verbose(l"onReceive ${RichIntent(intent)}")
9892

99-
accounts.zmsInstances.head.foreach { zs =>
100-
zs.map(_.selfUserId).foreach(PushTokenCheckJob(_))
101-
} (Threading.Background)
102-
103-
104-
controller.serviceInForeground.head.foreach {
105-
case true =>
106-
verbose(l"startForegroundService")
107-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
108-
context.startForegroundService(new Intent(context, classOf[WebSocketService]))
109-
else
110-
WebSocketService(context)
111-
case false =>
112-
verbose(l"foreground service not needed, will wait for application to start service if necessary")
113-
} (Threading.Ui)
93+
WireApplication.APP_INSTANCE.ensureInitialized()
94+
95+
injector.binding[AccountsService] match {
96+
case Some(accounts) =>
97+
verbose(l"AccountsService loaded")
98+
accounts().zmsInstances.head.foreach { zs =>
99+
zs.map(_.selfUserId).foreach(PushTokenCheckJob(_))
100+
} (Threading.Background)
101+
102+
case _ =>
103+
error(l"Failed to load AccountsService")
104+
}
105+
106+
injector.binding[WebSocketController] match {
107+
case Some(controller) =>
108+
verbose(l"WebSocketController loaded")
109+
controller().serviceInForeground.head.foreach {
110+
case true =>
111+
verbose(l"startForegroundService")
112+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
113+
context.startForegroundService(new Intent(context, classOf[WebSocketService]))
114+
else
115+
WebSocketService(context)
116+
case false =>
117+
verbose(l"foreground service not needed, will wait for application to start service if necessary")
118+
} (Threading.Ui)
119+
120+
case None =>
121+
error(l"Failed to load WebSocketController")
122+
}
114123
}
115124
}
116125

0 commit comments

Comments
 (0)