1 /* 2 * Copyright (C) 2017 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 package com.android.server.usb.descriptors.report; 17 18 import com.android.server.usb.descriptors.UsbDescriptorParser; 19 20 /** 21 * @hide 22 * A concrete implementation of ReportCanvas class which generates "Plain Text" output. 23 */ 24 public final class TextReportCanvas extends ReportCanvas { 25 private static final String TAG = "TextReportCanvas"; 26 27 private final StringBuilder mStringBuilder; 28 private int mListIndent; 29 private static final int LIST_INDENT_AMNT = 2; 30 31 /** 32 * Constructor. Connects plain-text output to the provided StringBuilder. 33 * @param connection The USB connection object used to retrieve strings 34 * from the USB device. 35 * @param stringBuilder Generated output gets written into this object. 36 */ 37 public TextReportCanvas(UsbDescriptorParser parser, StringBuilder stringBuilder) { 38 super(parser); 39 40 mStringBuilder = stringBuilder; 41 } 42 43 private void writeListIndent() { 44 for (int space = 0; space < mListIndent; space++) { 45 mStringBuilder.append(" "); 46 } 47 } 48 49 @Override 50 public void write(String text) { 51 mStringBuilder.append(text); 52 } 53 54 @Override 55 public void openHeader(int level) { 56 writeListIndent(); 57 mStringBuilder.append("["); 58 } 59 60 @Override 61 public void closeHeader(int level) { 62 mStringBuilder.append("]\n"); 63 } 64 65 @Override 66 public void openParagraph(boolean emphasis) { 67 writeListIndent(); 68 } 69 70 @Override 71 public void closeParagraph() { 72 mStringBuilder.append("\n"); 73 } 74 75 @Override 76 public void writeParagraph(String text, boolean inRed) { 77 openParagraph(inRed); 78 if (inRed) { 79 mStringBuilder.append("*" + text + "*"); 80 } else { 81 mStringBuilder.append(text); 82 } 83 closeParagraph(); 84 } 85 86 @Override 87 public void openList() { 88 mListIndent += LIST_INDENT_AMNT; 89 } 90 91 @Override 92 public void closeList() { 93 mListIndent -= LIST_INDENT_AMNT; 94 } 95 96 @Override 97 public void openListItem() { 98 writeListIndent(); 99 mStringBuilder.append("- "); 100 } 101 102 @Override 103 public void closeListItem() { 104 mStringBuilder.append("\n"); 105 } 106 } 107