Fixed null pointer in connection check
[pub/Android/ownCloud.git] / src / com / owncloud / android / CrashlogSendActivity.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */
18
19 package com.owncloud.android;
20
21 import java.io.File;
22
23 import org.apache.commons.httpclient.HttpClient;
24 import org.apache.commons.httpclient.methods.PostMethod;
25 import org.apache.commons.httpclient.methods.multipart.FilePart;
26 import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
27 import org.apache.commons.httpclient.methods.multipart.Part;
28
29 import android.app.AlertDialog;
30 import android.app.Dialog;
31 import android.content.DialogInterface;
32 import android.content.DialogInterface.OnCancelListener;
33 import android.content.DialogInterface.OnClickListener;
34 import android.os.Bundle;
35 import android.util.Log;
36
37 import com.actionbarsherlock.app.SherlockActivity;
38
39 import com.owncloud.android.R;
40
41 public class CrashlogSendActivity extends SherlockActivity implements OnClickListener, OnCancelListener {
42
43 private static final String TAG = "CrashlogSendActivity";
44 private static final String CRASHLOG_SUBMIT_URL = "http://alefzero.eu/a/crashlog/";
45 private static final int DIALOG_SUBMIT = 5;
46
47 private String mLogFilename;
48
49 @Override
50 protected void onCreate(Bundle savedInstanceState) {
51 super.onCreate(savedInstanceState);
52 mLogFilename = getIntent().getStringExtra(CrashHandler.KEY_CRASH_FILENAME);
53 if (mLogFilename == null) {
54 Log.wtf(TAG, "No file crashlog path given!");
55 finish();
56 return;
57 }
58 Log.i(TAG, "crashlog file path " + mLogFilename);
59
60 showDialog(DIALOG_SUBMIT);
61 }
62
63
64 @Override
65 protected Dialog onCreateDialog(int id) {
66 if (id == DIALOG_SUBMIT) {
67 AlertDialog.Builder builder = new AlertDialog.Builder(this);
68 builder.setMessage(R.string.crashlog_message);
69 builder.setNegativeButton(R.string.crashlog_dont_send_report, this);
70 builder.setPositiveButton(R.string.crashlog_send_report, this);
71 builder.setCancelable(true);
72 builder.setOnCancelListener(this);
73 return builder.create();
74 }
75 return super.onCreateDialog(id);
76 }
77
78
79 @Override
80 public void onClick(DialogInterface dialog, final int which) {
81 new Thread(new Runnable() {
82
83 @Override
84 public void run() {
85 // TODO Auto-generated method stub
86 File file = new File(mLogFilename);
87 if (which == Dialog.BUTTON_POSITIVE) {
88 try {
89 HttpClient client = new HttpClient();
90 PostMethod post = new PostMethod(CRASHLOG_SUBMIT_URL);
91 Part[] parts = {new FilePart("crashfile", file)};
92 post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
93 client.executeMethod(post);
94 post.releaseConnection();
95 } catch (Exception e) {
96 e.printStackTrace();
97 }
98 }
99 file.delete();
100 finish();
101 }
102 }).start();
103 }
104
105
106 @Override
107 public void onCancel(DialogInterface dialog) {
108 new File(mLogFilename).delete();
109 finish();
110 }
111
112 }