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.0KB

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