1
0
Fork 0
fenix/app/src/main/java/org/mozilla/fenix/components/BackgroundServices.kt

82 lines
3.3 KiB
Kotlin
Raw Normal View History

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.fenix.components
import android.content.Context
import androidx.lifecycle.ProcessLifecycleOwner
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import mozilla.components.browser.storage.sync.PlacesBookmarksStorage
import mozilla.components.browser.storage.sync.PlacesHistoryStorage
import mozilla.components.concept.sync.DeviceCapability
import mozilla.components.concept.sync.DeviceEvent
import mozilla.components.concept.sync.DeviceEventsObserver
import mozilla.components.concept.sync.DeviceType
import mozilla.components.feature.sync.BackgroundSyncManager
import mozilla.components.feature.sync.GlobalSyncableStoreProvider
import mozilla.components.service.fxa.Config
import mozilla.components.service.fxa.manager.DeviceTuple
import mozilla.components.service.fxa.manager.FxaAccountManager
import mozilla.components.support.base.log.logger.Logger
import org.mozilla.fenix.R
import org.mozilla.fenix.test.Mockable
/**
* Component group for background services. These are the components that need to be accessed from within a
* background worker.
*/
@Mockable
class BackgroundServices(
context: Context,
historyStorage: PlacesHistoryStorage,
bookmarkStorage: PlacesBookmarksStorage,
notificationManager: NotificationManager
) {
companion object {
const val CLIENT_ID = "a2270f727f45f648"
const val REDIRECT_URL = "https://accounts.firefox.com/oauth/success/$CLIENT_ID"
}
// This is slightly messy - here we need to know the union of all "scopes"
// needed by components which rely on FxA integration. If this list
// grows too far we probably want to find a way to determine the set
// at runtime.
private val scopes: Array<String> = arrayOf("profile", "https://identity.mozilla.com/apps/oldsync")
private val config = Config.release(CLIENT_ID, REDIRECT_URL)
init {
// Make the "history" and "bookmark" stores accessible to workers spawned by the sync manager.
GlobalSyncableStoreProvider.configureStore("history" to historyStorage)
GlobalSyncableStoreProvider.configureStore("bookmarks" to bookmarkStorage)
}
val syncManager = BackgroundSyncManager("https://identity.mozilla.com/apps/oldsync").also {
it.addStore("history")
it.addStore("bookmarks")
}
private val deviceEventObserver = object : DeviceEventsObserver {
private val logger = Logger("DeviceEventsObserver")
override fun onEvents(events: List<DeviceEvent>) {
logger.info("Received ${events.size} device event(s)")
events.filter { it is DeviceEvent.TabReceived }.forEach {
notificationManager.showReceivedTabs(it as DeviceEvent.TabReceived)
}
}
}
val accountManager = FxaAccountManager(
context,
config,
scopes,
DeviceTuple(context.getString(R.string.app_name), DeviceType.MOBILE, listOf(DeviceCapability.SEND_TAB)),
syncManager
).also {
it.registerForDeviceEvents(deviceEventObserver, ProcessLifecycleOwner.get(), true)
CoroutineScope(Dispatchers.Main).launch { it.initAsync().await() }
}
}