Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that a PushProvider is available on a device before using it. #2248

Merged
merged 2 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/2248.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fallback to UnifiedPush (if available) if the PlayServices are not installed on the device.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ interface PushService {
// TODO Move away
fun notificationStyleChanged()

/**
* Return the list of push providers, available at compile time, and
* available at runtime, sorted by index.
*/
fun getAvailablePushProviders(): List<PushProvider>

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@
}

override fun getAvailablePushProviders(): List<PushProvider> {
return pushProviders.sortedBy { it.index }
return pushProviders
.filter { it.isAvailable() }
.sortedBy { it.index }

Check warning on line 43 in libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/DefaultPushService.kt

View check run for this annotation

Codecov / codecov/patch

libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/DefaultPushService.kt#L41-L43

Added lines #L41 - L43 were not covered by tests
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ interface PushProvider {
*/
val name: String

/**
* Return true if the push provider is available on this device.
*/
fun isAvailable(): Boolean

fun getDistributors(): List<Distributor>

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@

package io.element.android.libraries.pushproviders.firebase

import android.content.Context
import com.google.android.gms.common.ConnectionResult
import com.google.android.gms.common.GoogleApiAvailability
import com.squareup.anvil.annotations.ContributesMultibinding
import io.element.android.libraries.core.log.logger.LoggerTag
import io.element.android.libraries.di.AppScope
import io.element.android.libraries.di.ApplicationContext
import io.element.android.libraries.matrix.api.MatrixClient
import io.element.android.libraries.pushproviders.api.Distributor
import io.element.android.libraries.pushproviders.api.PushProvider
Expand All @@ -30,13 +34,27 @@

@ContributesMultibinding(AppScope::class)
class FirebasePushProvider @Inject constructor(
@ApplicationContext private val context: Context,

Check warning on line 37 in libraries/pushproviders/firebase/src/main/kotlin/io/element/android/libraries/pushproviders/firebase/FirebasePushProvider.kt

View check run for this annotation

Codecov / codecov/patch

libraries/pushproviders/firebase/src/main/kotlin/io/element/android/libraries/pushproviders/firebase/FirebasePushProvider.kt#L37

Added line #L37 was not covered by tests
private val firebaseStore: FirebaseStore,
private val firebaseTroubleshooter: FirebaseTroubleshooter,
private val pusherSubscriber: PusherSubscriber,
) : PushProvider {
override val index = FirebaseConfig.INDEX
override val name = FirebaseConfig.NAME

override fun isAvailable(): Boolean {
// The PlayServices has to be available
val apiAvailability = GoogleApiAvailability.getInstance()
val resultCode = apiAvailability.isGooglePlayServicesAvailable(context)

Check warning on line 48 in libraries/pushproviders/firebase/src/main/kotlin/io/element/android/libraries/pushproviders/firebase/FirebasePushProvider.kt

View check run for this annotation

Codecov / codecov/patch

libraries/pushproviders/firebase/src/main/kotlin/io/element/android/libraries/pushproviders/firebase/FirebasePushProvider.kt#L47-L48

Added lines #L47 - L48 were not covered by tests
return if (resultCode == ConnectionResult.SUCCESS) {
Timber.tag(loggerTag.value).d("Google Play Services is available")
true

Check warning on line 51 in libraries/pushproviders/firebase/src/main/kotlin/io/element/android/libraries/pushproviders/firebase/FirebasePushProvider.kt

View check run for this annotation

Codecov / codecov/patch

libraries/pushproviders/firebase/src/main/kotlin/io/element/android/libraries/pushproviders/firebase/FirebasePushProvider.kt#L50-L51

Added lines #L50 - L51 were not covered by tests
} else {
Timber.tag(loggerTag.value).w("Google Play Services is not available")
false

Check warning on line 54 in libraries/pushproviders/firebase/src/main/kotlin/io/element/android/libraries/pushproviders/firebase/FirebasePushProvider.kt

View check run for this annotation

Codecov / codecov/patch

libraries/pushproviders/firebase/src/main/kotlin/io/element/android/libraries/pushproviders/firebase/FirebasePushProvider.kt#L53-L54

Added lines #L53 - L54 were not covered by tests
}
}

override fun getDistributors(): List<Distributor> {
return listOf(Distributor("Firebase", "Firebase"))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@
import android.content.Context
import com.squareup.anvil.annotations.ContributesMultibinding
import io.element.android.libraries.androidutils.system.getApplicationLabel
import io.element.android.libraries.core.log.logger.LoggerTag
import io.element.android.libraries.di.AppScope
import io.element.android.libraries.di.ApplicationContext
import io.element.android.libraries.matrix.api.MatrixClient
import io.element.android.libraries.pushproviders.api.Distributor
import io.element.android.libraries.pushproviders.api.PushProvider
import io.element.android.libraries.pushstore.api.clientsecret.PushClientSecret
import org.unifiedpush.android.connector.UnifiedPush
import timber.log.Timber
import javax.inject.Inject

private val loggerTag = LoggerTag("UnifiedPushProvider", LoggerTag.PushLoggerTag)

Check warning on line 33 in libraries/pushproviders/unifiedpush/src/main/kotlin/io/element/android/libraries/pushproviders/unifiedpush/UnifiedPushProvider.kt

View check run for this annotation

Codecov / codecov/patch

libraries/pushproviders/unifiedpush/src/main/kotlin/io/element/android/libraries/pushproviders/unifiedpush/UnifiedPushProvider.kt#L33

Added line #L33 was not covered by tests

@ContributesMultibinding(AppScope::class)
class UnifiedPushProvider @Inject constructor(
@ApplicationContext private val context: Context,
Expand All @@ -38,6 +42,17 @@
override val index = UnifiedPushConfig.INDEX
override val name = UnifiedPushConfig.NAME

override fun isAvailable(): Boolean {
val isAvailable = getDistributors().isNotEmpty()

Check warning on line 46 in libraries/pushproviders/unifiedpush/src/main/kotlin/io/element/android/libraries/pushproviders/unifiedpush/UnifiedPushProvider.kt

View check run for this annotation

Codecov / codecov/patch

libraries/pushproviders/unifiedpush/src/main/kotlin/io/element/android/libraries/pushproviders/unifiedpush/UnifiedPushProvider.kt#L46

Added line #L46 was not covered by tests
return if (isAvailable) {
Timber.tag(loggerTag.value).d("UnifiedPush is available")
true

Check warning on line 49 in libraries/pushproviders/unifiedpush/src/main/kotlin/io/element/android/libraries/pushproviders/unifiedpush/UnifiedPushProvider.kt

View check run for this annotation

Codecov / codecov/patch

libraries/pushproviders/unifiedpush/src/main/kotlin/io/element/android/libraries/pushproviders/unifiedpush/UnifiedPushProvider.kt#L48-L49

Added lines #L48 - L49 were not covered by tests
} else {
Timber.tag(loggerTag.value).w("UnifiedPush is not available")
false

Check warning on line 52 in libraries/pushproviders/unifiedpush/src/main/kotlin/io/element/android/libraries/pushproviders/unifiedpush/UnifiedPushProvider.kt

View check run for this annotation

Codecov / codecov/patch

libraries/pushproviders/unifiedpush/src/main/kotlin/io/element/android/libraries/pushproviders/unifiedpush/UnifiedPushProvider.kt#L51-L52

Added lines #L51 - L52 were not covered by tests
}
}

override fun getDistributors(): List<Distributor> {
val distributors = UnifiedPush.getDistributors(context)
return distributors.mapNotNull {
Expand Down
Loading