blob: 7e858e1dc390b693c8eeeb8155113bc12160a055 [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
Aurimas Liutikas4d1699d2019-08-28 13:01:05 -070019import android.annotation.NonNull;
Joe Onorato1754d742016-11-21 17:51:35 -080020import android.annotation.SystemApi;
21import android.annotation.TestApi;
Joe Onorato1754d742016-11-21 17:51:35 -080022import android.os.Parcel;
23import android.os.Parcelable;
24import android.util.IntArray;
25
26import java.util.ArrayList;
27
28/**
29 * The arguments for an incident report.
30 * {@hide}
31 */
32@SystemApi
33@TestApi
34public final class IncidentReportArgs implements Parcelable {
35
36 private final IntArray mSections = new IntArray();
37 private final ArrayList<byte[]> mHeaders = new ArrayList<byte[]>();
38 private boolean mAll;
Joe Onorato8daca752019-01-28 19:41:58 -080039 private int mPrivacyPolicy;
Yao Chena8e78b92019-03-04 17:23:38 -080040 private String mReceiverPkg;
41 private String mReceiverCls;
Joe Onorato1754d742016-11-21 17:51:35 -080042
43 /**
44 * Construct an incident report args with no fields.
45 */
46 public IncidentReportArgs() {
Joe Onorato8daca752019-01-28 19:41:58 -080047 mPrivacyPolicy = IncidentManager.PRIVACY_POLICY_AUTO;
Joe Onorato1754d742016-11-21 17:51:35 -080048 }
49
50 /**
51 * Construct an incdent report args from the given parcel.
52 */
53 public IncidentReportArgs(Parcel in) {
54 readFromParcel(in);
55 }
56
Yi Jinf32af482017-08-11 15:00:49 -070057 @Override
Joe Onorato1754d742016-11-21 17:51:35 -080058 public int describeContents() {
59 return 0;
60 }
61
Yi Jinf32af482017-08-11 15:00:49 -070062 @Override
Joe Onorato1754d742016-11-21 17:51:35 -080063 public void writeToParcel(Parcel out, int flags) {
64 out.writeInt(mAll ? 1 : 0);
65
66 int N = mSections.size();
67 out.writeInt(N);
68 for (int i=0; i<N; i++) {
69 out.writeInt(mSections.get(i));
70 }
71
72 N = mHeaders.size();
73 out.writeInt(N);
74 for (int i=0; i<N; i++) {
75 out.writeByteArray(mHeaders.get(i));
76 }
Yi Jin0f047162017-09-05 13:44:22 -070077
Joe Onorato8daca752019-01-28 19:41:58 -080078 out.writeInt(mPrivacyPolicy);
Yao Chena8e78b92019-03-04 17:23:38 -080079
80 out.writeString(mReceiverPkg);
81
82 out.writeString(mReceiverCls);
Joe Onorato1754d742016-11-21 17:51:35 -080083 }
84
85 public void readFromParcel(Parcel in) {
86 mAll = in.readInt() != 0;
87
88 mSections.clear();
89 int N = in.readInt();
90 for (int i=0; i<N; i++) {
91 mSections.add(in.readInt());
92 }
93
94 mHeaders.clear();
95 N = in.readInt();
96 for (int i=0; i<N; i++) {
97 mHeaders.add(in.createByteArray());
98 }
Yi Jin0f047162017-09-05 13:44:22 -070099
Joe Onorato8daca752019-01-28 19:41:58 -0800100 mPrivacyPolicy = in.readInt();
Yao Chena8e78b92019-03-04 17:23:38 -0800101
102 mReceiverPkg = in.readString();
103
104 mReceiverCls = in.readString();
Joe Onorato1754d742016-11-21 17:51:35 -0800105 }
106
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700107 public static final @android.annotation.NonNull Parcelable.Creator<IncidentReportArgs> CREATOR
Joe Onorato1754d742016-11-21 17:51:35 -0800108 = new Parcelable.Creator<IncidentReportArgs>() {
109 public IncidentReportArgs createFromParcel(Parcel in) {
110 return new IncidentReportArgs(in);
111 }
112
113 public IncidentReportArgs[] newArray(int size) {
114 return new IncidentReportArgs[size];
115 }
116 };
117
118 /**
119 * Print this report as a string.
120 */
Aurimas Liutikas4d1699d2019-08-28 13:01:05 -0700121 @NonNull
Yi Jinf32af482017-08-11 15:00:49 -0700122 @Override
Joe Onorato1754d742016-11-21 17:51:35 -0800123 public String toString() {
124 final StringBuilder sb = new StringBuilder("Incident(");
125 if (mAll) {
126 sb.append("all");
127 } else {
128 final int N = mSections.size();
129 if (N > 0) {
130 sb.append(mSections.get(0));
131 }
132 for (int i=1; i<N; i++) {
133 sb.append(" ");
134 sb.append(mSections.get(i));
135 }
136 }
137 sb.append(", ");
138 sb.append(mHeaders.size());
Yi Jin0f047162017-09-05 13:44:22 -0700139 sb.append(" headers), ");
Joe Onorato8daca752019-01-28 19:41:58 -0800140 sb.append("privacy: ").append(mPrivacyPolicy);
Yao Chena8e78b92019-03-04 17:23:38 -0800141 sb.append("receiver pkg: ").append(mReceiverPkg);
142 sb.append("receiver cls: ").append(mReceiverCls);
Joe Onorato1754d742016-11-21 17:51:35 -0800143 return sb.toString();
144 }
145
146 /**
147 * Set this incident report to include all fields.
148 */
149 public void setAll(boolean all) {
150 mAll = all;
151 if (all) {
152 mSections.clear();
153 }
154 }
155
156 /**
Yi Jin0f047162017-09-05 13:44:22 -0700157 * Set this incident report privacy policy spec.
Yi Jin0f047162017-09-05 13:44:22 -0700158 */
Joe Onorato8daca752019-01-28 19:41:58 -0800159 public void setPrivacyPolicy(int privacyPolicy) {
160 switch (privacyPolicy) {
161 case IncidentManager.PRIVACY_POLICY_LOCAL:
162 case IncidentManager.PRIVACY_POLICY_EXPLICIT:
163 case IncidentManager.PRIVACY_POLICY_AUTO:
164 mPrivacyPolicy = privacyPolicy;
Yi Jin3ec5cc72018-01-26 13:42:43 -0800165 break;
166 default:
Joe Onorato8daca752019-01-28 19:41:58 -0800167 mPrivacyPolicy = IncidentManager.PRIVACY_POLICY_AUTO;
Yi Jin3ec5cc72018-01-26 13:42:43 -0800168 }
Yi Jin0f047162017-09-05 13:44:22 -0700169 }
170
171 /**
Yi Jinf32af482017-08-11 15:00:49 -0700172 * Add this section to the incident report. Skip if the input is smaller than 2 since section
173 * id are only valid for positive integer as Protobuf field id. Here 1 is reserved for Header.
Joe Onorato1754d742016-11-21 17:51:35 -0800174 */
175 public void addSection(int section) {
Yi Jinf32af482017-08-11 15:00:49 -0700176 if (!mAll && section > 1) {
Joe Onorato1754d742016-11-21 17:51:35 -0800177 mSections.add(section);
178 }
179 }
180
181 /**
182 * Returns whether the incident report will include all fields.
183 */
184 public boolean isAll() {
185 return mAll;
186 }
187
188 /**
189 * Returns whether this section will be included in the incident report.
190 */
191 public boolean containsSection(int section) {
192 return mAll || mSections.indexOf(section) >= 0;
193 }
194
195 public int sectionCount() {
196 return mSections.size();
197 }
198
199 public void addHeader(byte[] header) {
200 mHeaders.add(header);
201 }
Joe Onorato1754d742016-11-21 17:51:35 -0800202}
203