瀏覽代碼

Add people names and photos

master
Chris Smith 14 年之前
父節點
當前提交
236697549a

+ 1
- 0
code/ContextHome/AndroidManifest.xml 查看文件

@@ -14,6 +14,7 @@
14 14
     </application>
15 15
 
16 16
     <uses-permission android:name="android.permission.READ_SMS"/>
17
+    <uses-permission android:name="android.permission.READ_CONTACTS"/>
17 18
 
18 19
     <uses-sdk android:minSdkVersion="3" />
19 20
 </manifest>

二進制
code/ContextHome/dist/ContextHome.apk 查看文件


+ 22
- 0
code/ContextHome/res/layout/titledimage.xml 查看文件

@@ -0,0 +1,22 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3
+        android:layout_width="fill_parent" android:layout_height="wrap_content">
4
+    <ImageView
5
+        android:id="@+id/image"
6
+        android:layout_width="14pt"
7
+        android:layout_height="14pt"
8
+        android:layout_marginRight="4pt"
9
+        android:layout_centerVertical="true"
10
+        android:src="@drawable/icon"/>
11
+    <TextView
12
+        android:id="@+id/title"
13
+        android:layout_toRightOf="@id/image"
14
+        android:layout_width="wrap_content"
15
+        android:layout_height="wrap_content"/>
16
+    <TextView
17
+        android:id="@+id/body"
18
+        android:layout_below="@id/title"
19
+        android:layout_toRightOf="@id/image"
20
+        android:layout_width="wrap_content"
21
+        android:layout_height="wrap_content"/>
22
+</RelativeLayout>

+ 9
- 1
code/ContextHome/src/uk/co/md87/android/contexthome/R.java 查看文件

@@ -13,7 +13,15 @@ public final class R {
13 13
     public static final class drawable {
14 14
         public static final int icon=0x7f020000;
15 15
     }
16
+    public static final class id {
17
+        public static final int body=0x7f050002;
18
+        public static final int image=0x7f050000;
19
+        public static final int title=0x7f050001;
20
+    }
21
+    public static final class layout {
22
+        public static final int titledimage=0x7f030000;
23
+    }
16 24
     public static final class string {
17
-        public static final int app_name=0x7f030000;
25
+        public static final int app_name=0x7f040000;
18 26
     }
19 27
 }

+ 53
- 4
code/ContextHome/src/uk/co/md87/android/contexthome/modules/SmsModule.java 查看文件

@@ -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
 }

Loading…
取消
儲存