conversations-classic/src/main/java/eu/siacs/conversations/utils/ExceptionHelper.java

119 lines
4.3 KiB
Java
Raw Normal View History

2014-10-22 16:38:44 +00:00
package eu.siacs.conversations.utils;
2018-02-16 12:29:38 +00:00
import android.support.v7.app.AlertDialog;
2015-07-20 12:26:29 +00:00
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
2015-07-20 12:26:29 +00:00
import android.preference.PreferenceManager;
import android.util.Log;
2014-10-22 16:38:44 +00:00
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
2014-10-22 16:38:44 +00:00
import java.util.List;
2017-11-06 13:20:58 +00:00
import java.util.Locale;
2014-10-22 16:38:44 +00:00
import eu.siacs.conversations.Config;
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 eu.siacs.conversations.ui.XmppActivity;
2014-11-06 19:45:38 +00:00
2014-10-22 16:38:44 +00:00
public class ExceptionHelper {
2017-11-06 13:20:58 +00:00
private static final String FILENAME = "stacktrace.txt";
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
2014-10-22 16:38:44 +00:00
public static void init(Context context) {
if (!(Thread.getDefaultUncaughtExceptionHandler() instanceof ExceptionHandler)) {
Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(
context));
}
}
public static boolean checkForCrash(XmppActivity activity) {
2014-10-22 16:38:44 +00:00
try {
final XmppConnectionService service = activity == null ? null : activity.xmppConnectionService;
if (service == null) {
return false;
}
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
2014-10-22 16:38:44 +00:00
boolean neverSend = preferences.getBoolean("never_send", false);
if (neverSend || Config.BUG_REPORTS == null) {
return false;
2014-10-22 16:38:44 +00:00
}
List<Account> accounts = service.getAccounts();
Account account = null;
for (int i = 0; i < accounts.size(); ++i) {
if (accounts.get(i).isEnabled()) {
2014-10-22 16:38:44 +00:00
account = accounts.get(i);
break;
}
}
if (account == null) {
return false;
2014-10-22 16:38:44 +00:00
}
final Account finalAccount = account;
2017-11-06 13:20:58 +00:00
FileInputStream file = activity.openFileInput(FILENAME);
2014-10-22 16:38:44 +00:00
InputStreamReader inputStreamReader = new InputStreamReader(file);
BufferedReader stacktrace = new BufferedReader(inputStreamReader);
final StringBuilder report = new StringBuilder();
PackageManager pm = activity.getPackageManager();
PackageInfo packageInfo;
2014-10-22 16:38:44 +00:00
try {
packageInfo = pm.getPackageInfo(activity.getPackageName(), PackageManager.GET_SIGNATURES);
2017-11-06 13:20:58 +00:00
report.append("Version: ").append(packageInfo.versionName).append('\n');
report.append("Last Update: ").append(DATE_FORMAT.format(new Date(packageInfo.lastUpdateTime))).append('\n');
Signature[] signatures = packageInfo.signatures;
if (signatures != null && signatures.length >= 1) {
2017-11-06 13:20:58 +00:00
report.append("SHA-1: ").append(CryptoHelper.getFingerprintCert(packageInfo.signatures[0].toByteArray())).append('\n');
}
report.append('\n');
} catch (Exception e) {
e.printStackTrace();
return false;
2014-10-22 16:38:44 +00:00
}
String line;
while ((line = stacktrace.readLine()) != null) {
report.append(line);
report.append('\n');
}
file.close();
2017-11-06 13:20:58 +00:00
activity.deleteFile(FILENAME);
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle(activity.getString(R.string.crash_report_title));
builder.setMessage(activity.getText(R.string.crash_report_message));
builder.setPositiveButton(activity.getText(R.string.send_now), (dialog, which) -> {
2014-10-22 16:38:44 +00:00
Log.d(Config.LOGTAG, "using account=" + finalAccount.getJid().asBareJid() + " to send in stack trace");
Conversation conversation = service.findOrCreateConversation(finalAccount, Config.BUG_REPORTS, false, true);
Message message = new Message(conversation, report.toString(), Message.ENCRYPTION_NONE);
service.sendMessage(message);
});
builder.setNegativeButton(activity.getText(R.string.send_never), (dialog, which) -> preferences.edit().putBoolean("never_send", true).apply());
2014-10-22 16:38:44 +00:00
builder.create().show();
return true;
2014-11-06 19:45:38 +00:00
} catch (final IOException ignored) {
return false;
}
2014-10-22 16:38:44 +00:00
}
static void writeToStacktraceFile(Context context, String msg) {
try {
2017-11-06 13:20:58 +00:00
OutputStream os = context.openFileOutput(FILENAME, Context.MODE_PRIVATE);
os.write(msg.getBytes());
os.flush();
os.close();
} catch (IOException ignored) {
}
}
2014-10-22 16:38:44 +00:00
}