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. Can change audio state. */ 39 static final int TYPE_INTERACTIVE = 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_SILENT = 1; 46 47 /** Annotation for bug report types. */ 48 @Retention(SOURCE) 49 @IntDef({TYPE_INTERACTIVE, TYPE_SILENT}) 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 */ getFilePath()78 public abstract String getFilePath(); 79 80 /** 81 * @return filename of the bug report zip file stored under the system user. 82 */ getBugReportFileName()83 public abstract String getBugReportFileName(); 84 85 /** 86 * @return filename of the audio message file stored under the system user. 87 */ getAudioFileName()88 public abstract String getAudioFileName(); 89 90 /** 91 * @return {@link Status} of the bug upload. 92 */ getStatus()93 public abstract int getStatus(); 94 95 /** 96 * @return StatusMessage of the bug upload. 97 */ getStatusMessage()98 public abstract String getStatusMessage(); 99 100 /** 101 * @return {@link BugReportType}. 102 */ getType()103 public abstract int getType(); 104 105 /** 106 * @return how many TTL (time-to-live) points left until the bugreport gets 107 * {@link Status#STATUS_EXPIRED}. 108 */ getTtlPoints()109 public abstract int getTtlPoints(); 110 111 /** @return {@link Builder} from the meta bug report. */ toBuilder()112 public abstract Builder toBuilder(); 113 114 @Override describeContents()115 public int describeContents() { 116 return 0; 117 } 118 119 @Override writeToParcel(Parcel dest, int flags)120 public void writeToParcel(Parcel dest, int flags) { 121 dest.writeInt(getId()); 122 dest.writeString(getTimestamp()); 123 dest.writeString(getTitle()); 124 dest.writeString(getUserName()); 125 dest.writeString(getFilePath()); 126 dest.writeString(getBugReportFileName()); 127 dest.writeString(getAudioFileName()); 128 dest.writeInt(getStatus()); 129 dest.writeString(getStatusMessage()); 130 dest.writeInt(getType()); 131 dest.writeInt(getTtlPoints()); 132 } 133 134 /** Converts {@link Date} to bugreport timestamp. */ toBugReportTimestamp(Date date)135 static String toBugReportTimestamp(Date date) { 136 return BUG_REPORT_TIMESTAMP_DATE_FORMAT.format(date); 137 } 138 139 /** Creates a {@link Builder} with default, non-null values. */ builder()140 static Builder builder() { 141 return new AutoValue_MetaBugReport.Builder() 142 .setTimestamp("") 143 .setFilePath("") 144 .setBugReportFileName("") 145 .setAudioFileName("") 146 .setStatusMessage("") 147 .setTitle("") 148 .setUserName(""); 149 } 150 151 /** A creator that's used by Parcelable. */ 152 public static final Parcelable.Creator<MetaBugReport> CREATOR = 153 new Parcelable.Creator<MetaBugReport>() { 154 public MetaBugReport createFromParcel(Parcel in) { 155 int id = in.readInt(); 156 String timestamp = in.readString(); 157 String title = in.readString(); 158 String username = in.readString(); 159 String filePath = in.readString(); 160 String bugReportFileName = in.readString(); 161 String audioFileName = in.readString(); 162 int status = in.readInt(); 163 String statusMessage = in.readString(); 164 int type = in.readInt(); 165 int ttlPoints = in.readInt(); 166 return MetaBugReport.builder() 167 .setId(id) 168 .setTimestamp(timestamp) 169 .setTitle(title) 170 .setUserName(username) 171 .setFilePath(filePath) 172 .setBugReportFileName(bugReportFileName) 173 .setAudioFileName(audioFileName) 174 .setStatus(status) 175 .setStatusMessage(statusMessage) 176 .setType(type) 177 .setTtlPoints(ttlPoints) 178 .build(); 179 } 180 181 public MetaBugReport[] newArray(int size) { 182 return new MetaBugReport[size]; 183 } 184 }; 185 186 /** Builder for MetaBugReport. */ 187 @AutoValue.Builder 188 abstract static class Builder { 189 /** Sets id. */ setId(int id)190 public abstract Builder setId(int id); 191 192 /** Sets timestamp. */ setTimestamp(String timestamp)193 public abstract Builder setTimestamp(String timestamp); 194 195 /** Sets title. */ setTitle(String title)196 public abstract Builder setTitle(String title); 197 198 /** Sets username. */ setUserName(String username)199 public abstract Builder setUserName(String username); 200 201 /** Sets filepath. */ setFilePath(String filePath)202 public abstract Builder setFilePath(String filePath); 203 204 /** Sets bugReportFileName. */ setBugReportFileName(String bugReportFileName)205 public abstract Builder setBugReportFileName(String bugReportFileName); 206 207 /** Sets audioFileName. */ setAudioFileName(String audioFileName)208 public abstract Builder setAudioFileName(String audioFileName); 209 210 /** Sets {@link Status}. */ setStatus(int status)211 public abstract Builder setStatus(int status); 212 213 /** Sets statusmessage. */ setStatusMessage(String statusMessage)214 public abstract Builder setStatusMessage(String statusMessage); 215 216 /** Sets the {@link BugReportType}. */ setType(@ugReportType int type)217 public abstract Builder setType(@BugReportType int type); 218 219 /** Sets the bugreport TTL (time-to-live) points. */ setTtlPoints(int ttlPoints)220 public abstract Builder setTtlPoints(int ttlPoints); 221 build()222 public abstract MetaBugReport build(); 223 } 224 } 225