Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.os; |
| 18 | |
| 19 | import android.annotation.SystemApi; |
| 20 | import android.annotation.TestApi; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 21 | import android.os.Parcel; |
| 22 | import android.os.Parcelable; |
| 23 | import android.util.IntArray; |
| 24 | |
| 25 | import java.util.ArrayList; |
| 26 | |
| 27 | /** |
| 28 | * The arguments for an incident report. |
| 29 | * {@hide} |
| 30 | */ |
| 31 | @SystemApi |
| 32 | @TestApi |
| 33 | public 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 Onorato | 8daca75 | 2019-01-28 19:41:58 -0800 | [diff] [blame] | 38 | private int mPrivacyPolicy; |
Yao Chen | a8e78b9 | 2019-03-04 17:23:38 -0800 | [diff] [blame] | 39 | private String mReceiverPkg; |
| 40 | private String mReceiverCls; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * Construct an incident report args with no fields. |
| 44 | */ |
| 45 | public IncidentReportArgs() { |
Joe Onorato | 8daca75 | 2019-01-28 19:41:58 -0800 | [diff] [blame] | 46 | mPrivacyPolicy = IncidentManager.PRIVACY_POLICY_AUTO; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Construct an incdent report args from the given parcel. |
| 51 | */ |
| 52 | public IncidentReportArgs(Parcel in) { |
| 53 | readFromParcel(in); |
| 54 | } |
| 55 | |
Yi Jin | f32af48 | 2017-08-11 15:00:49 -0700 | [diff] [blame] | 56 | @Override |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 57 | public int describeContents() { |
| 58 | return 0; |
| 59 | } |
| 60 | |
Yi Jin | f32af48 | 2017-08-11 15:00:49 -0700 | [diff] [blame] | 61 | @Override |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 62 | 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 Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 76 | |
Joe Onorato | 8daca75 | 2019-01-28 19:41:58 -0800 | [diff] [blame] | 77 | out.writeInt(mPrivacyPolicy); |
Yao Chen | a8e78b9 | 2019-03-04 17:23:38 -0800 | [diff] [blame] | 78 | |
| 79 | out.writeString(mReceiverPkg); |
| 80 | |
| 81 | out.writeString(mReceiverCls); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 82 | } |
| 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 Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 98 | |
Joe Onorato | 8daca75 | 2019-01-28 19:41:58 -0800 | [diff] [blame] | 99 | mPrivacyPolicy = in.readInt(); |
Yao Chen | a8e78b9 | 2019-03-04 17:23:38 -0800 | [diff] [blame] | 100 | |
| 101 | mReceiverPkg = in.readString(); |
| 102 | |
| 103 | mReceiverCls = in.readString(); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 106 | public static final @android.annotation.NonNull Parcelable.Creator<IncidentReportArgs> CREATOR |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 107 | = 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 Jin | f32af48 | 2017-08-11 15:00:49 -0700 | [diff] [blame] | 120 | @Override |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 121 | 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 Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 137 | sb.append(" headers), "); |
Joe Onorato | 8daca75 | 2019-01-28 19:41:58 -0800 | [diff] [blame] | 138 | sb.append("privacy: ").append(mPrivacyPolicy); |
Yao Chen | a8e78b9 | 2019-03-04 17:23:38 -0800 | [diff] [blame] | 139 | sb.append("receiver pkg: ").append(mReceiverPkg); |
| 140 | sb.append("receiver cls: ").append(mReceiverCls); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 141 | 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 Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 155 | * Set this incident report privacy policy spec. |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 156 | */ |
Joe Onorato | 8daca75 | 2019-01-28 19:41:58 -0800 | [diff] [blame] | 157 | 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 Jin | 3ec5cc7 | 2018-01-26 13:42:43 -0800 | [diff] [blame] | 163 | break; |
| 164 | default: |
Joe Onorato | 8daca75 | 2019-01-28 19:41:58 -0800 | [diff] [blame] | 165 | mPrivacyPolicy = IncidentManager.PRIVACY_POLICY_AUTO; |
Yi Jin | 3ec5cc7 | 2018-01-26 13:42:43 -0800 | [diff] [blame] | 166 | } |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /** |
Yi Jin | f32af48 | 2017-08-11 15:00:49 -0700 | [diff] [blame] | 170 | * 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 Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 172 | */ |
| 173 | public void addSection(int section) { |
Yi Jin | f32af48 | 2017-08-11 15:00:49 -0700 | [diff] [blame] | 174 | if (!mAll && section > 1) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 175 | 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 Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 200 | } |
| 201 | |