-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
180 additions
and
56 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
20 changes: 10 additions & 10 deletions
20
analytics/src/main/java/com/sofakingforever/analytics/AnalyticsSettings.kt
This file contains 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 |
---|---|---|
@@ -1,26 +1,26 @@ | ||
package com.sofakingforever.analytics | ||
|
||
import android.content.Context | ||
|
||
/** | ||
* Holds some things for the Analytics class | ||
* @property isAnalyticsEnabled - no events will be sent if this is set to *false* | ||
* @property checkForUpdates - should the library check for updates | ||
* @property exceptionHandler - implementation of @ExceptionHandler | ||
*/ | ||
class AnalyticsSettings { | ||
class AnalyticsSettings(val context: Context) { | ||
|
||
@Volatile | ||
var isAnalyticsEnabled = true | ||
var checkForUpdates = true | ||
|
||
var exceptionHandler: ExceptionHandler? = null | ||
val enabledKits: ServiceEnabledMap<AnalyticsKit> = ServiceEnabledMap() | ||
val enabledDispatchers: ServiceEnabledMap<String> = ServiceEnabledMap() | ||
|
||
/** | ||
* Just an exception callback to log/monitor exceptions, | ||
* thrown by the *Analytics* class or any of its dispatchers. | ||
*/ | ||
interface ExceptionHandler { | ||
class ServiceEnabledMap<Key> : LinkedHashMap<Key, Boolean>() { | ||
|
||
fun onException(e: Exception) | ||
fun isDisabled(key: Key): Boolean = this[key] == false | ||
|
||
} | ||
} | ||
|
||
|
||
} |
7 changes: 0 additions & 7 deletions
7
analytics/src/main/java/com/sofakingforever/analytics/EnabledMap.kt
This file was deleted.
Oops, something went wrong.
7 changes: 7 additions & 0 deletions
7
...s/src/main/java/com/sofakingforever/analytics/exceptions/IllegalVersionFormatException.kt
This file contains 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,7 @@ | ||
package com.sofakingforever.analytics.exceptions | ||
|
||
import java.net.MalformedURLException | ||
|
||
class IllegalVersionFormatException(version : String) : MalformedURLException("Version '$version' is illegal") { | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
analytics/src/main/java/com/sofakingforever/analytics/version/Version.kt
This file contains 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,37 @@ | ||
package com.sofakingforever.analytics.version | ||
|
||
import com.sofakingforever.analytics.exceptions.IllegalVersionFormatException | ||
|
||
class Version : Comparable<Version> { | ||
|
||
private val version: String | ||
|
||
@Throws(IllegalVersionFormatException::class) constructor(version: String) { | ||
if (!version.contains(".")) throw IllegalVersionFormatException(version) | ||
this.version = version | ||
} | ||
|
||
override fun toString(): String { | ||
return version | ||
} | ||
|
||
override fun compareTo(that: Version): Int { | ||
|
||
val levels1 = this.version.split("\\.".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() | ||
val levels2 = that.version.split("\\.".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() | ||
|
||
val length = Math.max(levels1.size, levels2.size) | ||
|
||
for (i in 0 until length) { | ||
val v1 = if (i < levels1.size) Integer.parseInt(levels1[i]) else 0 | ||
val v2 = if (i < levels2.size) Integer.parseInt(levels2[i]) else 0 | ||
val compare = v1.compareTo(v2) | ||
if (compare != 0) { | ||
return compare | ||
} | ||
} | ||
|
||
return 0 | ||
} | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
analytics/src/main/java/com/sofakingforever/analytics/version/VersionChecker.kt
This file contains 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,58 @@ | ||
package com.sofakingforever.analytics.version | ||
|
||
import android.util.Log | ||
import com.sofakingforever.analytics.exceptions.IllegalVersionFormatException | ||
import com.sofakingforever.library.BuildConfig | ||
import okhttp3.* | ||
import java.io.IOException | ||
|
||
object VersionChecker { | ||
|
||
fun onCheckVersion() { | ||
|
||
val currentVersion = Version(BuildConfig.VERSION_NAME) | ||
|
||
val client = OkHttpClient.Builder() | ||
.followRedirects(true) | ||
.build() | ||
|
||
val request = Request.Builder() | ||
.url("https://bintray.com/sofakingforever/analytics/kotlin-analytics/_latestVersion") | ||
.get() | ||
.build() | ||
|
||
client.newCall(request).enqueue(object : Callback { | ||
|
||
override fun onFailure(call: Call?, e: IOException?) { | ||
// ignore failure, no need to fill logcat with bullshit | ||
} | ||
|
||
override fun onResponse(call: Call?, response: Response?) { | ||
// try to finish up | ||
response?.url().also { url -> | ||
|
||
try { | ||
val latestVersion = Version(extractVersion(url)) | ||
|
||
if (currentVersion < latestVersion) { | ||
// user should update | ||
Log.w("Analytics", "Latest kotlin-analytics version is $latestVersion > $currentVersion") | ||
} | ||
} catch (e: IllegalVersionFormatException) { | ||
// ignore failure, no need to fill logcat with bullshit | ||
} | ||
|
||
|
||
} | ||
} | ||
|
||
}) | ||
} | ||
|
||
fun extractVersion(it: HttpUrl?) : String = it?.encodedPathSegments()?.last() ?: "" | ||
|
||
private fun Response?.url(): HttpUrl? { | ||
return this?.networkResponse()?.request()?.url() | ||
} | ||
|
||
} |
This file contains 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
This file contains 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
This file contains 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
4 changes: 3 additions & 1 deletion
4
app/src/main/java/com/sofakingforever/example/events/EventPerKit.kt
This file contains 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
Oops, something went wrong.