Context-detection API for Android developed as a university project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DataHelper.java 6.3KB

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