Add Foreground service when data are updated.

This commit is contained in:
Benoit Marty 2022-07-19 16:46:04 +02:00 committed by Benoit Marty
parent b294c9a1fd
commit 9dda647c52
4 changed files with 88 additions and 0 deletions

View file

@ -380,6 +380,11 @@
android:exported="false"
android:foregroundServiceType="location" />
<service
android:name=".features.start.StartAppAndroidService"
android:exported="false"
android:foregroundServiceType="dataSync" />
<service
android:name=".features.call.webrtc.ScreenCaptureAndroidService"
android:exported="false"

View file

@ -21,6 +21,7 @@ import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Parcelable
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
@ -48,6 +49,7 @@ import im.vector.app.features.session.VectorSessionStore
import im.vector.app.features.settings.VectorPreferences
import im.vector.app.features.signout.hard.SignedOutActivity
import im.vector.app.features.start.StartAppAction
import im.vector.app.features.start.StartAppAndroidService
import im.vector.app.features.start.StartAppViewEvent
import im.vector.app.features.start.StartAppViewModel
import im.vector.app.features.start.StartAppViewState
@ -144,6 +146,11 @@ class MainActivity : VectorBaseActivity<ActivityMainBinding>(), UnlockedActivity
views.status.setText(R.string.updating_your_data)
}
views.status.isVisible = state.duration > 0
if (state.duration == 1L && startAppViewModel.shouldStartApp()) {
// Start foreground service, because the operation may take a while
val intent = Intent(this, StartAppAndroidService::class.java)
ContextCompat.startForegroundService(this, intent)
}
}
private fun handleViewEvents(event: StartAppViewEvent) {

View file

@ -568,6 +568,19 @@ class NotificationUtils @Inject constructor(
.build()
}
/**
* Creates a notification that indicates the application is initializing.
*/
fun buildStartAppNotification(): Notification {
return NotificationCompat.Builder(context, LISTENING_FOR_EVENTS_NOTIFICATION_CHANNEL_ID)
.setContentTitle(stringProvider.getString(R.string.updating_your_data))
.setSmallIcon(R.drawable.sync)
.setColor(ThemeUtils.getColor(context, android.R.attr.colorPrimary))
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.setPriority(NotificationCompat.PRIORITY_LOW)
.build()
}
fun buildDownloadFileNotification(uri: Uri, fileName: String, mimeType: String): Notification {
return NotificationCompat.Builder(context, SILENT_NOTIFICATION_CHANNEL_ID)
.setGroup(stringProvider.getString(R.string.app_name))

View file

@ -0,0 +1,63 @@
/*
* Copyright (c) 2022 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.app.features.start
import android.content.Intent
import dagger.hilt.android.AndroidEntryPoint
import im.vector.app.core.di.ActiveSessionHolder
import im.vector.app.core.di.NamedGlobalScope
import im.vector.app.core.services.VectorAndroidService
import im.vector.app.features.notifications.NotificationUtils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import javax.inject.Inject
import kotlin.random.Random
import kotlin.time.Duration.Companion.seconds
/**
* A simple foreground service that let the app (and the SDK) time to initialize.
* Will self stop itself once the active session is set.
*/
@AndroidEntryPoint
class StartAppAndroidService : VectorAndroidService() {
@NamedGlobalScope @Inject lateinit var globalScope: CoroutineScope
@Inject lateinit var notificationUtils: NotificationUtils
@Inject lateinit var activeSessionHolder: ActiveSessionHolder
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
showStickyNotification()
startPollingActiveSession()
return START_STICKY
}
private fun startPollingActiveSession() {
globalScope.launch {
do {
delay(1.seconds.inWholeMilliseconds)
} while (activeSessionHolder.hasActiveSession().not())
myStopSelf()
}
}
private fun showStickyNotification() {
val notificationId = Random.nextInt()
val notification = notificationUtils.buildStartAppNotification()
startForeground(notificationId, notification)
}
}