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