aboutsummaryrefslogtreecommitdiffstats
path: root/src/eu/siacs/conversations/utils/ExceptionHelper.java
blob: f9cd375eb7c0b7e1a3460ca6571fc31b3a2e9a69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package eu.siacs.conversations.utils;

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;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnClickListener;
import android.preference.PreferenceManager;
import android.util.Log;

public class ExceptionHelper {
	public static void init(Context context) {
		if(!(Thread.getDefaultUncaughtExceptionHandler() instanceof ExceptionHandler)) {
		    Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(context));
		}
	}
	
	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;
			}
			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;
			FileInputStream file = context.openFileInput("stacktrace.txt");
			InputStreamReader inputStreamReader = new InputStreamReader(
                    file);
            BufferedReader bufferedReader = new BufferedReader(
                    inputStreamReader);
            final StringBuilder stacktrace = new StringBuilder();
            String line;
            while((line = bufferedReader.readLine()) != null) {
            	stacktrace.append(line);
            	stacktrace.append('\n');
            }
            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) {
					
						Log.d("xmppService","using account="+finalAccount.getJid()+" to send in stack trace");
						Conversation conversation = service.findOrCreateConversation(finalAccount, "bugs@siacs.eu", false);
						Message message = new Message(conversation, stacktrace.toString(), Message.ENCRYPTION_NONE);
						service.sendMessage(message, null);
				}
			});
			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) {
			return;
		} catch (IOException e) {
			return;
		}
		
	}
}