|
@@ -0,0 +1,151 @@
|
|
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.database.Cursor;
|
|
27
|
+import android.database.sqlite.SQLiteDatabase;
|
|
28
|
+import android.database.sqlite.SQLiteOpenHelper;
|
|
29
|
+import android.database.sqlite.SQLiteStatement;
|
|
30
|
+
|
|
31
|
+import java.util.HashMap;
|
|
32
|
+import java.util.List;
|
|
33
|
+import java.util.Map;
|
|
34
|
+
|
|
35
|
+/**
|
|
36
|
+ * Facilitates accessing the SQLite database used for storing historical
|
|
37
|
+ * interaction information.
|
|
38
|
+ *
|
|
39
|
+ * @author chris
|
|
40
|
+ */
|
|
41
|
+public class DataHelper {
|
|
42
|
+
|
|
43
|
+ private static final String DATABASE_NAME = "contexthome.db";
|
|
44
|
+ private static final int DATABASE_VERSION = 1;
|
|
45
|
+
|
|
46
|
+ private static final String INSERT_ACTION = "insert into actions ("
|
|
47
|
+ + "module, actiontype, actionvalue, contexttype, contextvalue, number) "
|
|
48
|
+ + "VALUES (?, ?, ?, ?, ?, 0)";
|
|
49
|
+ private static final String UPDATE_ACTION = "update actions set number = number + 1"
|
|
50
|
+ + " WHERE module = ? AND actiontype = ? AND actionvalue = ?"
|
|
51
|
+ + " AND contexttype = ? AND contextvalue = ?";
|
|
52
|
+
|
|
53
|
+ private final SQLiteDatabase db;
|
|
54
|
+ private final SQLiteStatement insertActionStatement,
|
|
55
|
+ updateActionStatement;
|
|
56
|
+
|
|
57
|
+ public DataHelper(final Context context) {
|
|
58
|
+ final OpenHelper helper = new OpenHelper(context);
|
|
59
|
+ this.db = helper.getWritableDatabase();
|
|
60
|
+
|
|
61
|
+ this.insertActionStatement = db.compileStatement(INSERT_ACTION);
|
|
62
|
+ this.updateActionStatement = db.compileStatement(UPDATE_ACTION);
|
|
63
|
+ }
|
|
64
|
+
|
|
65
|
+ public void registerAction(final String module, final List<ContextType> contexts,
|
|
66
|
+ final Map<String, String> actions) {
|
|
67
|
+ // TODO: It'd be nice if the two statements could be merged
|
|
68
|
+ insertActionStatement.bindString(0, module);
|
|
69
|
+ updateActionStatement.bindString(0, module);
|
|
70
|
+
|
|
71
|
+ for (Map.Entry<String, String> action : actions.entrySet()) {
|
|
72
|
+ insertActionStatement.bindString(1, action.getKey());
|
|
73
|
+ insertActionStatement.bindString(2, action.getValue());
|
|
74
|
+ updateActionStatement.bindString(1, action.getKey());
|
|
75
|
+ updateActionStatement.bindString(2, action.getValue());
|
|
76
|
+
|
|
77
|
+ for (ContextType context : contexts) {
|
|
78
|
+ insertActionStatement.bindString(3, context.getName());
|
|
79
|
+ insertActionStatement.bindString(4, context.getValue());
|
|
80
|
+ updateActionStatement.bindString(3, context.getName());
|
|
81
|
+ updateActionStatement.bindString(4, context.getValue());
|
|
82
|
+ insertActionStatement.execute();
|
|
83
|
+ updateActionStatement.execute();
|
|
84
|
+ }
|
|
85
|
+ }
|
|
86
|
+ }
|
|
87
|
+
|
|
88
|
+ public Map<String, Map<String, Integer>> getActions(final String module,
|
|
89
|
+ final List<ContextType> contexts) {
|
|
90
|
+ final Map<String, Map<String, Integer>> res = new HashMap<String, Map<String, Integer>>();
|
|
91
|
+
|
|
92
|
+ final StringBuilder query = new StringBuilder("SELECT sum(number) AS total, "
|
|
93
|
+ + " actiontype, actionvalue FROM actions WHERE module = ? AND (0");
|
|
94
|
+ final String[] params = new String[1 + contexts.size() * 2];
|
|
95
|
+ final String extraQuery = " OR (contexttype = ? AND contextvalue = ?)";
|
|
96
|
+
|
|
97
|
+ int i = 1;
|
|
98
|
+ for (ContextType context : contexts) {
|
|
99
|
+ params[i++] = context.getName();
|
|
100
|
+ params[i++] = context.getValue();
|
|
101
|
+ query.append(extraQuery);
|
|
102
|
+ }
|
|
103
|
+
|
|
104
|
+ query.append(") GROUP BY contexttype, contextvalue ORDER BY total DESC");
|
|
105
|
+ final Cursor cursor = db.rawQuery(query.toString(), params);
|
|
106
|
+
|
|
107
|
+ if (cursor.moveToFirst()) {
|
|
108
|
+ final int totalColumn = cursor.getColumnIndex("total");
|
|
109
|
+ final int typeColumn = cursor.getColumnIndex("actiontype");
|
|
110
|
+ final int valueColumn = cursor.getColumnIndex("actionvalue");
|
|
111
|
+
|
|
112
|
+ do {
|
|
113
|
+ final String type = cursor.getString(typeColumn);
|
|
114
|
+
|
|
115
|
+ if (!res.containsKey(type)) {
|
|
116
|
+ res.put(type, new HashMap<String, Integer>());
|
|
117
|
+ }
|
|
118
|
+
|
|
119
|
+ res.get(type).put(cursor.getString(valueColumn), cursor.getInt(totalColumn));
|
|
120
|
+ } while (cursor.moveToNext());
|
|
121
|
+ }
|
|
122
|
+
|
|
123
|
+ cursor.close();
|
|
124
|
+
|
|
125
|
+ return res;
|
|
126
|
+ }
|
|
127
|
+
|
|
128
|
+ private static class OpenHelper extends SQLiteOpenHelper {
|
|
129
|
+
|
|
130
|
+ public OpenHelper(final Context context) {
|
|
131
|
+ super(context, DATABASE_NAME, null, DATABASE_VERSION);
|
|
132
|
+ }
|
|
133
|
+
|
|
134
|
+ @Override
|
|
135
|
+ public void onCreate(final SQLiteDatabase db) {
|
|
136
|
+ db.execSQL("CREATE TABLE actions (_id INTEGER PRIMARY KEY AUTOINCREMENT, "
|
|
137
|
+ + "module TEXT, actiontype TEXT, actionvalue TEXT, contexttype TEXT, "
|
|
138
|
+ + "contextvalue TEXT, number INTEGER, UNIQUE (module, actiontype, "
|
|
139
|
+ + "actionvalue contexttype, contextvalue) "
|
|
140
|
+ + "ON CONFLICT IGNORE)");
|
|
141
|
+ }
|
|
142
|
+
|
|
143
|
+ @Override
|
|
144
|
+ public void onUpgrade(final SQLiteDatabase db, final int oldVersion,
|
|
145
|
+ final int newVersion) {
|
|
146
|
+ db.execSQL("DROP TABLE actions");
|
|
147
|
+ onCreate(db);
|
|
148
|
+ }
|
|
149
|
+ }
|
|
150
|
+
|
|
151
|
+}
|