streaming: fix streaming may be disabled without running service, fix crash on start up

This commit is contained in:
Alibek Omarov 2020-10-05 15:11:09 +03:00
parent 2e9b2cf409
commit 1514ee65f7
3 changed files with 33 additions and 15 deletions

View File

@ -201,8 +201,6 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, HasAndroidInje
setupTabs(showNotificationTab)
initPullNotifications()
eventHub.events
.observeOn(AndroidSchedulers.mainThread())
.autoDispose(this, Lifecycle.Event.ON_DESTROY)
@ -227,8 +225,8 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, HasAndroidInje
private fun initPullNotifications() {
if (NotificationHelper.areNotificationsEnabled(this, accountManager)) {
if(accountManager.areNotificationsStreamingEnabled()) {
NotificationHelper.disablePullNotifications(this)
StreamingService.startStreaming(this)
NotificationHelper.disablePullNotifications(this)
} else {
StreamingService.stopStreaming(this)
NotificationHelper.enablePullNotifications(this)
@ -601,9 +599,7 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, HasAndroidInje
conversationRepository.deleteCacheForAccount(activeAccount.id)
removeShortcut(this, activeAccount)
val newAccount = accountManager.logActiveAccountOut()
if (!NotificationHelper.areNotificationsEnabled(this, accountManager)) {
NotificationHelper.disablePullNotifications(this)
}
initPullNotifications()
val intent = if (newAccount == null) {
LoginActivity.getIntent(this, false)
} else {
@ -659,6 +655,8 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, HasAndroidInje
accountManager.updateActiveAccount(me)
NotificationHelper.createNotificationChannelsForAccount(accountManager.activeAccount!!, this)
initPullNotifications()
// Show follow requests in the menu, if this is a locked account.
if (me.locked && mainDrawer.getDrawerItem(DRAWER_ITEM_FOLLOW_REQUESTS) == null) {
val followRequestsItem = primaryDrawerItem {

View File

@ -716,7 +716,7 @@ public class NotificationHelper {
Poll poll = notification.getStatus().getPoll();
for(PollOption option: poll.getOptions()) {
builder.append(buildDescription(option.getTitle(),
PollViewDataKt.calculatePercent(option.getVotesCount(), poll.getVotersCount(), poll.getVoterCount()),
PollViewDataKt.calculatePercent(option.getVotesCount(), poll.getVotersCount(), poll.getVotesCount()),
context));
builder.append('\n');
}

View File

@ -63,7 +63,7 @@ class StreamingService: Service(), Injectable {
}
}
private fun stopStreaming() : Int {
private fun stopStreaming() {
for(sock in sockets) {
sock.value.close(1000, null)
}
@ -74,7 +74,10 @@ class StreamingService: Service(), Injectable {
}
notificationManager.cancel(1337)
return START_NOT_STICKY
synchronized(serviceRunning) {
serviceRunning = false
}
}
override fun onDestroy() {
@ -83,7 +86,7 @@ class StreamingService: Service(), Injectable {
}
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
if(intent.hasExtra(KEY_STOP_STREAMING)) {
if(intent.getBooleanExtra(KEY_STOP_STREAMING, false)) {
Log.d(TAG, "Stream goes suya..")
stopStreaming()
stopSelfResult(startId)
@ -118,7 +121,9 @@ class StreamingService: Service(), Injectable {
if(count <= 0) {
Log.d(TAG, "No accounts. Stopping stream")
return stopStreaming()
stopStreaming()
stopSelfResult(startId)
return START_NOT_STICKY
}
if (NotificationHelper.NOTIFICATION_USE_CHANNELS) {
@ -142,6 +147,10 @@ class StreamingService: Service(), Injectable {
notificationManager.notify(1337, builder.build())
}
synchronized(serviceRunning) {
serviceRunning = true
}
return START_NOT_STICKY
}
@ -150,6 +159,9 @@ class StreamingService: Service(), Injectable {
val KEY_STOP_STREAMING = "stop_streaming"
val TAG = "StreamingService"
@JvmStatic
var serviceRunning = false
@JvmStatic
private fun startForegroundService(ctx: Context, intent: Intent) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
@ -162,6 +174,7 @@ class StreamingService: Service(), Injectable {
@JvmStatic
fun startStreaming(context: Context) {
val intent = Intent(context, StreamingService::class.java)
intent.putExtra(KEY_STOP_STREAMING, false)
Log.d(TAG, "Starting notifications streaming service...")
@ -170,12 +183,19 @@ class StreamingService: Service(), Injectable {
@JvmStatic
fun stopStreaming(context: Context) {
val intent = Intent(context, StreamingService::class.java)
intent.putExtra(KEY_STOP_STREAMING, 0)
synchronized(serviceRunning) {
if(!serviceRunning)
return
Log.d(TAG, "Stopping notifications streaming service...")
val intent = Intent(context, StreamingService::class.java)
intent.putExtra(KEY_STOP_STREAMING, true)
startForegroundService(context, intent)
Log.d(TAG, "Stopping notifications streaming service...")
serviceRunning = false
startForegroundService(context, intent)
}
}
}