2014-10-22 16:38:44 +00:00
|
|
|
package eu.siacs.conversations.utils;
|
|
|
|
|
2015-07-20 12:26:29 +00:00
|
|
|
import android.app.AlertDialog;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.DialogInterface;
|
|
|
|
import android.content.DialogInterface.OnClickListener;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.content.pm.PackageInfo;
|
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
import android.content.pm.PackageManager.NameNotFoundException;
|
2016-05-28 09:04:18 +00:00
|
|
|
import android.content.pm.Signature;
|
2015-07-20 12:26:29 +00:00
|
|
|
import android.preference.PreferenceManager;
|
|
|
|
import android.text.format.DateUtils;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.FileInputStream;
|
2016-05-21 06:54:29 +00:00
|
|
|
import java.io.FileNotFoundException;
|
2014-10-22 16:38:44 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStreamReader;
|
2016-05-21 06:54:29 +00:00
|
|
|
import java.io.OutputStream;
|
2016-05-28 09:04:18 +00:00
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.Date;
|
2014-10-22 16:38:44 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
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;
|
2016-01-11 10:17:45 +00:00
|
|
|
import eu.siacs.conversations.ui.ConversationActivity;
|
2014-11-06 19:45:38 +00:00
|
|
|
import eu.siacs.conversations.xmpp.jid.InvalidJidException;
|
|
|
|
import eu.siacs.conversations.xmpp.jid.Jid;
|
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
public class ExceptionHelper {
|
2016-05-28 09:04:18 +00:00
|
|
|
private static SimpleDateFormat DATE_FORMATs = new SimpleDateFormat("yyyy-MM-dd");
|
2014-10-22 16:38:44 +00:00
|
|
|
public static void init(Context context) {
|
|
|
|
if (!(Thread.getDefaultUncaughtExceptionHandler() instanceof ExceptionHandler)) {
|
|
|
|
Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(
|
|
|
|
context));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-11 10:17:45 +00:00
|
|
|
public static boolean checkForCrash(ConversationActivity activity, final XmppConnectionService service) {
|
2014-10-22 16:38:44 +00:00
|
|
|
try {
|
|
|
|
final SharedPreferences preferences = PreferenceManager
|
2016-01-11 10:17:45 +00:00
|
|
|
.getDefaultSharedPreferences(activity);
|
2014-10-22 16:38:44 +00:00
|
|
|
boolean neverSend = preferences.getBoolean("never_send", false);
|
2016-05-28 09:04:18 +00:00
|
|
|
if (neverSend || Config.BUG_REPORTS == null) {
|
2016-01-11 10:17:45 +00:00
|
|
|
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).isOptionSet(Account.OPTION_DISABLED)) {
|
|
|
|
account = accounts.get(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (account == null) {
|
2016-01-11 10:17:45 +00:00
|
|
|
return false;
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
final Account finalAccount = account;
|
2016-01-11 10:17:45 +00:00
|
|
|
FileInputStream file = activity.openFileInput("stacktrace.txt");
|
2014-10-22 16:38:44 +00:00
|
|
|
InputStreamReader inputStreamReader = new InputStreamReader(file);
|
|
|
|
BufferedReader stacktrace = new BufferedReader(inputStreamReader);
|
|
|
|
final StringBuilder report = new StringBuilder();
|
2016-01-11 10:17:45 +00:00
|
|
|
PackageManager pm = activity.getPackageManager();
|
2016-05-28 09:04:18 +00:00
|
|
|
PackageInfo packageInfo;
|
2014-10-22 16:38:44 +00:00
|
|
|
try {
|
2016-05-28 09:04:18 +00:00
|
|
|
packageInfo = pm.getPackageInfo(activity.getPackageName(), PackageManager.GET_SIGNATURES);
|
2014-10-22 16:38:44 +00:00
|
|
|
report.append("Version: " + packageInfo.versionName + '\n');
|
2016-05-28 09:04:18 +00:00
|
|
|
report.append("Last Update: " + DATE_FORMATs.format(new Date(packageInfo.lastUpdateTime)) + '\n');
|
|
|
|
Signature[] signatures = packageInfo.signatures;
|
|
|
|
if (signatures != null && signatures.length >= 1) {
|
|
|
|
report.append("SHA-1: " + CryptoHelper.getFingerprintCert(packageInfo.signatures[0].toByteArray()) + "\n");
|
|
|
|
}
|
|
|
|
report.append('\n');
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
2016-01-11 10:17:45 +00:00
|
|
|
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();
|
2016-01-11 10:17:45 +00:00
|
|
|
activity.deleteFile("stacktrace.txt");
|
|
|
|
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),
|
2014-10-22 16:38:44 +00:00
|
|
|
new OnClickListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
|
|
|
|
Log.d(Config.LOGTAG, "using account="
|
2014-11-09 15:57:22 +00:00
|
|
|
+ finalAccount.getJid().toBareJid()
|
2014-10-22 16:38:44 +00:00
|
|
|
+ " to send in stack trace");
|
2016-01-11 10:17:45 +00:00
|
|
|
Conversation conversation = null;
|
|
|
|
try {
|
|
|
|
conversation = service.findOrCreateConversation(finalAccount,
|
2016-05-28 09:04:18 +00:00
|
|
|
Jid.fromString(Config.BUG_REPORTS), false);
|
2016-01-11 10:17:45 +00:00
|
|
|
} catch (final InvalidJidException ignored) {
|
|
|
|
}
|
|
|
|
Message message = new Message(conversation, report
|
2014-10-22 16:38:44 +00:00
|
|
|
.toString(), Message.ENCRYPTION_NONE);
|
|
|
|
service.sendMessage(message);
|
|
|
|
}
|
|
|
|
});
|
2016-01-11 10:17:45 +00:00
|
|
|
builder.setNegativeButton(activity.getText(R.string.send_never),
|
2014-10-22 16:38:44 +00:00
|
|
|
new OnClickListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
preferences.edit().putBoolean("never_send", true)
|
2014-11-06 19:45:38 +00:00
|
|
|
.apply();
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
builder.create().show();
|
2016-01-11 10:17:45 +00:00
|
|
|
return true;
|
2014-11-06 19:45:38 +00:00
|
|
|
} catch (final IOException ignored) {
|
2016-01-11 10:17:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
2016-05-21 06:54:29 +00:00
|
|
|
|
|
|
|
public static void writeToStacktraceFile(Context context, String msg) {
|
|
|
|
try {
|
|
|
|
OutputStream os = context.openFileOutput("stacktrace.txt", Context.MODE_PRIVATE);
|
|
|
|
os.write(msg.getBytes());
|
|
|
|
os.flush();
|
|
|
|
os.close();
|
|
|
|
} catch (IOException ignored) {
|
|
|
|
}
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|