瀏覽代碼

Add module system and very basic SMS module to context home

master
Chris Smith 14 年之前
父節點
當前提交
081fb02506

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

@@ -6,11 +6,14 @@
6 6
             android:icon="@drawable/icon" android:theme="@android:style/Theme.Black.NoTitleBar">
7 7
             <intent-filter>
8 8
                 <action android:name="android.intent.action.MAIN"/>
9
+                <category android:name="android.intent.category.LAUNCHER"/>
9 10
                 <category android:name="android.intent.category.HOME"/>
10 11
                 <category android:name="android.intent.category.DEFAULT"/>
11 12
             </intent-filter>
12 13
         </activity>
13 14
     </application>
14 15
 
16
+    <uses-permission android:name="android.permission.READ_SMS"/>
17
+
15 18
     <uses-sdk android:minSdkVersion="3" />
16 19
 </manifest>

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


+ 0
- 10
code/ContextHome/res/layout/main.xml 查看文件

@@ -1,10 +0,0 @@
1
-<?xml version="1.0" encoding="UTF-8"?>
2
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3
-    android:orientation="vertical"
4
-    android:layout_width="fill_parent"
5
-    android:layout_height="fill_parent">" 
6
-    <TextView
7
-        android:layout_width="fill_parent"
8
-        android:layout_height="wrap_content"
9
-        android:text="Hello Android from NetBeans"/>
10
-</LinearLayout>

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

@@ -24,6 +24,9 @@ package uk.co.md87.android.contexthome;
24 24
 
25 25
 import android.app.Activity;
26 26
 import android.os.Bundle;
27
+import android.widget.LinearLayout.LayoutParams;
28
+import android.widget.LinearLayout;
29
+import uk.co.md87.android.contexthome.modules.SmsModule;
27 30
 
28 31
 /**
29 32
  * A home screen that displays e-mails, text messages, etc, and responds to
@@ -33,11 +36,35 @@ import android.os.Bundle;
33 36
  */
34 37
 public class ContextHome extends Activity {
35 38
 
39
+    private static final LayoutParams MODULE_PARAMS
40
+            = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
41
+
42
+    private LinearLayout layout;
43
+    private final Module[] modules = new Module[]{
44
+        new SmsModule(), new SmsModule(), new SmsModule(), new SmsModule(),
45
+        new SmsModule(), new SmsModule(), new SmsModule(), new SmsModule(),
46
+        new SmsModule(), new SmsModule(),
47
+    };
48
+
36 49
     /** Called when the activity is first created. */
37 50
     @Override
38 51
     public void onCreate(Bundle icicle) {
39 52
         super.onCreate(icicle);
40
-        // ToDo add your GUI initialization code here        
53
+        
54
+        layout = new LinearLayout(this);
55
+        MODULE_PARAMS.weight = (float) 1 / 10;
56
+        layout.setOrientation(LinearLayout.VERTICAL);
57
+        setContentView(layout);
58
+
59
+        initLayout();
60
+    }
61
+
62
+    private void initLayout() {
63
+        layout.removeAllViews();
64
+
65
+        for (Module module : modules) {
66
+            layout.addView(module.getView(this, 1), MODULE_PARAMS);
67
+        }
41 68
     }
42 69
 
43 70
 }

+ 37
- 0
code/ContextHome/src/uk/co/md87/android/contexthome/Module.java 查看文件

@@ -0,0 +1,37 @@
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;
24
+
25
+import android.content.Context;
26
+import android.view.View;
27
+
28
+/**
29
+ * A module which can be displayed on the context-aware home screen.
30
+ *
31
+ * @author chris
32
+ */
33
+public interface Module {
34
+
35
+    View getView(final Context context, final int weight);
36
+
37
+}

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

@@ -13,10 +13,7 @@ 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 layout {
17
-        public static final int main=0x7f030000;
18
-    }
19 16
     public static final class string {
20
-        public static final int app_name=0x7f040000;
17
+        public static final int app_name=0x7f030000;
21 18
     }
22 19
 }

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

@@ -0,0 +1,60 @@
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.Context;
26
+import android.database.Cursor;
27
+import android.net.Uri;
28
+import android.view.View;
29
+import android.widget.LinearLayout;
30
+import android.widget.TextView;
31
+import uk.co.md87.android.contexthome.Module;
32
+
33
+/**
34
+ * A module which displays SMS messages.
35
+ *
36
+ * @author chris
37
+ */
38
+public class SmsModule implements Module {
39
+
40
+    private static final Uri INBOX_URI = Uri.parse("content://sms/inbox");
41
+
42
+    /** {@inheritDoc} */
43
+    @Override
44
+    public View getView(final Context context, final int weight) {
45
+        final LinearLayout layout = new LinearLayout(context);
46
+        final TextView text = new TextView(context);
47
+
48
+        final Cursor cursor = context.getContentResolver().query(INBOX_URI,
49
+                new String[] { "_id", "date", "body" }, null, null, "date DESC");
50
+        if (cursor.moveToFirst()) {
51
+            text.setText(cursor.getString(cursor.getColumnIndex("body")));
52
+        }
53
+        cursor.close();
54
+        
55
+        layout.addView(text);
56
+
57
+        return layout;
58
+    }
59
+
60
+}

Loading…
取消
儲存