Android development tool of ActivityTracker

Disclaimer: This article is xing_star original article, please indicate the source!

This article synchronization from http://javaexception.com/archives/113

Android development tool of ActivityTracker

Today, the water inside the group, a small partner to ask a question, "joined the company more manifest file activity is not easy to find and locate, what the solution." The problem I think is very experienced, to recommend a ActivityTracker other software, this software is the role of thing, is to open a floating window, the name of Activity of the current page will be displayed, regardless of the system App, or to take over the new company App, through the Activity class name display, can easily find this piece of code where the business logic. This gadget can really improve development efficiency, so be prepared to be recorded, this thing is used for several years, and today it was not asked, nor have the idea to write an article specially recorded ActivityTracker.

Source code analysis

This gadget is on Github open source, address https://github.com/fashare2015/ActivityTracker , also had the privilege mentioned pr, the amount of code on four or five classes, it is easy to read it. It uses essentially the Android AccessibilityService This auxiliary services mechanism, the detection timing corresponding to the screen Activity, simultaneously displayed with the view suspended WindowManager Service.

Specific to the code level, when the user opens the ancillary services, TrackerService is activated, onAccessibilityEvent method will continue to be performed

@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
    Log.d(TAG, "onAccessibilityEvent: " + event.getPackageName());
    if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
 
        CharSequence packageName = event.getPackageName();
        CharSequence className = event.getClassName();
        if (!TextUtils.isEmpty(packageName) && !TextUtils.isEmpty(className)) {
            EventBus.getDefault().post(new ActivityChangedEvent(
                    event.getPackageName().toString(),
                    event.getClassName().toString()
            ));
        }
    }
}

When triggered onAcessibilityEvent satisfy TYPE_WINDOW_STATE_CHANGED (Window window to indicate changes) the conditions, by the transfer package name EventBus current page, Activity class name to FloatingView followed refreshed FloatingView corresponding package name in the class name value.

public void onEventMainThread(TrackerService.ActivityChangedEvent event){
    Log.d(TAG, "event:" + event.getPackageName() + ": " + event.getClassName());
    String packageName = event.getPackageName(),
            className = event.getClassName();
 
    mTvPackageName.setText(packageName);
    mTvClassName.setText(
            className.startsWith(packageName)?
            className.substring(packageName.length()):
            className
    );
    Log.d(TAG, "event:" + event.getPackageName() + ": " + event.getClassName() + ", end invoked!");
}

This is the core logic.

FloatingView is how to show hidden Service in it, you can see TrackerService which has a member variable mTrackerWindowManager, he used to control the display to hide FloatingView.

TrackerWindowManager static code block setting default suspension view display position, and can accept a touch gesture

static {
    WindowManager.LayoutParams params = new WindowManager.LayoutParams();
    params.x = 0;
    params.y = 0;
    params.width = WindowManager.LayoutParams.WRAP_CONTENT;
    params.height = WindowManager.LayoutParams.WRAP_CONTENT;
    params.gravity = Gravity.LEFT | Gravity.TOP;
    params.type = WindowManager.LayoutParams.TYPE_PHONE;
    params.format = PixelFormat.RGBA_8888;
    params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
            | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
 
    LAYOUT_PARAMS = params;
}
public void addView() {
    if(mFloatingView == null){
        mFloatingView = new FloatingView(mContext);
        mFloatingView.setLayoutParams(LAYOUT_PARAMS);
 
        mWindowManager.addView(mFloatingView, LAYOUT_PARAMS);
    }
}

addView method is to add FloatingView to WindowManager Service in, and displayed, this requires an understanding of WindowManager, understand its usage.

In the process we will find a real use, ActivityTracker obviously Activity page has finish, but the view is still suspended displayed on the current screen, the suspension control view shows the WindowManager, and this windowManager alive in the Service. So basically it will stay with, less likely to be killed by the system.

After analysis found that the gadget is not difficult, the main use is AccessibilityService, WindowManager add FloatingView, and EventBus. I believe we have achieved this gadget.

project address

https://github.com/fashare2015/ActivityTracker

Guess you like

Origin www.cnblogs.com/xing-star/p/10962004.html