Home | History | Annotate | Download | only in os
      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;
     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 static final int DEST_EXPLICIT = 100;
     36     private static final int DEST_AUTO = 200;
     37 
     38     private final IntArray mSections = new IntArray();
     39     private final ArrayList<byte[]> mHeaders = new ArrayList<byte[]>();
     40     private boolean mAll;
     41     private int mDest;
     42 
     43     /**
     44      * Construct an incident report args with no fields.
     45      */
     46     public IncidentReportArgs() {
     47         mDest = DEST_AUTO;
     48     }
     49 
     50     /**
     51      * Construct an incdent report args from the given parcel.
     52      */
     53     public IncidentReportArgs(Parcel in) {
     54         readFromParcel(in);
     55     }
     56 
     57     @Override
     58     public int describeContents() {
     59         return 0;
     60     }
     61 
     62     @Override
     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         }
     77 
     78         out.writeInt(mDest);
     79     }
     80 
     81     public void readFromParcel(Parcel in) {
     82         mAll = in.readInt() != 0;
     83 
     84         mSections.clear();
     85         int N = in.readInt();
     86         for (int i=0; i<N; i++) {
     87             mSections.add(in.readInt());
     88         }
     89 
     90         mHeaders.clear();
     91         N = in.readInt();
     92         for (int i=0; i<N; i++) {
     93             mHeaders.add(in.createByteArray());
     94         }
     95 
     96         mDest = in.readInt();
     97     }
     98 
     99     public static final Parcelable.Creator<IncidentReportArgs> CREATOR
    100             = new Parcelable.Creator<IncidentReportArgs>() {
    101         public IncidentReportArgs createFromParcel(Parcel in) {
    102             return new IncidentReportArgs(in);
    103         }
    104 
    105         public IncidentReportArgs[] newArray(int size) {
    106             return new IncidentReportArgs[size];
    107         }
    108     };
    109 
    110     /**
    111      * Print this report as a string.
    112      */
    113     @Override
    114     public String toString() {
    115         final StringBuilder sb = new StringBuilder("Incident(");
    116         if (mAll) {
    117             sb.append("all");
    118         } else {
    119             final int N = mSections.size();
    120             if (N > 0) {
    121                 sb.append(mSections.get(0));
    122             }
    123             for (int i=1; i<N; i++) {
    124                 sb.append(" ");
    125                 sb.append(mSections.get(i));
    126             }
    127         }
    128         sb.append(", ");
    129         sb.append(mHeaders.size());
    130         sb.append(" headers), ");
    131         sb.append("Dest enum value: ").append(mDest);
    132         return sb.toString();
    133     }
    134 
    135     /**
    136      * Set this incident report to include all fields.
    137      */
    138     public void setAll(boolean all) {
    139         mAll = all;
    140         if (all) {
    141             mSections.clear();
    142         }
    143     }
    144 
    145     /**
    146      * Set this incident report privacy policy spec.
    147      * @hide
    148      */
    149     public void setPrivacyPolicy(int dest) {
    150         switch (dest) {
    151             case DEST_EXPLICIT:
    152             case DEST_AUTO:
    153                 mDest = dest;
    154                 break;
    155             default:
    156                 mDest = DEST_AUTO;
    157         }
    158     }
    159 
    160     /**
    161      * Add this section to the incident report. Skip if the input is smaller than 2 since section
    162      * id are only valid for positive integer as Protobuf field id. Here 1 is reserved for Header.
    163      */
    164     public void addSection(int section) {
    165         if (!mAll && section > 1) {
    166             mSections.add(section);
    167         }
    168     }
    169 
    170     /**
    171      * Returns whether the incident report will include all fields.
    172      */
    173     public boolean isAll() {
    174         return mAll;
    175     }
    176 
    177     /**
    178      * Returns whether this section will be included in the incident report.
    179      */
    180     public boolean containsSection(int section) {
    181         return mAll || mSections.indexOf(section) >= 0;
    182     }
    183 
    184     public int sectionCount() {
    185         return mSections.size();
    186     }
    187 
    188     public void addHeader(byte[] header) {
    189         mHeaders.add(header);
    190     }
    191 }
    192 
    193