blob: abb3161713097a6d2f5cd922d30d0d8ab82e48bb [file] [log] [blame]
Joe Onorato1754d742016-11-21 17:51:35 -08001/*
2 * Copyright (C) 2005 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
17package android.os;
18
19import android.annotation.SystemApi;
20import android.annotation.TestApi;
Joe Onorato1754d742016-11-21 17:51:35 -080021import android.os.Parcel;
22import android.os.Parcelable;
23import android.util.IntArray;
24
25import java.util.ArrayList;
26
27/**
28 * The arguments for an incident report.
29 * {@hide}
30 */
31@SystemApi
32@TestApi
33public final class IncidentReportArgs implements Parcelable {
34
35 private final IntArray mSections = new IntArray();
36 private final ArrayList<byte[]> mHeaders = new ArrayList<byte[]>();
37 private boolean mAll;
38
39 /**
40 * Construct an incident report args with no fields.
41 */
42 public IncidentReportArgs() {
43 }
44
45 /**
46 * Construct an incdent report args from the given parcel.
47 */
48 public IncidentReportArgs(Parcel in) {
49 readFromParcel(in);
50 }
51
Yi Jinf32af482017-08-11 15:00:49 -070052 @Override
Joe Onorato1754d742016-11-21 17:51:35 -080053 public int describeContents() {
54 return 0;
55 }
56
Yi Jinf32af482017-08-11 15:00:49 -070057 @Override
Joe Onorato1754d742016-11-21 17:51:35 -080058 public void writeToParcel(Parcel out, int flags) {
59 out.writeInt(mAll ? 1 : 0);
60
61 int N = mSections.size();
62 out.writeInt(N);
63 for (int i=0; i<N; i++) {
64 out.writeInt(mSections.get(i));
65 }
66
67 N = mHeaders.size();
68 out.writeInt(N);
69 for (int i=0; i<N; i++) {
70 out.writeByteArray(mHeaders.get(i));
71 }
72 }
73
74 public void readFromParcel(Parcel in) {
75 mAll = in.readInt() != 0;
76
77 mSections.clear();
78 int N = in.readInt();
79 for (int i=0; i<N; i++) {
80 mSections.add(in.readInt());
81 }
82
83 mHeaders.clear();
84 N = in.readInt();
85 for (int i=0; i<N; i++) {
86 mHeaders.add(in.createByteArray());
87 }
88 }
89
90 public static final Parcelable.Creator<IncidentReportArgs> CREATOR
91 = new Parcelable.Creator<IncidentReportArgs>() {
92 public IncidentReportArgs createFromParcel(Parcel in) {
93 return new IncidentReportArgs(in);
94 }
95
96 public IncidentReportArgs[] newArray(int size) {
97 return new IncidentReportArgs[size];
98 }
99 };
100
101 /**
102 * Print this report as a string.
103 */
Yi Jinf32af482017-08-11 15:00:49 -0700104 @Override
Joe Onorato1754d742016-11-21 17:51:35 -0800105 public String toString() {
106 final StringBuilder sb = new StringBuilder("Incident(");
107 if (mAll) {
108 sb.append("all");
109 } else {
110 final int N = mSections.size();
111 if (N > 0) {
112 sb.append(mSections.get(0));
113 }
114 for (int i=1; i<N; i++) {
115 sb.append(" ");
116 sb.append(mSections.get(i));
117 }
118 }
119 sb.append(", ");
120 sb.append(mHeaders.size());
121 sb.append(" headers)");
122 return sb.toString();
123 }
124
125 /**
126 * Set this incident report to include all fields.
127 */
128 public void setAll(boolean all) {
129 mAll = all;
130 if (all) {
131 mSections.clear();
132 }
133 }
134
135 /**
Yi Jinf32af482017-08-11 15:00:49 -0700136 * Add this section to the incident report. Skip if the input is smaller than 2 since section
137 * id are only valid for positive integer as Protobuf field id. Here 1 is reserved for Header.
Joe Onorato1754d742016-11-21 17:51:35 -0800138 */
139 public void addSection(int section) {
Yi Jinf32af482017-08-11 15:00:49 -0700140 if (!mAll && section > 1) {
Joe Onorato1754d742016-11-21 17:51:35 -0800141 mSections.add(section);
142 }
143 }
144
145 /**
146 * Returns whether the incident report will include all fields.
147 */
148 public boolean isAll() {
149 return mAll;
150 }
151
152 /**
153 * Returns whether this section will be included in the incident report.
154 */
155 public boolean containsSection(int section) {
156 return mAll || mSections.indexOf(section) >= 0;
157 }
158
159 public int sectionCount() {
160 return mSections.size();
161 }
162
163 public void addHeader(byte[] header) {
164 mHeaders.add(header);
165 }
166
167 /**
168 * Parses an incident report config as described in the system setting.
169 *
170 * @see IncidentManager#reportIncident
171 */
172 public static IncidentReportArgs parseSetting(String setting)
173 throws IllegalArgumentException {
174 if (setting == null || setting.length() == 0) {
175 return null;
176 }
177 setting = setting.trim();
178 if (setting.length() == 0 || "disabled".equals(setting)) {
179 return null;
180 }
181
182 final IncidentReportArgs args = new IncidentReportArgs();
183
184 if ("all".equals(setting)) {
185 args.setAll(true);
186 return args;
187 } else if ("none".equals(setting)) {
188 return args;
189 }
190
191 final String[] splits = setting.split(",");
192 final int N = splits.length;
193 for (int i=0; i<N; i++) {
194 final String str = splits[i].trim();
195 if (str.length() == 0) {
196 continue;
197 }
198 int section;
199 try {
200 section = Integer.parseInt(str);
201 } catch (NumberFormatException ex) {
202 throw new IllegalArgumentException("Malformed setting. Bad integer at section"
203 + " index " + i + ": section='" + str + "' setting='" + setting + "'");
204 }
205 if (section < 1) {
206 throw new IllegalArgumentException("Malformed setting. Illegal section at"
207 + " index " + i + ": section='" + str + "' setting='" + setting + "'");
208 }
209 args.addSection(section);
210 }
211
212 return args;
213 }
214}
215