Skip to content

Commit

Permalink
Merge pull request #102 from Notificare/release/3.6.1
Browse files Browse the repository at this point in the history
3.6.1
  • Loading branch information
hpinhal committed Sep 8, 2023
2 parents b6544df + 415391f commit 6c5e315
Show file tree
Hide file tree
Showing 16 changed files with 143 additions and 151 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 3.6.1

- Fix race condition when synchronising monitored regions

## 3.6.0

- Allow checking which regions are being monitored
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ buildscript {
maven { url 'https://developer.huawei.com/repo' }
}
dependencies {
classpath 'com.android.tools.build:gradle:8.0.0'
classpath 'com.android.tools.build:gradle:8.1.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 're.notifica.gradle:notificare-services:1.0.1'
classpath 'com.google.gms:google-services:4.3.14'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.google.android.gms.common.GoogleApiAvailability
import com.google.android.gms.location.*
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.tasks.asDeferred
import kotlinx.coroutines.tasks.await
import re.notifica.InternalNotificareApi
import re.notifica.Notificare
import re.notifica.geo.gms.LocationReceiver
Expand Down Expand Up @@ -151,7 +152,7 @@ public class ServiceManager : ServiceManager() {
}

@SuppressLint("MissingPermission")
override fun startMonitoringRegions(regions: List<NotificareRegion>) {
override suspend fun startMonitoringRegions(regions: List<NotificareRegion>) {
val geofences = regions.map { region ->
Geofence.Builder()
.setRequestId(region.id)
Expand All @@ -171,32 +172,31 @@ public class ServiceManager : ServiceManager() {
.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER or GeofencingRequest.INITIAL_TRIGGER_DWELL or GeofencingRequest.INITIAL_TRIGGER_EXIT)
.build()

geofencingClient.addGeofences(request, geofencingPendingIntent)
.addOnSuccessListener {
NotificareLogger.debug("Successfully started monitoring ${geofences.size} geofences.")
}
.addOnFailureListener {
NotificareLogger.error("Failed to start monitoring ${geofences.size} geofences.", it)
}
try {
geofencingClient.addGeofences(request, geofencingPendingIntent)
.await()

NotificareLogger.debug("Successfully started monitoring ${geofences.size} geofences.")
} catch (e: Exception) {
NotificareLogger.error("Failed to start monitoring ${geofences.size} geofences.", e)
}
}

override fun stopMonitoringRegions(regions: List<NotificareRegion>) {
geofencingClient.removeGeofences(regions.map { it.id })
.addOnSuccessListener {
NotificareLogger.debug("Successfully stopped monitoring ${regions.size} geofences.")
}
.addOnFailureListener {
NotificareLogger.error("Failed to stop monitoring ${regions.size} geofences.", it)
}
override suspend fun stopMonitoringRegions(regions: List<NotificareRegion>) {
try {
geofencingClient.removeGeofences(regions.map { it.id }).await()
NotificareLogger.debug("Successfully stopped monitoring ${regions.size} geofences.")
} catch (e: Exception) {
NotificareLogger.error("Failed to stop monitoring ${regions.size} geofences.", e)
}
}

override fun clearMonitoringRegions() {
geofencingClient.removeGeofences(geofencingPendingIntent)
.addOnSuccessListener {
NotificareLogger.debug("Successfully stopped monitoring all geofences.")
}
.addOnFailureListener {
NotificareLogger.error("Failed to stop monitoring all geofences.", it)
}
override suspend fun clearMonitoringRegions() {
try {
geofencingClient.removeGeofences(geofencingPendingIntent)
NotificareLogger.debug("Successfully stopped monitoring all geofences.")
} catch (e: Exception) {
NotificareLogger.error("Failed to stop monitoring all geofences.", e)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public class ServiceManager : ServiceManager() {
return fusedLocationClient.lastLocation.asDeferred()
}

override fun startMonitoringRegions(regions: List<NotificareRegion>) {
override suspend fun startMonitoringRegions(regions: List<NotificareRegion>) {
val geofences = regions.map { region ->
Geofence.Builder()
.setUniqueId(region.id)
Expand All @@ -155,32 +155,38 @@ public class ServiceManager : ServiceManager() {
.setInitConversions(GeofenceRequest.ENTER_INIT_CONVERSION or GeofenceRequest.DWELL_INIT_CONVERSION or GeofenceRequest.EXIT_INIT_CONVERSION)
.build()

geofencingClient.createGeofenceList(request, geofencingPendingIntent)
.addOnSuccessListener {
NotificareLogger.debug("Successfully started monitoring ${geofences.size} geofences.")
}
.addOnFailureListener {
NotificareLogger.error("Failed to start monitoring ${geofences.size} geofences.", it)
}
try {
geofencingClient.createGeofenceList(request, geofencingPendingIntent)
.asDeferred()
.await()

NotificareLogger.debug("Successfully started monitoring ${geofences.size} geofences.")
} catch (e: Exception) {
NotificareLogger.error("Failed to start monitoring ${geofences.size} geofences.", e)
}
}

override fun stopMonitoringRegions(regions: List<NotificareRegion>) {
geofencingClient.deleteGeofenceList(regions.map { it.id })
.addOnSuccessListener {
NotificareLogger.debug("Successfully stopped monitoring ${regions.size} geofences.")
}
.addOnFailureListener {
NotificareLogger.error("Failed to stop monitoring ${regions.size} geofences.", it)
}
override suspend fun stopMonitoringRegions(regions: List<NotificareRegion>) {
try {
geofencingClient.deleteGeofenceList(regions.map { it.id })
.asDeferred()
.await()

NotificareLogger.debug("Successfully stopped monitoring ${regions.size} geofences.")
} catch (e: Exception) {
NotificareLogger.error("Failed to stop monitoring ${regions.size} geofences.", e)
}
}

override fun clearMonitoringRegions() {
geofencingClient.deleteGeofenceList(geofencingPendingIntent)
.addOnSuccessListener {
NotificareLogger.debug("Successfully stopped monitoring all geofences.")
}
.addOnFailureListener {
NotificareLogger.error("Failed to stop monitoring all geofences.", it)
}
override suspend fun clearMonitoringRegions() {
try {
geofencingClient.deleteGeofenceList(geofencingPendingIntent)
.asDeferred()
.await()

NotificareLogger.debug("Successfully stopped monitoring all geofences.")
} catch (e: Exception) {
NotificareLogger.error("Failed to stop monitoring all geofences.", e)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -450,11 +450,25 @@ internal object NotificareGeoImpl : NotificareModule(), NotificareGeo, Notificar

NotificareLogger.info("Updating device location.")
updateLocation(location, country)
} catch (e: Exception) {
NotificareLogger.error("Failed to process a location update.", e)
return@launch
}

val regions = try {
NotificareLogger.debug("Loading nearest regions.")
loadNearestRegions(location)
fetchNearestRegions(location)
} catch (e: Exception) {
NotificareLogger.error("Failed to process a location update.", e)
NotificareLogger.error("Failed to load nearest regions.", e)
return@launch
}

try {
NotificareLogger.debug("Monitoring nearest regions.")
monitorRegions(regions)
} catch (e: Exception) {
NotificareLogger.error("Failed to load nearest regions.", e)
return@launch
}
}
}
Expand Down Expand Up @@ -807,22 +821,16 @@ internal object NotificareGeoImpl : NotificareModule(), NotificareGeo, Notificar
})
}

private suspend fun loadNearestRegions(location: Location): Unit = withContext(Dispatchers.IO) {
try {
val regions = NotificareRequest.Builder()
.get("/region/bylocation/${location.latitude}/${location.longitude}")
.query("limit", monitoredRegionsLimit.toString())
.responseDecodable(FetchRegionsResponse::class)
.regions
.map { it.toModel() }

monitorRegions(regions)
} catch (e: Exception) {
NotificareLogger.error("Failed to load nearest regions.", e)
}
private suspend fun fetchNearestRegions(location: Location): List<NotificareRegion> = withContext(Dispatchers.IO) {
NotificareRequest.Builder()
.get("/region/bylocation/${location.latitude}/${location.longitude}")
.query("limit", monitoredRegionsLimit.toString())
.responseDecodable(FetchRegionsResponse::class)
.regions
.map { it.toModel() }
}

private fun monitorRegions(regions: List<NotificareRegion>) {
private suspend fun monitorRegions(regions: List<NotificareRegion>) {
if (!hasBackgroundLocationPermission) {
NotificareLogger.debug("Background location permission not granted. Skipping geofencing functionality.")
return
Expand Down Expand Up @@ -1097,13 +1105,19 @@ internal object NotificareGeoImpl : NotificareModule(), NotificareGeo, Notificar
}

private fun clearRegions() {
// Remove the cached regions.
localStorage.monitoredRegions = emptyMap()
localStorage.enteredRegions = emptySet()
localStorage.clearRegionSessions()
Notificare.coroutineScope.launch {
try {
// Stop monitoring all regions.
serviceManager?.clearMonitoringRegions()

// Stop monitoring all regions.
serviceManager?.clearMonitoringRegions()
// Remove the cached regions.
localStorage.monitoredRegions = emptyMap()
localStorage.enteredRegions = emptySet()
localStorage.clearRegionSessions()
} catch (e: Exception) {
NotificareLogger.error("Failed to clear monitored regions.", e)
}
}
}

private fun clearBeacons() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ public abstract class ServiceManager : AbstractServiceManager() {

public abstract fun getCurrentLocationAsync(): Deferred<Location>

public abstract fun startMonitoringRegions(regions: List<NotificareRegion>)
public abstract suspend fun startMonitoringRegions(regions: List<NotificareRegion>)

public abstract fun stopMonitoringRegions(regions: List<NotificareRegion>)
public abstract suspend fun stopMonitoringRegions(regions: List<NotificareRegion>)

public abstract fun clearMonitoringRegions()
public abstract suspend fun clearMonitoringRegions()

internal companion object {
private const val GMS_FQN = "re.notifica.geo.gms.internal.ServiceManager"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal data class FetchRegionsResponse(
val major: Int?,
val distance: Double,
val timezone: String,
val timeZoneOffset: Int,
val timeZoneOffset: Double,
) {

@JsonClass(generateAdapter = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public data class NotificareRegion(
val major: Int?,
val distance: Double,
val timeZone: String,
val timeZoneOffset: Int,
val timeZoneOffset: Double,
) : Parcelable {
public companion object;

Expand Down
2 changes: 1 addition & 1 deletion notificare/src/main/java/re/notifica/internal/Version.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package re.notifica.internal

internal const val NOTIFICARE_VERSION = "3.6.0"
internal const val NOTIFICARE_VERSION = "3.6.1"
17 changes: 16 additions & 1 deletion sample/src/main/java/re/notifica/sample/SampleActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import com.google.android.material.elevation.SurfaceColors
import com.google.android.material.snackbar.Snackbar
import re.notifica.Notificare
import re.notifica.geo.ktx.INTENT_ACTION_BEACON_NOTIFICATION_OPENED
import re.notifica.geo.ktx.geo
import re.notifica.iam.NotificareInAppMessaging
import re.notifica.iam.ktx.inAppMessaging
import re.notifica.iam.models.NotificareInAppMessage
import re.notifica.models.NotificareApplication
import re.notifica.models.NotificareNotification
import re.notifica.monetize.NotificareMonetize
import re.notifica.push.ktx.INTENT_ACTION_ACTION_OPENED
Expand All @@ -27,7 +29,7 @@ import re.notifica.push.ui.ktx.pushUI
import re.notifica.sample.databinding.ActivitySampleBinding
import timber.log.Timber

class SampleActivity : AppCompatActivity(), NotificarePushUI.NotificationLifecycleListener,
class SampleActivity : AppCompatActivity(), Notificare.Listener, NotificarePushUI.NotificationLifecycleListener,
NotificareMonetize.Listener {

private lateinit var binding: ActivitySampleBinding
Expand All @@ -53,6 +55,7 @@ class SampleActivity : AppCompatActivity(), NotificarePushUI.NotificationLifecyc

if (intent != null) handleIntent(intent)

Notificare.addListener(this)
Notificare.pushUI().addLifecycleListener(this)
Notificare.inAppMessaging().addLifecycleListener(messageLifecycleListener)
}
Expand All @@ -61,6 +64,7 @@ class SampleActivity : AppCompatActivity(), NotificarePushUI.NotificationLifecyc
override fun onDestroy() {
super.onDestroy()

Notificare.removeListener(this)
Notificare.pushUI().removeLifecycleListener(this)
Notificare.inAppMessaging().removeLifecycleListener(messageLifecycleListener)
}
Expand Down Expand Up @@ -135,6 +139,17 @@ class SampleActivity : AppCompatActivity(), NotificarePushUI.NotificationLifecyc
Toast.makeText(this, "Deep link = $uri", Toast.LENGTH_SHORT).show()
}

// Notificare is now safe to use
override fun onReady(application: NotificareApplication) {
if (Notificare.push().hasRemoteNotificationsEnabled) {
Notificare.push().enableRemoteNotifications()
}

if (Notificare.geo().hasLocationServicesEnabled) {
Notificare.geo().enableLocationUpdates()
}
}

// Lifecycle Listeners
private val messageLifecycleListener = object : NotificareInAppMessaging.MessageLifecycleListener {
override fun onMessagePresented(message: NotificareInAppMessage) {
Expand Down
5 changes: 0 additions & 5 deletions sample/src/main/res/color/switch_track_color.xml

This file was deleted.

20 changes: 6 additions & 14 deletions sample/src/main/res/layout/fragment_main_dnd_card.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,18 @@
android:layout_height="wrap_content"
android:padding="16dp">

<TextView
android:id="@+id/dnd_title"
<com.google.android.material.materialswitch.MaterialSwitch
android:id="@+id/dnd_switch"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:drawablePadding="8dp"
android:text="@string/main_fragment_dnd_title"
android:textAppearance="?attr/textAppearanceTitleMedium"
app:drawableStartCompat="@drawable/ic_baseline_do_not_disturb_24"
app:layout_constraintBottom_toBottomOf="@+id/dnd_switch"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<com.google.android.material.switchmaterial.SwitchMaterial
android:id="@+id/dnd_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:trackTint="@color/switch_track_color" />

<LinearLayout
android:id="@+id/dnd_start_time_container"
android:layout_width="0dp"
Expand All @@ -41,8 +33,8 @@
android:orientation="horizontal"
android:paddingVertical="6dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/dnd_title"
app:layout_constraintTop_toBottomOf="@+id/dnd_title">
app:layout_constraintStart_toStartOf="@+id/dnd_switch"
app:layout_constraintTop_toBottomOf="@+id/dnd_switch">

<TextView
android:layout_width="0dp"
Expand Down Expand Up @@ -70,7 +62,7 @@
android:paddingVertical="6dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/dnd_title"
app:layout_constraintStart_toStartOf="@+id/dnd_switch"
app:layout_constraintTop_toBottomOf="@id/dnd_start_time_container">

<TextView
Expand Down

0 comments on commit 6c5e315

Please sign in to comment.