Skip to content

Commit

Permalink
Support class level timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
winfoozmnayef committed Nov 27, 2018
1 parent edf8c61 commit 0e848b9
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
@Analytics(events = {
@Data(value = @Value("post.title"), key = @Key("title")),
@Data(value = @Value("post.body"), key = @Key("body"))
})
}, timestamp = true)
public interface JavaMainActivityAnalytics {

@Keep
Expand All @@ -36,11 +36,11 @@ public interface JavaMainActivityAnalytics {

@Keep
@CallFailure(value = "posts", name = "requestFailed")
@Event(value = "Failed get posts", timestamp = true)
@Event(value = "Failed get posts")
void failedGetPosts(Post post);

@Keep
@CallFailure(value = "posts", name = "requestFailed")
@Event(value = "Failed get posts1", timestamp = true)
@Event(value = "Failed get posts1")
void failed1GetPosts(Post post);
}
2 changes: 1 addition & 1 deletion winanalytics-annotations/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ POM_DESCRIPTION=Annotations for use in your project
POM_BINTRAY_NAME=com.winfooz:annotations
POM_ARTIFACT_ID=winanalytics-annotations
POM_PACKAGING=jar
POM_VERSION=1.0.3-RC
POM_VERSION=1.0.4-RC
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ package com.winfooz
@Target(AnnotationTarget.CLASS)
@MustBeDocumented
annotation class Analytics(
val events: Array<Data> = []
val events: Array<Data> = [],
val timestamp: Boolean = false
)
2 changes: 1 addition & 1 deletion winanalytics-compiler/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ POM_DESCRIPTION=For process annotations and generate code
POM_BINTRAY_NAME=com.winfooz:compiler
POM_ARTIFACT_ID=winanalytics-compiler
POM_PACKAGING=jar
POM_VERSION=1.0.3-RC
POM_VERSION=1.0.4-RC
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class JavaProcessor(
it.reference
)
}
if (event.event.timestamp) {
if (event.event.timestamp || element.analytics.timestamp) {
method.addStatement(
"\$N.put(\$S, \$N)",
"pair.second",
Expand Down
2 changes: 1 addition & 1 deletion winanalytics-okhttp/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ POM_DESCRIPTION=For support auto events when http request failed or success
POM_BINTRAY_NAME=com.winfooz:okhttp
POM_ARTIFACT_ID=winanalytics-okhttp
POM_PACKAGING=jar
POM_VERSION=1.0.3-RC
POM_VERSION=1.0.4-RC
2 changes: 1 addition & 1 deletion winanalytics/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ POM_DESCRIPTION=For log analytics to firebase,fabric,mixpanel etc...
POM_BINTRAY_NAME=com.winfooz:winanalytics
POM_ARTIFACT_ID=winanalytics
POM_PACKAGING=jar
POM_VERSION=1.0.3-RC
POM_VERSION=1.0.4-RC
96 changes: 71 additions & 25 deletions winanalytics/src/main/java/com/winfooz/WinAnalytics.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package com.winfooz

import android.app.Activity
import android.app.Dialog
import android.support.annotation.CheckResult
import android.support.annotation.NonNull
import android.support.annotation.Nullable
import android.support.annotation.UiThread
import android.util.Log
import android.view.View
import java.lang.reflect.Constructor
import java.util.*
import java.util.concurrent.Executors

Expand Down Expand Up @@ -110,6 +117,8 @@ class WinAnalytics private constructor(private val configuration: WinConfigurati

companion object {

private val ANALYTICS: MutableMap<Class<*>?, Constructor<out Destroyable>?> = LinkedHashMap()

@JvmStatic
private lateinit var instance: WinAnalytics

Expand All @@ -135,31 +144,6 @@ class WinAnalytics private constructor(private val configuration: WinConfigurati
}
}

@JvmStatic
fun bind(target: Activity): Destroyable {
val cls = target.javaClass
val clsName = cls.name
val classLoader = cls.classLoader
if (classLoader != null) {
try {
return classLoader
.loadClass(clsName + "_Analytics")
.getConstructor(target.javaClass)
.newInstance(target) as Destroyable
} catch (exception: NoSuchMethodException) {
try {
return classLoader
.loadClass(clsName + "_Analytics")
.getDeclaredConstructor()
.newInstance() as Destroyable
} catch (ignored: Exception) {
}
} catch (ignored: Exception) {
}
}
return Destroyable.EMPTY_DESTROYABLE
}

@Suppress("UNCHECKED_CAST")
@JvmStatic
fun <T> create(cls: Class<T>): T {
Expand All @@ -178,5 +162,67 @@ class WinAnalytics private constructor(private val configuration: WinConfigurati
throw RuntimeException("Unable to find analytics wrapper class for $clsName", e)
}
}

@NonNull
@UiThread
fun bind(target: Activity): Destroyable {
return bind(target, target.window.decorView)
}

@NonNull
@UiThread
fun bind(@NonNull target: View): Destroyable {
return bind(target, target)
}

@NonNull
@UiThread
fun bind(@NonNull target: Dialog): Destroyable {
return bind(target, target.window?.decorView)
}

@NonNull
@UiThread
fun bind(@NonNull target: Any, @NonNull source: Activity): Destroyable {
return bind(target, source.window.decorView)
}

@NonNull
@UiThread
fun bind(@NonNull target: Any, @NonNull source: Dialog): Destroyable {
return bind(target, source.window?.decorView)
}

@NonNull
@UiThread
fun bind(@NonNull target: Any, @NonNull source: View?): Destroyable {
val constructor = findBindingConstructorForClass(target.javaClass)
?: return Destroyable.EMPTY_DESTROYABLE
return try {
constructor.newInstance(target, source)
} catch (e: Exception) {
Destroyable.EMPTY_DESTROYABLE
}
}

@Suppress("UNCHECKED_CAST")
@Nullable
@CheckResult
@UiThread
private fun findBindingConstructorForClass(cls: Class<*>?): Constructor<out Destroyable>? {
var bindingCtor: Constructor<out Destroyable>? = ANALYTICS[cls]
if (bindingCtor != null || ANALYTICS.containsKey(cls)) {
return bindingCtor
}
val clsName = cls?.name
bindingCtor = try {
val bindingClass = cls?.classLoader?.loadClass(clsName + "_ViewBinding")
bindingClass?.getConstructor(cls, View::class.java) as Constructor<out Destroyable>
} catch (e: ClassNotFoundException) {
findBindingConstructorForClass(cls?.superclass)
}
ANALYTICS[cls] = bindingCtor
return bindingCtor
}
}
}

0 comments on commit 0e848b9

Please sign in to comment.