|
@@ -22,13 +22,21 @@
|
22
|
22
|
|
23
|
23
|
package uk.co.md87.android.contexthome.modules;
|
24
|
24
|
|
|
25
|
+import android.content.ContentUris;
|
25
|
26
|
import android.content.Context;
|
26
|
27
|
import android.database.Cursor;
|
27
|
28
|
import android.net.Uri;
|
|
29
|
+import android.provider.Contacts;
|
|
30
|
+import android.provider.Contacts.People;
|
|
31
|
+import android.text.Html;
|
28
|
32
|
import android.view.View;
|
|
33
|
+import android.view.ViewGroup;
|
|
34
|
+import android.widget.ImageView;
|
29
|
35
|
import android.widget.LinearLayout;
|
|
36
|
+import android.widget.LinearLayout.LayoutParams;
|
30
|
37
|
import android.widget.TextView;
|
31
|
38
|
import uk.co.md87.android.contexthome.Module;
|
|
39
|
+import uk.co.md87.android.contexthome.R;
|
32
|
40
|
|
33
|
41
|
/**
|
34
|
42
|
* A module which displays SMS messages.
|
|
@@ -46,12 +54,21 @@ public class SmsModule implements Module {
|
46
|
54
|
layout.setOrientation(LinearLayout.VERTICAL);
|
47
|
55
|
|
48
|
56
|
final Cursor cursor = context.getContentResolver().query(INBOX_URI,
|
49
|
|
- new String[] { "_id", "date", "body" }, null, null, "date DESC");
|
|
57
|
+ new String[] { "_id", "date", "body", "address" }, null, null, "date DESC");
|
|
58
|
+ final int bodyIndex = cursor.getColumnIndex("body");
|
|
59
|
+ final int addressIndex = cursor.getColumnIndex("address");
|
|
60
|
+
|
|
61
|
+ final LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,
|
|
62
|
+ LayoutParams.WRAP_CONTENT);
|
|
63
|
+ params.weight = 1;
|
|
64
|
+
|
50
|
65
|
boolean success = cursor.moveToFirst();
|
51
|
66
|
for (int i = 0; i < weight && success; i++) {
|
52
|
|
- final TextView text = new TextView(context);
|
53
|
|
- text.setText(cursor.getString(cursor.getColumnIndex("body")));
|
54
|
|
- layout.addView(text);
|
|
67
|
+ final String body = cursor.getString(bodyIndex);
|
|
68
|
+ final String address = cursor.getString(addressIndex);
|
|
69
|
+
|
|
70
|
+ layout.addView(getView(context, layout, body, address), params);
|
|
71
|
+
|
55
|
72
|
success = cursor.moveToNext();
|
56
|
73
|
}
|
57
|
74
|
cursor.close();
|
|
@@ -60,4 +77,36 @@ public class SmsModule implements Module {
|
60
|
77
|
return layout;
|
61
|
78
|
}
|
62
|
79
|
|
|
80
|
+ private View getView(Context context, ViewGroup layout, String text, String address) {
|
|
81
|
+ final View view = View.inflate(context, R.layout.titledimage, null);
|
|
82
|
+
|
|
83
|
+ final Uri contactUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL,
|
|
84
|
+ Uri.encode(address));
|
|
85
|
+
|
|
86
|
+ final Cursor cursor = context.getContentResolver().query(contactUri,
|
|
87
|
+ new String[] { Contacts.Phones.PERSON_ID,
|
|
88
|
+ Contacts.Phones.DISPLAY_NAME }, null, null, null);
|
|
89
|
+
|
|
90
|
+ final TextView title = (TextView) view.findViewById(R.id.title);
|
|
91
|
+ final ImageView image = (ImageView) view.findViewById(R.id.image);
|
|
92
|
+
|
|
93
|
+ if (cursor.moveToFirst()) {
|
|
94
|
+ title.setText(Html.fromHtml("<b>" + cursor.getString(cursor
|
|
95
|
+ .getColumnIndex(Contacts.Phones.DISPLAY_NAME)) + "</b>"));
|
|
96
|
+ Uri uri = ContentUris.withAppendedId(People.CONTENT_URI,
|
|
97
|
+ cursor.getLong(cursor.getColumnIndex(Contacts.Phones.PERSON_ID)));
|
|
98
|
+ image.setImageBitmap(Contacts.People.loadContactPhoto(context,
|
|
99
|
+ uri, R.drawable.icon, null));
|
|
100
|
+ } else {
|
|
101
|
+ title.setText(Html.fromHtml("<b>" + address + "</b>"));
|
|
102
|
+ }
|
|
103
|
+
|
|
104
|
+ cursor.close();
|
|
105
|
+
|
|
106
|
+ final TextView body = (TextView) view.findViewById(R.id.body);
|
|
107
|
+ body.setText(text);
|
|
108
|
+
|
|
109
|
+ return view;
|
|
110
|
+ }
|
|
111
|
+
|
63
|
112
|
}
|