blob: a1f2430f4ee31589c10d3f8c9c368bad59b1f955 [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;
Yao Chena8e78b92019-03-04 17:23:38 -080039 private String mReceiverPkg;
40 private String mReceiverCls;
Joe Onorato1754d742016-11-21 17:51:35 -080041
42 /**
43 * Construct an incident report args with no fields.
44 */
45 public IncidentReportArgs() {
Joe Onorato8daca752019-01-28 19:41:58 -080046 mPrivacyPolicy = IncidentManager.PRIVACY_POLICY_AUTO;
Joe Onorato1754d742016-11-21 17:51:35 -080047 }
48
49 /**
50 * Construct an incdent report args from the given parcel.
51 */
52 public IncidentReportArgs(Parcel in) {
53 readFromParcel(in);
54 }
55
Yi Jinf32af482017-08-11 15:00:49 -070056 @Override
Joe Onorato1754d742016-11-21 17:51:35 -080057 public int describeContents() {
58 return 0;
59 }
60
Yi Jinf32af482017-08-11 15:00:49 -070061 @Override
Joe Onorato1754d742016-11-21 17:51:35 -080062 public void writeToParcel(Parcel out, int flags) {
63 out.writeInt(mAll ? 1 : 0);
64
65 int N = mSections.size();
66 out.writeInt(N);
67 for (int i=0; i<N; i++) {
68 out.writeInt(mSections.get(i));
69 }
70
71 N = mHeaders.size();
72 out.writeInt(N);
73 for (int i=0; i<N; i++) {
74 out.writeByteArray(mHeaders.get(i));
75 }
Yi Jin0f047162017-09-05 13:44:22 -070076
Joe Onorato8daca752019-01-28 19:41:58 -080077 out.writeInt(mPrivacyPolicy);
Yao Chena8e78b92019-03-04 17:23:38 -080078
79 out.writeString(mReceiverPkg);
80
81 out.writeString(mReceiverCls);
Joe Onorato1754d742016-11-21 17:51:35 -080082 }
83
84 public void readFromParcel(Parcel in) {
85 mAll = in.readInt() != 0;
86
87 mSections.clear();
88 int N = in.readInt();
89 for (int i=0; i<N; i++) {
90 mSections.add(in.readInt());
91 }
92
93 mHeaders.clear();
94 N = in.readInt();
95 for (int i=0; i<N; i++) {
96 mHeaders.add(in.createByteArray());
97 }
Yi Jin0f047162017-09-05 13:44:22 -070098
Joe Onorato8daca752019-01-28 19:41:58 -080099 mPrivacyPolicy = in.readInt();
Yao Chena8e78b92019-03-04 17:23:38 -0800100
101 mReceiverPkg = in.readString();
102
103 mReceiverCls = in.readString();
Joe Onorato1754d742016-11-21 17:51:35 -0800104 }
105
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700106 public static final @android.annotation.NonNull Parcelable.Creator<IncidentReportArgs> CREATOR
Joe Onorato1754d742016-11-21 17:51:35 -0800107 = new Parcelable.Creator<IncidentReportArgs>() {
108 public IncidentReportArgs createFromParcel(Parcel in) {
109 return new IncidentReportArgs(in);
110 }
111
112 public IncidentReportArgs[] newArray(int size) {
113 return new IncidentReportArgs[size];
114 }
115 };
116
117 /**
118 * Print this report as a string.
119 */
Yi Jinf32af482017-08-11 15:00:49 -0700120 @Override
Joe Onorato1754d742016-11-21 17:51:35 -0800121 public String toString() {
122 final StringBuilder sb = new StringBuilder("Incident(");
123 if (mAll) {
124 sb.append("all");
125 } else {
126 final int N = mSections.size();
127 if (N > 0) {
128 sb.append(mSections.get(0));
129 }
130 for (int i=1; i<N; i++) {
131 sb.append(" ");
132 sb.append(mSections.get(i));
133 }
134 }
135 sb.append(", ");
136 sb.append(mHeaders.size());
Yi Jin0f047162017-09-05 13:44:22 -0700137 sb.append(" headers), ");
Joe Onorato8daca752019-01-28 19:41:58 -0800138 sb.append("privacy: ").append(mPrivacyPolicy);
Yao Chena8e78b92019-03-04 17:23:38 -0800139 sb.append("receiver pkg: ").append(mReceiverPkg);
140 sb.append("receiver cls: ").append(mReceiverCls);
Joe Onorato1754d742016-11-21 17:51:35 -0800141 return sb.toString();
142 }
143
144 /**
145 * Set this incident report to include all fields.
146 */
147 public void setAll(boolean all) {
148 mAll = all;
149 if (all) {
150 mSections.clear();
151 }
152 }
153
154 /**
Yi Jin0f047162017-09-05 13:44:22 -0700155 * Set this incident report privacy policy spec.
Yi Jin0f047162017-09-05 13:44:22 -0700156 */
Joe Onorato8daca752019-01-28 19:41:58 -0800157 public void setPrivacyPolicy(int privacyPolicy) {
158 switch (privacyPolicy) {
159 case IncidentManager.PRIVACY_POLICY_LOCAL:
160 case IncidentManager.PRIVACY_POLICY_EXPLICIT:
161 case IncidentManager.PRIVACY_POLICY_AUTO:
162 mPrivacyPolicy = privacyPolicy;
Yi Jin3ec5cc72018-01-26 13:42:43 -0800163 break;
164 default:
Joe Onorato8daca752019-01-28 19:41:58 -0800165 mPrivacyPolicy = IncidentManager.PRIVACY_POLICY_AUTO;
Yi Jin3ec5cc72018-01-26 13:42:43 -0800166 }
Yi Jin0f047162017-09-05 13:44:22 -0700167 }
168
169 /**
Yi Jinf32af482017-08-11 15:00:49 -0700170 * Add this section to the incident report. Skip if the input is smaller than 2 since section
171 * id are only valid for positive integer as Protobuf field id. Here 1 is reserved for Header.
Joe Onorato1754d742016-11-21 17:51:35 -0800172 */
173 public void addSection(int section) {
Yi Jinf32af482017-08-11 15:00:49 -0700174 if (!mAll && section > 1) {
Joe Onorato1754d742016-11-21 17:51:35 -0800175 mSections.add(section);
176 }
177 }
178
179 /**
180 * Returns whether the incident report will include all fields.
181 */
182 public boolean isAll() {
183 return mAll;
184 }
185
186 /**
187 * Returns whether this section will be included in the incident report.
188 */
189 public boolean containsSection(int section) {
190 return mAll || mSections.indexOf(section) >= 0;
191 }
192
193 public int sectionCount() {
194 return mSections.size();
195 }
196
197 public void addHeader(byte[] header) {
198 mHeaders.add(header);
199 }
Joe Onorato1754d742016-11-21 17:51:35 -0800200}
201