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.google.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     /** @return {@link Builder} from the meta bug report. */
toBuilder()106     public abstract Builder toBuilder();
107 
108     @Override
describeContents()109     public int describeContents() {
110         return 0;
111     }
112 
113     @Override
writeToParcel(Parcel dest, int flags)114     public void writeToParcel(Parcel dest, int flags) {
115         dest.writeInt(getId());
116         dest.writeString(getTimestamp());
117         dest.writeString(getTitle());
118         dest.writeString(getUserName());
119         dest.writeString(getFilePath());
120         dest.writeString(getBugReportFileName());
121         dest.writeString(getAudioFileName());
122         dest.writeInt(getStatus());
123         dest.writeString(getStatusMessage());
124         dest.writeInt(getType());
125     }
126 
127     /** Converts {@link Date} to bugreport timestamp. */
toBugReportTimestamp(Date date)128     static String toBugReportTimestamp(Date date) {
129         return BUG_REPORT_TIMESTAMP_DATE_FORMAT.format(date);
130     }
131 
132     /** Creates a {@link Builder} with default, non-null values. */
builder()133     static Builder builder() {
134         return new AutoValue_MetaBugReport.Builder()
135                 .setTimestamp("")
136                 .setFilePath("")
137                 .setBugReportFileName("")
138                 .setAudioFileName("")
139                 .setStatusMessage("")
140                 .setTitle("")
141                 .setUserName("");
142     }
143 
144     /** A creator that's used by Parcelable. */
145     public static final Parcelable.Creator<MetaBugReport> CREATOR =
146             new Parcelable.Creator<MetaBugReport>() {
147                 public MetaBugReport createFromParcel(Parcel in) {
148                     int id = in.readInt();
149                     String timestamp = in.readString();
150                     String title = in.readString();
151                     String username = in.readString();
152                     String filePath = in.readString();
153                     String bugReportFileName = in.readString();
154                     String audioFileName = in.readString();
155                     int status = in.readInt();
156                     String statusMessage = in.readString();
157                     int type = in.readInt();
158                     return MetaBugReport.builder()
159                             .setId(id)
160                             .setTimestamp(timestamp)
161                             .setTitle(title)
162                             .setUserName(username)
163                             .setFilePath(filePath)
164                             .setBugReportFileName(bugReportFileName)
165                             .setAudioFileName(audioFileName)
166                             .setStatus(status)
167                             .setStatusMessage(statusMessage)
168                             .setType(type)
169                             .build();
170                 }
171 
172                 public MetaBugReport[] newArray(int size) {
173                     return new MetaBugReport[size];
174                 }
175             };
176 
177     /** Builder for MetaBugReport. */
178     @AutoValue.Builder
179     abstract static class Builder {
180         /** Sets id. */
setId(int id)181         public abstract Builder setId(int id);
182 
183         /** Sets timestamp. */
setTimestamp(String timestamp)184         public abstract Builder setTimestamp(String timestamp);
185 
186         /** Sets title. */
setTitle(String title)187         public abstract Builder setTitle(String title);
188 
189         /** Sets username. */
setUserName(String username)190         public abstract Builder setUserName(String username);
191 
192         /** Sets filepath. */
setFilePath(String filePath)193         public abstract Builder setFilePath(String filePath);
194 
195         /** Sets bugReportFileName. */
setBugReportFileName(String bugReportFileName)196         public abstract Builder setBugReportFileName(String bugReportFileName);
197 
198         /** Sets audioFileName. */
setAudioFileName(String audioFileName)199         public abstract Builder setAudioFileName(String audioFileName);
200 
201         /** Sets {@link Status}. */
setStatus(int status)202         public abstract Builder setStatus(int status);
203 
204         /** Sets statusmessage. */
setStatusMessage(String statusMessage)205         public abstract Builder setStatusMessage(String statusMessage);
206 
207         /** Sets the {@link BugReportType}. */
setType(@ugReportType int type)208         public abstract Builder setType(@BugReportType int type);
209 
build()210         public abstract MetaBugReport build();
211     }
212 }
213