preface
- I wrote an introduction to the previous two articles: Gradle plug-in + ASM practice - Introduction and Gradle+ASM actual combat - advanced level , those who are not familiar with gradle+ASM can go to the previous article to check
- github address: https://github.com/Peakmain/AsmActualCombat
Demand background
- The new company was replaced at the end of December, and the project structure of the new company needs to be reconstructed, so there is the previous article: Basic library -- encapsulates a framework based on kotlin+jetpack+mvvm to improve Android development efficiency
- At the same time, the company's project is rectifying the privacy policy. One of the requirements is that when the privacy policy pops up, click Cancel to enter the simple app instead of killing it directly
- In fact, there are many ways for a simple app. First, make a h5 directly. Second, restrict some functions. Third, enter the home page, but click any button to pop up the privacy pop-up box that needs authorization
- Today we mainly talk about the third way
Solution ideas
- Judge and intercept before each button click event
view.setOnClickListener(new OnClickListener() { public void onClick(View var1) { //isIntercept() sets whether to intercept events boolean isIntercept =isIntercept(); if(!isIntercept){ return; } //Original event handling } });
- Disadvantages: the disadvantages are obvious. If there are 100 click events, you need to copy one before each click
- At this time, we can consider inserting ASM bytecode stake to automatically add interception before each click event. We only need to set interception events when interception is needed
- Look directly at the ASM compiled code we have done
Function introduction
- Whether to enable the plug-in can be dynamically configured. It is enabled by default
- By default, it solves most problems of repeated clicks
- The method can be dynamically set to intercept the click event before processing
- Time consuming to obtain methods
How to use
ASM plug-in dependencies
- Step 1. Add the JitPack repository to your build file
Add it in your root build.gradle at the end of repositories:
buildscript { dependencies { classpath "io.github.peakmain:plugin:1.0.0" } }
- Step 2. Add the Use
Add it in your app module build.gradle at the end of repositories:
apply plugin: "com.peakmain.plugin"
Intercept event sdk dependency
- Step 1. Add the JitPack repository to your build file
Add it in your root build.gradle at the end of repositories:
allprojects { repositories { ... maven { url 'https://jitpack.io' } } }
- Step 2. Add the dependency
dependencies { implementation 'com.github.Peakmain:AsmActualCombat:0.1.1' }
Working with documents
- 1. Initialization in Application
SensorsDataAPI.init(this);
- 2. Dynamically set whether to open the plug-in
In gradle Set the following parameters in properties. False means not to close the plug-in, true means to close the plug-in, and the default is false
peakmainPlugin.disableAppClick=false
- 3. Set the interception event on the page to be intercepted
SensorsDataAPI.getInstance().setOnUserAgreementListener(new SensorsDataAPI.OnUserAgreementListener() { @Override public boolean onUserAgreement() { } })
The complete example code is as follows:
private void setUserAgreementListener(Activity activity) { isAcceptUserPrivacy = (Boolean) PreferencesUtils.getInstance(this).getParam(Constants.HAS_ACCEPT_USER_PRIVACY, false); SensorsDataAPI.getInstance().setOnUserAgreementListener(new SensorsDataAPI.OnUserAgreementListener() { @Override public boolean onUserAgreement() { if (!isAcceptUserPrivacy) { //No consent AlertDialog dialog = new AlertDialog.Builder(activity) .setContentView(R.layout.dialog_user_agreement) .setCancelable(false)//Click blank to cancel .show(); //Set the interception event of user protocol to NULL SensorsDataAPI.getInstance().setOnUserAgreementListener(null); dialog.setOnClickListener(R.id.stv_refuse, v -> { //Click event of cancel button dialog.dismiss(); setUserAgreementListener(activity); }); dialog.setOnClickListener(R.id.stv_agreement, v -> { //Click event of consent button com.peakmain.ui.utils.ToastUtils.showLong("Agreed"); PreferencesUtils.getInstance(activity).saveParams(Constants.HAS_ACCEPT_USER_PRIVACY, true); dialog.dismiss(); }); } return isAcceptUserPrivacy; } });
- 4. Time consuming to get method
Add the following comments above the time-consuming methods that need to be obtained
@LogMessageTime
summary
- It is very convenient to use. If you also have relevant needs: you can use this library if you want to intercept relevant code before clicking on events to handle things
- Of course, you'd better go down by yourself, run in your own project, and then change it to your own. You can directly raise your B grid and make your leaders look at you with new eyes