1 /* 2 * Copyright (C) 2019 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.car.bugreport; 17 18 import static java.lang.annotation.RetentionPolicy.SOURCE; 19 20 import android.annotation.IntDef; 21 import android.os.Parcel; 22 import android.os.Parcelable; 23 24 import com.google.auto.value.AutoValue; 25 26 import java.lang.annotation.Retention; 27 import java.text.DateFormat; 28 import java.text.SimpleDateFormat; 29 import java.util.Date; 30 31 /** Represents the information that a bugreport can contain. */ 32 @AutoValue 33 abstract class MetaBugReport implements Parcelable { 34 35 private static final DateFormat BUG_REPORT_TIMESTAMP_DATE_FORMAT = 36 new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss"); 37 38 /** The app records audio message when initiated. It can change audio state. */ 39 static final int TYPE_AUDIO_FIRST = 0; 40 41 /** 42 * The app doesn't show dialog and doesn't record audio when initiated. It allows user to 43 * add audio message when bugreport is collected. 44 */ 45 static final int TYPE_AUDIO_LATER = 1; 46 47 /** Annotation for bug report types. */ 48 @Retention(SOURCE) 49 @IntDef({TYPE_AUDIO_FIRST, TYPE_AUDIO_LATER}) 50 @interface BugReportType {}; 51 52 /** 53 * @return Id of the bug report. Bug report id monotonically increases and is unique. 54 */ getId()55 public abstract int getId(); 56 57 /** 58 * @return Username (LDAP) that created this bugreport 59 */ getUserName()60 public abstract String getUserName(); 61 62 /** 63 * @return Title of the bug. 64 */ getTitle()65 public abstract String getTitle(); 66 67 /** 68 * @return Timestamp when the bug report is initialized. 69 */ getTimestamp()70 public abstract String getTimestamp(); 71 72 /** 73 * @return path to the zip file stored under the system user. 74 * 75 * <p>NOTE: This is the old way of storing final zipped bugreport. See 76 * {@link BugStorageProvider#URL_SEGMENT_OPEN_FILE} for more info. 77 * 78 * <p>@deprecated getBugReportFileName() is used now. 79 */ getFilePath()80 public abstract String getFilePath(); 81 82 /** 83 * @return filename of the bug report zip file stored under the system user. 84 */ getBugReportFileName()85 public abstract String getBugReportFileName(); 86 87 /** 88 * @return filename of the audio message file stored under the system user. 89 */ getAudioFileName()90 public abstract String getAudioFileName(); 91 92 /** 93 * @return {@link Status} of the bug upload. 94 */ getStatus()95 public abstract int getStatus(); 96 97 /** 98 * @return StatusMessage of the bug upload. 99 */ getStatusMessage()100 public abstract String getStatusMessage(); 101 102 /** 103 * @return {@link BugReportType}. 104 */ getType()105 public abstract int getType(); 106 107 /** 108 * @return how many TTL (time-to-live) points left until the bugreport gets 109 * {@link Status#STATUS_EXPIRED}. 110 */ getTtlPoints()111 public abstract int getTtlPoints(); 112 113 /** @return {@link Builder} from the meta bug report. */ toBuilder()114 public abstract Builder toBuilder(); 115 116 @Override describeContents()117 public int describeContents() { 118 return 0; 119 } 120 121 @Override writeToParcel(Parcel dest, int flags)122 public void writeToParcel(Parcel dest, int flags) { 123 dest.writeInt(getId()); 124 dest.writeString(getTimestamp()); 125 dest.writeString(getTitle()); 126 dest.writeString(getUserName()); 127 dest.writeString(getFilePath()); 128 dest.writeString(getBugReportFileName()); 129 dest.writeString(getAudioFileName()); 130 dest.writeInt(getStatus()); 131 dest.writeString(getStatusMessage()); 132 dest.writeInt(getType()); 133 dest.writeInt(getTtlPoints()); 134 } 135 136 /** Converts {@link Date} to bugreport timestamp. */ toBugReportTimestamp(Date date)137 static String toBugReportTimestamp(Date date) { 138 return BUG_REPORT_TIMESTAMP_DATE_FORMAT.format(date); 139 } 140 141 /** Creates a {@link Builder} with default, non-null values. */ builder()142 static Builder builder() { 143 return new AutoValue_MetaBugReport.Builder() 144 .setTimestamp("") 145 .setFilePath("") 146 .setBugReportFileName("") 147 .setAudioFileName("") 148 .setStatusMessage("") 149 .setTitle("") 150 .setUserName(""); 151 } 152 153 /** A creator that's used by Parcelable. */ 154 public static final Parcelable.Creator<MetaBugReport> CREATOR = 155 new Parcelable.Creator<MetaBugReport>() { 156 public MetaBugReport createFromParcel(Parcel in) { 157 int id = in.readInt(); 158 String timestamp = in.readString(); 159 String title = in.readString(); 160 String username = in.readString(); 161 String filePath = in.readString(); 162 String bugReportFileName = in.readString(); 163 String audioFileName = in.readString(); 164 int status = in.readInt(); 165 String statusMessage = in.readString(); 166 int type = in.readInt(); 167 int ttlPoints = in.readInt(); 168 return MetaBugReport.builder() 169 .setId(id) 170 .setTimestamp(timestamp) 171 .setTitle(title) 172 .setUserName(username) 173 .setFilePath(filePath) 174 .setBugReportFileName(bugReportFileName) 175 .setAudioFileName(audioFileName) 176 .setStatus(status) 177 .setStatusMessage(statusMessage) 178 .setType(type) 179 .setTtlPoints(ttlPoints) 180 .build(); 181 } 182 183 public MetaBugReport[] newArray(int size) { 184 return new MetaBugReport[size]; 185 } 186 }; 187 188 /** Builder for MetaBugReport. */ 189 @AutoValue.Builder 190 abstract static class Builder { 191 /** Sets id. */ setId(int id)192 public abstract Builder setId(int id); 193 194 /** Sets timestamp. */ setTimestamp(String timestamp)195 public abstract Builder setTimestamp(String timestamp); 196 197 /** Sets title. */ setTitle(String title)198 public abstract Builder setTitle(String title); 199 200 /** Sets username. */ setUserName(String username)201 public abstract Builder setUserName(String username); 202 203 /** Sets filepath. */ setFilePath(String filePath)204 public abstract Builder setFilePath(String filePath); 205 206 /** Sets bugReportFileName. */ setBugReportFileName(String bugReportFileName)207 public abstract Builder setBugReportFileName(String bugReportFileName); 208 209 /** Sets audioFileName. */ setAudioFileName(String audioFileName)210 public abstract Builder setAudioFileName(String audioFileName); 211 212 /** Sets {@link Status}. */ setStatus(int status)213 public abstract Builder setStatus(int status); 214 215 /** Sets statusmessage. */ setStatusMessage(String statusMessage)216 public abstract Builder setStatusMessage(String statusMessage); 217 218 /** Sets the {@link BugReportType}. */ setType(@ugReportType int type)219 public abstract Builder setType(@BugReportType int type); 220 221 /** Sets the bugreport TTL (time-to-live) points. */ setTtlPoints(int ttlPoints)222 public abstract Builder setTtlPoints(int ttlPoints); 223 build()224 public abstract MetaBugReport build(); 225 } 226 } 227