1/* 2 * Copyright (C) 2006 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.server.data; 18 19import java.io.DataInput; 20import java.io.DataOutput; 21import java.io.IOException; 22 23import static com.android.internal.util.Objects.nonNull; 24 25/** 26 * Crash data transfer object. Keep in sync. with the server side version. 27 */ 28public class CrashData { 29 30 final String id; 31 final String activity; 32 final long time; 33 final BuildData buildData; 34 final ThrowableData throwableData; 35 final byte[] state; 36 37 public CrashData(String id, String activity, BuildData buildData, 38 ThrowableData throwableData) { 39 this.id = nonNull(id); 40 this.activity = nonNull(activity); 41 this.buildData = nonNull(buildData); 42 this.throwableData = nonNull(throwableData); 43 this.time = System.currentTimeMillis(); 44 this.state = null; 45 } 46 47 public CrashData(String id, String activity, BuildData buildData, 48 ThrowableData throwableData, byte[] state) { 49 this.id = nonNull(id); 50 this.activity = nonNull(activity); 51 this.buildData = nonNull(buildData); 52 this.throwableData = nonNull(throwableData); 53 this.time = System.currentTimeMillis(); 54 this.state = state; 55 } 56 57 public CrashData(DataInput in) throws IOException { 58 int dataVersion = in.readInt(); 59 if (dataVersion != 0 && dataVersion != 1) { 60 throw new IOException("Expected 0 or 1. Got: " + dataVersion); 61 } 62 63 this.id = in.readUTF(); 64 this.activity = in.readUTF(); 65 this.time = in.readLong(); 66 this.buildData = new BuildData(in); 67 this.throwableData = new ThrowableData(in); 68 if (dataVersion == 1) { 69 int len = in.readInt(); 70 if (len == 0) { 71 this.state = null; 72 } else { 73 this.state = new byte[len]; 74 in.readFully(this.state, 0, len); 75 } 76 } else { 77 this.state = null; 78 } 79 } 80 81 public CrashData(String tag, Throwable throwable) { 82 id = ""; 83 activity = tag; 84 buildData = new BuildData(); 85 throwableData = new ThrowableData(throwable); 86 time = System.currentTimeMillis(); 87 state = null; 88 } 89 90 public void write(DataOutput out) throws IOException { 91 // version 92 if (this.state == null) { 93 out.writeInt(0); 94 } else { 95 out.writeInt(1); 96 } 97 98 out.writeUTF(this.id); 99 out.writeUTF(this.activity); 100 out.writeLong(this.time); 101 buildData.write(out); 102 throwableData.write(out); 103 if (this.state != null) { 104 out.writeInt(this.state.length); 105 out.write(this.state, 0, this.state.length); 106 } 107 } 108 109 public BuildData getBuildData() { 110 return buildData; 111 } 112 113 public ThrowableData getThrowableData() { 114 return throwableData; 115 } 116 117 public String getId() { 118 return id; 119 } 120 121 public String getActivity() { 122 return activity; 123 } 124 125 public long getTime() { 126 return time; 127 } 128 129 public byte[] getState() { 130 return state; 131 } 132 133 /** 134 * Return a brief description of this CrashData record. The details of the 135 * representation are subject to change. 136 * 137 * @return Returns a String representing the contents of the object. 138 */ 139 @Override 140 public String toString() { 141 return "[CrashData: id=" + id + " activity=" + activity + " time=" + time + 142 " buildData=" + buildData.toString() + 143 " throwableData=" + throwableData.toString() + "]"; 144 } 145} 146