Procházet zdrojové kódy

Sorting in apps and contacts

master
Chris Smith před 14 roky
rodič
revize
6f802618a7

binární
code/ContextHome/dist/ContextHome.apk Zobrazit soubor


+ 31
- 21
code/ContextHome/src/uk/co/md87/android/contexthome/DataHelper.java Zobrazit soubor

@@ -28,6 +28,7 @@ import android.database.sqlite.SQLiteDatabase;
28 28
 import android.database.sqlite.SQLiteOpenHelper;
29 29
 import android.database.sqlite.SQLiteStatement;
30 30
 import android.util.Log;
31
+import java.util.Arrays;
31 32
 
32 33
 import java.util.HashMap;
33 34
 import java.util.List;
@@ -41,6 +42,8 @@ import java.util.Map;
41 42
  */
42 43
 public class DataHelper {
43 44
 
45
+    public static final String[] SHARED_KEYS = new String[] { "contactid" };
46
+
44 47
     private static final String DATABASE_NAME = "contexthome.db";
45 48
     private static final int DATABASE_VERSION = 1;
46 49
 
@@ -67,42 +70,52 @@ public class DataHelper {
67 70
 
68 71
     public void registerAction(final String module, final Map<String, String> actions) {
69 72
         // TODO: It'd be nice if the two statements could be merged
70
-        insertActionStatement.bindString(0, module);
71
-        updateActionStatement.bindString(0, module);
73
+        insertActionStatement.bindString(1, module);
74
+        updateActionStatement.bindString(1, module);
72 75
 
73 76
         for (Map.Entry<String, String> action : actions.entrySet()) {
74
-            insertActionStatement.bindString(1, action.getKey());
75
-            insertActionStatement.bindString(2, action.getValue());
76
-            updateActionStatement.bindString(1, action.getKey());
77
-            updateActionStatement.bindString(2, action.getValue());
77
+            insertActionStatement.bindString(2, action.getKey());
78
+            insertActionStatement.bindString(3, action.getValue());
79
+            updateActionStatement.bindString(2, action.getKey());
80
+            updateActionStatement.bindString(3, action.getValue());
78 81
 
79 82
             for (ContextType context : contexts) {
80
-                insertActionStatement.bindString(3, context.getName());
81
-                insertActionStatement.bindString(4, context.getValue());
82
-                updateActionStatement.bindString(3, context.getName());
83
-                updateActionStatement.bindString(4, context.getValue());
83
+                insertActionStatement.bindString(4, context.getName());
84
+                insertActionStatement.bindString(5, context.getValue());
85
+                updateActionStatement.bindString(4, context.getName());
86
+                updateActionStatement.bindString(5, context.getValue());
84 87
                 insertActionStatement.execute();
85 88
                 updateActionStatement.execute();
86 89
             }
87 90
         }
88 91
     }
89 92
 
90
-    public Map<String, Map<String, Integer>> getActions(final String module) {
91
-        final Map<String, Map<String, Integer>> res = new HashMap<String, Map<String, Integer>>();
93
+    public Map<String, Integer> getActions(final String module) {
94
+        final Map<String, Integer> res = new HashMap<String, Integer>();
92 95
 
93 96
         final StringBuilder query = new StringBuilder("SELECT sum(number) AS total, "
94
-                + " actiontype, actionvalue FROM actions WHERE module = ? AND (0");
97
+                + " actiontype, actionvalue FROM actions WHERE (module = ?");
98
+
99
+        for (String key : SHARED_KEYS) {
100
+            query.append(" OR actiontype = \'");
101
+            query.append(key);
102
+            query.append('\'');
103
+        }
104
+
105
+        query.append(") AND (0");
95 106
         final String[] params = new String[1 + contexts.size() * 2];
96 107
         final String extraQuery = " OR (contexttype = ? AND contextvalue = ?)";
97 108
 
98 109
         int i = 1;
110
+        params[0] = module;
99 111
         for (ContextType context : contexts) {
100 112
             params[i++] = context.getName();
101 113
             params[i++] = context.getValue();
102 114
             query.append(extraQuery);
103 115
         }
104 116
 
105
-        query.append(") GROUP BY contexttype, contextvalue ORDER BY total DESC");
117
+        query.append(") GROUP BY actiontype, actionvalue ORDER BY total DESC");
118
+        Log.d("DataHelper", "Query: " + query + ", Params: " + Arrays.toString(params));
106 119
         final Cursor cursor = db.rawQuery(query.toString(), params);
107 120
 
108 121
         if (cursor.moveToFirst()) {
@@ -111,18 +124,15 @@ public class DataHelper {
111 124
             final int valueColumn = cursor.getColumnIndex("actionvalue");
112 125
 
113 126
             do {
114
-                final String type = cursor.getString(typeColumn);
115
-
116
-                if (!res.containsKey(type)) {
117
-                    res.put(type, new HashMap<String, Integer>());
118
-                }
119
-
120
-                res.get(type).put(cursor.getString(valueColumn), cursor.getInt(totalColumn));
127
+                res.put(cursor.getString(typeColumn) + "/"
128
+                        + cursor.getString(valueColumn), cursor.getInt(totalColumn));
121 129
             } while (cursor.moveToNext());
122 130
         }
123 131
 
124 132
         cursor.close();
125 133
 
134
+        Log.d("DataHelper", res.toString());
135
+
126 136
         return res;
127 137
     }
128 138
 

+ 28
- 0
code/ContextHome/src/uk/co/md87/android/contexthome/Module.java Zobrazit soubor

@@ -25,6 +25,8 @@ package uk.co.md87.android.contexthome;
25 25
 import android.content.Context;
26 26
 import android.view.View;
27 27
 
28
+import java.util.Map;
29
+
28 30
 /**
29 31
  * A module which can be displayed on the context-aware home screen.
30 32
  *
@@ -32,10 +34,36 @@ import android.view.View;
32 34
  */
33 35
 public abstract class Module {
34 36
 
37
+    private final String module;
35 38
     private final DataHelper helper;
39
+    private Map<String, Integer> actions;
36 40
 
37 41
     public Module(final DataHelper helper) {
42
+        this.module = getClass().getSimpleName().replace("Module", "").toLowerCase();
38 43
         this.helper = helper;
44
+
45
+        refreshActions();
46
+    }
47
+
48
+    public void refreshActions() {
49
+        actions = helper.getActions(module);
50
+    }
51
+
52
+    protected int getScore(final Map<String, String> params) {
53
+        int total = 0;
54
+
55
+        for (Map.Entry<String, String> pair : params.entrySet()) {
56
+            final String key = pair.getKey() + "/" + pair.getValue();
57
+            if (actions.containsKey(key)) {
58
+                total += actions.get(key);
59
+            }
60
+        }
61
+
62
+        return total;
63
+    }
64
+
65
+    protected void recordAction(final Map<String, String> params) {
66
+        helper.registerAction(module, params);
39 67
     }
40 68
 
41 69
     public abstract View getView(final Context context, final int weight);

+ 28
- 8
code/ContextHome/src/uk/co/md87/android/contexthome/modules/AppsModule.java Zobrazit soubor

@@ -22,17 +22,21 @@
22 22
 
23 23
 package uk.co.md87.android.contexthome.modules;
24 24
 
25
-import android.content.ComponentName;
26 25
 import android.content.Context;
27 26
 import android.content.Intent;
28 27
 import android.content.pm.ActivityInfo;
29
-import android.content.pm.PackageInfo;
30 28
 import android.content.pm.PackageManager;
31 29
 import android.content.pm.ResolveInfo;
32
-import android.util.Log;
33 30
 import android.view.View;
34 31
 import android.widget.ImageView;
35 32
 import android.widget.LinearLayout;
33
+import java.util.Collections;
34
+import java.util.Comparator;
35
+
36
+import java.util.HashMap;
37
+import java.util.List;
38
+import java.util.Map;
39
+
36 40
 import uk.co.md87.android.contexthome.DataHelper;
37 41
 import uk.co.md87.android.contexthome.Module;
38 42
 import uk.co.md87.android.contexthome.R;
@@ -42,7 +46,7 @@ import uk.co.md87.android.contexthome.R;
42 46
  *
43 47
  * @author chris
44 48
  */
45
-public class AppsModule extends Module {
49
+public class AppsModule extends Module implements Comparator<ResolveInfo> {
46 50
 
47 51
     public AppsModule(DataHelper helper) {
48 52
         super(helper);
@@ -58,22 +62,27 @@ public class AppsModule extends Module {
58 62
         final View.OnClickListener listener = new View.OnClickListener() {
59 63
 
60 64
             public void onClick(View view) {
61
-                final ActivityInfo info = (ActivityInfo) view.getTag();
65
+                final ResolveInfo info = (ResolveInfo) view.getTag();
62 66
                 final Intent intent = new Intent();
63 67
                 intent.setAction(Intent.ACTION_MAIN);
64
-                intent.setClassName(info.packageName, info.name);
68
+                intent.setClassName(info.activityInfo.packageName, info.activityInfo.name);
65 69
                 context.startActivity(intent);
70
+
71
+                recordAction(getMap(info));
66 72
             }
67 73
         };
68 74
 
69 75
         intent.addCategory(Intent.CATEGORY_LAUNCHER);
76
+
77
+        final List<ResolveInfo> infos = pm.queryIntentActivities(intent, 0);
78
+        Collections.sort(infos, this);
70 79
         
71
-        for (ResolveInfo res : pm.queryIntentActivities(intent, 0)) {
80
+        for (ResolveInfo res : infos) {
72 81
             final ImageView image = new ImageView(context);
73 82
             image.setImageDrawable(res.activityInfo.loadIcon(pm));
74 83
             image.setFocusable(true);
75 84
             image.setClickable(true);
76
-            image.setTag(res.activityInfo);
85
+            image.setTag(res);
77 86
             image.setOnClickListener(listener);
78 87
             image.setPadding(2, 2, 2, 2);
79 88
             image.setBackgroundResource(R.drawable.grid_selector);
@@ -83,4 +92,15 @@ public class AppsModule extends Module {
83 92
         return view;
84 93
     }
85 94
 
95
+    public Map<String, String> getMap(final ResolveInfo info) {
96
+        final Map<String, String> params = new HashMap<String, String>();
97
+        params.put("app", info.activityInfo.packageName);
98
+
99
+        return params;
100
+    }
101
+
102
+    public int compare(final ResolveInfo arg0, final ResolveInfo arg1) {
103
+        return getScore(getMap(arg1)) - getScore(getMap(arg0));
104
+    }
105
+
86 106
 }

+ 28
- 2
code/ContextHome/src/uk/co/md87/android/contexthome/modules/ContactsModule.java Zobrazit soubor

@@ -32,6 +32,12 @@ import android.provider.Contacts.Photos;
32 32
 import android.view.View;
33 33
 import android.widget.ImageView;
34 34
 import android.widget.LinearLayout;
35
+import java.util.ArrayList;
36
+import java.util.Collections;
37
+import java.util.Comparator;
38
+import java.util.HashMap;
39
+import java.util.List;
40
+import java.util.Map;
35 41
 import uk.co.md87.android.contexthome.DataHelper;
36 42
 
37 43
 import uk.co.md87.android.contexthome.Module;
@@ -42,7 +48,7 @@ import uk.co.md87.android.contexthome.R;
42 48
  *
43 49
  * @author chris
44 50
  */
45
-public class ContactsModule extends Module {
51
+public class ContactsModule extends Module implements Comparator<Long> {
46 52
 
47 53
     public ContactsModule(DataHelper helper) {
48 54
         super(helper);
@@ -62,6 +68,7 @@ public class ContactsModule extends Module {
62 68
                 intent.setData((Uri) view.getTag());
63 69
                 context.startActivity(intent);
64 70
 
71
+                recordAction(getMap(ContentUris.parseId((Uri) view.getTag())));
65 72
             }
66 73
         };
67 74
 
@@ -69,11 +76,19 @@ public class ContactsModule extends Module {
69 76
                 new String[] { "person" }, "exists_on_server != 0", null, null);
70 77
 
71 78
         final int column = cursor.getColumnIndex("person");
79
+        final List<Long> hits = new ArrayList<Long>(cursor.getCount());
72 80
         if (cursor.moveToFirst()) {
81
+            int i = 0;
73 82
             do {
74
-                layout.addView(getView(context, listener, cursor.getLong(column)), 52, 52);
83
+                hits.add(cursor.getLong(column));
75 84
             } while (cursor.moveToNext());
76 85
         }
86
+        
87
+        Collections.sort(hits, this);
88
+
89
+        for (Long id : hits) {
90
+            layout.addView(getView(context, listener, id), 52, 52);
91
+        }
77 92
 
78 93
         return view;
79 94
     }
@@ -94,4 +109,15 @@ public class ContactsModule extends Module {
94 109
         return image;
95 110
     }
96 111
 
112
+    public Map<String, String> getMap(final Long id) {
113
+        final Map<String, String> params = new HashMap<String, String>();
114
+        params.put("contactid", String.valueOf(id));
115
+
116
+        return params;
117
+    }
118
+
119
+    public int compare(final Long arg0, final Long arg1) {
120
+        return getScore(getMap(arg1)) - getScore(getMap(arg0));
121
+    }
122
+
97 123
 }

+ 2
- 1
code/ContextHome/src/uk/co/md87/android/contexthome/modules/EmailModule.java Zobrazit soubor

@@ -121,7 +121,8 @@ public class EmailModule extends Module {
121 121
         return layout;
122 122
     }
123 123
 
124
-    private View getView(final Context context, String text, String address, int count) {
124
+    private View getView(final Context context, final String text, final String address,
125
+            final int count) {
125 126
         final View view = View.inflate(context, R.layout.titledimage, null);
126 127
         view.setClickable(true);
127 128
         view.setFocusable(true);

Načítá se…
Zrušit
Uložit