OLD | NEW |
(Empty) | |
| 1 package org.adblockplus.android; |
| 2 |
| 3 import java.io.IOException; |
| 4 import java.io.StringWriter; |
| 5 import java.util.regex.Pattern; |
| 6 |
| 7 import org.apache.http.HttpResponse; |
| 8 import org.apache.http.StatusLine; |
| 9 import org.apache.http.client.ClientProtocolException; |
| 10 import org.apache.http.client.HttpClient; |
| 11 import org.apache.http.client.methods.HttpPost; |
| 12 import org.apache.http.entity.StringEntity; |
| 13 import org.apache.http.impl.client.DefaultHttpClient; |
| 14 import org.apache.http.util.EntityUtils; |
| 15 import org.xmlpull.v1.XmlSerializer; |
| 16 |
| 17 import android.app.Activity; |
| 18 import android.os.Bundle; |
| 19 import android.text.TextUtils; |
| 20 import android.util.Log; |
| 21 import android.util.Xml; |
| 22 import android.view.View; |
| 23 import android.view.Window; |
| 24 import android.widget.EditText; |
| 25 import android.widget.Toast; |
| 26 |
| 27 /** |
| 28 * Shows crash report dialog asking user to submit crash report together with co
mments. |
| 29 */ |
| 30 public final class CrashReportDialog extends Activity |
| 31 { |
| 32 private final static String TAG = "CrashReportDialog"; |
| 33 private String report; |
| 34 |
| 35 @Override |
| 36 protected void onCreate(Bundle savedInstanceState) |
| 37 { |
| 38 super.onCreate(savedInstanceState); |
| 39 requestWindowFeature(Window.FEATURE_LEFT_ICON); |
| 40 setContentView(R.layout.crashreport); |
| 41 |
| 42 Bundle extras = getIntent().getExtras(); |
| 43 if (extras == null) |
| 44 { |
| 45 finish(); |
| 46 return; |
| 47 } |
| 48 report = extras.getString("report"); |
| 49 |
| 50 getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, android.R.d
rawable.ic_dialog_alert); |
| 51 } |
| 52 |
| 53 public void onOk(View v) |
| 54 { |
| 55 String comment = ((EditText) findViewById(R.id.comments)).getText().toString
(); |
| 56 |
| 57 try |
| 58 { |
| 59 String[] reportLines = report.split(System.getProperty("line.separator")); |
| 60 int api = Integer.parseInt(reportLines[0]); |
| 61 int build = Integer.parseInt(reportLines[1]); |
| 62 |
| 63 XmlSerializer xmlSerializer = Xml.newSerializer(); |
| 64 StringWriter writer = new StringWriter(); |
| 65 |
| 66 xmlSerializer.setOutput(writer); |
| 67 xmlSerializer.startDocument("UTF-8", true); |
| 68 xmlSerializer.startTag("", "crashreport"); |
| 69 xmlSerializer.attribute("", "version", "1"); |
| 70 xmlSerializer.attribute("", "api", String.valueOf(api)); |
| 71 xmlSerializer.attribute("", "build", String.valueOf(build)); |
| 72 xmlSerializer.startTag("", "error"); |
| 73 xmlSerializer.attribute("", "type", reportLines[2]); |
| 74 xmlSerializer.startTag("", "message"); |
| 75 xmlSerializer.text(reportLines[3]); |
| 76 xmlSerializer.endTag("", "message"); |
| 77 xmlSerializer.startTag("", "stacktrace"); |
| 78 Pattern p = Pattern.compile("\\|"); |
| 79 boolean hasCause = false; |
| 80 int i = 4; |
| 81 while (i < reportLines.length) |
| 82 { |
| 83 if ("cause".equals(reportLines[i])) |
| 84 { |
| 85 xmlSerializer.endTag("", "stacktrace"); |
| 86 xmlSerializer.startTag("", "cause"); |
| 87 hasCause = true; |
| 88 i++; |
| 89 xmlSerializer.attribute("", "type", reportLines[i]); |
| 90 i++; |
| 91 xmlSerializer.startTag("", "message"); |
| 92 xmlSerializer.text(reportLines[i]); |
| 93 i++; |
| 94 xmlSerializer.endTag("", "message"); |
| 95 xmlSerializer.startTag("", "stacktrace"); |
| 96 continue; |
| 97 } |
| 98 Log.e(TAG, "Line: " + reportLines[i]); |
| 99 String[] element = TextUtils.split(reportLines[i], p); |
| 100 xmlSerializer.startTag("", "frame"); |
| 101 xmlSerializer.attribute("", "class", element[0]); |
| 102 xmlSerializer.attribute("", "method", element[1]); |
| 103 xmlSerializer.attribute("", "isnative", element[2]); |
| 104 xmlSerializer.attribute("", "file", element[3]); |
| 105 xmlSerializer.attribute("", "line", element[4]); |
| 106 xmlSerializer.endTag("", "frame"); |
| 107 i++; |
| 108 } |
| 109 xmlSerializer.endTag("", "stacktrace"); |
| 110 if (hasCause) |
| 111 xmlSerializer.endTag("", "cause"); |
| 112 xmlSerializer.endTag("", "error"); |
| 113 xmlSerializer.startTag("", "comment"); |
| 114 xmlSerializer.text(comment); |
| 115 xmlSerializer.endTag("", "comment"); |
| 116 xmlSerializer.endTag("", "crashreport"); |
| 117 xmlSerializer.endDocument(); |
| 118 |
| 119 String xml = writer.toString(); |
| 120 HttpClient httpclient = new DefaultHttpClient(); |
| 121 HttpPost httppost = new HttpPost(getString(R.string.crash_report_url)); |
| 122 httppost.setHeader("Content-Type", "text/xml; charset=UTF-8"); |
| 123 httppost.addHeader("X-Adblock-Plus", "yes"); |
| 124 httppost.setEntity(new StringEntity(xml)); |
| 125 HttpResponse httpresponse = httpclient.execute(httppost); |
| 126 StatusLine statusLine = httpresponse.getStatusLine(); |
| 127 Log.e(TAG, statusLine.getStatusCode() + " " + statusLine.getReasonPhrase()
); |
| 128 Log.e(TAG, EntityUtils.toString(httpresponse.getEntity())); |
| 129 if (statusLine.getStatusCode() != 200) |
| 130 throw new ClientProtocolException(); |
| 131 String response = EntityUtils.toString(httpresponse.getEntity()); |
| 132 if (!"saved".equals(response)) |
| 133 throw new ClientProtocolException(); |
| 134 deleteFile(CrashHandler.REPORT_FILE); |
| 135 } |
| 136 catch (ClientProtocolException e) |
| 137 { |
| 138 Log.e(TAG, "Failed to submit a crash", e); |
| 139 Toast.makeText(this, R.string.msg_crash_submission_failure, Toast.LENGTH_L
ONG).show(); |
| 140 } |
| 141 catch (IOException e) |
| 142 { |
| 143 Log.e(TAG, "Failed to submit a crash", e); |
| 144 Toast.makeText(this, R.string.msg_crash_submission_failure, Toast.LENGTH_L
ONG).show(); |
| 145 } |
| 146 catch (Exception e) |
| 147 { |
| 148 Log.e(TAG, "Failed to create report", e); |
| 149 // Assuming corrupted report file, just silently deleting it |
| 150 deleteFile(CrashHandler.REPORT_FILE); |
| 151 } |
| 152 finish(); |
| 153 } |
| 154 |
| 155 public void onCancel(View v) |
| 156 { |
| 157 deleteFile(CrashHandler.REPORT_FILE); |
| 158 finish(); |
| 159 } |
| 160 } |
OLD | NEW |