ffad248ccded018ae5a71b60bee8ce4fafb3d3f5
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / CrashHandler.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 eu.alefzero.owncloud;
20
21 import java.io.BufferedReader;
22 import java.io.File;
23 import java.io.FileWriter;
24 import java.io.InputStreamReader;
25 import java.io.PrintWriter;
26 import java.io.StringWriter;
27 import java.io.Writer;
28 import java.lang.Thread.UncaughtExceptionHandler;
29 import java.util.LinkedList;
30 import java.util.List;
31
32 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
33
34 import android.accounts.Account;
35 import android.accounts.AccountManager;
36 import android.app.AlarmManager;
37 import android.app.PendingIntent;
38 import android.content.Context;
39 import android.content.Intent;
40 import android.net.ConnectivityManager;
41 import android.os.Environment;
42 import android.util.Log;
43
44 public class CrashHandler implements UncaughtExceptionHandler {
45
46 public static final String KEY_CRASH_FILENAME = "KEY_CRASH_FILENAME";
47
48 private Context mContext;
49 private static final String TAG = "CrashHandler";
50 private static final String crash_filename_template = "crash";
51 private static List<String> TAGS;
52 private UncaughtExceptionHandler defaultUEH;
53
54 // TODO: create base activity which will register for crashlog tag automaticly
55 static {
56 TAGS = new LinkedList<String>();
57 TAGS.add("AccountAuthenticator");
58 TAGS.add("AccountAuthenticator");
59 TAGS.add("ConnectionCheckerRunnable");
60 TAGS.add("EasySSLSocketFactory");
61 TAGS.add("FileDataStorageManager");
62 TAGS.add("PhotoTakenBroadcastReceiver");
63 TAGS.add("InstantUploadService");
64 TAGS.add("FileDownloader");
65 TAGS.add("FileUploader");
66 TAGS.add("LocationUpdateService");
67 TAGS.add("FileSyncAdapter");
68 TAGS.add("AuthActivity");
69 TAGS.add("OwnCloudPreferences");
70 TAGS.add("FileDetailFragment");
71 TAGS.add("FileListFragment");
72 TAGS.add("ownCloudUploader");
73 TAGS.add("WebdavClient");
74 }
75
76 public CrashHandler(Context context) {
77 mContext = context;
78 defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
79 }
80
81 @Override
82 public void uncaughtException(Thread thread, Throwable ex) {
83 final Writer writer = new StringWriter();
84 final PrintWriter printwriter = new PrintWriter(writer);
85 ex.printStackTrace(printwriter);
86 final String startrace = writer.toString();
87 printwriter.close();
88 File ocdir = new File(Environment.getExternalStorageDirectory().getAbsoluteFile(), "owncloud");
89 ocdir.mkdirs();
90
91 String crash_filename = crash_filename_template + System.currentTimeMillis() + ".txt";
92 File crashfile = new File(ocdir, crash_filename);
93 try {
94 ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
95 String header = String.format("Model: %s, SDK: %d, Current net: %s\n\n",
96 android.os.Build.MODEL,
97 android.os.Build.VERSION.SDK_INT,
98 cm.getActiveNetworkInfo() != null ? cm.getActiveNetworkInfo().getTypeName() : "NONE");
99 Account account = AccountUtils.getCurrentOwnCloudAccount(mContext);
100 AccountManager am = AccountManager.get(mContext);
101 String header2 = String.format("Account: %s, OCUrl: %s, OCVersion: %s\n\n",
102 account.name,
103 am.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL),
104 am.getUserData(account, AccountAuthenticator.KEY_OC_VERSION));
105
106 crashfile.createNewFile();
107 FileWriter fw = new FileWriter(crashfile);
108 fw.write(header);
109 fw.write(header2);
110 fw.write(startrace);
111 fw.write("\n\n");
112
113 String logcat = "logcat -d *:S ";
114
115 for (String s : TAGS)
116 logcat += s + ":V ";
117
118 Process process = Runtime.getRuntime().exec(logcat);
119 BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
120 String logline;
121 while ((logline = br.readLine()) != null)
122 fw.write(logline+"\n");
123
124 br.close();
125 fw.close();
126
127 Intent dataintent = new Intent(mContext, CrashlogSendActivity.class);
128 dataintent.putExtra(KEY_CRASH_FILENAME, crashfile.getAbsolutePath());
129 PendingIntent intent = PendingIntent.getActivity(mContext.getApplicationContext(), 0, dataintent, Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
130 AlarmManager mngr = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);
131 if (mngr == null) {
132 Log.e(TAG, "Couldn't retrieve alarm manager!");
133 defaultUEH.uncaughtException(thread, ex);
134 return;
135 }
136 mngr.set(AlarmManager.RTC, System.currentTimeMillis(), intent);
137 System.exit(2);
138 } catch (Exception e1) {
139 Log.e(TAG, "Crash handler failed!");
140 Log.e(TAG, e1.toString());
141 defaultUEH.uncaughtException(thread, ex);
142 return;
143 }
144 }
145 }