|
@@ -29,9 +29,7 @@ import android.net.Uri;
|
29
|
29
|
import android.provider.Contacts;
|
30
|
30
|
import android.provider.Contacts.People;
|
31
|
31
|
import android.text.Html;
|
32
|
|
-import android.util.Log;
|
33
|
32
|
import android.view.View;
|
34
|
|
-import android.view.ViewGroup;
|
35
|
33
|
import android.widget.ImageView;
|
36
|
34
|
import android.widget.LinearLayout;
|
37
|
35
|
import android.widget.LinearLayout.LayoutParams;
|
|
@@ -47,7 +45,8 @@ import uk.co.md87.android.contexthome.R;
|
47
|
45
|
*/
|
48
|
46
|
public class EmailModule implements Module {
|
49
|
47
|
|
50
|
|
- private static final Uri INBOX_URI = Uri.parse("content://gmail-ls/conversations/chris87@gmail.com");
|
|
48
|
+ private static final Uri INBOX_URI = Uri.parse("content://gmail-ls/"
|
|
49
|
+ + "conversations/chris87@gmail.com");
|
51
|
50
|
|
52
|
51
|
/** {@inheritDoc} */
|
53
|
52
|
@Override
|
|
@@ -56,10 +55,9 @@ public class EmailModule implements Module {
|
56
|
55
|
layout.setOrientation(LinearLayout.VERTICAL);
|
57
|
56
|
|
58
|
57
|
final Cursor cursor = context.getContentResolver().query(INBOX_URI,
|
59
|
|
- new String[] { "subject", "fromAddress" }, null, null, null);
|
|
58
|
+ new String[] { "conversation_id" }, null, null, null);
|
60
|
59
|
|
61
|
|
- final int subjectIndex = cursor.getColumnIndex("subject");
|
62
|
|
- final int addressIndex = cursor.getColumnIndex("fromAddress");
|
|
60
|
+ final int convIdIndex = cursor.getColumnIndex("conversation_id");
|
63
|
61
|
|
64
|
62
|
final LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,
|
65
|
63
|
LayoutParams.WRAP_CONTENT);
|
|
@@ -67,10 +65,25 @@ public class EmailModule implements Module {
|
67
|
65
|
|
68
|
66
|
boolean success = cursor.moveToFirst();
|
69
|
67
|
for (int i = 0; i < weight && success; i++) {
|
70
|
|
- final String body = cursor.getString(subjectIndex);
|
71
|
|
- final String address = cursor.getString(addressIndex);
|
|
68
|
+ final long convId = cursor.getLong(convIdIndex);
|
|
69
|
+ final Uri uri = INBOX_URI.buildUpon().appendEncodedPath(String.valueOf(convId)
|
|
70
|
+ + "/messages").build();
|
72
|
71
|
|
73
|
|
- layout.addView(getView(context, layout, body, address), params);
|
|
72
|
+ final Cursor messageCursor = context.getContentResolver().query(uri,
|
|
73
|
+ new String[] { "fromAddress", "subject", "messageId" }, null, null, null);
|
|
74
|
+
|
|
75
|
+ messageCursor.moveToFirst();
|
|
76
|
+
|
|
77
|
+ final int subjectIndex = messageCursor.getColumnIndex("subject");
|
|
78
|
+ final int addressIndex = messageCursor.getColumnIndex("fromAddress");
|
|
79
|
+
|
|
80
|
+ final String body = messageCursor.getString(subjectIndex);
|
|
81
|
+ final String address = messageCursor.getString(addressIndex);
|
|
82
|
+ final int count = messageCursor.getCount();
|
|
83
|
+
|
|
84
|
+ layout.addView(getView(context, body, address, count), params);
|
|
85
|
+
|
|
86
|
+ messageCursor.close();
|
74
|
87
|
|
75
|
88
|
success = cursor.moveToNext();
|
76
|
89
|
}
|
|
@@ -80,28 +93,31 @@ public class EmailModule implements Module {
|
80
|
93
|
return layout;
|
81
|
94
|
}
|
82
|
95
|
|
83
|
|
- private View getView(Context context, ViewGroup layout, String text, String address) {
|
|
96
|
+ private View getView(Context context, String text, String address, int count) {
|
84
|
97
|
final View view = View.inflate(context, R.layout.titledimage, null);
|
85
|
98
|
|
86
|
|
- final Uri contactUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL,
|
87
|
|
- Uri.encode(address));
|
|
99
|
+ final Uri contactUri = Uri.parse("content://contacts/contact_methods");
|
|
100
|
+
|
|
101
|
+ final String name = address.replaceAll("^.*\"(.*?)\".*$", "$1")
|
|
102
|
+ .replaceAll("(.*), (.*)", "$2 $1");
|
|
103
|
+ final String email = address.replaceAll("^.*<(.*?)>.*$", "$1");
|
88
|
104
|
|
89
|
105
|
final Cursor cursor = context.getContentResolver().query(contactUri,
|
90
|
|
- new String[] { Contacts.Phones.PERSON_ID,
|
91
|
|
- Contacts.Phones.DISPLAY_NAME }, null, null, null);
|
|
106
|
+ new String[] { "person", "name" }, "data LIKE ?", new String[] { email }, null);
|
92
|
107
|
|
93
|
108
|
final TextView title = (TextView) view.findViewById(R.id.title);
|
94
|
109
|
final ImageView image = (ImageView) view.findViewById(R.id.image);
|
95
|
110
|
|
96
|
111
|
if (cursor.moveToFirst()) {
|
97
|
112
|
title.setText(Html.fromHtml("<b>" + cursor.getString(cursor
|
98
|
|
- .getColumnIndex(Contacts.Phones.DISPLAY_NAME)) + "</b>"));
|
|
113
|
+ .getColumnIndex("name")) + "</b> (" + count + ")"));
|
99
|
114
|
Uri uri = ContentUris.withAppendedId(People.CONTENT_URI,
|
100
|
|
- cursor.getLong(cursor.getColumnIndex(Contacts.Phones.PERSON_ID)));
|
|
115
|
+ cursor.getLong(cursor.getColumnIndex("person")));
|
101
|
116
|
image.setImageBitmap(Contacts.People.loadContactPhoto(context,
|
102
|
117
|
uri, R.drawable.blank, null));
|
103
|
118
|
} else {
|
104
|
|
- title.setText(Html.fromHtml("<b>" + address.split("\n")[2] + "</b>"));
|
|
119
|
+ title.setText(Html.fromHtml("<b>" + (name.length() == 0 ? email : name) + "</b> ("
|
|
120
|
+ + count + ")"));
|
105
|
121
|
}
|
106
|
122
|
|
107
|
123
|
cursor.close();
|