欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

一文詳解無痕埋點(diǎn)在Android中的實(shí)現(xiàn)

 更新時(shí)間:2023年02月18日 14:24:44   作者:小寶一號(hào)  
很多時(shí)候因?yàn)楫a(chǎn)品都會(huì)要獲取用戶的行為,需要客戶端進(jìn)行相關(guān)的埋點(diǎn),下面這篇文章主要給大家介紹了關(guān)于無痕埋點(diǎn)在Android中實(shí)現(xiàn)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下

前言

本篇技術(shù)實(shí)現(xiàn)主要是運(yùn)行是代理,不涉及到插樁技術(shù),不引入插件,對(duì)業(yè)務(wù)影響點(diǎn)最小

技術(shù)難點(diǎn)

1. 如何攔截到所有的view的點(diǎn)擊事件

view有個(gè)setAccessibilityDelegate方法可以通過自定義一個(gè)全局的AccessibilityDelegate對(duì)象來監(jiān)聽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)
            }
        }
    }
}

通過給每個(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è)問題,就得攔截到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屬性,
            // 如果沒有就創(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ì)象;思路來了,我們可以通過自定義主題樣式中viewInflaterClass屬性,來接管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主要是通過全路徑名以反射形式創(chuàng)建view;你可以參考AppCompatViewInflater類中實(shí)現(xiàn)

DBSAppCompatViewInflater方法我們實(shí)現(xiàn)了自定義view的方法;(但它只是view創(chuàng)建的一部分,所以此處沒有對(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 {
                // 無痕埋點(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ù)寫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è)無痕埋點(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)方案:可以通過插樁方式修改viewInflaterClassName的值,對(duì)于我們自己業(yè)務(wù)類(通過context判斷)設(shè)置我們自定義的InflaterClassName,第三方sdk可以控制保持不變

總結(jié) 

到此這篇關(guān)于無痕埋點(diǎn)在Android中實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Android實(shí)現(xiàn)無痕埋點(diǎn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論