Snap for 4632767 from cbdd3b49a2ade97fa5df36b2eb3f042570e9bb13 to pi-release

Change-Id: Id5554ae09a827d3bd81073c4aafa1bf538c97a15
diff --git a/Android.bp b/Android.bp
index 18104ff..7f9f571 100644
--- a/Android.bp
+++ b/Android.bp
@@ -12,6 +12,11 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
+droiddoc_template {
+    name: "droiddoc-templates-sdk",
+    path: "res/assets/templates-sdk",
+}
+
 java_library_host {
     name: "doclava",
     srcs: [
diff --git a/res/assets/templates-sdk/yaml_navtree2.cs b/res/assets/templates-sdk/yaml_navtree2.cs
new file mode 100644
index 0000000..a8ec241
--- /dev/null
+++ b/res/assets/templates-sdk/yaml_navtree2.cs
@@ -0,0 +1,15 @@
+toc:
+<?cs
+if:docs.packages.link ?>
+- title: Class Index
+  path: /<?cs var:docs.classes.link ?><?cs
+    if:dac ?>
+  status_text: no-toggle<?cs
+    /if?>
+- title: Package Index
+  path: /<?cs var:docs.packages.link ?><?cs
+    if:dac ?>
+  status_text: no-toggle<?cs
+    /if?><?cs
+/if ?>
+<?cs var:reference_tree ?>
\ No newline at end of file
diff --git a/src/com/google/doclava/Doclava.java b/src/com/google/doclava/Doclava.java
index 2241098..b059a64 100644
--- a/src/com/google/doclava/Doclava.java
+++ b/src/com/google/doclava/Doclava.java
@@ -120,6 +120,7 @@
   public static Map<String, String> annotationDocumentationMap = null;
   public static boolean referenceOnly = false;
   public static boolean staticOnly = false;
+  public static boolean yamlV2 = false; /* whether to build the new version of the yaml file */
   public static AuxSource auxSource = new EmptyAuxSource();
   public static Linter linter = new EmptyLinter();
   public static boolean android = false;
@@ -365,6 +366,8 @@
         NAVTREE_ONLY = true;
       } else if (a[0].equals("-atLinksNavtree")) {
         AT_LINKS_NAVTREE = true;
+      } else if (a[0].equals("-yamlV2")) {
+        yamlV2 = true;
       } else if (a[0].equals("-devsite")) {
         // Don't copy any assets to devsite output
         includeAssets = false;
@@ -512,6 +515,12 @@
         // Write yaml tree.
         if (yamlNavFile != null){
           NavTree.writeYamlTree(javadocDir, yamlNavFile);
+          if (yamlV2) {
+            // Generate both for good measure, to make transitions easier, but change the filename
+            // for the new one so there's yet another explicit opt-in required by fixing the name.
+            yamlNavFile = "_NEW" + yamlNavFile;
+            NavTree.writeYamlTree2(javadocDir, yamlNavFile);
+          }
         }
 
         // Packages Pages
@@ -742,6 +751,9 @@
     if (option.equals("-devsite")) {
       return 1;
     }
+    if (option.equals("-yamlV2")) {
+      return 1;
+    }
     if (option.equals("-dac_libraryroot")) {
       return 2;
     }
diff --git a/src/com/google/doclava/NavTree.java b/src/com/google/doclava/NavTree.java
index de9dd4e..72a396c 100644
--- a/src/com/google/doclava/NavTree.java
+++ b/src/com/google/doclava/NavTree.java
@@ -60,6 +60,40 @@
 
   /**
    * Write the YAML formatted navigation tree.
+   * This is intended to replace writeYamlTree(), but for now requires an explicit opt-in via
+   * the yamlV2 flag in the doclava command. This version creates a yaml file with all classes,
+   * interface, exceptions, etc. separated into collapsible groups.
+   */
+  public static void writeYamlTree2(String dir, String fileName){
+    List<Node> children = new ArrayList<Node>();
+    for (PackageInfo pkg : Doclava.choosePackages()) {
+      children.add(makePackageNode(pkg));
+    }
+    Node node = new Node("Reference", Doclava.ensureSlash(dir) + "packages.html", children, null);
+    StringBuilder buf = new StringBuilder();
+
+    node.renderChildrenYaml(buf, 0);
+
+    Data data = Doclava.makeHDF();
+    data.setValue("reference_tree", buf.toString());
+
+    if (Doclava.USE_DEVSITE_LOCALE_OUTPUT_PATHS && (Doclava.libraryRoot != null)) {
+      dir = Doclava.ensureSlash(dir) + Doclava.libraryRoot;
+    }
+
+    data.setValue("docs.classes.link", Doclava.ensureSlash(dir) + "classes.html");
+    data.setValue("docs.packages.link", Doclava.ensureSlash(dir) + "packages.html");
+
+    ClearPage.write(data, "yaml_navtree2.cs", Doclava.ensureSlash(dir) + fileName);
+
+  }
+
+
+  /**
+   * Write the YAML formatted navigation tree (legacy version).
+   * This creates a yaml file with package names followed by all
+   * classes, interfaces, exceptions, etc. But they are not separated by classes, interfaces, etc.
+   * It also nests any nested classes under the parent class, instead of listing them as siblings.
    * @see "http://yaml.org/"
    */
   public static void writeYamlTree(String dir, String fileName){
@@ -254,5 +288,74 @@
       renderString(buf, mArtifact);
       buf.append(" ]");
     }
+
+
+    // YAML VERSION
+
+
+    static void renderStringYaml(StringBuilder buf, String s) {
+      if (s != null) {
+        final int N = s.length();
+        for (int i = 0; i < N; i++) {
+          char c = s.charAt(i);
+          if (c >= ' ' && c <= '~' && c != '"' && c != '\\') {
+            buf.append(c);
+          } else {
+            buf.append("\\u");
+            for (int j = 0; i < 4; i++) {
+              char x = (char) (c & 0x000f);
+              if (x >= 10) {
+                x = (char) (x - 10 + 'a');
+              } else {
+                x = (char) (x + '0');
+              }
+              buf.append(x);
+              c >>= 4;
+            }
+          }
+        }
+      }
+    }
+    void renderChildrenYaml(StringBuilder buf, int depth) {
+      List<Node> list = mChildren;
+      if (list != null && list.size() > 0) {
+        if (depth > 0) {
+          buf.append("\n\n" + getIndent(depth));
+          buf.append("section:");
+        }
+        final int N = list.size();
+        for (int i = 0; i < N; i++) {
+          // get each child Node and render it
+          list.get(i).renderYaml(buf, depth);
+        }
+        // Extra line break after each "section"
+        buf.append("\n");
+      }
+    }
+    void renderYaml(StringBuilder buf, int depth) {
+      buf.append("\n" + getIndent(depth));
+      buf.append("- title: \"");
+      renderStringYaml(buf, mLabel);
+      buf.append("\"");
+      // Add link path, if it exists (the class/interface toggles don't have links)
+      if (mLink != null) {
+        buf.append("\n" + getIndent(depth));
+        buf.append("  path: ");
+        renderStringYaml(buf, "/" + mLink);
+        // and the API level also only makes sense if there's a link
+        buf.append("\n" + getIndent(depth));
+        buf.append("  version_added: ");
+        renderStringYaml(buf, mSince);
+      }
+      // try rendering child Nodes
+      renderChildrenYaml(buf, depth + 1);
+    }
+    String getIndent(int depth) {
+      String spaces = "";
+      for (int i = 0; i < depth; i++) {
+        spaces += "  ";
+      }
+      return spaces;
+    }
   }
-}
+}
\ No newline at end of file