1 | package org.tomdroid.util; |
2 | |
3 | import org.tomdroid.Note; |
4 | |
5 | import android.app.Activity; |
6 | import android.app.AlertDialog; |
7 | import android.content.DialogInterface; |
8 | import android.content.Intent; |
9 | import android.content.DialogInterface.OnClickListener; |
10 | import android.os.Handler; |
11 | import android.os.Message; |
12 | import android.text.SpannableStringBuilder; |
13 | |
14 | public class Send { |
15 | |
16 | private Activity activity; |
17 | private Note note; |
18 | private SpannableStringBuilder noteContent; |
19 | |
20 | public Send(Activity activity, Note note) { |
21 | this.activity = activity; |
22 | this.note = note; |
23 | } |
24 | |
25 | public void send() { |
26 | if (note != null) { |
27 | noteContent = note.getNoteContent(noteContentHandler); |
28 | } |
29 | } |
30 | |
31 | private Handler noteContentHandler = new Handler() { |
32 | |
33 | @Override |
34 | public void handleMessage(Message msg) { |
35 | |
36 | //parsed ok - show |
37 | if(msg.what == NoteContentBuilder.PARSE_OK) { |
38 | |
39 | String body = noteContent.toString(); |
40 | |
41 | // Create a new Intent to send messages |
42 | Intent sendIntent = new Intent(Intent.ACTION_SEND); |
43 | // Add attributes to the intent |
44 | |
45 | sendIntent.putExtra(Intent.EXTRA_SUBJECT, note.getTitle()); |
46 | sendIntent.putExtra(Intent.EXTRA_TEXT, body); |
47 | sendIntent.setType("text/plain"); |
48 | |
49 | activity.startActivity(Intent.createChooser(sendIntent, note.getTitle())); |
50 | |
51 | //parsed not ok - error |
52 | } else if(msg.what == NoteContentBuilder.PARSE_ERROR) { |
53 | |
54 | // TODO put this String in a translatable resource |
55 | new AlertDialog.Builder(activity) |
56 | .setMessage("The requested note could not be parsed. If you see this error " + |
57 | " and you are able to replicate it, please file a bug!") |
58 | .setTitle("Error") |
59 | .setNeutralButton("Ok", new OnClickListener() { |
60 | public void onClick(DialogInterface dialog, int which) { |
61 | dialog.dismiss(); |
62 | }}) |
63 | .show(); |
64 | } |
65 | } |
66 | }; |
67 | } |