Переглянути джерело

Sorting in apps and contacts

master
Chris Smith 14 роки тому
джерело
коміт
6f802618a7

BIN
code/ContextHome/dist/ContextHome.apk Переглянути файл


+ 31
- 21
code/ContextHome/src/uk/co/md87/android/contexthome/DataHelper.java Переглянути файл

28
 import android.database.sqlite.SQLiteOpenHelper;
28
 import android.database.sqlite.SQLiteOpenHelper;
29
 import android.database.sqlite.SQLiteStatement;
29
 import android.database.sqlite.SQLiteStatement;
30
 import android.util.Log;
30
 import android.util.Log;
31
+import java.util.Arrays;
31
 
32
 
32
 import java.util.HashMap;
33
 import java.util.HashMap;
33
 import java.util.List;
34
 import java.util.List;
41
  */
42
  */
42
 public class DataHelper {
43
 public class DataHelper {
43
 
44
 
45
+    public static final String[] SHARED_KEYS = new String[] { "contactid" };
46
+
44
     private static final String DATABASE_NAME = "contexthome.db";
47
     private static final String DATABASE_NAME = "contexthome.db";
45
     private static final int DATABASE_VERSION = 1;
48
     private static final int DATABASE_VERSION = 1;
46
 
49
 
67
 
70
 
68
     public void registerAction(final String module, final Map<String, String> actions) {
71
     public void registerAction(final String module, final Map<String, String> actions) {
69
         // TODO: It'd be nice if the two statements could be merged
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
         for (Map.Entry<String, String> action : actions.entrySet()) {
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
             for (ContextType context : contexts) {
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
                 insertActionStatement.execute();
87
                 insertActionStatement.execute();
85
                 updateActionStatement.execute();
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
         final StringBuilder query = new StringBuilder("SELECT sum(number) AS total, "
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
         final String[] params = new String[1 + contexts.size() * 2];
106
         final String[] params = new String[1 + contexts.size() * 2];
96
         final String extraQuery = " OR (contexttype = ? AND contextvalue = ?)";
107
         final String extraQuery = " OR (contexttype = ? AND contextvalue = ?)";
97
 
108
 
98
         int i = 1;
109
         int i = 1;
110
+        params[0] = module;
99
         for (ContextType context : contexts) {
111
         for (ContextType context : contexts) {
100
             params[i++] = context.getName();
112
             params[i++] = context.getName();
101
             params[i++] = context.getValue();
113
             params[i++] = context.getValue();
102
             query.append(extraQuery);
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
         final Cursor cursor = db.rawQuery(query.toString(), params);
119
         final Cursor cursor = db.rawQuery(query.toString(), params);
107
 
120
 
108
         if (cursor.moveToFirst()) {
121
         if (cursor.moveToFirst()) {
111
             final int valueColumn = cursor.getColumnIndex("actionvalue");
124
             final int valueColumn = cursor.getColumnIndex("actionvalue");
112
 
125
 
113
             do {
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
             } while (cursor.moveToNext());
129
             } while (cursor.moveToNext());
122
         }
130
         }
123
 
131
 
124
         cursor.close();
132
         cursor.close();
125
 
133
 
134
+        Log.d("DataHelper", res.toString());
135
+
126
         return res;
136
         return res;
127
     }
137
     }
128
 
138
 

+ 28
- 0
code/ContextHome/src/uk/co/md87/android/contexthome/Module.java Переглянути файл

25
 import android.content.Context;
25
 import android.content.Context;
26
 import android.view.View;
26
 import android.view.View;
27
 
27
 
28
+import java.util.Map;
29
+
28
 /**
30
 /**
29
  * A module which can be displayed on the context-aware home screen.
31
  * A module which can be displayed on the context-aware home screen.
30
  *
32
  *
32
  */
34
  */
33
 public abstract class Module {
35
 public abstract class Module {
34
 
36
 
37
+    private final String module;
35
     private final DataHelper helper;
38
     private final DataHelper helper;
39
+    private Map<String, Integer> actions;
36
 
40
 
37
     public Module(final DataHelper helper) {
41
     public Module(final DataHelper helper) {
42
+        this.module = getClass().getSimpleName().replace("Module", "").toLowerCase();
38
         this.helper = helper;
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
     public abstract View getView(final Context context, final int weight);
69
     public abstract View getView(final Context context, final int weight);

+ 28
- 8
code/ContextHome/src/uk/co/md87/android/contexthome/modules/AppsModule.java Переглянути файл

22
 
22
 
23
 package uk.co.md87.android.contexthome.modules;
23
 package uk.co.md87.android.contexthome.modules;
24
 
24
 
25
-import android.content.ComponentName;
26
 import android.content.Context;
25
 import android.content.Context;
27
 import android.content.Intent;
26
 import android.content.Intent;
28
 import android.content.pm.ActivityInfo;
27
 import android.content.pm.ActivityInfo;
29
-import android.content.pm.PackageInfo;
30
 import android.content.pm.PackageManager;
28
 import android.content.pm.PackageManager;
31
 import android.content.pm.ResolveInfo;
29
 import android.content.pm.ResolveInfo;
32
-import android.util.Log;
33
 import android.view.View;
30
 import android.view.View;
34
 import android.widget.ImageView;
31
 import android.widget.ImageView;
35
 import android.widget.LinearLayout;
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
 import uk.co.md87.android.contexthome.DataHelper;
40
 import uk.co.md87.android.contexthome.DataHelper;
37
 import uk.co.md87.android.contexthome.Module;
41
 import uk.co.md87.android.contexthome.Module;
38
 import uk.co.md87.android.contexthome.R;
42
 import uk.co.md87.android.contexthome.R;
42
  *
46
  *
43
  * @author chris
47
  * @author chris
44
  */
48
  */
45
-public class AppsModule extends Module {
49
+public class AppsModule extends Module implements Comparator<ResolveInfo> {
46
 
50
 
47
     public AppsModule(DataHelper helper) {
51
     public AppsModule(DataHelper helper) {
48
         super(helper);
52
         super(helper);
58
         final View.OnClickListener listener = new View.OnClickListener() {
62
         final View.OnClickListener listener = new View.OnClickListener() {
59
 
63
 
60
             public void onClick(View view) {
64
             public void onClick(View view) {
61
-                final ActivityInfo info = (ActivityInfo) view.getTag();
65
+                final ResolveInfo info = (ResolveInfo) view.getTag();
62
                 final Intent intent = new Intent();
66
                 final Intent intent = new Intent();
63
                 intent.setAction(Intent.ACTION_MAIN);
67
                 intent.setAction(Intent.ACTION_MAIN);
64
-                intent.setClassName(info.packageName, info.name);
68
+                intent.setClassName(info.activityInfo.packageName, info.activityInfo.name);
65
                 context.startActivity(intent);
69
                 context.startActivity(intent);
70
+
71
+                recordAction(getMap(info));
66
             }
72
             }
67
         };
73
         };
68
 
74
 
69
         intent.addCategory(Intent.CATEGORY_LAUNCHER);
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
             final ImageView image = new ImageView(context);
81
             final ImageView image = new ImageView(context);
73
             image.setImageDrawable(res.activityInfo.loadIcon(pm));
82
             image.setImageDrawable(res.activityInfo.loadIcon(pm));
74
             image.setFocusable(true);
83
             image.setFocusable(true);
75
             image.setClickable(true);
84
             image.setClickable(true);
76
-            image.setTag(res.activityInfo);
85
+            image.setTag(res);
77
             image.setOnClickListener(listener);
86
             image.setOnClickListener(listener);
78
             image.setPadding(2, 2, 2, 2);
87
             image.setPadding(2, 2, 2, 2);
79
             image.setBackgroundResource(R.drawable.grid_selector);
88
             image.setBackgroundResource(R.drawable.grid_selector);
83
         return view;
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 Переглянути файл

32
 import android.view.View;
32
 import android.view.View;
33
 import android.widget.ImageView;
33
 import android.widget.ImageView;
34
 import android.widget.LinearLayout;
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
 import uk.co.md87.android.contexthome.DataHelper;
41
 import uk.co.md87.android.contexthome.DataHelper;
36
 
42
 
37
 import uk.co.md87.android.contexthome.Module;
43
 import uk.co.md87.android.contexthome.Module;
42
  *
48
  *
43
  * @author chris
49
  * @author chris
44
  */
50
  */
45
-public class ContactsModule extends Module {
51
+public class ContactsModule extends Module implements Comparator<Long> {
46
 
52
 
47
     public ContactsModule(DataHelper helper) {
53
     public ContactsModule(DataHelper helper) {
48
         super(helper);
54
         super(helper);
62
                 intent.setData((Uri) view.getTag());
68
                 intent.setData((Uri) view.getTag());
63
                 context.startActivity(intent);
69
                 context.startActivity(intent);
64
 
70
 
71
+                recordAction(getMap(ContentUris.parseId((Uri) view.getTag())));
65
             }
72
             }
66
         };
73
         };
67
 
74
 
69
                 new String[] { "person" }, "exists_on_server != 0", null, null);
76
                 new String[] { "person" }, "exists_on_server != 0", null, null);
70
 
77
 
71
         final int column = cursor.getColumnIndex("person");
78
         final int column = cursor.getColumnIndex("person");
79
+        final List<Long> hits = new ArrayList<Long>(cursor.getCount());
72
         if (cursor.moveToFirst()) {
80
         if (cursor.moveToFirst()) {
81
+            int i = 0;
73
             do {
82
             do {
74
-                layout.addView(getView(context, listener, cursor.getLong(column)), 52, 52);
83
+                hits.add(cursor.getLong(column));
75
             } while (cursor.moveToNext());
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
         return view;
93
         return view;
79
     }
94
     }
94
         return image;
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 Переглянути файл

121
         return layout;
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
         final View view = View.inflate(context, R.layout.titledimage, null);
126
         final View view = View.inflate(context, R.layout.titledimage, null);
126
         view.setClickable(true);
127
         view.setClickable(true);
127
         view.setFocusable(true);
128
         view.setFocusable(true);

Завантаження…
Відмінити
Зберегти