|
@@ -0,0 +1,114 @@
|
|
1
|
+/*
|
|
2
|
+ * Copyright (c) 2009-2010 Chris Smith
|
|
3
|
+ *
|
|
4
|
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+ * of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+ * in the Software without restriction, including without limitation the rights
|
|
7
|
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+ * copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+ * furnished to do so, subject to the following conditions:
|
|
10
|
+ *
|
|
11
|
+ * The above copyright notice and this permission notice shall be included in
|
|
12
|
+ * all copies or substantial portions of the Software.
|
|
13
|
+ *
|
|
14
|
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+ * SOFTWARE.
|
|
21
|
+ */
|
|
22
|
+
|
|
23
|
+package uk.co.md87.android.contexthome.modules;
|
|
24
|
+
|
|
25
|
+import android.content.ContentUris;
|
|
26
|
+import android.content.Context;
|
|
27
|
+import android.database.Cursor;
|
|
28
|
+import android.net.Uri;
|
|
29
|
+import android.provider.Contacts;
|
|
30
|
+import android.provider.Contacts.People;
|
|
31
|
+import android.text.Html;
|
|
32
|
+import android.view.View;
|
|
33
|
+import android.view.ViewGroup;
|
|
34
|
+import android.widget.ImageView;
|
|
35
|
+import android.widget.LinearLayout;
|
|
36
|
+import android.widget.LinearLayout.LayoutParams;
|
|
37
|
+import android.widget.TextView;
|
|
38
|
+
|
|
39
|
+import uk.co.md87.android.contexthome.Module;
|
|
40
|
+import uk.co.md87.android.contexthome.R;
|
|
41
|
+
|
|
42
|
+/**
|
|
43
|
+ * A module which displays SMS messages.
|
|
44
|
+ *
|
|
45
|
+ * @author chris
|
|
46
|
+ */
|
|
47
|
+public class EmailModule implements Module {
|
|
48
|
+
|
|
49
|
+ private static final Uri INBOX_URI = Uri.parse("content://gmail-ls/conversations/chris87@gmail.com");
|
|
50
|
+
|
|
51
|
+ /** {@inheritDoc} */
|
|
52
|
+ @Override
|
|
53
|
+ public View getView(final Context context, final int weight) {
|
|
54
|
+ final LinearLayout layout = new LinearLayout(context);
|
|
55
|
+ layout.setOrientation(LinearLayout.VERTICAL);
|
|
56
|
+
|
|
57
|
+ final Cursor cursor = context.getContentResolver().query(INBOX_URI,
|
|
58
|
+ new String[] { "subject", "fromAddress" }, null, null, null);
|
|
59
|
+
|
|
60
|
+ final int subjectIndex = cursor.getColumnIndex("subject");
|
|
61
|
+ final int addressIndex = cursor.getColumnIndex("fromAddress");
|
|
62
|
+
|
|
63
|
+ final LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,
|
|
64
|
+ LayoutParams.WRAP_CONTENT);
|
|
65
|
+ params.weight = 1;
|
|
66
|
+
|
|
67
|
+ boolean success = cursor.moveToFirst();
|
|
68
|
+ for (int i = 0; i < weight && success; i++) {
|
|
69
|
+ final String body = cursor.getString(subjectIndex);
|
|
70
|
+ final String address = cursor.getString(addressIndex);
|
|
71
|
+
|
|
72
|
+ layout.addView(getView(context, layout, body, address), params);
|
|
73
|
+
|
|
74
|
+ success = cursor.moveToNext();
|
|
75
|
+ }
|
|
76
|
+ cursor.close();
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+ return layout;
|
|
80
|
+ }
|
|
81
|
+
|
|
82
|
+ private View getView(Context context, ViewGroup layout, String text, String address) {
|
|
83
|
+ final View view = View.inflate(context, R.layout.titledimage, null);
|
|
84
|
+
|
|
85
|
+ final Uri contactUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL,
|
|
86
|
+ Uri.encode(address));
|
|
87
|
+
|
|
88
|
+ final Cursor cursor = context.getContentResolver().query(contactUri,
|
|
89
|
+ new String[] { Contacts.Phones.PERSON_ID,
|
|
90
|
+ Contacts.Phones.DISPLAY_NAME }, null, null, null);
|
|
91
|
+
|
|
92
|
+ final TextView title = (TextView) view.findViewById(R.id.title);
|
|
93
|
+ final ImageView image = (ImageView) view.findViewById(R.id.image);
|
|
94
|
+
|
|
95
|
+ if (cursor.moveToFirst()) {
|
|
96
|
+ title.setText(Html.fromHtml("<b>" + cursor.getString(cursor
|
|
97
|
+ .getColumnIndex(Contacts.Phones.DISPLAY_NAME)) + "</b>"));
|
|
98
|
+ Uri uri = ContentUris.withAppendedId(People.CONTENT_URI,
|
|
99
|
+ cursor.getLong(cursor.getColumnIndex(Contacts.Phones.PERSON_ID)));
|
|
100
|
+ image.setImageBitmap(Contacts.People.loadContactPhoto(context,
|
|
101
|
+ uri, R.drawable.icon, null));
|
|
102
|
+ } else {
|
|
103
|
+ title.setText(Html.fromHtml("<b>" + address + "</b>"));
|
|
104
|
+ }
|
|
105
|
+
|
|
106
|
+ cursor.close();
|
|
107
|
+
|
|
108
|
+ final TextView body = (TextView) view.findViewById(R.id.body);
|
|
109
|
+ body.setText(text);
|
|
110
|
+
|
|
111
|
+ return view;
|
|
112
|
+ }
|
|
113
|
+
|
|
114
|
+}
|