一文詳解無(wú)痕埋點(diǎn)在Android中的實(shí)現(xiàn)
前言
本篇技術(shù)實(shí)現(xiàn)主要是運(yùn)行是代理,不涉及到插樁技術(shù),不引入插件,對(duì)業(yè)務(wù)影響點(diǎn)最小
技術(shù)難點(diǎn)
1. 如何攔截到所有的view的點(diǎn)擊事件
view有個(gè)setAccessibilityDelegate方法可以通過(guò)自定義一個(gè)全局的AccessibilityDelegate對(duì)象來(lái)監(jiān)聽(tīng)view的點(diǎn)擊事件
object EventTrackerAccessibilityDelegate : View.AccessibilityDelegate() {
override fun sendAccessibilityEvent(host: View?, eventType: Int) {
super.sendAccessibilityEvent(host, eventType)
if (eventType == AccessibilityEvent.TYPE_VIEW_CLICKED) {
host?.let {
// 統(tǒng)一做埋點(diǎn)
}
}
}
}
通過(guò)給每個(gè)View設(shè)置上述單例對(duì)象,這樣每當(dāng)View被點(diǎn)擊時(shí),View.performClick內(nèi)部就會(huì)觸發(fā)上述方法。這樣就能夠攔截view的點(diǎn)擊事件,而不用修改業(yè)務(wù)層代碼。
2. 如何對(duì)app所有的view設(shè)置setAccessibilityDelegate
解決這個(gè)問(wèn)題,就得攔截到app中view的創(chuàng)建。我們先要對(duì)Android中View的創(chuàng)建流程需要明白,對(duì)于android中的view創(chuàng)建,我們先從AppCompatActivity.onCreate方法入手
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
final AppCompatDelegate delegate = getDelegate();
delegate.installViewFactory(); //重點(diǎn)
delegate.onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
}
我們重點(diǎn)看installViewFactory方法,delegate返回的實(shí)際類型為AppCompatDelegateImpl,它繼承了AppCompatDelegate抽象類
// AppCompatDelegateImpl.java
@Override
public void installViewFactory() {
LayoutInflater layoutInflater = LayoutInflater.from(mContext);
if (layoutInflater.getFactory() == null) {
LayoutInflaterCompat.setFactory2(layoutInflater, this);
} else {
if (!(layoutInflater.getFactory2() instanceof AppCompatDelegateImpl)) {
Log.i(TAG, "The Activity's LayoutInflater already has a Factory installed"
+ " so we can not install AppCompat's");
}
}
}
這里面可以看到內(nèi)部調(diào)用了LayoutInflaterCompat**.setFactory2方法,第二個(gè)參數(shù)傳入了this;其實(shí)可以理解view的創(chuàng)建托管給了AppCompatDelegateImpl.onCreateView了;我們繼續(xù)看onCreateView**內(nèi)部做了什么
public View createView(View parent, final String name, @NonNull Context context,
@NonNull AttributeSet attrs) {
if (mAppCompatViewInflater == null) {
TypedArray a = mContext.obtainStyledAttributes(R.styleable.AppCompatTheme);
String viewInflaterClassName =
a.getString(R.styleable.AppCompatTheme_viewInflaterClass);
// 讀取當(dāng)前活動(dòng)theme中是否聲明了viewInflaterClass屬性,
// 如果沒(méi)有就創(chuàng)建一個(gè)AppCompatViewInflater對(duì)象,否則使用自定義屬性對(duì)象
if ((viewInflaterClassName == null)
|| AppCompatViewInflater.class.getName().equals(viewInflaterClassName)) {
// Either default class name or set explicitly to null. In both cases
// create the base inflater (no reflection)
mAppCompatViewInflater = new AppCompatViewInflater();
} else {
try {
Class<?> viewInflaterClass = Class.forName(viewInflaterClassName);
mAppCompatViewInflater =
(AppCompatViewInflater) viewInflaterClass.getDeclaredConstructor()
.newInstance();
} catch (Throwable t) {
Log.i(TAG, "Failed to instantiate custom view inflater "
+ viewInflaterClassName + ". Falling back to default.", t);
mAppCompatViewInflater = new AppCompatViewInflater();
}
}
}
...
// 返回view
return mAppCompatViewInflater.createView(parent, name, context, attrs, inheritContext,
IS_PRE_LOLLIPOP, /* Only read android:theme pre-L (L+ handles this anyway) */
true, /* Read read app:theme as a fallback at all times for legacy reasons */
VectorEnabledTintResources.shouldBeUsed() /* Only tint wrap the context if enabled */
);
}
從上述代碼可以看到負(fù)責(zé)view的創(chuàng)建的其實(shí)是mAppCompatViewInflater對(duì)象;思路來(lái)了,我們可以通過(guò)自定義主題樣式中viewInflaterClass屬性,來(lái)接管view的創(chuàng)建
Style.xml中添加配置
<!-- Base application theme. -->
<style name="AppTheme" parent="AppThemeBase" >
...
<item name="viewInflaterClass">com.dbs.module.framework.event.tracker.DBSAppCompatViewInflater</item>
</style>
view創(chuàng)建
@Keep
class DBSAppCompatViewInflater : AppCompatViewInflater() {
private val mViewCreateHelper by lazy { ViewCreateHelper() }
override fun createView(context: Context?, name: String?, attrs: AttributeSet?): View? {
return when (name) {
try {
mViewCreateHelper.createViewFromTag(context, name, attrs)
} catch (e: Exception) {
// noNeed throw exception, just return null
null
}
}
}
}
ViewCreateHelper主要是通過(guò)全路徑名以反射形式創(chuàng)建view;你可以參考AppCompatViewInflater類中實(shí)現(xiàn)
DBSAppCompatViewInflater方法我們實(shí)現(xiàn)了自定義view的方法;(但它只是view創(chuàng)建的一部分,所以此處沒(méi)有對(duì)view設(shè)置EventTrackerAccessibilityDelegate),外部調(diào)用的只是AppCompatViewInflater.createView;
所以為了攔截所有view的創(chuàng)建,我們需要對(duì)activity中g(shù)etDelagate方法做包裝; 有人可能會(huì)想能不能自定義Delegate,自己實(shí)現(xiàn)AppCompatDelegate抽象類嗎?;答案是不行(抽象類中聲明了私有方法,子類直接繼承編譯報(bào)錯(cuò))也不建議這樣做,自定義類去做需要實(shí)現(xiàn)許多方法,穩(wěn)定性太差;能不能直接繼承AppCompatDelegateImpl類呢?答案也是不行
@RestrictTo(LIBRARY)
class AppCompatDelegateImpl extends AppCompatDelegate
implements MenuBuilder.Callback, LayoutInflater.Factory2 {
}
從源碼可以看出compat包中對(duì)AppCompatDelegateImpl類做了限制,只能用在那個(gè)庫(kù)中LIBRARY中使用
Restrict usage to code within the same library (e.g. the same gradle group ID and artifact ID).
所以我們只能對(duì)Delegate增加一層包裝,delegate現(xiàn)在已經(jīng)擁有創(chuàng)建view的能力,我們只要在install之前對(duì)LayoutInflater設(shè)置Factory2中方法,在方法中直接引用delegate對(duì)象創(chuàng)建view就可以了;
實(shí)現(xiàn)一個(gè)LayoutIInflater.Factory2接口
class AppLayoutInflaterFactory2Proxy(private val delegate: AppCompatDelegate)
: LayoutInflater.Factory2 {
override fun onCreateView(parent: View?, name: String?, context: Context?, attrs: AttributeSet): View? {
context ?: return null
delegate.createView(parent, name, context, attrs)?.apply {
// 無(wú)痕埋點(diǎn)啟用,則綁定,否則不做處理
if (EventAutoTrackerCfg.enable) {
if (ViewCompat.getAccessibilityDelegate(this) == null) {
accessibilityDelegate = EventTrackerAccessibilityDelegate
}
}
}
}
override fun onCreateView(name: String?, context: Context?, attrs: AttributeSet): View? {
return onCreateView(null, name, context, attrs)
}
}
Activity基類中復(fù)寫(xiě)getDelegate方法
override fun getDelegate(): AppCompatDelegate {
val delegate = super.getDelegate()
try {
val inflater = LayoutInflater.from(this)
// avoid throw exception when invoking method multiple times
if (inflater.factory == null) {
LayoutInflaterCompat.setFactory2(inflater, MKAppLayoutInflaterFactory2Proxy(delegate) )
}
} catch (e: Exception) {
// do nothing
}
return delegate
}
這樣整個(gè)無(wú)痕埋點(diǎn)技術(shù)實(shí)現(xiàn)方案已經(jīng)完成了
可以優(yōu)化的點(diǎn)
當(dāng)前技術(shù)實(shí)現(xiàn)中需要在Style.xml中添加相關(guān)viewInflaterClass配置,有些耦合
優(yōu)化技術(shù)實(shí)現(xiàn)方案:可以通過(guò)插樁方式修改viewInflaterClassName的值,對(duì)于我們自己業(yè)務(wù)類(通過(guò)context判斷)設(shè)置我們自定義的InflaterClassName,第三方sdk可以控制保持不變

總結(jié)
到此這篇關(guān)于無(wú)痕埋點(diǎn)在Android中實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Android實(shí)現(xiàn)無(wú)痕埋點(diǎn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android自定義DataTimePicker日期時(shí)間選擇器使用詳解
這篇文章主要為大家詳細(xì)介紹了Android自定義DataTimePicker日期時(shí)間選擇器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09
Android實(shí)現(xiàn)繪畫(huà)板功能
這是一款android的畫(huà)圖板實(shí)現(xiàn)代碼,基本功能齊全,適合初學(xué)者開(kāi)發(fā)的例子程序,感興趣的朋友就來(lái)看看吧2021-05-05
android通過(guò)jxl讀excel存入sqlite3數(shù)據(jù)庫(kù)
本文主要介紹了android通過(guò)jxl去讀excel的內(nèi)容,然后存入sqlite3數(shù)據(jù)庫(kù)表,需要用到j(luò)xl的jar包和sqlite 的jar包,圖片是excel的數(shù)據(jù)格式,需要的朋友可以參考下2014-03-03
Android創(chuàng)建與解析XML(三)——詳解Sax方式
本篇文章主要介紹了Android創(chuàng)建與解析XML(三)——詳解Sax方式 ,這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。2016-11-11
Android開(kāi)發(fā)Activity毛玻璃背景效果
這篇文章主要為大家詳細(xì)介紹了Android開(kāi)發(fā)Activity毛玻璃背景效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
Android中Service與Activity之間通信的幾種方式
本篇文章主要介紹了Android中Service與Activity之間通信的幾種方式,Activity主要負(fù)責(zé)前臺(tái)頁(yè)面的展示,Service主要負(fù)責(zé)需要長(zhǎng)期運(yùn)行的任務(wù),具有一定的參考價(jià)值,有興趣的可以了解一下。2017-02-02
Android viewpager在最后一頁(yè)滑動(dòng)之后跳轉(zhuǎn)到主頁(yè)面的實(shí)例代碼
這篇文章主要介紹了Android viewpager在最后一頁(yè)滑動(dòng)之后跳轉(zhuǎn)到主頁(yè)面的實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-08-08
Android中EditText+Button組合導(dǎo)致輸入板無(wú)法收起的原因分析及解決辦法
這篇文章主要介紹了Android中EditText+Button組合導(dǎo)致輸入板無(wú)法收起的原因分析及解決辦法的相關(guān)資料,需要的朋友可以參考下2016-01-01

