-
Notifications
You must be signed in to change notification settings - Fork 4
WMSDK-524: initPushProviders automatically #117
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
android/src/main/java/com/mindboxsdk/MindboxPushServicesDiscovery.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| package com.mindboxsdk | ||
|
|
||
| import android.os.Bundle | ||
| import android.util.Log | ||
| import cloud.mindbox.mobile_sdk.pushes.MindboxPushService | ||
|
|
||
| internal class MindboxPushServicesDiscovery(private val meta: Bundle?) { | ||
|
|
||
| companion object { | ||
| private const val TAG = "MindboxPushServicesDiscovery" | ||
| private const val PUSH_PROVIDER_PREFIX = "cloud.mindbox.push.provider." | ||
| } | ||
|
|
||
| private val providerPriority: Map<String, Int> by lazy(LazyThreadSafetyMode.NONE) { | ||
| mapOf("FCM" to 0, "HCM" to 1, "RuStore" to 2) | ||
| } | ||
|
|
||
| val services: List<MindboxPushService> by lazy(LazyThreadSafetyMode.NONE) { | ||
| if (meta == null) { | ||
| return@lazy emptyList() | ||
| } | ||
| meta.keySet() | ||
| .asSequence() | ||
| .filter { it.startsWith(PUSH_PROVIDER_PREFIX) } | ||
| .mapNotNull { key -> meta.getString(key)?.takeIf { it.isNotBlank() } } | ||
| .mapNotNull { className -> loadPushService(className) } | ||
| .distinctBy { it::class.java.name } | ||
| .sortedWith( | ||
| compareBy<MindboxPushService> { providerPriority[it.tag] ?: Int.MAX_VALUE } | ||
| .thenBy { it.tag } | ||
| ) | ||
| .toList() | ||
| .also { list -> | ||
| Log.i(TAG, "Found ${list.size} push services: ${list.joinToString { it.tag }}") | ||
| list | ||
| } | ||
| } | ||
|
|
||
| private fun loadPushService(className: String): MindboxPushService? { | ||
| return runCatching { | ||
| val clazz = Class.forName(className) | ||
|
|
||
| if (!MindboxPushService::class.java.isAssignableFrom(clazz)) { | ||
| Log.w(TAG, "Class $className does not implement MindboxPushService") | ||
| return@runCatching null | ||
| } | ||
| getPushServiceInstance(clazz)?.also { | ||
| Log.i(TAG, "Loaded push provider: $className") | ||
| } ?: run { | ||
| Log.e(TAG, "Failed to create instance for provider: $className") | ||
| null | ||
| } | ||
| }.getOrElse { exception -> | ||
| when (exception) { | ||
| is ClassNotFoundException -> Log.e(TAG, "Push provider class not found: $className", exception) | ||
| else -> Log.e(TAG, "Failed to load push provider: $className", exception) | ||
| } | ||
| null | ||
| } | ||
| } | ||
|
|
||
| private fun getPushServiceInstance(clazz: Class<*>): MindboxPushService? { | ||
| return runCatching { | ||
| val instanceField = clazz.getDeclaredField("INSTANCE") | ||
| instanceField.isAccessible = true | ||
| instanceField.get(null) as? MindboxPushService | ||
| }.getOrElse { | ||
| Log.w(TAG, "Failed to get instance for ${clazz.name}") | ||
| null | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.