io.selendroid.server.android.ViewHierarchyAnalyzer Maven / Gradle / Ivy
/*
* Copyright 2012-2014 eBay Software Foundation and selendroid committers.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package io.selendroid.server.android;
import io.selendroid.server.model.Factories;
import io.selendroid.server.ServerInstrumentation;
import io.selendroid.server.util.InstanceOfPredicate;
import io.selendroid.server.util.ListUtil;
import io.selendroid.server.util.SelendroidLogger;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import android.app.Activity;
import android.content.res.Resources;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.AbsListView;
import android.widget.ScrollView;
import com.android.internal.util.Predicate;
public class ViewHierarchyAnalyzer {
private static final ViewHierarchyAnalyzer INSTANCE = new ViewHierarchyAnalyzer();
public static ViewHierarchyAnalyzer getDefaultInstance() {
return INSTANCE;
}
public Set getTopLevelViews() {
Class> windowManager;
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(Arrays.asList(((View[]) views.get(instance))));
} else {
return new HashSet((ArrayList) views.get(instance));
}
}
} catch (Exception e) {
SelendroidLogger.error("Cannot get top level views", e);
return new HashSet();
}
}
private String getWindowManagerString() {
if (android.os.Build.VERSION.SDK_INT >= 17) {
return "sDefaultWindowManager";
} else if (android.os.Build.VERSION.SDK_INT >= 13) {
return "sWindowManager";
} else {
return "mWindowManager";
}
}
public View getRecentDecorView() {
return getRecentDecorView(getTopLevelViews());
}
private View getRecentDecorView(Set views) {
Predicate