put version code into crash report

This commit is contained in:
Daniel Gultsch 2020-04-25 20:13:20 +02:00
parent c41033e83c
commit 006d7447a3
1 changed files with 77 additions and 74 deletions

View File

@ -29,82 +29,85 @@ import eu.siacs.conversations.ui.XmppActivity;
public class ExceptionHelper { public class ExceptionHelper {
private static final String FILENAME = "stacktrace.txt"; private static final String FILENAME = "stacktrace.txt";
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH); private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
public static void init(Context context) { public static void init(Context context) {
if (!(Thread.getDefaultUncaughtExceptionHandler() instanceof ExceptionHandler)) { if (!(Thread.getDefaultUncaughtExceptionHandler() instanceof ExceptionHandler)) {
Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler( Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(
context)); context));
} }
} }
public static boolean checkForCrash(XmppActivity activity) { public static boolean checkForCrash(XmppActivity activity) {
try { try {
final XmppConnectionService service = activity == null ? null : activity.xmppConnectionService; final XmppConnectionService service = activity == null ? null : activity.xmppConnectionService;
if (service == null) { if (service == null) {
return false; return false;
} }
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity); final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
boolean neverSend = preferences.getBoolean("never_send", false); boolean neverSend = preferences.getBoolean("never_send", false);
if (neverSend || Config.BUG_REPORTS == null) { if (neverSend || Config.BUG_REPORTS == null) {
return false; return false;
} }
final Account account = AccountUtils.getFirstEnabled(service); final Account account = AccountUtils.getFirstEnabled(service);
if (account == null) { if (account == null) {
return false; return false;
} }
FileInputStream file = activity.openFileInput(FILENAME); FileInputStream file = activity.openFileInput(FILENAME);
InputStreamReader inputStreamReader = new InputStreamReader(file); InputStreamReader inputStreamReader = new InputStreamReader(file);
BufferedReader stacktrace = new BufferedReader(inputStreamReader); BufferedReader stacktrace = new BufferedReader(inputStreamReader);
final StringBuilder report = new StringBuilder(); final StringBuilder report = new StringBuilder();
PackageManager pm = activity.getPackageManager(); PackageManager pm = activity.getPackageManager();
PackageInfo packageInfo; PackageInfo packageInfo;
try { try {
packageInfo = pm.getPackageInfo(activity.getPackageName(), PackageManager.GET_SIGNATURES); packageInfo = pm.getPackageInfo(activity.getPackageName(), PackageManager.GET_SIGNATURES);
report.append("Version: ").append(packageInfo.versionName).append('\n'); final String versionName = packageInfo.versionName;
report.append("Last Update: ").append(DATE_FORMAT.format(new Date(packageInfo.lastUpdateTime))).append('\n'); final int versionCode = packageInfo.versionCode;
Signature[] signatures = packageInfo.signatures; final int version = versionCode > 10000 ? (versionCode / 100) : versionCode;
if (signatures != null && signatures.length >= 1) { report.append(String.format(Locale.ROOT, "Version: %s(%d)", versionName, version)).append('\n');
report.append("SHA-1: ").append(CryptoHelper.getFingerprintCert(packageInfo.signatures[0].toByteArray())).append('\n'); report.append("Last Update: ").append(DATE_FORMAT.format(new Date(packageInfo.lastUpdateTime))).append('\n');
} Signature[] signatures = packageInfo.signatures;
report.append('\n'); if (signatures != null && signatures.length >= 1) {
} catch (Exception e) { report.append("SHA-1: ").append(CryptoHelper.getFingerprintCert(packageInfo.signatures[0].toByteArray())).append('\n');
e.printStackTrace(); }
return false; report.append('\n');
} } catch (Exception e) {
String line; e.printStackTrace();
while ((line = stacktrace.readLine()) != null) { return false;
report.append(line); }
report.append('\n'); String line;
} while ((line = stacktrace.readLine()) != null) {
file.close(); report.append(line);
activity.deleteFile(FILENAME); report.append('\n');
AlertDialog.Builder builder = new AlertDialog.Builder(activity); }
builder.setTitle(activity.getString(R.string.crash_report_title)); file.close();
builder.setMessage(activity.getText(R.string.crash_report_message)); activity.deleteFile(FILENAME);
builder.setPositiveButton(activity.getText(R.string.send_now), (dialog, which) -> { 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) -> {
Log.d(Config.LOGTAG, "using account=" + account.getJid().asBareJid() + " to send in stack trace"); Log.d(Config.LOGTAG, "using account=" + account.getJid().asBareJid() + " to send in stack trace");
Conversation conversation = service.findOrCreateConversation(account, Config.BUG_REPORTS, false, true); Conversation conversation = service.findOrCreateConversation(account, Config.BUG_REPORTS, false, true);
Message message = new Message(conversation, report.toString(), Message.ENCRYPTION_NONE); Message message = new Message(conversation, report.toString(), Message.ENCRYPTION_NONE);
service.sendMessage(message); service.sendMessage(message);
}); });
builder.setNegativeButton(activity.getText(R.string.send_never), (dialog, which) -> preferences.edit().putBoolean("never_send", true).apply()); builder.setNegativeButton(activity.getText(R.string.send_never), (dialog, which) -> preferences.edit().putBoolean("never_send", true).apply());
builder.create().show(); builder.create().show();
return true; return true;
} catch (final IOException ignored) { } catch (final IOException ignored) {
return false; return false;
} }
} }
static void writeToStacktraceFile(Context context, String msg) { static void writeToStacktraceFile(Context context, String msg) {
try { try {
OutputStream os = context.openFileOutput(FILENAME, Context.MODE_PRIVATE); OutputStream os = context.openFileOutput(FILENAME, Context.MODE_PRIVATE);
os.write(msg.getBytes()); os.write(msg.getBytes());
os.flush(); os.flush();
os.close(); os.close();
} catch (IOException ignored) { } catch (IOException ignored) {
} }
} }
} }