Selaa lähdekoodia

Sorting and threading butchery

master
Chris Smith 14 vuotta sitten
vanhempi
commit
99ed7981c3

BIN
code/ContextHome/dist/ContextHome.apk Näytä tiedosto


+ 7
- 0
code/ContextHome/res/layout/z.xml Näytä tiedosto

@@ -0,0 +1,7 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3
+        android:layout_width="fill_parent"
4
+        android:layout_height="fill_parent"
5
+        android:id="@+id/score">
6
+            <!-- Dummy file to force compiler to create a "score" id -->
7
+</LinearLayout>

+ 53
- 1
code/ContextHome/src/uk/co/md87/android/contexthome/ContextHome.java Näytä tiedosto

@@ -24,7 +24,10 @@ package uk.co.md87.android.contexthome;
24 24
 
25 25
 import android.app.Activity;
26 26
 import android.os.Bundle;
27
+import android.os.Handler;
27 28
 import android.util.Log;
29
+import android.view.Menu;
30
+import android.view.View;
28 31
 import android.widget.LinearLayout.LayoutParams;
29 32
 import android.widget.LinearLayout;
30 33
 import java.util.Arrays;
@@ -38,7 +41,9 @@ import uk.co.md87.android.contexthome.modules.*;
38 41
  *
39 42
  * @author chris
40 43
  */
41
-public class ContextHome extends Activity {
44
+public class ContextHome extends Activity implements Runnable {
45
+
46
+    public static int TAG_SCORE = 10;
42 47
 
43 48
     private LinearLayout layout, fixedLayout;
44 49
 
@@ -46,8 +51,12 @@ public class ContextHome extends Activity {
46 51
 
47 52
     private DataHelper helper;
48 53
 
54
+    private int previousCount = 0;
55
+
49 56
     private Module[] modules, fixedModules;
50 57
 
58
+    private final Handler handler = new Handler();
59
+
51 60
     /** Called when the activity is first created. */
52 61
     @Override
53 62
     public void onCreate(Bundle icicle) {
@@ -94,6 +103,49 @@ public class ContextHome extends Activity {
94 103
             end = System.currentTimeMillis();
95 104
             Log.v("ContextHome", module.getClass().getSimpleName() + ": " + (end - start));
96 105
         }
106
+
107
+        handler.postDelayed(this, 1000);
108
+    }
109
+
110
+    public boolean sortModules() {
111
+        boolean changed = false;
112
+        
113
+        int lastScore = Integer.MAX_VALUE;
114
+        
115
+        for (int i = 0; i < layout.getChildCount(); i++) {
116
+            final View view = layout.getChildAt(i);
117
+
118
+            if (view.getTag(R.id.score) == null) {
119
+                Log.w("ContextHome", "Removing view without score");
120
+                layout.removeViewAt(i);
121
+                i--;
122
+                continue;
123
+            }
124
+
125
+            final int score = (Integer) view.getTag(R.id.score);
126
+
127
+            while (score > lastScore) {
128
+                // TODO: This could be optimised quite a lot
129
+                changed = true;
130
+                layout.removeViewAt(i);
131
+                layout.addView(view, --i);
132
+                lastScore = i == 0 ? Integer.MAX_VALUE
133
+                        : (Integer) layout.getChildAt(i - 1).getTag(R.id.score);
134
+            }
135
+
136
+            lastScore = (Integer) layout.getChildAt(i).getTag(R.id.score);
137
+        }
138
+
139
+        changed |= layout.getChildCount() > previousCount;
140
+        previousCount = layout.getChildCount();
141
+
142
+        return changed;
143
+    }
144
+
145
+    public void run() {
146
+        if (sortModules()) {
147
+            handler.postDelayed(this, 1000);
148
+        }
97 149
     }
98 150
 
99 151
 }

+ 2
- 0
code/ContextHome/src/uk/co/md87/android/contexthome/R.java Näytä tiedosto

@@ -22,6 +22,7 @@ public final class R {
22 22
         public static final int content=0x7f050002;
23 23
         public static final int fixedcontent=0x7f050000;
24 24
         public static final int image=0x7f050003;
25
+        public static final int score=0x7f050006;
25 26
         public static final int scroll=0x7f050001;
26 27
         public static final int title=0x7f050004;
27 28
     }
@@ -29,6 +30,7 @@ public final class R {
29 30
         public static final int container=0x7f030000;
30 31
         public static final int scroller=0x7f030001;
31 32
         public static final int titledimage=0x7f030002;
33
+        public static final int z=0x7f030003;
32 34
     }
33 35
     public static final class string {
34 36
         public static final int app_name=0x7f040000;

+ 5
- 1
code/ContextHome/src/uk/co/md87/android/contexthome/modules/Email.java Näytä tiedosto

@@ -43,13 +43,15 @@ public class Email implements Runnable {
43 43
     private final long convId;
44 44
     private final Handler handler;
45 45
     private final Context context;
46
+    private final EmailModule module;
46 47
     private final View view;
47 48
     private long id = -1;
48 49
 
49
-    public Email(Handler handler, Context context, long convId, View view) {
50
+    public Email(Handler handler, Context context, EmailModule module, long convId, View view) {
50 51
         this.handler = handler;
51 52
         this.convId = convId;
52 53
         this.context = context;
54
+        this.module = module;
53 55
         this.view = view;
54 56
         
55 57
         new Thread(this).start(); // Ick!
@@ -143,6 +145,8 @@ public class Email implements Runnable {
143 145
 
144 146
         cursor.close();
145 147
 
148
+        module.updateScore(this);
149
+
146 150
         final TextView body = (TextView) view.findViewById(R.id.body);
147 151
 
148 152
         handler.post(new Runnable() {

+ 11
- 13
code/ContextHome/src/uk/co/md87/android/contexthome/modules/EmailModule.java Näytä tiedosto

@@ -36,6 +36,7 @@ import java.util.Comparator;
36 36
 import java.util.HashMap;
37 37
 import java.util.List;
38 38
 import java.util.Map;
39
+import uk.co.md87.android.contexthome.ContextHome;
39 40
 import uk.co.md87.android.contexthome.DataHelper;
40 41
 
41 42
 import uk.co.md87.android.contexthome.Module;
@@ -78,8 +79,6 @@ public class EmailModule extends Module implements Comparator<Email> {
78 79
                 LayoutParams.WRAP_CONTENT);
79 80
         params.weight = 1;
80 81
 
81
-        final List<Email> messages = new ArrayList<Email>(weight);
82
-
83 82
         if (cursor.moveToFirst()) {
84 83
             do {
85 84
                 final View view = View.inflate(context, R.layout.titledimage, null);
@@ -96,30 +95,29 @@ public class EmailModule extends Module implements Comparator<Email> {
96 95
                     }
97 96
                 });
98 97
 
99
-                final Email message = new Email(handler, context,
98
+                final Email message = new Email(handler, context, EmailModule.this,
100 99
                         cursor.getLong(convIdIndex), view);
101 100
                 view.setTag(message);
102
-
103
-                messages.add(message);
104
-            } while (cursor.moveToNext());
105
-        }
106
-
107
-        cursor.close();
108
-
109
-        Collections.sort(messages, EmailModule.this);
101
+                updateScore(message);
110 102
 
111 103
         handler.post(new Runnable() {
112 104
 
113 105
                     public void run() {
114
-                                for (Email message : messages) {
115 106
             parent.addView(message.getView(), params);
116
-        }
117 107
                     }
118 108
                 });
109
+            } while (cursor.moveToNext());
110
+        }
111
+
112
+        cursor.close();
119 113
                     }
120 114
         }).start();
121 115
     }
122 116
 
117
+    public void updateScore(final Email email) {
118
+        email.getView().setTag(R.id.score, getScore(getMap(email)));
119
+    }
120
+
123 121
     public Map<String, String> getMap(final Email email) {
124 122
         final Map<String, String> params = new HashMap<String, String>();
125 123
         if (email.getId() >= 0) {

+ 28
- 16
code/ContextHome/src/uk/co/md87/android/contexthome/modules/SmsModule.java Näytä tiedosto

@@ -34,11 +34,9 @@ import android.text.Html;
34 34
 import android.view.View;
35 35
 import android.view.ViewGroup;
36 36
 import android.widget.ImageView;
37
-import android.widget.LinearLayout;
38
-import android.widget.LinearLayout.LayoutParams;
39 37
 import android.widget.TextView;
40
-import java.util.ArrayList;
41
-import java.util.List;
38
+import uk.co.md87.android.contexthome.ContextHome;
39
+
42 40
 import uk.co.md87.android.contexthome.DataHelper;
43 41
 import uk.co.md87.android.contexthome.Module;
44 42
 import uk.co.md87.android.contexthome.R;
@@ -70,17 +68,13 @@ public class SmsModule extends Module {
70 68
         final int bodyIndex = cursor.getColumnIndex("body");
71 69
         final int addressIndex = cursor.getColumnIndex("address");
72 70
 
73
-        final LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,
74
-                LayoutParams.WRAP_CONTENT);
75
-        params.weight = 1;
76
-
77
-        final List<View> views = new ArrayList<View>(weight);
78 71
         boolean success = cursor.moveToFirst();
79 72
         for (int i = 0; i < weight && success; i++) {
80 73
             final String body = cursor.getString(bodyIndex);
81 74
             final String address = cursor.getString(addressIndex);
82 75
 
83
-            final View view = getView(context, body, address, cursor.getLong(idIndex));
76
+            final View view = getView(context, handler, body, address, cursor.getLong(idIndex));
77
+            view.setTag(R.id.score, 0); // TODO
84 78
 
85 79
         handler.post(new Runnable() {
86 80
 
@@ -97,8 +91,8 @@ public class SmsModule extends Module {
97 91
         }).start();
98 92
     }
99 93
 
100
-    private View getView(final Context context, String text,
101
-            String address, final long threadId) {
94
+    private View getView(final Context context, final Handler handler,
95
+            final String text, final String address, final long threadId) {
102 96
         final View view = View.inflate(context, R.layout.titledimage, null);
103 97
         view.setClickable(true);
104 98
         view.setFocusable(true);
@@ -112,7 +106,10 @@ public class SmsModule extends Module {
112 106
             }
113 107
         });
114 108
 
115
-        final Uri contactUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL,
109
+        new Thread(new Runnable() {
110
+
111
+            public void run() {
112
+          final Uri contactUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL,
116 113
                 Uri.encode(address));
117 114
 
118 115
         final Cursor cursor = context.getContentResolver().query(contactUri,
@@ -123,12 +120,18 @@ public class SmsModule extends Module {
123 120
         final ImageView image = (ImageView) view.findViewById(R.id.image);
124 121
 
125 122
         if (cursor.moveToFirst()) {
126
-            title.setText(Html.fromHtml("<b>" + cursor.getString(cursor
127
-                    .getColumnIndex(Contacts.Phones.DISPLAY_NAME)) + "</b>"));
128
-            Uri uri = ContentUris.withAppendedId(People.CONTENT_URI,
123
+            final Uri uri = ContentUris.withAppendedId(People.CONTENT_URI,
129 124
                     cursor.getLong(cursor.getColumnIndex(Contacts.Phones.PERSON_ID)));
125
+            final String name = cursor.getString(cursor
126
+                    .getColumnIndex(Contacts.Phones.DISPLAY_NAME));
127
+            handler.post(new Runnable() {
128
+
129
+                        public void run() {
130
+            title.setText(Html.fromHtml("<b>" + name + "</b>"));
130 131
             image.setImageBitmap(Contacts.People.loadContactPhoto(context,
131 132
                     uri, R.drawable.blank, null));
133
+                                    }
134
+                    });
132 135
         } else {
133 136
             title.setText(Html.fromHtml("<b>" + address + "</b>"));
134 137
         }
@@ -136,7 +139,16 @@ public class SmsModule extends Module {
136 139
         cursor.close();
137 140
 
138 141
         final TextView body = (TextView) view.findViewById(R.id.body);
142
+
143
+                    handler.post(new Runnable() {
144
+
145
+                        public void run() {
139 146
         body.setText(text);
147
+                        }
148
+                    });
149
+
150
+                  }
151
+        }).start();
140 152
 
141 153
         return view;
142 154
     }

Loading…
Peruuta
Tallenna