HostageDBOpenHelper.java 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455
  1. package de.tudarmstadt.informatik.hostage.persistence;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.LinkedList;
  5. import java.util.List;
  6. import android.content.ContentValues;
  7. import android.content.Context;
  8. import android.database.Cursor;
  9. import android.database.sqlite.SQLiteDatabase;
  10. import android.database.sqlite.SQLiteOpenHelper;
  11. import android.util.Log;
  12. import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
  13. import de.tudarmstadt.informatik.hostage.logging.AttackRecord;
  14. import de.tudarmstadt.informatik.hostage.logging.MessageRecord;
  15. import de.tudarmstadt.informatik.hostage.logging.NetworkRecord;
  16. import de.tudarmstadt.informatik.hostage.logging.Record;
  17. import de.tudarmstadt.informatik.hostage.logging.SyncInfoRecord;
  18. import de.tudarmstadt.informatik.hostage.logging.MessageRecord.TYPE;
  19. import de.tudarmstadt.informatik.hostage.model.Profile;
  20. import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.AttackEntry;
  21. import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.NetworkEntry;
  22. import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.PacketEntry;
  23. import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.ProfileEntry;
  24. import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.SyncDeviceEntry;
  25. import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.SyncInfoEntry;
  26. import de.tudarmstadt.informatik.hostage.ui.helper.ColorSequenceGenerator;
  27. import de.tudarmstadt.informatik.hostage.ui.model.LogFilter;
  28. import de.tudarmstadt.informatik.hostage.ui.model.PlotComparisonItem;
  29. /**
  30. * Database Helper class to create, read and write the database.
  31. * @author Mihai Plasoianu
  32. * @author Lars Pandikow
  33. *
  34. */
  35. public class HostageDBOpenHelper extends SQLiteOpenHelper {
  36. private static final String DATABASE_NAME = "hostage.db";
  37. private static final int DATABASE_VERSION = 2;
  38. private Context context;
  39. static {
  40. // NETWORK
  41. StringBuilder networkSQLBuilder = new StringBuilder("CREATE TABLE ").append(NetworkEntry.TABLE_NAME).append("(");
  42. networkSQLBuilder.append(NetworkEntry.COLUMN_NAME_BSSID).append(" TEXT PRIMARY KEY,");
  43. networkSQLBuilder.append(NetworkEntry.COLUMN_NAME_SSID).append(" TEXT,");
  44. networkSQLBuilder.append(NetworkEntry.COLUMN_NAME_LATITUDE).append(" INTEGER,");
  45. networkSQLBuilder.append(NetworkEntry.COLUMN_NAME_LONGITUDE).append(" INTEGER,");
  46. networkSQLBuilder.append(NetworkEntry.COLUMN_NAME_ACCURACY).append(" INTEGER,");
  47. networkSQLBuilder.append(NetworkEntry.COLUMN_NAME_GEO_TIMESTAMP).append(" INTEGER");
  48. networkSQLBuilder.append(")");
  49. SQL_CREATE_NETWORK_ENTRIES = networkSQLBuilder.toString();
  50. // ATTACK
  51. StringBuilder attackSQLBuilder = new StringBuilder("CREATE TABLE ").append(AttackEntry.TABLE_NAME).append("(");
  52. attackSQLBuilder.append(AttackEntry.COLUMN_NAME_ATTACK_ID).append(" INTEGER PRIMARY KEY,");
  53. attackSQLBuilder.append(AttackEntry.COLUMN_NAME_PROTOCOL).append(" TEXT,");
  54. attackSQLBuilder.append(AttackEntry.COLUMN_NAME_EXTERNAL_IP).append(" TEXT,");
  55. attackSQLBuilder.append(AttackEntry.COLUMN_NAME_LOCAL_IP).append(" BLOB,");
  56. attackSQLBuilder.append(AttackEntry.COLUMN_NAME_LOCAL_PORT).append(" INTEGER,");
  57. attackSQLBuilder.append(AttackEntry.COLUMN_NAME_REMOTE_IP).append(" BLOB,");
  58. attackSQLBuilder.append(AttackEntry.COLUMN_NAME_REMOTE_PORT).append(" INTEGER,");
  59. attackSQLBuilder.append(AttackEntry.COLUMN_NAME_INTERNAL_ATTACK).append(" INTEGER,");
  60. attackSQLBuilder.append(AttackEntry.COLUMN_NAME_BSSID).append(" TEXT,");
  61. attackSQLBuilder.append(String.format("FOREIGN KEY(%s) REFERENCES %s(%s)", AttackEntry.COLUMN_NAME_BSSID, NetworkEntry.TABLE_NAME,
  62. NetworkEntry.COLUMN_NAME_BSSID));
  63. attackSQLBuilder.append(")");
  64. SQL_CREATE_ATTACK_ENTRIES = attackSQLBuilder.toString();
  65. // PACKET
  66. StringBuilder packetSQLBuilder = new StringBuilder("CREATE TABLE ").append(PacketEntry.TABLE_NAME).append("(");
  67. packetSQLBuilder.append(PacketEntry.COLUMN_NAME_ID).append(" INTEGER NOT NULL,");
  68. packetSQLBuilder.append(PacketEntry.COLUMN_NAME_ATTACK_ID).append(" INTEGER NOT NULL,");
  69. packetSQLBuilder.append(PacketEntry.COLUMN_NAME_TYPE).append(" TEXT,");
  70. packetSQLBuilder.append(PacketEntry.COLUMN_NAME_PACKET_TIMESTAMP).append(" INTEGER,");
  71. packetSQLBuilder.append(PacketEntry.COLUMN_NAME_PACKET).append(" TEXT,");
  72. packetSQLBuilder.append(String.format("PRIMARY KEY(%s,%s)", PacketEntry.COLUMN_NAME_ID, PacketEntry.COLUMN_NAME_ATTACK_ID));
  73. packetSQLBuilder.append(String.format("FOREIGN KEY(%s) REFERENCES %s(%s)", PacketEntry.COLUMN_NAME_ATTACK_ID, AttackEntry.TABLE_NAME,
  74. AttackEntry.COLUMN_NAME_ATTACK_ID));
  75. packetSQLBuilder.append(")");
  76. SQL_CREATE_PACKET_ENTRIES = packetSQLBuilder.toString();
  77. // SyncDeviceEntry
  78. StringBuilder syncDevicesSQLBuilder = new StringBuilder("CREATE TABLE ").append(SyncDeviceEntry.TABLE_NAME).append("(");
  79. syncDevicesSQLBuilder.append(SyncDeviceEntry.COLUMN_NAME_DEVICE_ID).append(" TEXT PRIMARY KEY,");
  80. syncDevicesSQLBuilder.append(SyncDeviceEntry.COLUMN_NAME_DEVICE_TIMESTAMP).append(" INTEGER");
  81. syncDevicesSQLBuilder.append(")");
  82. SQL_CREATE_SYNC_DEVICES_ENTRIES = syncDevicesSQLBuilder.toString();
  83. // SyncInfoEntry
  84. StringBuilder syncInfoSQLBuilder = new StringBuilder("CREATE TABLE ").append(SyncInfoEntry.TABLE_NAME).append("(");
  85. syncInfoSQLBuilder.append(SyncInfoEntry.COLUMN_NAME_DEVICE_ID).append(" TEXT,");
  86. syncInfoSQLBuilder.append(SyncInfoEntry.COLUMN_NAME_BSSID).append(" TEXT,");
  87. syncInfoSQLBuilder.append(SyncInfoEntry.COLUMN_NAME_NUMBER_ATTACKS).append(" INTEGER,");
  88. syncInfoSQLBuilder.append(SyncInfoEntry.COLUMN_NAME_NUMBER_PORTSCANS).append(" INTEGER,");
  89. syncInfoSQLBuilder.append(String.format("PRIMARY KEY(%s,%s)", SyncInfoEntry.COLUMN_NAME_DEVICE_ID, SyncInfoEntry.COLUMN_NAME_BSSID));
  90. syncInfoSQLBuilder.append(String.format("FOREIGN KEY(%s) REFERENCES %s(%s)", SyncInfoEntry.COLUMN_NAME_BSSID, NetworkEntry.TABLE_NAME,
  91. NetworkEntry.COLUMN_NAME_BSSID));
  92. syncInfoSQLBuilder.append(")");
  93. SQL_CREATE_SYNC_INFO_ENTRIES = syncInfoSQLBuilder.toString();
  94. // ProfileEntry
  95. StringBuilder profilSQLBuilder = new StringBuilder("CREATE TABLE ").append(ProfileEntry.TABLE_NAME).append("(");
  96. profilSQLBuilder.append(ProfileEntry.COLUMN_NAME_PROFILE_ID).append(" INTEGER PRIMARY KEY AUTOINCREMENT,");
  97. profilSQLBuilder.append(ProfileEntry.COLUMN_NAME_PROFILE_NAME).append(" TEXT,");
  98. profilSQLBuilder.append(ProfileEntry.COLUMN_NAME_PROFILE_DESCRIPTION ).append(" TEXT,");
  99. profilSQLBuilder.append(ProfileEntry.COLUMN_NAME_PROFILE_ICON).append(" TEXT,");
  100. profilSQLBuilder.append(ProfileEntry.COLUMN_NAME_PROFILE_ICON_NAME).append(" TEXT,");
  101. profilSQLBuilder.append(ProfileEntry.COLUMN_NAME_PROFILE_EDITABLE).append(" INTEGER,");
  102. profilSQLBuilder.append(ProfileEntry.COLUMN_NAME_PROFILE_ACTIVE).append(" INTEGER");
  103. profilSQLBuilder.append(")");
  104. SQL_CREATE_PROFILE_ENTRIES = profilSQLBuilder.toString();
  105. }
  106. private static final String SQL_CREATE_NETWORK_ENTRIES;
  107. private static final String SQL_CREATE_ATTACK_ENTRIES;
  108. private static final String SQL_CREATE_PACKET_ENTRIES;
  109. private static final String SQL_CREATE_PROFILE_ENTRIES;
  110. private static final String SQL_CREATE_SYNC_DEVICES_ENTRIES;
  111. private static final String SQL_CREATE_SYNC_INFO_ENTRIES;
  112. private static final String SQL_DELETE_PACKET_ENTRIES = "DROP TABLE IF EXISTS " + PacketEntry.TABLE_NAME;
  113. private static final String SQL_DELETE_ATTACK_ENTRIES = "DROP TABLE IF EXISTS " + AttackEntry.TABLE_NAME;
  114. private static final String SQL_DELETE_NETWORK_ENTRIES = "DROP TABLE IF EXISTS " + NetworkEntry.TABLE_NAME;
  115. private static final String SQL_DELETE_PROFILE_ENTRIES = "DROP TABLE IF EXISTS " + ProfileEntry.TABLE_NAME;
  116. private static final String SQL_DELETE_SYNC_DEVICES_ENTRIES = "DROP TABLE IF EXISTS " + SyncDeviceEntry.TABLE_NAME;
  117. private static final String SQL_DELETE_SYNC_INFO_ENTRIES = "DROP TABLE IF EXISTS " + SyncInfoEntry.TABLE_NAME;
  118. public HostageDBOpenHelper(Context context) {
  119. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  120. this.context = context;
  121. }
  122. @Override
  123. public void onCreate(SQLiteDatabase db) {
  124. db.execSQL(SQL_CREATE_NETWORK_ENTRIES);
  125. db.execSQL(SQL_CREATE_ATTACK_ENTRIES);
  126. db.execSQL(SQL_CREATE_PACKET_ENTRIES);
  127. db.execSQL(SQL_CREATE_PROFILE_ENTRIES);
  128. db.execSQL(SQL_CREATE_SYNC_DEVICES_ENTRIES);
  129. db.execSQL(SQL_CREATE_SYNC_INFO_ENTRIES);
  130. }
  131. @Override
  132. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  133. db.execSQL(SQL_DELETE_SYNC_INFO_ENTRIES);
  134. db.execSQL(SQL_DELETE_PACKET_ENTRIES);
  135. db.execSQL(SQL_DELETE_ATTACK_ENTRIES);
  136. db.execSQL(SQL_DELETE_PROFILE_ENTRIES);
  137. db.execSQL(SQL_DELETE_NETWORK_ENTRIES);
  138. db.execSQL(SQL_DELETE_SYNC_DEVICES_ENTRIES);
  139. onCreate(db);
  140. }
  141. /**
  142. * Adds a given {@link MessageRecord} to the database.
  143. *
  144. * @param record
  145. * The added {@link MessageRecord} .
  146. */
  147. public void addMessageRecord(MessageRecord record) {
  148. SQLiteDatabase db = this.getWritableDatabase();
  149. ContentValues recordValues = new ContentValues();
  150. recordValues.put(PacketEntry.COLUMN_NAME_ID, record.getId()); // Log Message Number
  151. recordValues.put(PacketEntry.COLUMN_NAME_ATTACK_ID, record.getAttack_id()); // Log Attack ID
  152. recordValues.put(PacketEntry.COLUMN_NAME_TYPE, record.getType().name()); // Log Type
  153. recordValues.put(PacketEntry.COLUMN_NAME_PACKET_TIMESTAMP, record.getTimestamp()); // Log Timestamp
  154. recordValues.put(PacketEntry.COLUMN_NAME_PACKET, record.getPacket()); // Log Packet
  155. // Inserting Rows
  156. db.insert(PacketEntry.TABLE_NAME, null, recordValues);
  157. db.close(); // Closing database connection
  158. }
  159. /**
  160. * Adds a given {@link AttackRecord} to the database.
  161. *
  162. * @param record
  163. * The added {@link AttackRecord} .
  164. */
  165. public void addAttackRecord(AttackRecord record) {
  166. Log.i("DBHelper", "Add Attack Record with id: " + record.getAttack_id());
  167. SQLiteDatabase db = this.getWritableDatabase();
  168. ContentValues attackValues = new ContentValues();
  169. attackValues.put(AttackEntry.COLUMN_NAME_ATTACK_ID, record.getAttack_id()); // Log Attack ID
  170. attackValues.put(AttackEntry.COLUMN_NAME_PROTOCOL, record.getProtocol().toString());
  171. attackValues.put(AttackEntry.COLUMN_NAME_EXTERNAL_IP, record.getExternalIP());
  172. attackValues.put(AttackEntry.COLUMN_NAME_LOCAL_IP, record.getLocalIP()); // Log Local IP
  173. attackValues.put(AttackEntry.COLUMN_NAME_LOCAL_PORT, record.getLocalPort());
  174. attackValues.put(AttackEntry.COLUMN_NAME_REMOTE_IP, record.getRemoteIP()); // Log Remote IP
  175. attackValues.put(AttackEntry.COLUMN_NAME_REMOTE_PORT, record.getRemotePort()); // Log Remote Port
  176. attackValues.put(AttackEntry.COLUMN_NAME_INTERNAL_ATTACK, record.getWasInternalAttack());
  177. attackValues.put(AttackEntry.COLUMN_NAME_BSSID, record.getBssid());
  178. // Inserting Rows
  179. db.insertWithOnConflict(AttackEntry.TABLE_NAME, null, attackValues, SQLiteDatabase.CONFLICT_REPLACE);
  180. db.close(); // Closing database connection
  181. }
  182. public void updateSyncAttackCounter(AttackRecord record){
  183. SQLiteDatabase db = this.getWritableDatabase();
  184. String mac = HelperUtils.getMacAdress(context);
  185. ContentValues syncDeviceValues = new ContentValues();
  186. syncDeviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_ID, mac);
  187. syncDeviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_TIMESTAMP, System.currentTimeMillis());
  188. String query = "SELECT * FROM " + SyncInfoEntry.TABLE_NAME +
  189. " WHERE " + SyncInfoEntry.COLUMN_NAME_DEVICE_ID + " = ? " +
  190. "AND " + SyncInfoEntry.COLUMN_NAME_BSSID + " = ?";
  191. Cursor cursor = db.rawQuery(query, new String[] {mac, record.getBssid()});
  192. long attackCount = 0;
  193. long portscanCount = 0;
  194. if (cursor.moveToFirst()){
  195. attackCount = cursor.getLong(2);
  196. portscanCount = cursor.getLong(3);
  197. }
  198. if("PORTSCAN".equals(record.getProtocol())){
  199. portscanCount++;
  200. }else { attackCount++; }
  201. Log.i("DBHelper", "Update number of attack: " + attackCount);
  202. ContentValues synInfoValues = new ContentValues();
  203. synInfoValues.put(SyncInfoEntry.COLUMN_NAME_BSSID, record.getBssid());
  204. synInfoValues.put(SyncInfoEntry.COLUMN_NAME_DEVICE_ID, mac);
  205. synInfoValues.put(SyncInfoEntry.COLUMN_NAME_NUMBER_ATTACKS, attackCount);
  206. synInfoValues.put(SyncInfoEntry.COLUMN_NAME_NUMBER_PORTSCANS, portscanCount);
  207. // Inserting Rows
  208. db.insertWithOnConflict(SyncInfoEntry.TABLE_NAME, null, synInfoValues, SQLiteDatabase.CONFLICT_REPLACE);
  209. db.insertWithOnConflict(SyncDeviceEntry.TABLE_NAME, null, syncDeviceValues, SQLiteDatabase.CONFLICT_REPLACE);
  210. db.close(); // Closing database connection
  211. }
  212. /**
  213. * Determines if a network with given BSSID has already been recorded as malicious.
  214. *
  215. * @param BSSID
  216. * The BSSID of the network.
  217. * @return True if an attack has been recorded in a network with the given
  218. * BSSID, else false.
  219. */
  220. public synchronized boolean bssidSeen(String BSSID) {
  221. String countQuery = "SELECT * FROM " + NetworkEntry.TABLE_NAME + " WHERE " + NetworkEntry.COLUMN_NAME_BSSID + " = ?";
  222. SQLiteDatabase db = this.getReadableDatabase();
  223. Cursor cursor = db.rawQuery(countQuery, new String[] {BSSID});
  224. int result = cursor.getCount();
  225. cursor.close();
  226. db.close();
  227. return result > 0;
  228. }
  229. /**
  230. * Determines if an attack has been recorded on a specific protocol in a
  231. * network with a given BSSID.
  232. *
  233. * @param protocol
  234. * The
  235. * {@link de.tudarmstadt.informatik.hostage.protocol.Protocol
  236. * Protocol} to inspect.
  237. * @param BSSID
  238. * The BSSID of the network.
  239. * @return True if an attack on the given protocol has been recorded in a
  240. * network with the given BSSID, else false.
  241. */
  242. public synchronized boolean bssidSeen(String protocol, String BSSID) {
  243. if(BSSID == null || protocol == null){
  244. return false;
  245. }
  246. String countQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME + " NATURAL JOIN " + NetworkEntry.TABLE_NAME + " WHERE "
  247. + AttackEntry.COLUMN_NAME_PROTOCOL + " = ? AND " + NetworkEntry.COLUMN_NAME_BSSID + " = ?";
  248. SQLiteDatabase db = this.getReadableDatabase();
  249. Cursor cursor = db.rawQuery(countQuery, new String[]{protocol, BSSID});
  250. int result = cursor.getCount();
  251. cursor.close();
  252. db.close();
  253. return result > 0;
  254. }
  255. public synchronized int numBssidSeen(String BSSID) {
  256. String countQuery = "SELECT COUNT(*) FROM " + AttackEntry.TABLE_NAME + " WHERE "
  257. + AttackEntry.TABLE_NAME + "." + AttackEntry.COLUMN_NAME_BSSID + " = " + "'" + BSSID + "'";
  258. SQLiteDatabase db = this.getReadableDatabase();
  259. Cursor cursor = db.rawQuery(countQuery, null);
  260. cursor.moveToFirst();
  261. int result = cursor.getInt(0);
  262. cursor.close();
  263. db.close();
  264. return result;
  265. }
  266. public int numBssidSeen(String protocol, String BSSID) {
  267. String countQuery = "SELECT COUNT(*) FROM " + AttackEntry.TABLE_NAME
  268. + " WHERE " + AttackEntry.TABLE_NAME + "." + AttackEntry.COLUMN_NAME_PROTOCOL + " = " + "'" + protocol + "'"
  269. + " AND " + AttackEntry.TABLE_NAME + "." + AttackEntry.COLUMN_NAME_BSSID + " = " + "'" + BSSID + "'";
  270. SQLiteDatabase db = this.getReadableDatabase();
  271. Cursor cursor = db.rawQuery(countQuery, null);
  272. cursor.moveToFirst();
  273. int result = cursor.getInt(0);
  274. cursor.close();
  275. db.close();
  276. return result;
  277. }
  278. /**
  279. * Returns a String array with all BSSIDs stored in the database.
  280. *
  281. * @return String[] of all recorded BSSIDs.
  282. */
  283. public synchronized String[] getAllBSSIDS() {
  284. String selectQuery = "SELECT * FROM " + NetworkEntry.TABLE_NAME;
  285. SQLiteDatabase db = this.getReadableDatabase();
  286. Cursor cursor = db.rawQuery(selectQuery, null);
  287. String[] bssidList = new String[cursor.getCount()];
  288. int counter = 0;
  289. // looping through all rows and adding to list
  290. if (cursor.moveToFirst()) {
  291. do {
  292. bssidList[counter] = cursor.getString(0);
  293. counter++;
  294. } while (cursor.moveToNext());
  295. }
  296. cursor.close();
  297. db.close();
  298. return bssidList;
  299. }
  300. /**
  301. * Determines the number of different attacks in the database.
  302. *
  303. * @return The number of different attacks in the database.
  304. */
  305. public synchronized int getAttackCount() {
  306. SQLiteDatabase db = this.getReadableDatabase();
  307. String countQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME +
  308. " WHERE " + AttackEntry.COLUMN_NAME_PROTOCOL + " <> ?";
  309. Cursor cursor = db.rawQuery(countQuery, new String[]{"PORTSCAN"});
  310. int result = cursor.getCount();
  311. cursor.close();
  312. // return count
  313. db.close();
  314. return result;
  315. }
  316. /**
  317. * Determines the number of different recorded attacks in a specific access point since the given attack_id.
  318. * The given attack_id is not included.
  319. * @param attack_id The attack id to match the query against.
  320. * @param bssid The BSSID of the access point.
  321. * @return The number of different attacks in the database since the given attack_id.
  322. */
  323. public synchronized int getAttackCount(int attack_id, String bssid) {
  324. SQLiteDatabase db = this.getReadableDatabase();
  325. String countQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME +
  326. " WHERE "+ AttackEntry.COLUMN_NAME_PROTOCOL + " <> ? " +
  327. "AND " + AttackEntry.COLUMN_NAME_ATTACK_ID + " > ? " +
  328. "AND " + AttackEntry.COLUMN_NAME_BSSID + " = ?";
  329. String[] selectArgs = new String[]{"PORTSCAN", attack_id + "", bssid};
  330. Cursor cursor = db.rawQuery(countQuery, selectArgs);
  331. int result = cursor.getCount();
  332. cursor.close();
  333. // return count
  334. db.close();
  335. return result;
  336. }
  337. /**
  338. * Determines the number of different attacks for a specific protocol in
  339. * the database.
  340. *
  341. * @param protocol
  342. * The String representation of the
  343. * {@link de.tudarmstadt.informatik.hostage.protocol.Protocol
  344. * Protocol}
  345. * @return The number of different attacks in the database.
  346. */
  347. public synchronized int getAttackPerProtocolCount(String protocol) {
  348. SQLiteDatabase db = this.getReadableDatabase();
  349. String countQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME +
  350. " WHERE "+ AttackEntry.COLUMN_NAME_PROTOCOL + " = ? ";
  351. Cursor cursor = db.rawQuery(countQuery, new String[]{protocol});
  352. int result = cursor.getCount();
  353. cursor.close();
  354. // return count
  355. db.close();
  356. return result;
  357. }
  358. /**
  359. * Determines the number of attacks for a specific protocol in
  360. * the database since the given attack_id.
  361. *
  362. * @param protocol
  363. * The String representation of the
  364. * {@link de.tudarmstadt.informatik.hostage.protocol.Protocol
  365. * Protocol}
  366. * @param attack_id The attack id to match the query against.
  367. * @return The number of different attacks in the database since the given attack_id.
  368. */
  369. public synchronized int getAttackPerProtocolCount(String protocol, int attack_id) {
  370. SQLiteDatabase db = this.getReadableDatabase();
  371. String countQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME +
  372. " WHERE "+ AttackEntry.COLUMN_NAME_PROTOCOL + " = ? " +
  373. "AND " + AttackEntry.COLUMN_NAME_ATTACK_ID + " > ? ";
  374. Cursor cursor = db.rawQuery(countQuery, new String[]{protocol, attack_id + ""});
  375. int result = cursor.getCount();
  376. cursor.close();
  377. // return count
  378. db.close();
  379. return result;
  380. }
  381. /**
  382. * Determines the number of recorded attacks for a specific protocol and accesss point since the given attack_id.
  383. *
  384. * @param protocol
  385. * The String representation of the
  386. * {@link de.tudarmstadt.informatik.hostage.protocol.Protocol
  387. * Protocol}
  388. * @param attack_id The attack id to match the query against.
  389. * @param bssid The BSSID of the access point.
  390. * @return The number of different attacks in the database since the given attack_id.
  391. */
  392. public synchronized int getAttackPerProtocolCount(String protocol, int attack_id, String bssid) {
  393. SQLiteDatabase db = this.getReadableDatabase();
  394. String countQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME +
  395. " WHERE "+ AttackEntry.COLUMN_NAME_PROTOCOL + " = ? " +
  396. "AND " + AttackEntry.COLUMN_NAME_ATTACK_ID + " > ? " +
  397. "AND " + AttackEntry.COLUMN_NAME_BSSID + " = ?";
  398. Cursor cursor = db.rawQuery(countQuery, new String[]{protocol, attack_id + "", bssid});
  399. int result = cursor.getCount();
  400. cursor.close();
  401. // return count
  402. db.close();
  403. return result;
  404. }
  405. /**
  406. * Determines the number of portscans stored in the database.
  407. *
  408. * @return The number of portscans stored in the database.
  409. */
  410. public synchronized int getPortscanCount() {
  411. return getAttackPerProtocolCount("PORTSCAN");
  412. }
  413. /**
  414. * Determines the number of recorded portscans since the given attack_id.
  415. * @param attack_id The attack id to match the query against.
  416. * @return The number of portscans stored in the database since the given attack_id.
  417. */
  418. public synchronized int getPortscanCount(int attack_id) {
  419. return getAttackPerProtocolCount("PORTSCAN", attack_id);
  420. }
  421. /**
  422. * Determines the number of recorded portscans in a specific access point since the given attack_id.
  423. * @param attack_id The attack id to match the query against.
  424. * @param bssid The BSSID of the access point.
  425. * @return The number of portscans stored in the database since the given attack_id.
  426. */
  427. public synchronized int getPortscanCount(int attack_id, String bssid) {
  428. return getAttackPerProtocolCount("PORTSCAN", attack_id, bssid);
  429. }
  430. /**
  431. * Determines the number of {@link Record Records} in the database.
  432. *
  433. * @return The number of {@link Record Records} in the database.
  434. */
  435. public synchronized int getRecordCount() {
  436. String countQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME;
  437. SQLiteDatabase db = this.getReadableDatabase();
  438. Cursor cursor = db.rawQuery(countQuery, null);
  439. int result = cursor.getCount();
  440. cursor.close();
  441. // return count
  442. db.close();
  443. return result;
  444. }
  445. //TODO ADD AGAIN ?
  446. /**
  447. * Returns the {@link AttackRecord} with the given attack id from the database.
  448. *
  449. * @param attack_id
  450. * The attack id of the {@link Record};
  451. * @return The {@link Record}.
  452. */
  453. /*
  454. public AttackRecord getRecordOfAttackId(long attack_id) {
  455. String selectQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME + " WHERE " + AttackEntry.COLUMN_NAME_ATTACK_ID + " = " + attack_id;
  456. SQLiteDatabase db = this.getReadableDatabase();
  457. Cursor cursor = db.rawQuery(selectQuery, null);
  458. AttackRecord record = null;
  459. if (cursor.moveToFirst()) {
  460. record = createAttackRecord(cursor);
  461. }
  462. cursor.close();
  463. // return record list
  464. db.close();
  465. return record;
  466. } */
  467. /**
  468. * Gets a {@link AttackRecord} for every attack identified by its attack id.
  469. *
  470. * @return A ArrayList with a {@link AttackRecord} for each attack id in the Database.
  471. */
  472. public synchronized ArrayList<AttackRecord> getRecordOfEachAttack() {
  473. ArrayList<AttackRecord> recordList = new ArrayList<AttackRecord>();
  474. String selectQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME;
  475. SQLiteDatabase db = this.getReadableDatabase();
  476. Cursor cursor = db.rawQuery(selectQuery, null);
  477. // looping through all rows and adding to list
  478. if (cursor.moveToFirst()) {
  479. do {
  480. AttackRecord record = createAttackRecord(cursor);
  481. // Adding record to list
  482. recordList.add(record);
  483. } while (cursor.moveToNext());
  484. }
  485. cursor.close();
  486. // return record list
  487. db.close();
  488. return recordList;
  489. }
  490. /**
  491. * Gets a AttackRecord for every attack with a higher attack id than the specified.
  492. *
  493. * @param attack_id
  494. * The attack id to match the query against.
  495. * @return A ArrayList with one {@link AttackRecord} for each attack id
  496. * higher than the given.
  497. */
  498. public synchronized ArrayList<AttackRecord> getRecordOfEachAttack(long attack_id) {
  499. ArrayList<AttackRecord> recordList = new ArrayList<AttackRecord>();
  500. String selectQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME + " WHERE " + AttackEntry.COLUMN_NAME_ATTACK_ID + " > " + attack_id;
  501. SQLiteDatabase db = this.getReadableDatabase();
  502. Cursor cursor = db.rawQuery(selectQuery, null);
  503. // looping through all rows and adding to list
  504. if (cursor.moveToFirst()) {
  505. do {
  506. AttackRecord record = createAttackRecord(cursor);
  507. // Adding record to list
  508. recordList.add(record);
  509. } while (cursor.moveToNext());
  510. }
  511. cursor.close();
  512. // return count
  513. db.close();
  514. return recordList;
  515. }
  516. /**
  517. * Determines the highest attack id stored in the database.
  518. *
  519. * @return The highest attack id stored in the database.
  520. */
  521. public synchronized long getHighestAttackId() {
  522. String selectQuery = "SELECT MAX(" + AttackEntry.COLUMN_NAME_ATTACK_ID + ") FROM " + AttackEntry.TABLE_NAME;
  523. SQLiteDatabase db = this.getReadableDatabase();
  524. Cursor cursor = db.rawQuery(selectQuery, null);
  525. int result;
  526. if (cursor.moveToFirst()) {
  527. result = cursor.getInt(0);
  528. } else {
  529. result = -1;
  530. }
  531. cursor.close();
  532. db.close();
  533. return result;
  534. }
  535. /**
  536. * Determines the smallest attack id stored in the database.
  537. *
  538. * @return The smallest attack id stored in the database.
  539. */
  540. public synchronized long getSmallestAttackId() {
  541. String selectQuery = "SELECT MIN(" + AttackEntry.COLUMN_NAME_ATTACK_ID + ") FROM " + AttackEntry.TABLE_NAME;
  542. SQLiteDatabase db = this.getReadableDatabase();
  543. Cursor cursor = db.rawQuery(selectQuery, null);
  544. int result;
  545. if (cursor.moveToFirst()) {
  546. result = cursor.getInt(0);
  547. } else {
  548. result = -1;
  549. }
  550. cursor.close();
  551. db.close();
  552. return result;
  553. }
  554. /**
  555. * Gets the last recorded SSID to a given BSSID.
  556. *
  557. * @param bssid
  558. * The BSSID to match against.
  559. * @return A String of the last SSID or null if the BSSID is not in the
  560. * database.
  561. */
  562. public synchronized String getSSID(String bssid) {
  563. String selectQuery = "SELECT " + NetworkEntry.COLUMN_NAME_SSID + " FROM " + NetworkEntry.TABLE_NAME + " WHERE " + NetworkEntry.COLUMN_NAME_BSSID
  564. + " = " + "'" + bssid + "'";
  565. SQLiteDatabase db = this.getReadableDatabase();
  566. Cursor cursor = db.rawQuery(selectQuery, null);
  567. String ssid = null;
  568. if (cursor.moveToFirst()) {
  569. ssid = cursor.getString(0);
  570. }
  571. cursor.close();
  572. db.close();
  573. return ssid;
  574. }
  575. /**
  576. * Gets all network related data stored in the database
  577. * @return An ArrayList with an Network for all Entry in the network table.
  578. */
  579. public synchronized ArrayList<NetworkRecord> getNetworkInformation() {
  580. String selectQuery = "SELECT * FROM " + NetworkEntry.TABLE_NAME;
  581. SQLiteDatabase db = this.getReadableDatabase();
  582. Cursor cursor = db.rawQuery(selectQuery, null);
  583. ArrayList<NetworkRecord> networkInformation = new ArrayList<NetworkRecord>();
  584. // looping through all rows and adding to list
  585. if (cursor.moveToFirst()) {
  586. do {
  587. NetworkRecord record = new NetworkRecord();
  588. record.setBssid(cursor.getString(0));
  589. record.setSsid(cursor.getString(1));
  590. record.setLatitude(Double.parseDouble(cursor.getString(2)));
  591. record.setLongitude(Double.parseDouble(cursor.getString(3)));
  592. record.setAccuracy(Float.parseFloat(cursor.getString(4)));
  593. record.setTimestampLocation(cursor.getLong(5));
  594. networkInformation.add(record);
  595. } while (cursor.moveToNext());
  596. }
  597. cursor.close();
  598. db.close();
  599. return networkInformation;
  600. }
  601. /**
  602. * Updates the network table with the information contained in the parameter.
  603. * @param networkInformation ArrayList of {@link NetworkRecord NetworkRecords}
  604. * @see {@link HostageDBOpenHelper#updateNetworkInformation(NetworkRecord record)}
  605. */
  606. public void updateNetworkInformation(ArrayList<NetworkRecord> networkInformation) {
  607. for (NetworkRecord record : networkInformation) {
  608. updateNetworkInformation(record);
  609. }
  610. }
  611. /**
  612. * Updated the network table with a new {@link NetworkRecord}.
  613. * If information about this BSSID are already in the database,
  614. * the table will only be updated if the new {@link NetworkRecord }
  615. * has a newer location time stamp.
  616. * @param record The new {@link NetworkRecord}.
  617. */
  618. public void updateNetworkInformation(NetworkRecord record) {
  619. SQLiteDatabase db = this.getReadableDatabase();
  620. String bssid = record.getBssid();
  621. String bssidQuery = "SELECT * FROM " + NetworkEntry.TABLE_NAME + " WHERE " + NetworkEntry.COLUMN_NAME_BSSID + " = ?";
  622. Cursor cursor = db.rawQuery(bssidQuery, new String[] {bssid});
  623. if (!cursor.moveToFirst() || cursor.getLong(5) < record.getTimestampLocation()){
  624. ContentValues bssidValues = new ContentValues();
  625. bssidValues.put(NetworkEntry.COLUMN_NAME_BSSID, bssid);
  626. bssidValues.put(NetworkEntry.COLUMN_NAME_SSID, record.getSsid());
  627. bssidValues.put(NetworkEntry.COLUMN_NAME_LATITUDE, record.getLatitude());
  628. bssidValues.put(NetworkEntry.COLUMN_NAME_LONGITUDE, record.getLongitude());
  629. bssidValues.put(NetworkEntry.COLUMN_NAME_ACCURACY, record.getAccuracy());
  630. bssidValues.put(NetworkEntry.COLUMN_NAME_GEO_TIMESTAMP, record.getTimestampLocation());
  631. db.insertWithOnConflict(NetworkEntry.TABLE_NAME, null, bssidValues, SQLiteDatabase.CONFLICT_REPLACE);
  632. }
  633. cursor.close();
  634. db.close();
  635. }
  636. /**
  637. * Updates the the timestamp of a single device id
  638. * @param devices The Device id
  639. * @param timestamp The synchronization timestamp
  640. */
  641. public void updateSyncDevice(String devices, long timestamp){
  642. SQLiteDatabase db = this.getReadableDatabase();
  643. ContentValues deviceValues = new ContentValues();
  644. deviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_ID, devices);
  645. deviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_TIMESTAMP, timestamp);
  646. db.insertWithOnConflict(SyncDeviceEntry.TABLE_NAME, null, deviceValues, SQLiteDatabase.CONFLICT_REPLACE);
  647. db.close();
  648. }
  649. /**
  650. * Updates the Timestamps of synchronization devices from a HashMap.
  651. * @param devices HashMap of device ids and their synchronization timestamps.
  652. */
  653. public void updateSyncDevices(HashMap<String, Long> devices){
  654. SQLiteDatabase db = this.getReadableDatabase();
  655. for(String key : devices.keySet()){
  656. ContentValues deviceValues = new ContentValues();
  657. deviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_ID, key);
  658. deviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_TIMESTAMP, devices.get(key));
  659. db.insertWithOnConflict(SyncDeviceEntry.TABLE_NAME, null, deviceValues, SQLiteDatabase.CONFLICT_REPLACE);
  660. }
  661. db.close();
  662. }
  663. /**
  664. * Returns a HashMap of all devices that were previously synchronized with.
  665. * @return HashMap containing device id's and the last synchronization timestamp.
  666. */
  667. public synchronized HashMap<String, Long> getSyncDevices(){
  668. SQLiteDatabase db = this.getReadableDatabase();
  669. HashMap<String, Long> devices = new HashMap<String, Long>();
  670. String query = "SELECT * FROM " + SyncDeviceEntry.TABLE_NAME;
  671. Cursor cursor = db.rawQuery(query, null);
  672. if (cursor.moveToFirst()) {
  673. do {
  674. devices.put(cursor.getString(0), cursor.getLong(1));
  675. } while (cursor.moveToNext());
  676. }
  677. cursor.close();
  678. db.close();
  679. return devices;
  680. }
  681. /**
  682. * Returns a ArrayList containing all information stored in the SyncInfo table.
  683. * @return ArrayList<SyncInfo>
  684. */
  685. public synchronized ArrayList<SyncInfoRecord> getSyncInfo(){
  686. SQLiteDatabase db = this.getReadableDatabase();
  687. ArrayList<SyncInfoRecord> syncInfo = new ArrayList<SyncInfoRecord>();
  688. String query = "SELECT * FROM " + SyncInfoEntry.TABLE_NAME;
  689. Cursor cursor = db.rawQuery(query, null);
  690. if (cursor.moveToFirst()) {
  691. do {
  692. SyncInfoRecord info = new SyncInfoRecord();
  693. info.setDeviceID(cursor.getString(0));
  694. info.setBSSID(cursor.getString(1));
  695. info.setNumber_of_attacks(cursor.getLong(2));
  696. info.setNumber_of_portscans(cursor.getLong(3));
  697. syncInfo.add(info);
  698. } while (cursor.moveToNext());
  699. }
  700. cursor.close();
  701. db.close();
  702. return syncInfo;
  703. }
  704. /**
  705. * Updates the sync_info table with the information contained in the parameter.
  706. * @param syncInfo ArrayList of {@link SyncInfoRecord SyncInfoRecords}
  707. * @see {@link HostageDBOpenHelper#updateSyncInfo(SyncInfoRecord syncInfo)}
  708. */
  709. public synchronized void updateSyncInfo(ArrayList<SyncInfoRecord> syncInfo){
  710. for(SyncInfoRecord info : syncInfo){
  711. updateSyncInfo(info);
  712. }
  713. }
  714. /**
  715. * Updated the network table with a new {@link SyncInfoRecord}.
  716. * Conflicting rows will be replaced.
  717. * @param syncInfo The new {@link NetworkRecord}.
  718. */
  719. public synchronized void updateSyncInfo(SyncInfoRecord syncInfo){
  720. SQLiteDatabase db = this.getReadableDatabase();
  721. ContentValues syncValues = new ContentValues();
  722. syncValues.put(SyncInfoEntry.COLUMN_NAME_BSSID, syncInfo.getBSSID());
  723. syncValues.put(SyncInfoEntry.COLUMN_NAME_DEVICE_ID, syncInfo.getDeviceID());
  724. syncValues.put(SyncInfoEntry.COLUMN_NAME_NUMBER_ATTACKS, syncInfo.getNumber_of_attacks());
  725. syncValues.put(SyncInfoEntry.COLUMN_NAME_NUMBER_PORTSCANS, syncInfo.getNumber_of_portscans());
  726. db.insertWithOnConflict(SyncInfoEntry.TABLE_NAME, null, syncValues, SQLiteDatabase.CONFLICT_REPLACE);
  727. db.close();
  728. }
  729. /**
  730. * Deletes a device with given id from the device {@link SyncDeviceEntry.TABLE_NAME} and also all data captured by this device in {@link SyncInfoEntry.TABLE_NAME}
  731. */
  732. public void clearSyncInfos(){
  733. SQLiteDatabase db = this.getReadableDatabase();
  734. db.delete(SyncDeviceEntry.TABLE_NAME, null, null);
  735. db.delete(SyncInfoEntry.TABLE_NAME, null, null);
  736. db.close();
  737. }
  738. /**
  739. * Deletes all records from {@link #PacketEntry.TABLE_NAME}.
  740. */
  741. public void clearData() {
  742. SQLiteDatabase db = this.getReadableDatabase();
  743. db.delete(PacketEntry.TABLE_NAME, null, null);
  744. db.delete(AttackEntry.TABLE_NAME, null, null);
  745. db.close();
  746. }
  747. /**
  748. * Deletes all records from {@link #PacketEntry.TABLE_NAME} with a specific BSSID.
  749. *
  750. * @param bssid
  751. * The BSSID to match against.
  752. */
  753. public synchronized void deleteByBSSID(String bssid) {
  754. SQLiteDatabase db = this.getReadableDatabase();
  755. db.delete(NetworkEntry.TABLE_NAME, NetworkEntry.COLUMN_NAME_BSSID + " = ?", new String[] { bssid });
  756. db.delete(AttackEntry.TABLE_NAME, AttackEntry.COLUMN_NAME_BSSID + " = ?", new String[] { bssid });
  757. db.close();
  758. }
  759. /**
  760. * Deletes all records from {@link #PacketEntry.TABLE_NAME} with a time stamp smaller
  761. * then the given
  762. *
  763. * @param date
  764. * A Date represented in milliseconds.
  765. */
  766. public synchronized void deleteByDate(long date) {
  767. SQLiteDatabase db = this.getReadableDatabase();
  768. String deleteQuery = "DELETE FROM " + PacketEntry.TABLE_NAME + " WHERE " + PacketEntry.COLUMN_NAME_PACKET_TIMESTAMP + " < " + date;
  769. db.execSQL(deleteQuery);
  770. db.close();
  771. }
  772. /**
  773. * Deletes all records from {@link #TABLE_RECORDS} with a specific Attack ID.
  774. *
  775. * @param attackID
  776. * The Attack ID to match against.
  777. */
  778. public synchronized void deleteByAttackID(long attackID) {
  779. SQLiteDatabase db = this.getReadableDatabase();
  780. db.delete(AttackEntry.TABLE_NAME, AttackEntry.COLUMN_NAME_ATTACK_ID + " = ?", new String[] { String.valueOf(attackID) });
  781. db.delete(PacketEntry.TABLE_NAME, PacketEntry.COLUMN_NAME_ATTACK_ID + " = ?", new String[] { String.valueOf(attackID) });
  782. db.close();
  783. }
  784. /**
  785. * Creates a {@link Record} from a Cursor. If the cursor does not show to a
  786. * valid data structure a runtime exception is thrown.
  787. *
  788. * @param cursor
  789. * @return Returns the created {@link Record} .
  790. */
  791. private synchronized MessageRecord createMessageRecord(Cursor cursor) {
  792. MessageRecord record = new MessageRecord();
  793. record.setId(Integer.parseInt(cursor.getString(0)));
  794. record.setAttack_id(cursor.getLong(1));
  795. record.setType(MessageRecord.TYPE.valueOf(cursor.getString(2)));
  796. record.setTimestamp(cursor.getLong(3));
  797. record.setPacket(cursor.getString(4));
  798. return record;
  799. }
  800. /**
  801. * Creates a {@link AttackRecord} from a Cursor. If the cursor does not show to a
  802. * valid data structure a runtime exception is thrown.
  803. *
  804. * @param cursor
  805. * @return Returns the created {@link Record} .
  806. */
  807. private synchronized AttackRecord createAttackRecord(Cursor cursor) {
  808. AttackRecord record = new AttackRecord();
  809. record.setAttack_id(cursor.getLong(0));
  810. record.setProtocol(cursor.getString(1));
  811. record.setExternalIP(cursor.getString(2));
  812. record.setLocalIP(cursor.getString(3));
  813. record.setLocalPort(Integer.parseInt(cursor.getString(4)));
  814. record.setRemoteIP(cursor.getString(5));
  815. record.setRemotePort(Integer.parseInt(cursor.getString(6)));
  816. record.setWasInternalAttack(cursor.getInt(7) == 1);
  817. record.setBssid(cursor.getString(8));
  818. return record;
  819. }
  820. /**
  821. * Creates a {@link Record} from a Cursor. If the cursor does not show to a
  822. * valid data structure a runtime exception is thrown.
  823. *
  824. * @param cursor
  825. * @return Returns the created {@link Record} .
  826. */
  827. private synchronized Record createRecord(Cursor cursor) {
  828. Record record = new Record();
  829. record.setId(Integer.parseInt(cursor.getString(0)));
  830. record.setAttack_id(cursor.getLong(1));
  831. record.setType(TYPE.valueOf(cursor.getString(2)));
  832. record.setTimestamp(cursor.getLong(3));
  833. record.setPacket(cursor.getString(4));
  834. record.setProtocol(cursor.getString(5));
  835. record.setExternalIP(cursor.getString(6));
  836. record.setLocalIP(cursor.getString(7));
  837. record.setLocalPort(Integer.parseInt(cursor.getString(8)));
  838. record.setRemoteIP(cursor.getString(9));
  839. record.setRemotePort(Integer.parseInt(cursor.getString(10)));
  840. record.setWasInternalAttack(Integer.parseInt(cursor.getString(11)) == 1);
  841. record.setBssid(cursor.getString(12));
  842. record.setSsid(cursor.getString(13));
  843. record.setLatitude(Double.parseDouble(cursor.getString(14)));
  844. record.setLongitude(Double.parseDouble(cursor.getString(15)));
  845. record.setAccuracy(Float.parseFloat(cursor.getString(16)));
  846. record.setTimestampLocation(cursor.getLong(17));
  847. return record;
  848. }
  849. /**
  850. * Gets all received {@link Record Records} for the specified information in
  851. * the LogFilter ordered by date.
  852. *
  853. * @return A ArrayList with all received {@link Record Records} for the
  854. * LogFilter.
  855. */
  856. public synchronized ArrayList<Record> getRecordsForFilter(LogFilter filter) {
  857. ArrayList<Record> recordList = new ArrayList<Record>();
  858. String selectQuery = this.selectionQueryFromFilter(filter, "*");
  859. SQLiteDatabase db = this.getReadableDatabase();
  860. Cursor cursor = db.rawQuery(selectQuery, null);
  861. // looping through all rows and adding to list
  862. if (cursor.moveToFirst()) {
  863. do {
  864. Record record = createRecord(cursor);
  865. // Adding record to list
  866. recordList.add(record);
  867. } while (cursor.moveToNext());
  868. }
  869. cursor.close();
  870. // return record list
  871. db.close();
  872. return recordList;
  873. }
  874. /**
  875. * Returns the query for the given filter.
  876. * @param filter (LogFilter)
  877. * @param selectionString (String) for everything: "*"
  878. * @return (String) query string
  879. */
  880. public String selectionQueryFromFilter(LogFilter filter, String selectionString)
  881. {
  882. String selectQuery = "SELECT " + selectionString + " FROM " + PacketEntry.TABLE_NAME + " NATURAL JOIN " + AttackEntry.TABLE_NAME + " JOIN " + NetworkEntry.TABLE_NAME + " USING " + "(" + NetworkEntry.COLUMN_NAME_BSSID
  883. + ")";
  884. if (filter == null) return selectQuery;
  885. // TIMESTAMPS
  886. selectQuery = selectQuery + " WHERE " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_PACKET_TIMESTAMP;
  887. selectQuery = selectQuery + " < " + filter.getBelowTimestamp();
  888. selectQuery = selectQuery + " AND " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_PACKET_TIMESTAMP;
  889. selectQuery = selectQuery + " > " + filter.getAboveTimestamp();
  890. if (filter.getBSSIDs() != null && filter.getBSSIDs().size() > 0) {
  891. selectQuery = selectQuery + " AND ";
  892. selectQuery = selectQuery + filter.getBSSIDQueryStatement(NetworkEntry.TABLE_NAME, NetworkEntry.COLUMN_NAME_BSSID);
  893. }
  894. if (filter.getESSIDs() != null && filter.getESSIDs().size() > 0) {
  895. selectQuery = selectQuery + " AND ";
  896. selectQuery = selectQuery + filter.getESSIDQueryStatement(NetworkEntry.TABLE_NAME, NetworkEntry.COLUMN_NAME_SSID);
  897. }
  898. if (filter.getProtocols() != null && filter.getProtocols().size() > 0) {
  899. selectQuery = selectQuery + " AND ";
  900. selectQuery = selectQuery + filter.getProtocolsQueryStatement(AttackEntry.TABLE_NAME, AttackEntry.COLUMN_NAME_PROTOCOL);
  901. }
  902. selectQuery = selectQuery + " GROUP BY " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_ATTACK_ID;
  903. if (filter.getSorttype() == LogFilter.SortType.packet_timestamp) {
  904. // DESC
  905. selectQuery = selectQuery + " ORDER BY " + filter.getSorttype() + " DESC";
  906. } else {
  907. selectQuery = selectQuery + " ORDER BY " + filter.getSorttype();
  908. }
  909. System.out.println(selectQuery);
  910. return selectQuery;
  911. }
  912. /**
  913. * Returns the Conversation of a specific attack id
  914. *
  915. * @param attack_id Tha attack id to match the query against.
  916. *
  917. * @return A arraylist with all {@link Record Records}s for an attack id.
  918. */
  919. public synchronized ArrayList<Record> getConversationForAttackID(long attack_id) {
  920. ArrayList<Record> recordList = new ArrayList<Record>();
  921. String selectQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME + " NATURAL JOIN " + AttackEntry.TABLE_NAME + " JOIN " + NetworkEntry.TABLE_NAME + " USING " + "(" + NetworkEntry.COLUMN_NAME_BSSID
  922. + ")" + " WHERE " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_ATTACK_ID + " = " + attack_id;
  923. SQLiteDatabase db = this.getReadableDatabase();
  924. Cursor cursor = db.rawQuery(selectQuery, null);
  925. if (cursor.moveToFirst()) {
  926. do {
  927. Record record = createRecord(cursor);
  928. recordList.add(record);
  929. } while (cursor.moveToNext());
  930. }
  931. cursor.close();
  932. db.close();
  933. return recordList;
  934. }
  935. /**
  936. * Gets a single {@link Record} with the given attack id from the database.
  937. *
  938. * @param attack_id
  939. * The attack id of the {@link Record};
  940. * @return The {@link Record}.
  941. */
  942. public synchronized Record getRecordOfAttackId(long attack_id) {
  943. String selectQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME + " NATURAL JOIN " + AttackEntry.TABLE_NAME + " JOIN " + NetworkEntry.TABLE_NAME + " USING " + "(" + NetworkEntry.COLUMN_NAME_BSSID
  944. + ")" + " WHERE " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_ATTACK_ID + " = " + attack_id + " GROUP BY " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_ID;
  945. SQLiteDatabase db = this.getReadableDatabase();
  946. Cursor cursor = db.rawQuery(selectQuery, null);
  947. Record record = null;
  948. if (cursor.moveToFirst()) {
  949. record = createRecord(cursor);
  950. }
  951. cursor.close();
  952. // return record list
  953. db.close();
  954. return record;
  955. }
  956. /**
  957. * Gets a single {@link Record} with the given ID from the database.
  958. *
  959. * @param id
  960. * The ID of the {@link Record};
  961. * @return The {@link Record}.
  962. */
  963. public synchronized Record getRecord(int id) {
  964. String selectQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME + " NATURAL JOIN " + AttackEntry.TABLE_NAME + " JOIN " + NetworkEntry.TABLE_NAME + " USING " + "(" + PacketEntry.COLUMN_NAME_ATTACK_ID
  965. + ")" + " WHERE " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_ID + " = " + id;
  966. SQLiteDatabase db = this.getReadableDatabase();
  967. Cursor cursor = db.rawQuery(selectQuery, null);
  968. Record record = null;
  969. if (cursor.moveToFirst()) {
  970. record = createRecord(cursor);
  971. }
  972. cursor.close();
  973. db.close();
  974. // return contact
  975. return record;
  976. }
  977. /**
  978. * Gets all {@link Record Records} saved in the database.
  979. *
  980. * @return A ArrayList of all the {@link Record Records} in the Database.
  981. */
  982. public synchronized ArrayList<Record> getAllRecords() {
  983. ArrayList<Record> recordList = new ArrayList<Record>();
  984. // Select All Query
  985. String selectQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME + " NATURAL JOIN " + AttackEntry.TABLE_NAME + " JOIN " + NetworkEntry.TABLE_NAME + " USING " + "(" + NetworkEntry.COLUMN_NAME_BSSID
  986. + ")";
  987. SQLiteDatabase db = this.getWritableDatabase();
  988. Cursor cursor = db.rawQuery(selectQuery, null);
  989. Log.i("Database", "Start loop");
  990. // looping through all rows and adding to list
  991. if (cursor.moveToFirst()) {
  992. do {
  993. Log.i("Database", "Add Record");
  994. Record record = createRecord(cursor);
  995. // Adding record to list
  996. recordList.add(record);
  997. } while (cursor.moveToNext());
  998. }
  999. cursor.close();
  1000. db.close();
  1001. // return record list
  1002. return recordList;
  1003. }
  1004. /**
  1005. * Gets all non duplicate Records For the key BSSID.
  1006. *
  1007. * @return A ArrayList with received Records.
  1008. */
  1009. public synchronized ArrayList<String> getUniqueBSSIDRecords() {
  1010. return this.getUniqueDataEntryForKeyType(NetworkEntry.COLUMN_NAME_BSSID, NetworkEntry.TABLE_NAME);
  1011. }
  1012. /**
  1013. * Gets all non duplicate Records For the key ESSID.
  1014. *
  1015. * @return A ArrayList with received Records.
  1016. */
  1017. public synchronized ArrayList<String> getUniqueESSIDRecords() {
  1018. return this.getUniqueDataEntryForKeyType(NetworkEntry.COLUMN_NAME_SSID, NetworkEntry.TABLE_NAME);
  1019. }
  1020. public synchronized ArrayList<String> getUniqueESSIDRecordsForProtocol(String protocol) {
  1021. return this.getUniqueIDForProtocol(NetworkEntry.COLUMN_NAME_SSID, protocol);
  1022. }
  1023. public synchronized ArrayList<String> getUniqueBSSIDRecordsForProtocol(String protocol) {
  1024. return this.getUniqueIDForProtocol(NetworkEntry.COLUMN_NAME_BSSID, protocol);
  1025. }
  1026. private synchronized ArrayList<String> getUniqueIDForProtocol(String id, String protocol) {
  1027. ArrayList<String> recordList = new ArrayList<String>();
  1028. String selectQuery = "SELECT DISTINCT " + id + " FROM " + AttackEntry.TABLE_NAME + " JOIN " + NetworkEntry.TABLE_NAME + " USING " + "(" + NetworkEntry.COLUMN_NAME_BSSID + ") " + " WHERE "
  1029. + AttackEntry.TABLE_NAME + "." + AttackEntry.COLUMN_NAME_PROTOCOL + " = " + "'" + protocol + "'" + " ORDER BY " + id; // " NATURAL JOIN "
  1030. // +
  1031. // TABLE_ATTACK_INFO
  1032. // +
  1033. // " NATURAL JOIN "
  1034. // +
  1035. // TABLE_BSSIDS
  1036. // +
  1037. // " NATURAL JOIN "
  1038. // +
  1039. // TABLE_PORTS
  1040. // +
  1041. // ORDERED BY TIME
  1042. System.out.println(selectQuery);
  1043. SQLiteDatabase db = this.getReadableDatabase();
  1044. Cursor cursor = db.rawQuery(selectQuery, null);
  1045. // looping through all rows and adding to list
  1046. if (cursor.moveToFirst()) {
  1047. do {
  1048. String record = cursor.getString(0);
  1049. recordList.add(record);
  1050. } while (cursor.moveToNext());
  1051. }
  1052. cursor.close();
  1053. // return record list
  1054. db.close();
  1055. return recordList;
  1056. }
  1057. /**
  1058. * Gets all non duplicate Data Entry For a specific KeyType ( e.g. BSSIDs).
  1059. *
  1060. * @return A ArrayList with received Records.
  1061. */
  1062. public synchronized ArrayList<String> getUniqueDataEntryForKeyType(String keyType, String table) {
  1063. ArrayList<String> recordList = new ArrayList<String>();
  1064. // String selectQuery = "SELECT * FROM " + TABLE_RECORDS +
  1065. // " NATURAL JOIN " + TABLE_ATTACK_INFO + " NATURAL JOIN " +
  1066. // TABLE_BSSIDS + " NATURAL JOIN " + TABLE_PORTS;
  1067. String selectQuery = "SELECT DISTINCT " + keyType + " FROM " + table + " ORDER BY " + keyType; // " NATURAL JOIN "
  1068. // +
  1069. // TABLE_ATTACK_INFO
  1070. // +
  1071. // " NATURAL JOIN "
  1072. // +
  1073. // TABLE_BSSIDS
  1074. // +
  1075. // " NATURAL JOIN "
  1076. // +
  1077. // TABLE_PORTS
  1078. // +
  1079. // ORDERED BY TIME
  1080. System.out.println(selectQuery);
  1081. SQLiteDatabase db = this.getReadableDatabase();
  1082. Cursor cursor = db.rawQuery(selectQuery, null);
  1083. // looping through all rows and adding to list
  1084. if (cursor.moveToFirst()) {
  1085. do {
  1086. String record = cursor.getString(0);
  1087. recordList.add(record);
  1088. } while (cursor.moveToNext());
  1089. }
  1090. cursor.close();
  1091. // return record list
  1092. db.close();
  1093. return recordList;
  1094. }
  1095. //TODO PROFILE DATABASE QUERIES - STILL NEEDED?
  1096. /**
  1097. * Retrieves all the profiles from the database
  1098. *
  1099. * @return list of profiles
  1100. */
  1101. public synchronized List<Profile> getAllProfiles() {
  1102. List<Profile> profiles = new LinkedList<Profile>();
  1103. // Select All Query
  1104. String selectQuery = "SELECT * FROM " + ProfileEntry.TABLE_NAME;
  1105. SQLiteDatabase db = this.getWritableDatabase();
  1106. Cursor cursor = db.rawQuery(selectQuery, null);
  1107. // looping through all rows and adding to list
  1108. if (cursor.moveToFirst()) {
  1109. do {
  1110. Profile profile = new Profile(cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getInt(5) == 1);
  1111. if (cursor.getInt(6) == 1) {
  1112. profile.mActivated = true;
  1113. }
  1114. profile.mIconName = cursor.getString(4);
  1115. // Adding record to list
  1116. profiles.add(profile);
  1117. } while (cursor.moveToNext());
  1118. }
  1119. cursor.close();
  1120. db.close();
  1121. // return record list
  1122. return profiles;
  1123. }
  1124. /**
  1125. * Persists the given profile into the database
  1126. *
  1127. * @param profile
  1128. * the profile which should be persisted
  1129. *
  1130. * @return
  1131. */
  1132. public synchronized long persistProfile(Profile profile) {
  1133. SQLiteDatabase db = this.getReadableDatabase();
  1134. ContentValues values = new ContentValues();
  1135. if (profile.mId != -1) {
  1136. values.put(ProfileEntry.COLUMN_NAME_PROFILE_ID, profile.mId);
  1137. }
  1138. values.put(ProfileEntry.COLUMN_NAME_PROFILE_NAME, profile.mLabel);
  1139. values.put(ProfileEntry.COLUMN_NAME_PROFILE_DESCRIPTION, profile.mText);
  1140. values.put(ProfileEntry.COLUMN_NAME_PROFILE_ICON, profile.mIconPath);
  1141. values.put(ProfileEntry.COLUMN_NAME_PROFILE_ICON_NAME, profile.mIconName);
  1142. values.put(ProfileEntry.COLUMN_NAME_PROFILE_ACTIVE, profile.mActivated);
  1143. values.put(ProfileEntry.COLUMN_NAME_PROFILE_EDITABLE, profile.mEditable);
  1144. return db.replace(ProfileEntry.TABLE_NAME, null, values);
  1145. }
  1146. /**
  1147. * private static final String CREATE_PROFILE_TABLE = "CREATE TABLE " +
  1148. * TABLE_PROFILES + "(" + KEY_PROFILE_ID +
  1149. * " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_PROFILE_NAME + " TEXT," +
  1150. * KEY_PROFILE_DESCRIPTION + " TEXT," + KEY_PROFILE_ICON + " TEXT," +
  1151. * KEY_PROFILE_ICON_ID + " INTEGER," + KEY_PROFILE_EDITABLE + " INTEGER," +
  1152. * KEY_PROFILE_ACTIVE + " INTEGER" + ")";
  1153. */
  1154. public synchronized Profile getProfile(int id) {
  1155. String selectQuery = "SELECT * FROM " + ProfileEntry.TABLE_NAME + " WHERE " + ProfileEntry.TABLE_NAME + "." + ProfileEntry.COLUMN_NAME_PROFILE_ID + " = " + id;
  1156. SQLiteDatabase db = this.getReadableDatabase();
  1157. Cursor cursor = db.rawQuery(selectQuery, null);
  1158. Profile profile = null;
  1159. if (cursor.moveToFirst()) {
  1160. profile = new Profile(cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getInt(5) == 1);
  1161. if (cursor.getInt(6) == 1) {
  1162. profile.mActivated = true;
  1163. }
  1164. profile.mIconName = cursor.getString(5);
  1165. }
  1166. cursor.close();
  1167. db.close();
  1168. // return contact
  1169. return profile;
  1170. }
  1171. public synchronized void deleteProfile(int id) {
  1172. SQLiteDatabase db = this.getReadableDatabase();
  1173. db.delete(ProfileEntry.TABLE_NAME, ProfileEntry.COLUMN_NAME_PROFILE_ID + "=?", new String[] { String.valueOf(id) });
  1174. }
  1175. /**
  1176. * Gets all received {@link Record Records} for every attack identified by
  1177. * its attack id and ordered by date.
  1178. *
  1179. * @return A ArrayList with all received {@link Record Records} for each
  1180. * attack id in the Database.
  1181. */
  1182. public synchronized ArrayList<Record> getAllReceivedRecordsOfEachAttack() {
  1183. ArrayList<Record> recordList = new ArrayList<Record>();
  1184. String selectQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME + " NATURAL JOIN " + AttackEntry.TABLE_NAME + " JOIN " + NetworkEntry.TABLE_NAME + " USING " + "(" + NetworkEntry.COLUMN_NAME_BSSID
  1185. + ")" + " WHERE " + PacketEntry.COLUMN_NAME_TYPE + "='RECEIVE'" + " ORDER BY " + PacketEntry.TABLE_NAME + "." + PacketEntry.COLUMN_NAME_PACKET_TIMESTAMP;
  1186. SQLiteDatabase db = this.getReadableDatabase();
  1187. Cursor cursor = db.rawQuery(selectQuery, null);
  1188. // looping through all rows and adding to list
  1189. if (cursor.moveToFirst()) {
  1190. do {
  1191. Record record = createRecord(cursor);
  1192. // Adding record to list
  1193. recordList.add(record);
  1194. } while (cursor.moveToNext());
  1195. }
  1196. cursor.close();
  1197. // return record list
  1198. db.close();
  1199. return recordList;
  1200. }
  1201. /**
  1202. * Returns PlotComparisionItems for attacks per essid.
  1203. * @param filter (LogFilter) filter object
  1204. * @return ArrayList<PlotComparisonItem>
  1205. */
  1206. public synchronized ArrayList<PlotComparisonItem> attacksPerESSID(LogFilter filter) {
  1207. String filterQuery = this.selectionQueryFromFilter(filter, AttackEntry.COLUMN_NAME_ATTACK_ID);
  1208. String attackPerESSID_Query = "SELECT " + NetworkEntry.COLUMN_NAME_SSID + " , " + "COUNT( " + AttackEntry.COLUMN_NAME_ATTACK_ID + " ) " + " "
  1209. + " FROM " + AttackEntry.TABLE_NAME + " a " + " , " + NetworkEntry.TABLE_NAME
  1210. + " WHERE " + " a." + AttackEntry.COLUMN_NAME_ATTACK_ID + " IN " + " ( " + filterQuery + " ) "
  1211. + " GROUP BY " + NetworkEntry.TABLE_NAME+"."+NetworkEntry.COLUMN_NAME_SSID;
  1212. SQLiteDatabase db = this.getReadableDatabase();
  1213. Cursor cursor = db.rawQuery(attackPerESSID_Query, null);
  1214. ArrayList<PlotComparisonItem> plots = new ArrayList<PlotComparisonItem>();
  1215. int counter = 0;
  1216. if (cursor.moveToFirst()) {
  1217. do {
  1218. String title = cursor.getString(0); // COLUMN_NAME_SSID
  1219. double value = cursor.getDouble(1); // COUNT
  1220. if (value == 0.) continue;
  1221. PlotComparisonItem plotItem = new PlotComparisonItem(title, this.getColor(counter), 0. , value);
  1222. plots.add(plotItem);
  1223. counter++;
  1224. } while (cursor.moveToNext());
  1225. }
  1226. cursor.close();
  1227. db.close();
  1228. return plots;
  1229. }
  1230. /**
  1231. * Attacks per BSSID
  1232. * @param filter (LogFilter) query filter
  1233. * @return ArrayList<PlotComparisonItem>
  1234. */
  1235. public synchronized ArrayList<PlotComparisonItem> attacksPerBSSID(LogFilter filter) {
  1236. String filterQuery = this.selectionQueryFromFilter(filter, AttackEntry.COLUMN_NAME_ATTACK_ID);
  1237. String attackPerBSSID_Query = "SELECT " + NetworkEntry.COLUMN_NAME_BSSID + " , " + "COUNT( " + AttackEntry.COLUMN_NAME_ATTACK_ID + " ) " + " "
  1238. + " FROM " + AttackEntry.TABLE_NAME + " a " + " , " + NetworkEntry.TABLE_NAME
  1239. + " WHERE " + " a." + AttackEntry.COLUMN_NAME_ATTACK_ID + " IN " + " ( " + filterQuery + " ) "
  1240. + " GROUP BY " + NetworkEntry.TABLE_NAME+"."+NetworkEntry.COLUMN_NAME_BSSID;
  1241. SQLiteDatabase db = this.getReadableDatabase();
  1242. Cursor cursor = db.rawQuery(attackPerBSSID_Query, null);
  1243. ArrayList<PlotComparisonItem> plots = new ArrayList<PlotComparisonItem>();
  1244. int counter = 0;
  1245. if (cursor.moveToFirst()) {
  1246. do {
  1247. String title = cursor.getString(0); // COLUMN_NAME_BSSID
  1248. double value = cursor.getDouble(1); // COUNT
  1249. if (value == 0.) continue;
  1250. PlotComparisonItem plotItem = new PlotComparisonItem(title, this.getColor(counter), 0. , value);
  1251. plots.add(plotItem);
  1252. counter++;
  1253. } while (cursor.moveToNext());
  1254. }
  1255. cursor.close();
  1256. db.close();
  1257. return plots;
  1258. }
  1259. /** Returns the color for the given index
  1260. * @return int color*/
  1261. public Integer getColor(int index) {
  1262. return ColorSequenceGenerator.getColorForIndex(index);
  1263. }
  1264. }