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