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; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * Construct an incident report args with no fields. |
| 42 | */ |
| 43 | public IncidentReportArgs() { |
Joe Onorato | 8daca75 | 2019-01-28 19:41:58 -0800 | [diff] [blame] | 44 | mPrivacyPolicy = IncidentManager.PRIVACY_POLICY_AUTO; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Construct an incdent report args from the given parcel. |
| 49 | */ |
| 50 | public IncidentReportArgs(Parcel in) { |
| 51 | readFromParcel(in); |
| 52 | } |
| 53 | |
Yi Jin | f32af48 | 2017-08-11 15:00:49 -0700 | [diff] [blame] | 54 | @Override |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 55 | public int describeContents() { |
| 56 | return 0; |
| 57 | } |
| 58 | |
Yi Jin | f32af48 | 2017-08-11 15:00:49 -0700 | [diff] [blame] | 59 | @Override |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 60 | 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 Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 74 | |
Joe Onorato | 8daca75 | 2019-01-28 19:41:58 -0800 | [diff] [blame] | 75 | out.writeInt(mPrivacyPolicy); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 76 | } |
| 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 Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 92 | |
Joe Onorato | 8daca75 | 2019-01-28 19:41:58 -0800 | [diff] [blame] | 93 | mPrivacyPolicy = in.readInt(); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 94 | } |
| 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 Jin | f32af48 | 2017-08-11 15:00:49 -0700 | [diff] [blame] | 110 | @Override |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 111 | 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 Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 127 | sb.append(" headers), "); |
Joe Onorato | 8daca75 | 2019-01-28 19:41:58 -0800 | [diff] [blame] | 128 | sb.append("privacy: ").append(mPrivacyPolicy); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 129 | 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 Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 143 | * Set this incident report privacy policy spec. |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 144 | */ |
Joe Onorato | 8daca75 | 2019-01-28 19:41:58 -0800 | [diff] [blame] | 145 | 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 Jin | 3ec5cc7 | 2018-01-26 13:42:43 -0800 | [diff] [blame] | 151 | break; |
| 152 | default: |
Joe Onorato | 8daca75 | 2019-01-28 19:41:58 -0800 | [diff] [blame] | 153 | mPrivacyPolicy = IncidentManager.PRIVACY_POLICY_AUTO; |
Yi Jin | 3ec5cc7 | 2018-01-26 13:42:43 -0800 | [diff] [blame] | 154 | } |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /** |
Yi Jin | f32af48 | 2017-08-11 15:00:49 -0700 | [diff] [blame] | 158 | * 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 Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 160 | */ |
| 161 | public void addSection(int section) { |
Yi Jin | f32af48 | 2017-08-11 15:00:49 -0700 | [diff] [blame] | 162 | if (!mAll && section > 1) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 163 | 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 Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 188 | } |
| 189 | |