1 /* 2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.cts.verifier.libs.ui; 18 19 import android.text.Html; 20 import android.view.View; 21 import android.webkit.WebView; 22 23 /** 24 * An implementation of TextFormatter for HTML output. 25 */ 26 public class HtmlFormatter extends TextFormatter { 27 28 /** 29 * Starts the HTML document block 30 * @return this TextFormatter to allow for cascading calls. 31 */ openDocument()32 public TextFormatter openDocument() { 33 mSB.append("<!DOCTYPE html>\n<html lang=\"en-US\">\n<body>\n"); 34 return this; 35 } 36 37 /** 38 * Closes the HTML document block 39 * @return this TextFormatter to allow for cascading calls. 40 */ closeDocument()41 public TextFormatter closeDocument() { 42 mSB.append("</body>\n</html>"); 43 return this; 44 } 45 46 /** 47 * Starts the HTML Heading block 48 * @param level The desired heading level. Should be between 1 and 6 inclusive. 49 * @return this TextFormatter to allow for cascading calls. 50 */ openHeading(int level)51 public TextFormatter openHeading(int level) { 52 mSB.append("<h" + level + ">"); 53 return this; 54 } 55 56 /** 57 * Ends the HTML Heading block 58 * @param level The heading level associated with the corresponding openHeading() call. 59 * @return this TextFormatter to allow for cascading calls. 60 */ closeHeading(int level)61 public TextFormatter closeHeading(int level) { 62 mSB.append("</h" + level + ">"); 63 return this; 64 } 65 66 /** 67 * Opens an HTML paragraph block. 68 * @return this TextFormatter to allow for cascading calls. 69 */ openParagraph()70 public TextFormatter openParagraph() { 71 mSB.append("<p>"); 72 return this; 73 } 74 75 /** 76 * Closes an HTML paragraph block. 77 * @return this TextFormatter to allow for cascading calls. 78 */ closeParagraph()79 public TextFormatter closeParagraph() { 80 mSB.append("</p>\n"); 81 return this; 82 } 83 84 /** 85 * Opens an HTML bold block. 86 * @return this TextFormatter to allow for cascading calls. 87 */ openBold()88 public TextFormatter openBold() { 89 mSB.append("<b>"); 90 return this; 91 } 92 93 /** 94 * Closes an HTML bold block. 95 * @return this TextFormatter to allow for cascading calls. 96 */ closeBold()97 public TextFormatter closeBold() { 98 mSB.append("</b>"); 99 return this; 100 } 101 102 /** 103 * Opens an HTML italic block. 104 * @return this TextFormatter to allow for cascading calls. 105 */ openItalic()106 public TextFormatter openItalic() { 107 mSB.append("<i>"); 108 return this; 109 } 110 111 /** 112 * Closes an HTML italic block. 113 * @return this TextFormatter to allow for cascading calls. 114 */ closeItalic()115 public TextFormatter closeItalic() { 116 mSB.append("</i>"); 117 return this; 118 } 119 120 /** 121 * Inserts a 'break' in the HTML 122 * @return this TextFormatter to allow for cascading calls. 123 */ appendBreak()124 public TextFormatter appendBreak() { 125 mSB.append("<br>\n"); 126 return this; 127 } 128 129 /** 130 * Opens a text color block 131 * @param color The desired color, i.e. "red", "blue"... 132 * @return this TextFormatter to allow for cascading calls. 133 */ openTextColor(String color)134 public TextFormatter openTextColor(String color) { 135 mSB.append("<font color=\"" + color + "\">"); 136 return this; 137 } 138 139 /** 140 * Closes a color block 141 * @return this TextFormatter to allow for cascading calls. 142 */ closeTextColor()143 public TextFormatter closeTextColor() { 144 mSB.append("</font>"); 145 return this; 146 } 147 148 /** 149 * Starts a bullets list. 150 * @return This TextFormatter to allow for cascading calls. 151 */ openBulletList()152 public TextFormatter openBulletList() { 153 mSB.append("<ul>"); 154 return this; 155 } 156 157 /** 158 * Ends a bullets list. 159 * @return This TextFormatter to allow for cascading calls. 160 */ closeBulletList()161 public TextFormatter closeBulletList() { 162 mSB.append("</ul>"); 163 return this; 164 } 165 166 /** 167 * Opens a list item in an enclosing bulleted list. 168 * @return This TextFormatter to allow for cascading calls. 169 */ openListItem()170 public TextFormatter openListItem() { 171 mSB.append("<li>"); 172 return this; 173 } 174 175 /** 176 * Closes a list item in an enclosing bulleted list. 177 * @return This TextFormatter to allow for cascading calls. 178 */ closeListItem()179 public TextFormatter closeListItem() { 180 mSB.append("</li>"); 181 return this; 182 } 183 184 /** 185 * Appends a link tag with the specified link target URL 186 * @param url The url for the link. 187 * @return This TextFormatter to allow for cascading calls. 188 */ openLink(String url)189 public TextFormatter openLink(String url) { 190 mSB.append("<a href=\"" + url + "\">"); 191 return this; 192 } 193 194 /** 195 * Closes a link tag. 196 * @return This TextFormatter to allow for cascading calls. 197 */ closeLink()198 public TextFormatter closeLink() { 199 mSB.append("</a>"); 200 return this; 201 } 202 203 /** 204 * Appends the specified text to the HTML stream. 205 * @return this TextFormatter to allow for cascading calls. 206 */ appendText(String text)207 public TextFormatter appendText(String text) { 208 mSB.append(Html.escapeHtml(text)); 209 return this; 210 } 211 212 /** 213 * Loads the formatted text into a view. 214 * 215 * @param view The View into which the formatted text will is to be displayed. 216 * Note: for an HtmlFormatter, this must be a WebView. 217 */ put(View view)218 public void put(View view) { 219 ((WebView) view).loadData(mSB.toString(), "text/html; charset=utf-8", "utf-8"); 220 } 221 } 222