Skip to content

Commit 114a1a1

Browse files
committed
修改了结构的参数,以便于正确的调用getString()等方法
1 parent 4ad8dfb commit 114a1a1

9 files changed

Lines changed: 37 additions & 23 deletions

File tree

app/src/main/java/com/foolchen/lib/tracker/demo/BaseActivity.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.foolchen.lib.tracker.demo
22

3+
import android.content.Context
34
import android.support.v7.app.AppCompatActivity
45
import com.foolchen.lib.tracker.lifecycle.ITrackerIgnore
56
import com.foolchen.lib.tracker.lifecycle.ITrackerHelper
@@ -16,9 +17,9 @@ open class BaseActivity : AppCompatActivity(), ITrackerHelper, ITrackerIgnore {
1617
// 则页面名称(别名)会直接取使用canonicalName来当做标题
1718
// 并且不会有附加的属性
1819
///////////////////////////////////////////////////////////////////////////
19-
override fun getTrackName(): String? = null
20+
override fun getTrackName(context: Context): String? = null
2021

21-
override fun getTrackProperties(): Map<String, Any?>? = null
22+
override fun getTrackProperties(context: Context): Map<String, Any?>? = null
2223

2324
///////////////////////////////////////////////////////////////////////////
2425
// ITrackerIgnore接口用于确定当前Activity中是否包含Fragment

app/src/main/java/com/foolchen/lib/tracker/demo/MainActivity.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.foolchen.lib.tracker.demo
22

3+
import android.content.Context
34
import android.os.Bundle
45
import android.support.v7.widget.LinearLayoutManager
56
import android.support.v7.widget.RecyclerView
@@ -44,12 +45,12 @@ class MainActivity : BaseActivity() {
4445
rv.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
4546
rv.adapter = adapter
4647

47-
title = getTrackName()
48+
title = getTrackName(this)
4849
}
4950

50-
override fun getTrackName() = "主Activity"
51+
override fun getTrackName(context: Context) = "主Activity"
5152

52-
override fun getTrackProperties() = null
53+
override fun getTrackProperties(context: Context) = null
5354

5455
private fun buildArgs(isVisibilityEnable: Boolean, isChildrenEnable: Boolean): Bundle {
5556
val args = Bundle()

app/src/main/java/com/foolchen/lib/tracker/demo/fragments/BaseFragment.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.foolchen.lib.tracker.demo.fragments
22

3+
import android.content.Context
34
import android.support.v4.app.Fragment
45
import com.foolchen.lib.tracker.Tracker
56
import com.foolchen.lib.tracker.lifecycle.ITrackerHelper
@@ -32,9 +33,9 @@ open class BaseFragment : Fragment(), ITrackerHelper, ITrackerIgnore {
3233
// 则页面名称(别名)会直接取使用canonicalName来当做标题
3334
// 并且不会有附加的属性
3435
///////////////////////////////////////////////////////////////////////////
35-
override fun getTrackName(): String? = null
36+
override fun getTrackName(context: Context): String? = null
3637

37-
override fun getTrackProperties(): Map<String, Any?>? = null
38+
override fun getTrackProperties(context: Context): Map<String, Any?>? = null
3839

3940
///////////////////////////////////////////////////////////////////////////
4041
// ITrackerIgnore接口用于确定当前Fragment中是否包含子Fragment

app/src/main/java/com/foolchen/lib/tracker/demo/fragments/NestedFragment.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.foolchen.lib.tracker.demo.fragments
22

3+
import android.content.Context
34
import android.os.Bundle
45
import android.view.LayoutInflater
56
import android.view.View
@@ -45,5 +46,5 @@ class NestedFragment : BaseFragment() {
4546
}
4647
}
4748

48-
override fun getTrackName(): String? = tv_desc.text.toString()
49+
override fun getTrackName(context: Context): String? = tv_desc?.text.toString()
4950
}

app/src/main/java/com/foolchen/lib/tracker/demo/fragments/PageFragment.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.foolchen.lib.tracker.demo.fragments
22

3+
import android.content.Context
34
import android.os.Bundle
45
import android.view.LayoutInflater
56
import android.view.View
@@ -58,5 +59,5 @@ class PageFragment : BaseFragment() {
5859

5960
override fun isIgnored(): Boolean = hasChildren
6061

61-
override fun getTrackName(): String? = tv_desc?.text.toString()
62+
override fun getTrackName(context: Context): String? = tv_desc?.text.toString()
6263
}

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ org.gradle.jvmargs=-Xmx1536m
1111
# This option should only be used with decoupled projects. More details, visit
1212
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
1313
# org.gradle.parallel=true
14-
VERSION_NAME=0.3.4-SNAPSHOT
14+
VERSION_NAME=0.4.0-SNAPSHOT
1515
POM_GROUP_ID=com.foolchen.lib
1616
POM_ARTIFACT_ID=tracker
1717
#库名称

tracker/src/main/java/com/foolchen/lib/tracker/Tracker.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import android.view.View
66
import android.widget.AdapterView
77
import com.foolchen.lib.tracker.data.*
88
import com.foolchen.lib.tracker.lifecycle.ITrackerContext
9-
import com.foolchen.lib.tracker.lifecycle.ITrackerFragmentVisible
109
import com.foolchen.lib.tracker.lifecycle.TrackerActivityLifeCycle
1110
import com.foolchen.lib.tracker.utils.*
1211
import java.util.*
@@ -57,7 +56,7 @@ object Tracker {
5756

5857
internal var appStartTime = 0L
5958

60-
internal var trackContext: ITrackerContext? = null
59+
internal lateinit var trackContext: ITrackerContext
6160

6261
internal var serviceHost: String? = null
6362
internal var servicePath: String? = null
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.foolchen.lib.tracker.lifecycle
22

3+
import android.content.Context
4+
35
/**
46
* 用于获取页面信息的接口
57
* @author chenchong
@@ -9,11 +11,19 @@ package com.foolchen.lib.tracker.lifecycle
911
interface ITrackerHelper {
1012
/**
1113
* 获取当前页面的名称
14+
*
15+
* **注意:在Fragment中使用时,不要使用Fragment.getString()等方法。
16+
* 由于setUserVisibleHint()方法的调用可能在所有的生命周期之前,如果调用Fragment.getString()等方法可能会导致错误。
17+
* 此处应该使用方法回传的[Context]**
1218
*/
13-
fun getTrackName(): String?
19+
fun getTrackName(context: Context): String?
1420

1521
/**
1622
* 获取当前页面需要附加的参数
23+
*
24+
* **注意:在Fragment中使用时,不要使用Fragment.getString()等方法。
25+
* 由于setUserVisibleHint()方法的调用可能在所有的生命周期之前,如果调用Fragment.getString()等方法可能会导致错误
26+
* 此处应该使用方法回传的[Context]**
1727
*/
18-
fun getTrackProperties(): Map<String, *>?
28+
fun getTrackProperties(context: Context): Map<String, *>?
1929
}

tracker/src/main/java/com/foolchen/lib/tracker/utils/TrackerUtils.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const val TAG = "AndroidTracker"
3131
internal fun Activity.getTrackName(): String {
3232
var name: String? = null
3333
if (this is ITrackerHelper) {
34-
name = this.getTrackName()
34+
name = this.getTrackName(Tracker.trackContext.getApplicationContext())
3535
}
3636
if (name.isNullOrEmpty()) {
3737
name = this.javaClass.canonicalName
@@ -48,7 +48,7 @@ internal fun Activity.getTrackName(): String {
4848
internal fun Fragment.getTrackName(): String {
4949
var name: String? = null
5050
if (this is ITrackerHelper) {
51-
name = this.getTrackName()
51+
name = this.getTrackName(Tracker.trackContext.getApplicationContext())
5252
}
5353
if (name.isNullOrEmpty()) {
5454
name = this.javaClass.canonicalName
@@ -69,9 +69,9 @@ internal fun Fragment.getTrackTitle(): String = activity?.getTrackTitle() ?: ""
6969
internal fun Activity.getTrackProperties(): Map<String, Any> {
7070
val properties = HashMap<String, Any>()
7171
if (this is ITrackerHelper) {
72-
this.getTrackProperties()?.let {
72+
this.getTrackProperties(Tracker.trackContext.getApplicationContext())?.let {
7373
it.filter { it.value != null }.forEach {
74-
properties.put(it.key, it.value!!)
74+
properties[it.key] = it.value!!
7575
}
7676
}
7777
}
@@ -84,9 +84,9 @@ internal fun Activity.getTrackProperties(): Map<String, Any> {
8484
internal fun Fragment.getTrackProperties(): Map<String, Any> {
8585
val properties = HashMap<String, Any>()
8686
if (this is ITrackerHelper) {
87-
this.getTrackProperties()?.let {
87+
this.getTrackProperties(Tracker.trackContext.getApplicationContext())?.let {
8888
it.filter { it.value != null }.forEach {
89-
properties.put(it.key, it.value!!)
89+
properties[it.key] = it.value!!
9090
}
9191
}
9292
}
@@ -96,9 +96,9 @@ internal fun Fragment.getTrackProperties(): Map<String, Any> {
9696
internal fun View.getTrackProperties(ev: MotionEvent?): Map<String, Any> {
9797
// 首先获取元素本身的属性
9898
val properties = HashMap<String, Any>()
99-
properties.put(ELEMENT_TYPE, this.javaClass.name)
99+
properties[ELEMENT_TYPE] = this.javaClass.name
100100
if (this is TextView) {
101-
properties.put(ELEMENT_CONTENT, this.text?.toString() ?: "")
101+
properties[ELEMENT_CONTENT] = this.text?.toString() ?: ""
102102
}
103103
/*ev?.let {
104104
properties.put(ELEMENT_X, ev.x)
@@ -108,7 +108,7 @@ internal fun View.getTrackProperties(ev: MotionEvent?): Map<String, Any> {
108108
// 然后获取开发者附加的属性
109109
val additionalProperties = Tracker.elementsProperties[this]
110110
additionalProperties?.filter { it.value != null }?.forEach {
111-
properties.put(it.key, it.value!!)
111+
properties[it.key] = it.value!!
112112
}
113113
Tracker.elementsProperties.remove(this)
114114
return properties

0 commit comments

Comments
 (0)