Skip to content

Commit

Permalink
First Kotlin conversion
Browse files Browse the repository at this point in the history
More Kotlin
  • Loading branch information
hannesa2 committed Sep 25, 2023
1 parent b07d36c commit f1da2bb
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 183 deletions.
66 changes: 0 additions & 66 deletions tracker/src/main/java/org/matomo/sdk/LegacySettingsPorter.java

This file was deleted.

64 changes: 64 additions & 0 deletions tracker/src/main/java/org/matomo/sdk/LegacySettingsPorter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.matomo.sdk

import android.content.SharedPreferences
import java.util.UUID

class LegacySettingsPorter(matomo: Matomo) {
private val mLegacyPrefs: SharedPreferences

init {
mLegacyPrefs = matomo.preferences
}

fun port(tracker: Tracker) {
val newSettings = tracker.preferences
if (mLegacyPrefs.getBoolean(LEGACY_PREF_OPT_OUT, false)) {
newSettings.edit()
.putBoolean(Tracker.PREF_KEY_TRACKER_OPTOUT, true)
.apply()
mLegacyPrefs.edit().remove(LEGACY_PREF_OPT_OUT).apply()
}
if (mLegacyPrefs.contains(LEGACY_PREF_USER_ID)) {
newSettings.edit()
.putString(Tracker.PREF_KEY_TRACKER_USERID, mLegacyPrefs.getString(LEGACY_PREF_USER_ID, UUID.randomUUID().toString()))
.apply()
mLegacyPrefs.edit().remove(LEGACY_PREF_USER_ID).apply()
}
if (mLegacyPrefs.contains(LEGACY_PREF_FIRST_VISIT)) {
newSettings.edit().putLong(
Tracker.PREF_KEY_TRACKER_FIRSTVISIT,
mLegacyPrefs.getLong(LEGACY_PREF_FIRST_VISIT, -1L)
).apply()
mLegacyPrefs.edit().remove(LEGACY_PREF_FIRST_VISIT).apply()
}
if (mLegacyPrefs.contains(LEGACY_PREF_VISITCOUNT)) {
newSettings.edit().putLong(
Tracker.PREF_KEY_TRACKER_VISITCOUNT,
mLegacyPrefs.getInt(LEGACY_PREF_VISITCOUNT, 0)
.toLong()
).apply()
mLegacyPrefs.edit().remove(LEGACY_PREF_VISITCOUNT).apply()
}
if (mLegacyPrefs.contains(LEGACY_PREF_PREV_VISIT)) {
newSettings.edit().putLong(
Tracker.PREF_KEY_TRACKER_PREVIOUSVISIT,
mLegacyPrefs.getLong(LEGACY_PREF_PREV_VISIT, -1)
).apply()
mLegacyPrefs.edit().remove(LEGACY_PREF_PREV_VISIT).apply()
}
for ((key) in mLegacyPrefs.all) {
if (key.startsWith("downloaded:")) {
newSettings.edit().putBoolean(key, true).apply()
mLegacyPrefs.edit().remove(key).apply()
}
}
}

companion object {
const val LEGACY_PREF_OPT_OUT = "matomo.optout"
const val LEGACY_PREF_USER_ID = "tracker.userid"
const val LEGACY_PREF_FIRST_VISIT = "tracker.firstvisit"
const val LEGACY_PREF_VISITCOUNT = "tracker.visitcount"
const val LEGACY_PREF_PREV_VISIT = "tracker.previousvisit"
}
}
117 changes: 0 additions & 117 deletions tracker/src/main/java/org/matomo/sdk/Matomo.java

This file was deleted.

98 changes: 98 additions & 0 deletions tracker/src/main/java/org/matomo/sdk/Matomo.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Android SDK for Matomo
*
* @link https://github.com/matomo-org/matomo-android-sdk
* @license https://github.com/matomo-org/matomo-sdk-android/blob/master/LICENSE BSD-3 Clause
*/
package org.matomo.sdk

import android.annotation.SuppressLint
import android.content.Context
import android.content.SharedPreferences
import org.matomo.sdk.dispatcher.DefaultDispatcherFactory
import org.matomo.sdk.dispatcher.DispatcherFactory
import org.matomo.sdk.tools.BuildInfo
import org.matomo.sdk.tools.Checksum
import org.matomo.sdk.tools.DeviceHelper
import org.matomo.sdk.tools.PropertySource
import timber.log.Timber

class Matomo private constructor(context: Context) {
private val mPreferenceMap: MutableMap<Tracker, SharedPreferences?> = HashMap()
val context: Context

/**
* Base preferences, tracker independent.
*/
val preferences: SharedPreferences

/**
* If you want to use your own [org.matomo.sdk.dispatcher.Dispatcher]
*/
var dispatcherFactory: DispatcherFactory = DefaultDispatcherFactory()

init {
this.context = context.applicationContext
preferences = context.getSharedPreferences(BASE_PREFERENCE_FILE, Context.MODE_PRIVATE)
}

/**
* @return Tracker specific settings object
*/
fun getTrackerPreferences(tracker: Tracker): SharedPreferences? {
synchronized(mPreferenceMap) {
var newPrefs = mPreferenceMap[tracker]
if (newPrefs == null) {
val prefName: String = try {
"org.matomo.sdk_" + Checksum.getMD5Checksum(tracker.name)
} catch (e: Exception) {
Timber.e(e)
"org.matomo.sdk_" + tracker.name
}
newPrefs = context.getSharedPreferences(prefName, Context.MODE_PRIVATE)
mPreferenceMap[tracker] = newPrefs
}
return newPrefs
}
}

val deviceHelper: DeviceHelper
get() = DeviceHelper(context, PropertySource(), BuildInfo())

companion object {
private const val LOGGER_PREFIX = "MATOMO:"
private const val BASE_PREFERENCE_FILE = "org.matomo.sdk"

@SuppressLint("StaticFieldLeak")
@Volatile
private var sInstance: Matomo? = null

@JvmStatic
@Synchronized
fun getInstance(context: Context): Matomo? {
if (sInstance == null) {
synchronized(Matomo::class.java) { if (sInstance == null) sInstance = Matomo(context) }
}
return sInstance
}

@JvmStatic
fun tag(vararg classes: Class<*>): String {
val tags = arrayOfNulls<String>(classes.size)
for (i in classes.indices) {
tags[i] = classes[i].simpleName
}
return tag(*tags)
}

@JvmStatic
fun tag(vararg tags: String?): String {
val sb = StringBuilder(LOGGER_PREFIX)
for (i in tags.indices) {
sb.append(tags[i])
if (i < tags.size - 1) sb.append(":")
}
return sb.toString()
}
}
}

0 comments on commit f1da2bb

Please sign in to comment.