Skip to content

Commit b12be6b

Browse files
Merge pull request #221 from mindbox-moscow/develop
Update master
2 parents 28d9585 + ca45f76 commit b12be6b

File tree

8 files changed

+150
-43
lines changed

8 files changed

+150
-43
lines changed

.github/workflows/publish.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ jobs:
9393

9494
- name: Publish to Jitpack
9595
# To create release in Jitpack we need to create Github release
96-
run: ./git-release.sh "Automatically generated Pre-release" "${{secrets.GITHUBACCESSTOKEN}}" "${{secrets.GITHUBUSER}}"
96+
run: ./git-release.sh "${{secrets.GITHUBACCESSTOKEN}}" "${{secrets.GITHUBUSER}}"
9797

9898
- name: Publish to Sonatype
9999
uses: eskatos/gradle-command-action@v1

git-release.sh

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,64 @@
11
#!/bin/bash
22
set -e
3-
version=$(cat gradle.properties |grep SDK_VERSION_NAME | cut -f2 -d"=")
4-
text=$1
5-
token=$2
6-
user=$3
3+
version=$(cat gradle.properties | grep SDK_VERSION_NAME | cut -f2 -d"=")
4+
token=$1
5+
user=$2
76
branch=${GITHUB_REF##*/}
87
repo_full_name=$(git config --get remote.origin.url | sed 's/.*:\/\/github.com\///;s/.git$//' | cut -f2 -d":")
9-
generate_post_data()
10-
{
8+
generate_post_data() {
119
cat <<EOF
1210
{
1311
"tag_name": "$version",
1412
"target_commitish": "$branch",
15-
"name": "Pre-release-$version",
16-
"body": "$text",
13+
"name": "$name",
14+
"body": "$body",
1715
"draft": false,
18-
"prerelease": true
16+
"prerelease": $prerelease
1917
}
2018
EOF
2119
}
22-
set-tag(){
20+
set-tag() {
2321
set-local-tag
2422
#set-remote-tag
2523
}
26-
set-local-tag(){
27-
if [ $(git tag -l | grep $version) ]; then
24+
prepare-release-data() {
25+
if [[ $version =~ ^[0-9.]+$ ]]; then
26+
name="Release-$version"
27+
# release_notes_1=$(curl -s --show-error --user "$user:$token" -H "Accept: application/vnd.github.v3+json" "https://api.github.com/repos/$repo_full_name/pulls?base=$branch" | grep "\"body\":" | cut -f2- -d:)
28+
# release_notes="${release_notes_1:2:${#release_notes_1}-4}"
29+
release_notes=""
30+
body="# What's new:\n$release_notes"
31+
prerelease=false
32+
else
33+
name="Pre-release-$version"
34+
body="Automatically generated pre-release"
35+
prerelease=true
36+
fi
37+
}
38+
39+
set-local-tag() {
40+
if [ $(git tag -l | grep $version) ]; then
2841
echo "Local tag cleanup"
2942
git tag -d $version
3043
git tag $version
31-
else
44+
else
3245
git tag $version
33-
fi
46+
fi
3447
}
35-
set-remote-tag(){
36-
if [ $(git ls-remote --tags https://$user:$token@github.com/$repo_full_name.git | cut -f3 -d"/" | grep $version) ]; then
48+
set-remote-tag() {
49+
if [ $(git ls-remote --tags https://$user:$token@github.com/$repo_full_name.git | cut -f3 -d"/" | grep $version) ]; then
3750
echo "Remote tag cleanup"
3851
git push --delete https://$user:$token@github.com/$repo_full_name.git $version
3952
git push https://$user:$token@github.com/$repo_full_name.git $version
40-
else
53+
else
4154
git push https://$user:$token@github.com/$repo_full_name.git $version
42-
fi
55+
fi
4356
}
4457
echo "Create release $version for repo: $repo_full_name branch: $branch"
4558
echo "Release settings: $(generate_post_data)"
46-
post-request(){
59+
post-request() {
4760
curl -s --show-error --user "$user:$token" --data "$(generate_post_data)" "https://api.github.com/repos/$repo_full_name/releases"
4861
}
4962
set-tag
50-
post-request
63+
prepare-release-data
64+
post-request

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ android.enableJetifier=true
2121
kotlin.code.style=official
2222

2323
# SDK version property
24-
SDK_VERSION_NAME=2.1.0
24+
SDK_VERSION_NAME=2.1.1

modulesCommon.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ android {
1414
buildToolsVersion "29.0.3"
1515

1616
defaultConfig {
17-
minSdkVersion MIN_SDK_VERSION
17+
minSdkVersion 19
1818
targetSdkVersion 31
1919
versionCode 1
2020
multiDexEnabled true

sdk/gradle.properties

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
ARTIFACT_ID=mobile-sdk
2-
ARTIFACT_NAME=Mindbox Android SDK
3-
MIN_SDK_VERSION=16
2+
ARTIFACT_NAME=Mindbox Android SDK

sdk/src/main/java/cloud/mindbox/mobile_sdk/Mindbox.kt

Lines changed: 101 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ object Mindbox {
6161
private const val DELIVER_TOKEN_DELAY = 1L
6262

6363
private val mindboxJob = SupervisorJob()
64-
internal val mindboxScope = CoroutineScope(Default + mindboxJob)
64+
private val coroutineExceptionHandler = CoroutineExceptionHandler { _, throwable ->
65+
MindboxLoggerImpl.e(Mindbox, "Mindbox caught unhandled error", throwable)
66+
}
67+
internal val mindboxScope = CoroutineScope(
68+
Default + mindboxJob + coroutineExceptionHandler,
69+
)
6570
private val tokenCallbacks = ConcurrentHashMap<String, (String?) -> Unit>()
6671
private val deviceUuidCallbacks = ConcurrentHashMap<String, (String) -> Unit>()
6772

@@ -186,7 +191,7 @@ object Mindbox {
186191
*/
187192
fun updatePushToken(context: Context, token: String) = LoggingExceptionHandler.runCatching {
188193
if (token.trim().isNotEmpty()) {
189-
initComponents(context, pushServiceHandler)
194+
initComponents(context)
190195

191196
if (!MindboxPreferences.isFirstInitialize) {
192197
mindboxScope.launch {
@@ -204,7 +209,7 @@ object Mindbox {
204209
* @param uniqKey - unique identifier of push notification
205210
*/
206211
fun onPushReceived(context: Context, uniqKey: String) = LoggingExceptionHandler.runCatching {
207-
initComponents(context, pushServiceHandler)
212+
initComponents(context)
208213
MindboxEventManager.pushDelivered(context, uniqKey)
209214

210215
if (!MindboxPreferences.isFirstInitialize) {
@@ -227,7 +232,7 @@ object Mindbox {
227232
uniqKey: String,
228233
buttonUniqKey: String?,
229234
) = LoggingExceptionHandler.runCatching {
230-
initComponents(context, pushServiceHandler)
235+
initComponents(context)
231236
MindboxEventManager.pushClicked(context, TrackClickData(uniqKey, buttonUniqKey))
232237

233238
if (!MindboxPreferences.isFirstInitialize) {
@@ -265,22 +270,25 @@ object Mindbox {
265270

266271
/**
267272
* Initializes the SDK for further work.
268-
* We recommend calling it in onCreate on an application class
273+
*
274+
* We recommend calling it synchronously in onCreate on an application class
275+
*
276+
* If you must call it the other way, invoke [Mindbox.setPushServiceHandler] in [Application.onCreate] or else pushes won't be shown when application is inactive
269277
*
270278
* @param context used to initialize the main tools
271279
* @param configuration contains the data that is needed to connect to the Mindbox
280+
* @param pushServices list, containing [MindboxPushService]s, i.e.
281+
* ```
282+
* listOf(MindboxFirebase, MindboxHuawei)
283+
* ```
272284
*/
273285
fun init(
274286
context: Context,
275287
configuration: MindboxConfiguration,
276288
pushServices: List<MindboxPushService>,
277289
) {
278290
LoggingExceptionHandler.runCatching {
279-
val pushService = pushServices
280-
.map { it.getServiceHandler(MindboxLoggerImpl, LoggingExceptionHandler) }
281-
.firstOrNull { it.isServiceAvailable(context) }
282-
283-
initComponents(context, pushService)
291+
initComponents(context, pushServices)
284292

285293
mindboxScope.launch {
286294
if (MindboxPreferences.isFirstInitialize) {
@@ -310,6 +318,14 @@ object Mindbox {
310318
"Incorrect context type for calling init in this place",
311319
)
312320
}
321+
if (isApplicationResumed || context !is Application) {
322+
MindboxLoggerImpl.w(
323+
this,
324+
"We recommend to call Mindbox.init() synchronously from " +
325+
"Application.onCreate. If you can't do so, don't forget to " +
326+
"call Mindbox.initPushServices from Application.onCreate",
327+
)
328+
}
313329

314330
lifecycleManager = LifecycleManager(
315331
currentActivityName = activity?.javaClass?.name,
@@ -333,6 +349,75 @@ object Mindbox {
333349
}
334350
}
335351

352+
/**
353+
* Method to initialise push services
354+
*
355+
* You must call this method in onCreate in your Application class if you call [Mindbox.init] not there
356+
*
357+
* @param context used to initialize the main tools
358+
* @param pushServices list, containing [MindboxPushService]s, i.e.
359+
* ```
360+
* listOf(MindboxFirebase, MindboxHuawei)
361+
* ```
362+
*/
363+
fun initPushServices(
364+
context: Context,
365+
pushServices: List<MindboxPushService>,
366+
) = initComponents(context, pushServices)
367+
368+
private fun setPushServiceHandler(
369+
context: Context,
370+
pushServices: List<MindboxPushService>? = null,
371+
) = LoggingExceptionHandler.runCatching {
372+
if (pushServiceHandler == null && pushServices != null) {
373+
val savedProvider = MindboxPreferences.notificationProvider
374+
selectPushServiceHandler(context, pushServices, savedProvider)
375+
?.let { pushServiceHandler ->
376+
this.pushServiceHandler = pushServiceHandler
377+
pushServiceHandler.notificationProvider
378+
.takeIf { it != savedProvider }
379+
?.let { newProvider ->
380+
MindboxPreferences.notificationProvider = newProvider
381+
if (!MindboxPreferences.isFirstInitialize) {
382+
mindboxScope.launch {
383+
updateAppInfo(context)
384+
}
385+
}
386+
}
387+
mindboxScope.launch {
388+
pushServiceHandler.initService(context)
389+
}
390+
}
391+
}
392+
}
393+
394+
private fun selectPushServiceHandler(
395+
context: Context,
396+
pushServices: List<MindboxPushService>,
397+
savedProvider: String,
398+
): PushServiceHandler? {
399+
val serviceHandlers = pushServices
400+
.map { it.getServiceHandler(MindboxLoggerImpl, LoggingExceptionHandler) }
401+
return serviceHandlers.firstOrNull { it.notificationProvider == savedProvider }
402+
?: initAvailablePushService(context, serviceHandlers, savedProvider)
403+
}
404+
405+
private fun initAvailablePushService(
406+
context: Context,
407+
serviceHandlers: List<PushServiceHandler>,
408+
savedProvider: String,
409+
) = if (savedProvider.isBlank()) {
410+
serviceHandlers.firstOrNull { it.isServiceAvailable(context) }
411+
} else {
412+
MindboxLoggerImpl.e(
413+
Mindbox,
414+
"Mindbox was previously initialized with $savedProvider push service but " +
415+
"Mindbox did not find it within pushServices. Check your Mindbox.init() and " +
416+
"Mindbox.initPushServices()",
417+
)
418+
null
419+
}
420+
336421
/**
337422
* Send track visit event after link or push was clicked for [Activity] with launchMode equals
338423
* "singleTop" or "singleTask" or if a client used the [Intent.FLAG_ACTIVITY_SINGLE_TOP] or
@@ -560,13 +645,10 @@ object Mindbox {
560645
}, DELIVER_TOKEN_DELAY, TimeUnit.SECONDS)
561646
}
562647

563-
internal fun initComponents(context: Context, pushServiceHandler: PushServiceHandler?) {
648+
internal fun initComponents(context: Context, pushServices: List<MindboxPushService>? = null) {
564649
SharedPreferencesManager.with(context)
565650
DbManager.init(context)
566-
this.pushServiceHandler = pushServiceHandler
567-
mindboxScope.launch {
568-
pushServiceHandler?.initService(context)
569-
}
651+
setPushServiceHandler(context, pushServices)
570652
}
571653

572654
private fun <T> asyncOperation(
@@ -596,7 +678,7 @@ object Mindbox {
596678
operationSystemName: String,
597679
) = LoggingExceptionHandler.runCatching(defaultValue = false) {
598680
if (operationSystemName.matches(OPERATION_NAME_REGEX.toRegex())) {
599-
initComponents(context, pushServiceHandler)
681+
initComponents(context)
600682
} else {
601683
MindboxLoggerImpl.w(
602684
this,
@@ -628,6 +710,7 @@ object Mindbox {
628710
DbManager.saveConfigurations(Configuration(configuration))
629711

630712
val isTokenAvailable = !pushToken.isNullOrEmpty()
713+
val notificationProvider = pushServiceHandler?.notificationProvider ?: ""
631714
val initData = InitData(
632715
token = pushToken ?: "",
633716
isTokenAvailable = isTokenAvailable,
@@ -636,13 +719,14 @@ object Mindbox {
636719
isNotificationsEnabled = isNotificationEnabled,
637720
subscribe = configuration.subscribeCustomerIfCreated,
638721
instanceId = instanceId,
639-
notificationProvider = pushServiceHandler?.notificationProvider ?: "",
722+
notificationProvider = notificationProvider,
640723
)
641724

642725
MindboxPreferences.deviceUuid = deviceUuid
643726
MindboxPreferences.pushToken = pushToken
644727
MindboxPreferences.isNotificationEnabled = isNotificationEnabled
645728
MindboxPreferences.instanceId = instanceId
729+
MindboxPreferences.notificationProvider = notificationProvider
646730

647731
MindboxEventManager.appInstalled(context, initData, configuration.shouldCreateCustomer)
648732

sdk/src/main/java/cloud/mindbox/mobile_sdk/managers/WorkerDelegate.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ internal class WorkerDelegate {
2323
MindboxLoggerImpl.d(parent, "Start working...")
2424

2525
try {
26-
Mindbox.initComponents(context, Mindbox.pushServiceHandler)
26+
Mindbox.initComponents(context)
2727

2828
Mindbox.pushServiceHandler?.ensureVersionCompatibility(context, parent)
2929

sdk/src/main/java/cloud/mindbox/mobile_sdk/repository/MindboxPreferences.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ internal object MindboxPreferences {
1515
"key_host_app_name" //need for scheduling and stopping one-time background service
1616
private const val KEY_INFO_UPDATED_VERSION = "key_info_updated_version"
1717
private const val KEY_INSTANCE_ID = "key_instance_id"
18+
private const val KEY_NOTIFICATION_PROVIDER = "key_notification_provider"
1819
private const val DEFAULT_INFO_UPDATED_VERSION = 1
1920

2021
var isFirstInitialize: Boolean
@@ -100,4 +101,13 @@ internal object MindboxPreferences {
100101
}
101102
}
102103

104+
var notificationProvider: String
105+
get() = LoggingExceptionHandler.runCatching(defaultValue = "") {
106+
SharedPreferencesManager.getString(KEY_NOTIFICATION_PROVIDER) ?: ""
107+
}
108+
set(value) {
109+
LoggingExceptionHandler.runCatching {
110+
SharedPreferencesManager.put(KEY_NOTIFICATION_PROVIDER, value)
111+
}
112+
}
103113
}

0 commit comments

Comments
 (0)