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

View File

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

View File

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