引入
最近在做項(xiàng)目優(yōu)化的時(shí)候祸挪,發(fā)現(xiàn)點(diǎn)擊某個(gè)控件使用某個(gè)功能時(shí)佑刷,事先要判斷用戶是否登錄,翻看代碼時(shí)發(fā)現(xiàn)很多地方寫了大量的if或else的操作棒动,代碼繁瑣,一旦一處修改將涉及到n處修改宾添,于是通過hook的方式船惨,劫持View的點(diǎn)擊事件,一行代碼做完所有的if或else缕陕,代碼簡潔粱锐。
什么是hook
hook網(wǎng)上有大量的解說,我自己認(rèn)為就是通過鉤子函數(shù)改變原函數(shù)的執(zhí)行行為扛邑。加入自己的思想怜浅。
參考:http://www.reibang.com/p/3382cc765b39
實(shí)踐
在沒有修改代碼之前我通常都是這樣寫:
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!isLogin) {
//彈出提示框,引導(dǎo)用戶登錄
} else {
//下一步操作
}
}
})蔬崩;
修改代碼后通過一行代碼做完if else恶座;
HookSetOnClickListenerHelper.getInstance().hook(getContext(), btn);
關(guān)鍵代碼
public void hook(Context context, final View view) {
try {
if (context == null) {
throw new NullPointerException("context is not init");
}
this.mCtx = context;
// 反射執(zhí)行View類的getListenerInfo()方法,拿到view的mListenerInfo對象沥阳,這個(gè)對象就是點(diǎn)擊事件的持有者
Method method = View.class.getDeclaredMethod("getListenerInfo");
//設(shè)置權(quán)限
method.setAccessible(true);
//得到點(diǎn)擊事件的持有者
Object listenerInfo = method.invoke(view);
//得到點(diǎn)擊事件對象
Class<?> listenerInfoClz = Class.forName("android.view.View$ListenerInfo");
Field field = listenerInfoClz.getDeclaredField("mOnClickListener");
final View.OnClickListener onClickListener = (View.OnClickListener) field.get(listenerInfo);
創(chuàng)建點(diǎn)擊事件代理
Object proxyOnclickListener = Proxy.newProxyInstance(context.getClass().getClassLoader(), new Class[]{View.OnClickListener.class}, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (!MyApplication.instance.getLogin()) {
goLogin();
return null;
}
return method.invoke(onClickListener, args);
}
});
//將點(diǎn)擊事件代理類設(shè)置到“持有者中”
field.set(listenerInfo, proxyOnclickListener);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
代理類代碼
public class ProxyOnClickListener implements View.OnClickListener {
View.OnClickListener mOnClickListener;
public ProxyOnClickListener(View.OnClickListener clickListener) {
this.mOnClickListener = clickListener;
}
@Override
public void onClick(View v) {
Log.d(">>>>>", "onClick: 點(diǎn)擊了事件");
if (mOnClickListener != null) {
mOnClickListener.onClick(v);
}
}
}
HookSetOnClickListenerHelper全部代碼
public class HookSetOnClickListenerHelper {
private static HookSetOnClickListenerHelper instance;
private Context mCtx;
private HookSetOnClickListenerHelper() {
}
public static HookSetOnClickListenerHelper getInstance() {
if (instance == null) {
synchronized (HookSetOnClickListenerHelper.class) {
if (instance == null) {
instance = new HookSetOnClickListenerHelper();
}
}
}
return instance;
}
public void hook(Context context, final View view) {
try {
if (context == null) {
throw new NullPointerException("context is not init");
}
this.mCtx = context;
// 反射執(zhí)行View類的getListenerInfo()方法跨琳,拿到view的mListenerInfo對象,這個(gè)對象就是點(diǎn)擊事件的持有者
Method method = View.class.getDeclaredMethod("getListenerInfo");
//設(shè)置權(quán)限
method.setAccessible(true);
//得到點(diǎn)擊事件的持有者
Object listenerInfo = method.invoke(view);
//得到點(diǎn)擊事件對象
Class<?> listenerInfoClz = Class.forName("android.view.View$ListenerInfo");
Field field = listenerInfoClz.getDeclaredField("mOnClickListener");
final View.OnClickListener onClickListener = (View.OnClickListener) field.get(listenerInfo);
創(chuàng)建點(diǎn)擊事件代理
Object proxyOnclickListener = Proxy.newProxyInstance(context.getClass().getClassLoader(), new Class[]{View.OnClickListener.class}, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (!MyApplication.instance.getLogin()) {
goLogin();
return null;
}
return method.invoke(onClickListener, args);
}
});
//將點(diǎn)擊事件代理類設(shè)置到“持有者中”
field.set(listenerInfo, proxyOnclickListener);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
private void goLogin() {
String tip = mCtx.getString(R.string.personal_center18);
WarningDialog dialog = new WarningDialog(Objects.requireNonNull(mCtx), tip, R.style.warningDialog, true) {
@Override
public void OnClickCancel() {
}
@Override
public void OnClickSure() {
Intent intent = new Intent(mCtx, LoginActivity.class);
mCtx.startActivity(intent);
}
};
dialog.show();
}
}
使用
//btn是需要代理的view
btn.setOnClickListener(v -> {
startActivityForResult(CouponActivity.newInstance(getContext(), true), Request.CODE.REQUEST_COMMENT);
});
//ps 這里一定要寫在事件之后
HookSetOnClickListenerHelper.getInstance().hook(getContext(), btn);
總結(jié)
關(guān)于login方法中的dialog代碼沒有貼出桐罕,可以自行修改脉让,做到這里我們是可以正常劫持view的點(diǎn)擊事件的桂敛,但是存在一個(gè)問題就是只能對單個(gè)View對象劫持,也就是每操作一個(gè)控件都需要加上 HookSetOnClickListenerHelper.getInstance().hook(getContext(), btn);依然有缺陷溅潜,后續(xù)改進(jìn)术唬。。伟恶。碴开。。