I am writing a module to detect which Android activity is currently running, and complete information regarding the view tree of that activity.
I understand Android framework lays out, and draws the views. Where can I find out the information such as which views are currently visible on the screen and their positions?
I tried using reflection as below. But it always returns empty set of views. Does anyone have pointers on how to do that?
try {
String windowManagerClassName;
if (android.os.Build.VERSION.SDK_INT >= 17) {
windowManagerClassName = "android.view.WindowManagerGlobal";
} else {
windowManagerClassName = "android.view.WindowManagerImpl";
}
windowManager = Class.forName(windowManagerClassName);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (SecurityException e) {
throw new RuntimeException(e);
}
Field views;
Field instanceField;
try {
views = windowManager.getDeclaredField("mViews");
instanceField = windowManager.getDeclaredField(getWindowManagerString());
views.setAccessible(true);
instanceField.setAccessible(true);
Object instance = instanceField.get(null);
synchronized (windowManager) {
if (android.os.Build.VERSION.SDK_INT <= 18) {
return new HashSet<View>(Arrays.asList(((View[]) views.get(instance))));
}
}
...