Ensure LayoutParams in widgets.txt are in correct package.

If a super class of LayoutParams is not in android.view or
android.widget, it can caus a NPE in the ADT. Do not add such
LayoutParams to the widgets.txt file.

Change-Id: I7878877b79f2e0e872cc06747f0f5e25f76bb17d
diff --git a/src/com/google/doclava/Doclava.java b/src/com/google/doclava/Doclava.java
index 79295fd..2fa0d2d 100644
--- a/src/com/google/doclava/Doclava.java
+++ b/src/com/google/doclava/Doclava.java
@@ -1474,6 +1474,9 @@
 
     ClassInfo[] classes = Converter.allClasses();
 
+    // The topmost LayoutParams class - android.view.ViewGroup.LayoutParams
+    ClassInfo topLayoutParams = null;
+
     // Go through all the fields of all the classes, looking SDK stuff.
     for (ClassInfo clazz : classes) {
 
@@ -1527,10 +1530,12 @@
         }
 
         if (annotated == false) {
-          // lets check if this is inside android.widget
-          PackageInfo pckg = clazz.containingPackage();
-          String packageName = pckg.name();
-          if ("android.widget".equals(packageName) || "android.view".equals(packageName)) {
+          if (topLayoutParams == null
+              && "android.view.ViewGroup.LayoutParams".equals(clazz.qualifiedName())) {
+            topLayoutParams = clazz;
+          }
+          // let's check if this is inside android.widget or android.view
+          if (isIncludedPackage(clazz)) {
             // now we check what this class inherits either from android.view.ViewGroup
             // or android.view.View, or android.view.ViewGroup.LayoutParams
             int type = checkInheritance(clazz);
@@ -1571,9 +1576,14 @@
     // before writing the list of classes, we do some checks, to make sure the layout params
     // are enclosed by a layout class (and not one that has been declared as a widget)
     for (int i = 0; i < layoutParams.size();) {
-      ClassInfo layoutParamClass = layoutParams.get(i);
-      ClassInfo containingClass = layoutParamClass.containingClass();
-      if (containingClass == null || layouts.indexOf(containingClass) == -1) {
+      ClassInfo clazz = layoutParams.get(i);
+      ClassInfo containingClass = clazz.containingClass();
+      boolean remove = containingClass == null || layouts.indexOf(containingClass) == -1;
+      // Also ensure that super classes of the layout params are in android.widget or android.view.
+      while (!remove && (clazz = clazz.superclass()) != null && !clazz.equals(topLayoutParams)) {
+        remove = !isIncludedPackage(clazz);
+      }
+      if (remove) {
         layoutParams.remove(i);
       } else {
         i++;
@@ -1584,6 +1594,14 @@
   }
 
   /**
+   * Check if the clazz is in package android.view or android.widget
+   */
+  private static boolean isIncludedPackage(ClassInfo clazz) {
+    String pckg = clazz.containingPackage().name();
+    return "android.widget".equals(pckg) || "android.view".equals(pckg);
+  }
+
+  /**
    * Writes a list of values into a text files.
    *
    * @param pathname the absolute os path of the output file.