blob: 8d4472702206d9e02ca0e6429ca4b1a116a8f8a5 [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;
Joe Onorato8daca752019-01-28 19:41:58 -080038 private int mPrivacyPolicy;
Joe Onorato1754d742016-11-21 17:51:35 -080039
40 /**
41 * Construct an incident report args with no fields.
42 */
43 public IncidentReportArgs() {
Joe Onorato8daca752019-01-28 19:41:58 -080044 mPrivacyPolicy = IncidentManager.PRIVACY_POLICY_AUTO;
Joe Onorato1754d742016-11-21 17:51:35 -080045 }
46
47 /**
48 * Construct an incdent report args from the given parcel.
49 */
50 public IncidentReportArgs(Parcel in) {
51 readFromParcel(in);
52 }
53
Yi Jinf32af482017-08-11 15:00:49 -070054 @Override
Joe Onorato1754d742016-11-21 17:51:35 -080055 public int describeContents() {
56 return 0;
57 }
58
Yi Jinf32af482017-08-11 15:00:49 -070059 @Override
Joe Onorato1754d742016-11-21 17:51:35 -080060 public void writeToParcel(Parcel out, int flags) {
61 out.writeInt(mAll ? 1 : 0);
62
63 int N = mSections.size();
64 out.writeInt(N);
65 for (int i=0; i<N; i++) {
66 out.writeInt(mSections.get(i));
67 }
68
69 N = mHeaders.size();
70 out.writeInt(N);
71 for (int i=0; i<N; i++) {
72 out.writeByteArray(mHeaders.get(i));
73 }
Yi Jin0f047162017-09-05 13:44:22 -070074
Joe Onorato8daca752019-01-28 19:41:58 -080075 out.writeInt(mPrivacyPolicy);
Joe Onorato1754d742016-11-21 17:51:35 -080076 }
77
78 public void readFromParcel(Parcel in) {
79 mAll = in.readInt() != 0;
80
81 mSections.clear();
82 int N = in.readInt();
83 for (int i=0; i<N; i++) {
84 mSections.add(in.readInt());
85 }
86
87 mHeaders.clear();
88 N = in.readInt();
89 for (int i=0; i<N; i++) {
90 mHeaders.add(in.createByteArray());
91 }
Yi Jin0f047162017-09-05 13:44:22 -070092
Joe Onorato8daca752019-01-28 19:41:58 -080093 mPrivacyPolicy = in.readInt();
Joe Onorato1754d742016-11-21 17:51:35 -080094 }
95
96 public static final Parcelable.Creator<IncidentReportArgs> CREATOR
97 = new Parcelable.Creator<IncidentReportArgs>() {
98 public IncidentReportArgs createFromParcel(Parcel in) {
99 return new IncidentReportArgs(in);
100 }
101
102 public IncidentReportArgs[] newArray(int size) {
103 return new IncidentReportArgs[size];
104 }
105 };
106
107 /**
108 * Print this report as a string.
109 */
Yi Jinf32af482017-08-11 15:00:49 -0700110 @Override
Joe Onorato1754d742016-11-21 17:51:35 -0800111 public String toString() {
112 final StringBuilder sb = new StringBuilder("Incident(");
113 if (mAll) {
114 sb.append("all");
115 } else {
116 final int N = mSections.size();
117 if (N > 0) {
118 sb.append(mSections.get(0));
119 }
120 for (int i=1; i<N; i++) {
121 sb.append(" ");
122 sb.append(mSections.get(i));
123 }
124 }
125 sb.append(", ");
126 sb.append(mHeaders.size());
Yi Jin0f047162017-09-05 13:44:22 -0700127 sb.append(" headers), ");
Joe Onorato8daca752019-01-28 19:41:58 -0800128 sb.append("privacy: ").append(mPrivacyPolicy);
Joe Onorato1754d742016-11-21 17:51:35 -0800129 return sb.toString();
130 }
131
132 /**
133 * Set this incident report to include all fields.
134 */
135 public void setAll(boolean all) {
136 mAll = all;
137 if (all) {
138 mSections.clear();
139 }
140 }
141
142 /**
Yi Jin0f047162017-09-05 13:44:22 -0700143 * Set this incident report privacy policy spec.
Yi Jin0f047162017-09-05 13:44:22 -0700144 */
Joe Onorato8daca752019-01-28 19:41:58 -0800145 public void setPrivacyPolicy(int privacyPolicy) {
146 switch (privacyPolicy) {
147 case IncidentManager.PRIVACY_POLICY_LOCAL:
148 case IncidentManager.PRIVACY_POLICY_EXPLICIT:
149 case IncidentManager.PRIVACY_POLICY_AUTO:
150 mPrivacyPolicy = privacyPolicy;
Yi Jin3ec5cc72018-01-26 13:42:43 -0800151 break;
152 default:
Joe Onorato8daca752019-01-28 19:41:58 -0800153 mPrivacyPolicy = IncidentManager.PRIVACY_POLICY_AUTO;
Yi Jin3ec5cc72018-01-26 13:42:43 -0800154 }
Yi Jin0f047162017-09-05 13:44:22 -0700155 }
156
157 /**
Yi Jinf32af482017-08-11 15:00:49 -0700158 * Add this section to the incident report. Skip if the input is smaller than 2 since section
159 * id are only valid for positive integer as Protobuf field id. Here 1 is reserved for Header.
Joe Onorato1754d742016-11-21 17:51:35 -0800160 */
161 public void addSection(int section) {
Yi Jinf32af482017-08-11 15:00:49 -0700162 if (!mAll && section > 1) {
Joe Onorato1754d742016-11-21 17:51:35 -0800163 mSections.add(section);
164 }
165 }
166
167 /**
168 * Returns whether the incident report will include all fields.
169 */
170 public boolean isAll() {
171 return mAll;
172 }
173
174 /**
175 * Returns whether this section will be included in the incident report.
176 */
177 public boolean containsSection(int section) {
178 return mAll || mSections.indexOf(section) >= 0;
179 }
180
181 public int sectionCount() {
182 return mSections.size();
183 }
184
185 public void addHeader(byte[] header) {
186 mHeaders.add(header);
187 }
Joe Onorato1754d742016-11-21 17:51:35 -0800188}
189