2014-03-09 12:21:28 +00:00
|
|
|
package eu.siacs.conversations.utils;
|
|
|
|
|
2014-03-27 15:09:23 +00:00
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import eu.siacs.conversations.R;
|
|
|
|
import eu.siacs.conversations.entities.Account;
|
|
|
|
import eu.siacs.conversations.entities.Conversation;
|
|
|
|
import eu.siacs.conversations.entities.Message;
|
|
|
|
import eu.siacs.conversations.services.XmppConnectionService;
|
|
|
|
import android.app.AlertDialog;
|
2014-03-09 12:21:28 +00:00
|
|
|
import android.content.Context;
|
2014-03-27 15:09:23 +00:00
|
|
|
import android.content.DialogInterface;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.content.DialogInterface.OnClickListener;
|
2014-07-24 10:33:56 +00:00
|
|
|
import android.content.pm.PackageInfo;
|
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
import android.content.pm.PackageManager.NameNotFoundException;
|
2014-03-27 15:09:23 +00:00
|
|
|
import android.preference.PreferenceManager;
|
2014-07-24 10:33:56 +00:00
|
|
|
import android.text.format.DateUtils;
|
2014-03-27 15:09:23 +00:00
|
|
|
import android.util.Log;
|
2014-03-09 12:21:28 +00:00
|
|
|
|
|
|
|
public class ExceptionHelper {
|
|
|
|
public static void init(Context context) {
|
|
|
|
if(!(Thread.getDefaultUncaughtExceptionHandler() instanceof ExceptionHandler)) {
|
|
|
|
Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(context));
|
|
|
|
}
|
|
|
|
}
|
2014-03-27 15:09:23 +00:00
|
|
|
|
|
|
|
public static void checkForCrash(Context context, final XmppConnectionService service) {
|
|
|
|
try {
|
|
|
|
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
boolean neverSend = preferences.getBoolean("never_send",false);
|
|
|
|
if (neverSend) {
|
|
|
|
return;
|
|
|
|
}
|
2014-04-10 11:19:58 +00:00
|
|
|
List<Account> accounts = service.getAccounts();
|
|
|
|
Account account = null;
|
|
|
|
for(int i = 0; i < accounts.size(); ++i) {
|
|
|
|
if (!accounts.get(i).isOptionSet(Account.OPTION_DISABLED)) {
|
|
|
|
account = accounts.get(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (account==null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final Account finalAccount = account;
|
2014-03-27 15:09:23 +00:00
|
|
|
FileInputStream file = context.openFileInput("stacktrace.txt");
|
|
|
|
InputStreamReader inputStreamReader = new InputStreamReader(
|
|
|
|
file);
|
2014-07-24 10:33:56 +00:00
|
|
|
BufferedReader stacktrace = new BufferedReader(
|
2014-03-27 15:09:23 +00:00
|
|
|
inputStreamReader);
|
2014-07-24 10:33:56 +00:00
|
|
|
final StringBuilder report = new StringBuilder();
|
|
|
|
PackageManager pm = context.getPackageManager();
|
|
|
|
PackageInfo packageInfo = null;
|
|
|
|
try {
|
|
|
|
packageInfo = pm.getPackageInfo(context.getPackageName(), 0);
|
|
|
|
report.append("Version: "+packageInfo.versionName+'\n');
|
|
|
|
report.append("Last Update: "+DateUtils.formatDateTime(context, packageInfo.lastUpdateTime, DateUtils.FORMAT_SHOW_TIME|DateUtils.FORMAT_SHOW_DATE)+'\n');
|
|
|
|
} catch (NameNotFoundException e) {}
|
2014-03-27 15:09:23 +00:00
|
|
|
String line;
|
2014-07-24 10:33:56 +00:00
|
|
|
while((line = stacktrace.readLine()) != null) {
|
|
|
|
report.append(line);
|
|
|
|
report.append('\n');
|
2014-03-27 15:09:23 +00:00
|
|
|
}
|
|
|
|
file.close();
|
|
|
|
context.deleteFile("stacktrace.txt");
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(context);
|
|
|
|
builder.setTitle(context.getString(R.string.crash_report_title));
|
|
|
|
builder.setMessage(context.getText(R.string.crash_report_message));
|
|
|
|
builder.setPositiveButton(context.getText(R.string.send_now), new OnClickListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
2014-04-10 11:19:58 +00:00
|
|
|
|
|
|
|
Log.d("xmppService","using account="+finalAccount.getJid()+" to send in stack trace");
|
|
|
|
Conversation conversation = service.findOrCreateConversation(finalAccount, "bugs@siacs.eu", false);
|
2014-07-24 10:33:56 +00:00
|
|
|
Message message = new Message(conversation, report.toString(), Message.ENCRYPTION_NONE);
|
2014-06-11 19:53:25 +00:00
|
|
|
service.sendMessage(message);
|
2014-03-27 15:09:23 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
builder.setNegativeButton(context.getText(R.string.send_never),new OnClickListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
preferences.edit().putBoolean("never_send", true).commit();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
builder.create().show();
|
|
|
|
} catch (FileNotFoundException e) {
|
2014-04-03 15:39:57 +00:00
|
|
|
return;
|
2014-03-27 15:09:23 +00:00
|
|
|
} catch (IOException e) {
|
2014-04-03 15:39:57 +00:00
|
|
|
return;
|
2014-03-27 15:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-03-09 12:21:28 +00:00
|
|
|
}
|