1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
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.
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.
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/>.
19 package eu
.alefzero
.owncloud
;
21 import java
.io
.BufferedReader
;
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
;
32 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
;
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
.content
.pm
.PackageInfo
;
41 import android
.net
.ConnectivityManager
;
42 import android
.os
.Environment
;
43 import android
.util
.Log
;
45 public class CrashHandler
implements UncaughtExceptionHandler
{
47 public static final String KEY_CRASH_FILENAME
= "KEY_CRASH_FILENAME";
49 private Context mContext
;
50 private static final String TAG
= "CrashHandler";
51 private static final String crash_filename_template
= "crash";
52 private static List
<String
> TAGS
;
53 private UncaughtExceptionHandler defaultUEH
;
55 // TODO: create base activity which will register for crashlog tag automaticly
57 TAGS
= new LinkedList
<String
>();
58 TAGS
.add("AccountAuthenticator");
59 TAGS
.add("AccountAuthenticator");
60 TAGS
.add("ConnectionCheckerRunnable");
61 TAGS
.add("EasySSLSocketFactory");
62 TAGS
.add("FileDataStorageManager");
63 TAGS
.add("PhotoTakenBroadcastReceiver");
64 TAGS
.add("InstantUploadService");
65 TAGS
.add("FileDownloader");
66 TAGS
.add("FileUploader");
67 TAGS
.add("LocationUpdateService");
68 TAGS
.add("FileSyncAdapter");
69 TAGS
.add("AuthActivity");
70 TAGS
.add("OwnCloudPreferences");
71 TAGS
.add("FileDetailFragment");
72 TAGS
.add("FileListFragment");
73 TAGS
.add("ownCloudUploader");
74 TAGS
.add("WebdavClient");
77 public CrashHandler(Context context
) {
79 defaultUEH
= Thread
.getDefaultUncaughtExceptionHandler();
83 public void uncaughtException(Thread thread
, Throwable ex
) {
84 final Writer writer
= new StringWriter();
85 final PrintWriter printwriter
= new PrintWriter(writer
);
86 ex
.printStackTrace(printwriter
);
87 final String startrace
= writer
.toString();
89 File ocdir
= new File(Environment
.getExternalStorageDirectory().getAbsoluteFile(), "owncloud");
92 String crash_filename
= crash_filename_template
+ System
.currentTimeMillis() + ".txt";
93 File crashfile
= new File(ocdir
, crash_filename
);
95 PackageInfo pi
= mContext
.getPackageManager().getPackageInfo(mContext
.getPackageName(), 0);
96 ConnectivityManager cm
= (ConnectivityManager
) mContext
.getSystemService(Context
.CONNECTIVITY_SERVICE
);
97 String header
= String
.format("Model: %s, SDK: %d, Current net: %s AppVersion: %s\n\n",
98 android
.os
.Build
.MODEL
,
99 android
.os
.Build
.VERSION
.SDK_INT
,
100 cm
.getActiveNetworkInfo() != null ? cm
.getActiveNetworkInfo().getTypeName() : "NONE",
102 Account account
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
103 AccountManager am
= AccountManager
.get(mContext
);
104 String header2
= String
.format("Account: %s, OCUrl: %s, OCVersion: %s\n\n",
106 am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
),
107 am
.getUserData(account
, AccountAuthenticator
.KEY_OC_VERSION
));
109 crashfile
.createNewFile();
110 FileWriter fw
= new FileWriter(crashfile
);
116 String logcat
= "logcat -d *:S ";
118 for (String s
: TAGS
)
121 Process process
= Runtime
.getRuntime().exec(logcat
);
122 BufferedReader br
= new BufferedReader(new InputStreamReader(process
.getInputStream()));
124 while ((logline
= br
.readLine()) != null
)
125 fw
.write(logline
+"\n");
130 Intent dataintent
= new Intent(mContext
, CrashlogSendActivity
.class);
131 dataintent
.putExtra(KEY_CRASH_FILENAME
, crashfile
.getAbsolutePath());
132 PendingIntent intent
;
133 if (android
.os
.Build
.VERSION
.SDK_INT
>= android
.os
.Build
.VERSION_CODES
.HONEYCOMB
) {
134 intent
= PendingIntent
.getActivity(mContext
.getApplicationContext(), 0, dataintent
, Intent
.FLAG_ACTIVITY_NEW_TASK
| Intent
.FLAG_ACTIVITY_CLEAR_TASK
);
136 intent
= PendingIntent
.getActivity(mContext
.getApplicationContext(), 0, dataintent
, Intent
.FLAG_ACTIVITY_NEW_TASK
| Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
138 AlarmManager mngr
= (AlarmManager
)mContext
.getSystemService(Context
.ALARM_SERVICE
);
140 Log
.e(TAG
, "Couldn't retrieve alarm manager!");
141 defaultUEH
.uncaughtException(thread
, ex
);
144 mngr
.set(AlarmManager
.RTC
, System
.currentTimeMillis(), intent
);
146 } catch (Exception e1
) {
147 Log
.e(TAG
, "Crash handler failed!");
148 Log
.e(TAG
, e1
.toString());
149 defaultUEH
.uncaughtException(thread
, ex
);