diff --git a/OmetriaSDK/build.gradle.kts b/OmetriaSDK/build.gradle.kts index 4ac1ea9..3433517 100644 --- a/OmetriaSDK/build.gradle.kts +++ b/OmetriaSDK/build.gradle.kts @@ -1,4 +1,4 @@ -val versionName = "1.7.0" +val versionName = "1.7.1" plugins { id("com.android.library") diff --git a/OmetriaSDK/src/main/java/com/android/ometriasdk/core/LocalCache.kt b/OmetriaSDK/src/main/java/com/android/ometriasdk/core/LocalCache.kt index 0a128ef..42690b9 100644 --- a/OmetriaSDK/src/main/java/com/android/ometriasdk/core/LocalCache.kt +++ b/OmetriaSDK/src/main/java/com/android/ometriasdk/core/LocalCache.kt @@ -4,6 +4,7 @@ import android.content.Context import android.content.SharedPreferences import androidx.security.crypto.EncryptedSharedPreferences import androidx.security.crypto.MasterKeys +import com.android.ometriasdk.core.Constants.Logger.CACHE import com.android.ometriasdk.core.event.OmetriaEvent import com.android.ometriasdk.core.network.toJson import com.android.ometriasdk.core.network.toOmetriaEventList @@ -52,8 +53,15 @@ internal class LocalCache(private val context: Context) { } fun saveIsFirstAppRun(isFirstAppRun: Boolean) { - localCacheEncryptedPreferences.edit().putBoolean(IS_FIRST_APP_RUN_KEY, isFirstAppRun) - .apply() + synchronized(this) { + try { + localCacheEncryptedPreferences.edit() + .putBoolean(IS_FIRST_APP_RUN_KEY, isFirstAppRun) + .apply() + } catch (e: Exception) { + Logger.e(CACHE, e.message ?: "Failed to save isFirstAppRun") + } + } } fun isFirstAppRun(): Boolean { @@ -61,7 +69,15 @@ internal class LocalCache(private val context: Context) { } fun saveInstallationId(installationId: String?) { - localCacheEncryptedPreferences.edit().putString(INSTALLATION_ID_KEY, installationId).apply() + synchronized(this) { + try { + localCacheEncryptedPreferences.edit() + .putString(INSTALLATION_ID_KEY, installationId) + .apply() + } catch (e: Exception) { + Logger.e(CACHE, e.message ?: "Failed to save installationId") + } + } } fun getInstallationId(): String? { @@ -69,19 +85,27 @@ internal class LocalCache(private val context: Context) { } fun saveEvent(ometriaEvent: OmetriaEvent) { - val eventsString = - localCacheEncryptedPreferences.getString(EVENTS_KEY, JSON_ARRAY) ?: JSON_ARRAY + val eventsString = localCacheEncryptedPreferences + .getString(EVENTS_KEY, JSON_ARRAY) ?: JSON_ARRAY val eventsList = eventsString.toOmetriaEventList() eventsList.add(ometriaEvent) - localCacheEncryptedPreferences.edit().putString(EVENTS_KEY, eventsList.toJson().toString()) - .apply() + synchronized(this) { + try { + localCacheEncryptedPreferences.edit() + .putString(EVENTS_KEY, eventsList.toJson().toString()) + .apply() + } catch (e: Exception) { + Logger.e(CACHE, e.message ?: "Failed to save event") + } + } } fun getEvents(): List { - val eventsString = localCacheEncryptedPreferences.getString(EVENTS_KEY, null) ?: JSON_ARRAY + val eventsString = localCacheEncryptedPreferences + .getString(EVENTS_KEY, null) ?: JSON_ARRAY return eventsString.toOmetriaEventList() } @@ -96,9 +120,15 @@ internal class LocalCache(private val context: Context) { isBeingFlushed } - localCacheEncryptedPreferences.edit() - .putString(EVENTS_KEY, cachedEvents.toJson().toString()) - .apply() + synchronized(this) { + try { + localCacheEncryptedPreferences.edit() + .putString(EVENTS_KEY, cachedEvents.toJson().toString()) + .apply() + } catch (e: Exception) { + Logger.e(CACHE, e.message ?: "Failed to update events") + } + } } fun removeEvents(eventsToRemove: List) { @@ -108,12 +138,25 @@ internal class LocalCache(private val context: Context) { eventsList.remove(eventsList.firstOrNull { it.eventId == event.eventId }) } - localCacheEncryptedPreferences.edit().putString(EVENTS_KEY, eventsList.toJson().toString()) - .apply() + synchronized(this) { + try { + localCacheEncryptedPreferences.edit() + .putString(EVENTS_KEY, eventsList.toJson().toString()) + .apply() + } catch (e: Exception) { + Logger.e(CACHE, e.message ?: "Failed to remove events") + } + } } fun savePushToken(pushToken: String) { - localCacheEncryptedPreferences.edit().putString(PUSH_TOKEN_KEY, pushToken).apply() + synchronized(this) { + try { + localCacheEncryptedPreferences.edit().putString(PUSH_TOKEN_KEY, pushToken).apply() + } catch (e: Exception) { + Logger.e(CACHE, e.message ?: "Failed to save pushToken") + } + } } fun getPushToken(): String? { @@ -121,11 +164,23 @@ internal class LocalCache(private val context: Context) { } fun clearEvents() { - localCacheEncryptedPreferences.edit().remove(EVENTS_KEY).apply() + synchronized(this) { + try { + localCacheEncryptedPreferences.edit().remove(EVENTS_KEY).apply() + } catch (e: Exception) { + Logger.e(CACHE, e.message ?: "Failed to clear events") + } + } } fun saveCustomerId(customerId: String?) { - localCacheEncryptedPreferences.edit().putString(CUSTOMER_ID_KEY, customerId).apply() + synchronized(this) { + try { + localCacheEncryptedPreferences.edit().putString(CUSTOMER_ID_KEY, customerId).apply() + } catch (e: Exception) { + Logger.e(CACHE, e.message ?: "Failed to save customerId") + } + } } fun getCustomerId(): String? { @@ -133,7 +188,13 @@ internal class LocalCache(private val context: Context) { } fun saveEmail(email: String?) { - localCacheEncryptedPreferences.edit().putString(EMAIL_KEY, email).apply() + synchronized(this) { + try { + localCacheEncryptedPreferences.edit().putString(EMAIL_KEY, email).apply() + } catch (e: Exception) { + Logger.e(CACHE, e.message ?: "Failed to save email") + } + } } fun getEmail(): String? { @@ -141,13 +202,26 @@ internal class LocalCache(private val context: Context) { } fun clearProfileIdentifiedData() { - localCacheEncryptedPreferences.edit().remove(CUSTOMER_ID_KEY).apply() - localCacheEncryptedPreferences.edit().remove(EMAIL_KEY).apply() + synchronized(this) { + try { + localCacheEncryptedPreferences.edit().remove(CUSTOMER_ID_KEY).apply() + localCacheEncryptedPreferences.edit().remove(EMAIL_KEY).apply() + } catch (e: Exception) { + Logger.e(CACHE, e.message ?: "Failed to clear profile identified data") + } + } } fun saveAreNotificationsEnabled(areNotificationsEnabled: Boolean) { - localCacheEncryptedPreferences.edit() - .putBoolean(ARE_NOTIFICATIONS_ENABLED_KEY, areNotificationsEnabled).apply() + synchronized(this) { + try { + localCacheEncryptedPreferences.edit() + .putBoolean(ARE_NOTIFICATIONS_ENABLED_KEY, areNotificationsEnabled) + .apply() + } catch (e: Exception) { + Logger.e(CACHE, e.message ?: "Failed to save areNotificationsEnabled") + } + } } fun areNotificationsEnabled(): Boolean { @@ -155,12 +229,22 @@ internal class LocalCache(private val context: Context) { } fun saveIsFirstPermissionsUpdateEvent(isFirstPermissionsUpdateEvent: Boolean) { - localCacheEncryptedPreferences.edit() - .putBoolean(IS_FIRST_PERMISSION_UPDATE_EVENT_KEY, isFirstPermissionsUpdateEvent).apply() + synchronized(this) { + try { + localCacheEncryptedPreferences.edit() + .putBoolean(IS_FIRST_PERMISSION_UPDATE_EVENT_KEY, isFirstPermissionsUpdateEvent) + .apply() + } catch (e: Exception) { + Logger.e(CACHE, e.message ?: "Failed to save isFirstPermissionsUpdateEvent") + } + } } fun isFirstPermissionsUpdateEvent(): Boolean { - return localCacheEncryptedPreferences.getBoolean(IS_FIRST_PERMISSION_UPDATE_EVENT_KEY, true) + return localCacheEncryptedPreferences.getBoolean( + IS_FIRST_PERMISSION_UPDATE_EVENT_KEY, + true + ) } fun getSdkVersionRN(): String? { @@ -168,10 +252,16 @@ internal class LocalCache(private val context: Context) { } fun saveApiToken(apiToken: String) { - localCacheEncryptedPreferences.edit().putString(API_TOKEN_KEY, apiToken).apply() + synchronized(this) { + try { + localCacheEncryptedPreferences.edit().putString(API_TOKEN_KEY, apiToken).apply() + } catch (e: Exception) { + Logger.e(CACHE, e.message ?: "Failed to save apiToken") + } + } } fun getApiToken(): String? { return localCacheEncryptedPreferences.getString(API_TOKEN_KEY, null) } -} \ No newline at end of file +} diff --git a/README.md b/README.md index aabf22b..4134253 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ To install the library inside **Android Studio**, declare it as dependency in yo ```gradle dependencies { - implementation 'com.ometria:android-sdk:1.7.0' + implementation 'com.ometria:android-sdk:1.7.1' } ``` diff --git a/app/build.gradle.kts b/app/build.gradle.kts index d633e52..4c3e19b 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -12,8 +12,8 @@ android { applicationId = "com.android.sample" minSdk = 23 targetSdk = 33 - versionCode = 11 - versionName = "1.2.0" + versionCode = 12 + versionName = "1.3.0" } signingConfigs { getByName("debug") {