UglyDbHelper.java 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. package de.tudarmstadt.informatik.hostage.logging;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import android.content.ContentValues;
  5. import android.content.Context;
  6. import android.database.Cursor;
  7. import android.database.sqlite.SQLiteDatabase;
  8. import android.database.sqlite.SQLiteOpenHelper;
  9. import android.util.Log;
  10. import de.tudarmstadt.informatik.hostage.logging.Record.TYPE;
  11. /**
  12. * This class creates SQL tables and handles all access to the database.<br>
  13. * It contains several methods with predefined queries to extract different
  14. * kinds of information from the database.<br>
  15. * The database contains two tables: {@link #TABLE_RECORDS} and
  16. * {@link #TABLE_BSSIDS}:<br>
  17. * {@link #TABLE_RECORDS} contains all logging information of a single message
  18. * record except the SSID.<br>
  19. * {@link #TABLE_BSSIDS} contains the BSSID of all recorded Networks and the
  20. * corresponding SSID.<br>
  21. *
  22. * @author Lars Pandikow
  23. */
  24. public class UglyDbHelper extends SQLiteOpenHelper {
  25. // All Static variables
  26. // Database Version
  27. private static final int DATABASE_VERSION = 1;
  28. // Database Name
  29. private static final String DATABASE_NAME = "recordManager";
  30. // Contacts table names
  31. private static final String TABLE_ATTACK_INFO = "attack_info";
  32. private static final String TABLE_RECORDS = "records";
  33. private static final String TABLE_BSSIDS = "bssids";
  34. // Contacts Table Columns names
  35. public static final String KEY_ID = "_id";
  36. public static final String KEY_ATTACK_ID = "_attack_id";
  37. public static final String KEY_TYPE = "type";
  38. public static final String KEY_TIME = "timestamp";
  39. public static final String KEY_PACKET = "packet";
  40. public static final String KEY_PROTOCOL = "protocol";
  41. public static final String KEY_EXTERNAL_IP = "externalIP";
  42. public static final String KEY_LOCAL_IP = "localIP";
  43. public static final String KEY_LOCAL_HOSTNAME = "localHostName";
  44. public static final String KEY_LOCAL_PORT = "localPort";
  45. public static final String KEY_REMOTE_IP = "remoteIP";
  46. public static final String KEY_REMOTE_HOSTNAME = "remoteHostName";
  47. public static final String KEY_REMOTE_PORT = "remotePort";
  48. public static final String KEY_BSSID = "_bssid";
  49. public static final String KEY_SSID = "ssid";
  50. public static final String KEY_LATITUDE = "latitude";
  51. public static final String KEY_LONGITUDE = "longitude";
  52. public static final String KEY_ACCURACY = "accuracy";
  53. // Database sql create statements
  54. private static final String CREATE_RECORD_TABLE =
  55. "CREATE TABLE " + TABLE_RECORDS + "("
  56. + KEY_ID + " INTEGER NOT NULL,"
  57. + KEY_ATTACK_ID + " INTEGER NOT NULL,"
  58. + KEY_TYPE + " TEXT,"
  59. + KEY_TIME + " INTEGER,"
  60. + KEY_PACKET + " TEXT,"
  61. + "FOREIGN KEY(" + KEY_ATTACK_ID + ") REFERENCES " + TABLE_ATTACK_INFO + "(" + KEY_ATTACK_ID + "),"
  62. + "PRIMARY KEY(" + KEY_ID + ", " + KEY_ATTACK_ID + ")"
  63. + ")";
  64. private static final String CREATE_ATTACK_INFO_TABLE =
  65. "CREATE TABLE " + TABLE_ATTACK_INFO + "("
  66. + KEY_ATTACK_ID + " INTEGER PRIMARY KEY,"
  67. + KEY_PROTOCOL + " TEXT,"
  68. + KEY_EXTERNAL_IP + " TEXT,"
  69. + KEY_LOCAL_IP + " BLOB,"
  70. + KEY_LOCAL_HOSTNAME + " TEXT,"
  71. + KEY_LOCAL_PORT + " INTEGER,"
  72. + KEY_REMOTE_IP + " BLOB,"
  73. + KEY_REMOTE_HOSTNAME + " TEXT,"
  74. + KEY_REMOTE_PORT + " INTEGER,"
  75. + KEY_BSSID + " TEXT,"
  76. + "FOREIGN KEY(" + KEY_BSSID + ") REFERENCES " + TABLE_BSSIDS + "(" + KEY_BSSID + ")"
  77. + ")";
  78. private static final String CREATE_BSSID_TABLE =
  79. "CREATE TABLE " + TABLE_BSSIDS + "("
  80. + KEY_BSSID + " TEXT PRIMARY KEY,"
  81. + KEY_SSID + " TEXT,"
  82. + KEY_LATITUDE + " INTEGER,"
  83. + KEY_LONGITUDE + " INTEGER,"
  84. + KEY_ACCURACY + " INTEGER,"
  85. + KEY_TIME + " INTEGER"
  86. + ")";
  87. public UglyDbHelper(Context context) {
  88. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  89. }
  90. // Creating Tables
  91. @Override
  92. public void onCreate(SQLiteDatabase db) {
  93. db.execSQL(CREATE_BSSID_TABLE);
  94. db.execSQL(CREATE_ATTACK_INFO_TABLE);
  95. db.execSQL(CREATE_RECORD_TABLE);
  96. }
  97. // Upgrading database
  98. @Override
  99. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  100. // Drop older table if existed
  101. db.execSQL("DROP TABLE IF EXISTS " + TABLE_RECORDS);
  102. db.execSQL("DROP TABLE IF EXISTS " + TABLE_ATTACK_INFO);
  103. db.execSQL("DROP TABLE IF EXISTS " + TABLE_BSSIDS);
  104. // Create tables again
  105. onCreate(db);
  106. }
  107. /**
  108. * Adds a given {@link Record} to the database.
  109. *
  110. * @param record
  111. * The added {@link Record} .
  112. */
  113. public void addRecord(Record record) {
  114. SQLiteDatabase db = this.getWritableDatabase();
  115. HashMap<String, Object> bssidValues = new HashMap<String, Object>();
  116. bssidValues.put(KEY_BSSID, record.getBssid());
  117. bssidValues.put(KEY_SSID, record.getSsid());
  118. bssidValues.put(KEY_LATITUDE, record.getLatitude());
  119. bssidValues.put(KEY_LONGITUDE, record.getLongitude());
  120. bssidValues.put(KEY_ACCURACY, record.getAccuracy());
  121. bssidValues.put(KEY_TIME, record.getTimestampLocation());
  122. ContentValues attackValues = new ContentValues();
  123. attackValues.put(KEY_ATTACK_ID, record.getAttack_id()); // Log Attack ID
  124. attackValues.put(KEY_PROTOCOL, record.getProtocol().toString());
  125. attackValues.put(KEY_EXTERNAL_IP, record.getExternalIP());
  126. attackValues.put(KEY_LOCAL_IP, record.getLocalIP()); // Log Local IP
  127. attackValues.put(KEY_LOCAL_HOSTNAME, record.getLocalHost());
  128. attackValues.put(KEY_LOCAL_PORT, record.getLocalPort());
  129. attackValues.put(KEY_REMOTE_IP, record.getRemoteIP()); // Log Remote IP
  130. attackValues.put(KEY_REMOTE_HOSTNAME, record.getRemoteHost());
  131. attackValues.put(KEY_REMOTE_PORT, record.getRemotePort()); // Log Remote Port
  132. attackValues.put(KEY_BSSID, record.getBssid());
  133. ContentValues recordValues = new ContentValues();
  134. recordValues.put(KEY_ID, record.getId()); // Log Message Number
  135. recordValues.put(KEY_ATTACK_ID, record.getAttack_id()); // Log Attack ID
  136. recordValues.put(KEY_TYPE, record.getType().name()); // Log Type
  137. recordValues.put(KEY_TIME, record.getTimestamp()); // Log Timestamp
  138. recordValues.put(KEY_PACKET, record.getPacket()); // Log Packet
  139. // Inserting Rows
  140. db.insertWithOnConflict(TABLE_ATTACK_INFO, null, attackValues,
  141. SQLiteDatabase.CONFLICT_REPLACE);
  142. db.insert(TABLE_RECORDS, null, recordValues);
  143. db.close(); // Closing database connection
  144. // Update Network Information
  145. updateNetworkInformation(bssidValues);
  146. }
  147. /**
  148. * Creates a {@link Record} from a Cursor. If the cursor does not show to a
  149. * valid data structure a runtime exception is thrown.
  150. *
  151. * @param cursor
  152. * @return Returns the created {@link Record} .
  153. */
  154. private Record createRecord(Cursor cursor) {
  155. Record record = new Record();
  156. record.setId(Integer.parseInt(cursor.getString(0)));
  157. record.setAttack_id(cursor.getLong(1));
  158. record.setType(cursor.getString(2).equals("SEND") ? TYPE.SEND : TYPE.RECEIVE);
  159. record.setTimestamp(cursor.getLong(3));
  160. record.setPacket(cursor.getString(4));
  161. record.setProtocol(cursor.getString(5));
  162. record.setExternalIP(cursor.getString(6));
  163. record.setLocalIP(cursor.getString(7));
  164. record.setLocalHost(cursor.getString(8));
  165. record.setLocalPort(Integer.parseInt(cursor.getString(9)));
  166. record.setRemoteIP(cursor.getString(10));
  167. record.setRemoteHost(cursor.getString(11));
  168. record.setRemotePort(Integer.parseInt(cursor.getString(12)));
  169. record.setBssid(cursor.getString(13));
  170. record.setSsid(cursor.getString(14));
  171. record.setLatitude(Double.parseDouble(cursor.getString(15)));
  172. record.setLongitude(Double.parseDouble(cursor.getString(16)));
  173. record.setAccuracy(Float.parseFloat(cursor.getString(17)));
  174. record.setTimestampLocation(cursor.getLong(18));
  175. return record;
  176. }
  177. /**
  178. * Gets a single {@link Record} with the given ID from the database.
  179. *
  180. * @param id
  181. * The ID of the {@link Record};
  182. * @return The {@link Record}.
  183. */
  184. public Record getRecord(int id) {
  185. String selectQuery = "SELECT * FROM " + TABLE_RECORDS
  186. + " NATURAL JOIN " + TABLE_ATTACK_INFO
  187. + " NATURAL JOIN "+ TABLE_BSSIDS
  188. + " WHERE " + KEY_ID + " = " + id;
  189. SQLiteDatabase db = this.getReadableDatabase();
  190. Cursor cursor = db.rawQuery(selectQuery, null);
  191. Record record = null;
  192. if (cursor.moveToFirst()) {
  193. record = createRecord(cursor);
  194. }
  195. cursor.close();
  196. db.close();
  197. // return contact
  198. return record;
  199. }
  200. /**
  201. * Gets all {@link Record Records} saved in the database.
  202. *
  203. * @return A ArrayList of all the {@link Record Records} in the Database.
  204. */
  205. public ArrayList<Record> getAllRecords() {
  206. ArrayList<Record> recordList = new ArrayList<Record>();
  207. // Select All Query
  208. String selectQuery = "SELECT * FROM " + TABLE_RECORDS
  209. + " NATURAL JOIN " + TABLE_ATTACK_INFO
  210. + " NATURAL JOIN " + TABLE_BSSIDS;
  211. SQLiteDatabase db = this.getWritableDatabase();
  212. Cursor cursor = db.rawQuery(selectQuery, null);
  213. Log.i("Database", "Start loop");
  214. // looping through all rows and adding to list
  215. if (cursor.moveToFirst()) {
  216. do {
  217. Log.i("Database", "Add Record");
  218. Record record = createRecord(cursor);
  219. // Adding record to list
  220. recordList.add(record);
  221. } while (cursor.moveToNext());
  222. }
  223. cursor.close();
  224. db.close();
  225. // return record list
  226. return recordList;
  227. }
  228. /**
  229. * Gets a single {@link Record} with the given attack id from the database.
  230. *
  231. * @param attack_id
  232. * The attack id of the {@link Record};
  233. * @return The {@link Record}.
  234. */
  235. public Record getRecordOfAttackId(long attack_id) {
  236. String selectQuery = "SELECT * FROM " + TABLE_RECORDS
  237. + " NATURAL JOIN " + TABLE_ATTACK_INFO
  238. + " NATURAL JOIN " + TABLE_BSSIDS
  239. + " WHERE " + KEY_ATTACK_ID + " = " + attack_id
  240. + " GROUP BY " + KEY_ATTACK_ID;
  241. SQLiteDatabase db = this.getReadableDatabase();
  242. Cursor cursor = db.rawQuery(selectQuery, null);
  243. Record record = null;
  244. if (cursor.moveToFirst()) {
  245. record = createRecord(cursor);
  246. }
  247. cursor.close();
  248. // return record list
  249. db.close();
  250. return record;
  251. }
  252. /**
  253. * Gets all received {@link Record Records} for every attack identified by
  254. * its attack id and ordered by date.
  255. *
  256. * @return A ArrayList with all received {@link Record Records} for each
  257. * attack id in the Database.
  258. */
  259. public ArrayList<Record> getAllReceivedRecordsOfEachAttack() {
  260. ArrayList<Record> recordList = new ArrayList<Record>();
  261. String selectQuery = "SELECT * FROM " + TABLE_RECORDS
  262. + " NATURAL JOIN " + TABLE_ATTACK_INFO
  263. + " NATURAL JOIN " + TABLE_BSSIDS
  264. + " WHERE " + KEY_TYPE + "='RECEIVE'"
  265. + " ORDER BY " + KEY_TIME;
  266. SQLiteDatabase db = this.getReadableDatabase();
  267. Cursor cursor = db.rawQuery(selectQuery, null);
  268. // looping through all rows and adding to list
  269. if (cursor.moveToFirst()) {
  270. do {
  271. Record record = createRecord(cursor);
  272. // Adding record to list
  273. recordList.add(record);
  274. } while (cursor.moveToNext());
  275. }
  276. cursor.close();
  277. // return record list
  278. db.close();
  279. return recordList;
  280. }
  281. /**
  282. * Gets a representative {@link Record} for every attack identified by its
  283. * attack id.
  284. *
  285. * @return A ArrayList with one {@link Record Records} for each attack id in
  286. * the Database.
  287. */
  288. public ArrayList<Record> getRecordOfEachAttack() {
  289. ArrayList<Record> recordList = new ArrayList<Record>();
  290. String selectQuery = "SELECT * FROM " + TABLE_RECORDS
  291. + " NATURAL JOIN " + TABLE_ATTACK_INFO
  292. + " NATURAL JOIN " + TABLE_BSSIDS
  293. + " GROUP BY " + KEY_ATTACK_ID;
  294. SQLiteDatabase db = this.getReadableDatabase();
  295. Cursor cursor = db.rawQuery(selectQuery, null);
  296. // looping through all rows and adding to list
  297. if (cursor.moveToFirst()) {
  298. do {
  299. Record record = createRecord(cursor);
  300. // Adding record to list
  301. recordList.add(record);
  302. } while (cursor.moveToNext());
  303. }
  304. cursor.close();
  305. // return record list
  306. db.close();
  307. return recordList;
  308. }
  309. /**
  310. * Gets a representative {@link Record} for every attack with a higher
  311. * attack id than the specified.
  312. *
  313. * @param attack_id
  314. * The attack id to match the query against.
  315. * @return A ArrayList with one {@link Record Records} for each attack id
  316. * higher than the given.
  317. */
  318. public ArrayList<Record> getRecordOfEachAttack(long attack_id) {
  319. ArrayList<Record> recordList = new ArrayList<Record>();
  320. String selectQuery = "SELECT * FROM " + TABLE_RECORDS
  321. + " NATURAL JOIN " + TABLE_ATTACK_INFO
  322. + " NATURAL JOIN " + TABLE_BSSIDS
  323. + " WHERE " + KEY_ATTACK_ID + " > " + attack_id
  324. + " GROUP BY " + KEY_ATTACK_ID;
  325. SQLiteDatabase db = this.getReadableDatabase();
  326. Cursor cursor = db.rawQuery(selectQuery, null);
  327. // looping through all rows and adding to list
  328. if (cursor.moveToFirst()) {
  329. do {
  330. Record record = createRecord(cursor);
  331. // Adding record to list
  332. recordList.add(record);
  333. } while (cursor.moveToNext());
  334. }
  335. cursor.close();
  336. // return count
  337. db.close();
  338. return recordList;
  339. }
  340. /**
  341. * Determines the number of {@link Record Records} in the database.
  342. *
  343. * @return The number of {@link Record Records} in the database.
  344. */
  345. public int getRecordCount() {
  346. String countQuery = "SELECT * FROM " + TABLE_RECORDS;
  347. SQLiteDatabase db = this.getReadableDatabase();
  348. Cursor cursor = db.rawQuery(countQuery, null);
  349. int result = cursor.getCount();
  350. cursor.close();
  351. // return count
  352. db.close();
  353. return result;
  354. }
  355. /**
  356. * Determines the number of different attack_ids in the database.
  357. *
  358. * @return The number of different attack_ids in the database.
  359. */
  360. public int getAttackCount() {
  361. String countQuery = "SELECT * FROM " + TABLE_ATTACK_INFO;
  362. SQLiteDatabase db = this.getReadableDatabase();
  363. Cursor cursor = db.rawQuery(countQuery, null);
  364. int result = cursor.getCount();
  365. cursor.close();
  366. // return count
  367. db.close();
  368. return result;
  369. }
  370. /**
  371. * Determines the number of different attack_ids for a specific protocol in
  372. * the database.
  373. *
  374. * @param protocol
  375. * The String representation of the
  376. * {@link de.tudarmstadt.informatik.hostage.protocol.Protocol
  377. * Protocol}
  378. * @return The number of different attack_ids in the database.
  379. */
  380. public int getAttackPerProtocolCount(String protocol) {
  381. String countQuery = "SELECT * FROM " + TABLE_ATTACK_INFO + " WHERE "
  382. + KEY_PROTOCOL + " = " + "'" + protocol + "'";
  383. SQLiteDatabase db = this.getReadableDatabase();
  384. Cursor cursor = db.rawQuery(countQuery, null);
  385. int result = cursor.getCount();
  386. cursor.close();
  387. // return count
  388. db.close();
  389. return result;
  390. }
  391. /**
  392. * Determines the smallest attack id stored in the database.
  393. *
  394. * @return The smallest attack id stored in the database.
  395. */
  396. public long getSmallestAttackId() {
  397. String selectQuery = "SELECT MIN(" + KEY_ATTACK_ID + ") FROM "
  398. + TABLE_ATTACK_INFO;
  399. SQLiteDatabase db = this.getReadableDatabase();
  400. Cursor cursor = db.rawQuery(selectQuery, null);
  401. int result;
  402. if (cursor.moveToFirst()) {
  403. result = cursor.getInt(0);
  404. } else {
  405. result = -1;
  406. }
  407. cursor.close();
  408. db.close();
  409. return result;
  410. }
  411. /**
  412. * Determines the highest attack id stored in the database.
  413. *
  414. * @return The highest attack id stored in the database.
  415. */
  416. public long getHighestAttackId() {
  417. String selectQuery = "SELECT MAX(" + KEY_ATTACK_ID + ") FROM "
  418. + TABLE_ATTACK_INFO;
  419. SQLiteDatabase db = this.getReadableDatabase();
  420. Cursor cursor = db.rawQuery(selectQuery, null);
  421. int result;
  422. if (cursor.moveToFirst()) {
  423. result = cursor.getInt(0);
  424. } else {
  425. result = -1;
  426. }
  427. cursor.close();
  428. db.close();
  429. return result;
  430. }
  431. /**
  432. * Determines if a network with given BSSID has already been recorded as
  433. * malicious.
  434. *
  435. * @param BSSID
  436. * The BSSID of the network.
  437. * @return True if an attack has been recorded in a network with the given
  438. * BSSID, else false.
  439. */
  440. public boolean bssidSeen(String BSSID) {
  441. String countQuery = "SELECT * FROM " + TABLE_BSSIDS + " WHERE "
  442. + KEY_BSSID + " = " + "'" + BSSID + "'";
  443. SQLiteDatabase db = this.getReadableDatabase();
  444. Cursor cursor = db.rawQuery(countQuery, null);
  445. int result = cursor.getCount();
  446. cursor.close();
  447. db.close();
  448. return result > 0;
  449. }
  450. /**
  451. * Determines if an attack has been recorded on a specific protocol in a
  452. * network with a given BSSID.
  453. *
  454. * @param protocol
  455. * The
  456. * {@link de.tudarmstadt.informatik.hostage.protocol.Protocol
  457. * Protocol} to inspect.
  458. * @param BSSID
  459. * The BSSID of the network.
  460. * @return True if an attack on the given protocol has been recorded in a
  461. * network with the given BSSID, else false.
  462. */
  463. public boolean bssidSeen(String protocol, String BSSID) {
  464. String countQuery = "SELECT * FROM " + TABLE_ATTACK_INFO
  465. + " NATURAL JOIN " + TABLE_BSSIDS + " WHERE " + KEY_PROTOCOL
  466. + " = " + "'" + protocol + "'" + " AND " + KEY_BSSID + " = "
  467. + "'" + BSSID + "'";
  468. SQLiteDatabase db = this.getReadableDatabase();
  469. Cursor cursor = db.rawQuery(countQuery, null);
  470. int result = cursor.getCount();
  471. cursor.close();
  472. db.close();
  473. return result > 0;
  474. }
  475. /**
  476. * Returns a String array with all BSSIDs stored in the database.
  477. *
  478. * @return String[] of all recorded BSSIDs.
  479. */
  480. public String[] getAllBSSIDS() {
  481. String selectQuery = "SELECT * FROM " + TABLE_BSSIDS;
  482. SQLiteDatabase db = this.getReadableDatabase();
  483. Cursor cursor = db.rawQuery(selectQuery, null);
  484. String[] bssidList = new String[cursor.getCount()];
  485. int counter = 0;
  486. // looping through all rows and adding to list
  487. if (cursor.moveToFirst()) {
  488. do {
  489. bssidList[counter] = cursor.getString(0);
  490. counter++;
  491. } while (cursor.moveToNext());
  492. }
  493. cursor.close();
  494. db.close();
  495. return bssidList;
  496. }
  497. /**
  498. * Gets the last recorded SSID to a given BSSID.
  499. *
  500. * @param bssid
  501. * The BSSID to match against.
  502. * @return A String of the last SSID or null if the BSSID is not in the
  503. * database.
  504. */
  505. public String getSSID(String bssid) {
  506. String selectQuery = "SELECT " + KEY_SSID + " FROM " + TABLE_BSSIDS
  507. + " WHERE " + KEY_BSSID + " = " + "'" + bssid + "'";
  508. SQLiteDatabase db = this.getReadableDatabase();
  509. Cursor cursor = db.rawQuery(selectQuery, null);
  510. String ssid = null;
  511. if (cursor.moveToFirst()) {
  512. ssid = cursor.getString(0);
  513. }
  514. cursor.close();
  515. db.close();
  516. return ssid;
  517. }
  518. /**
  519. * Deletes all records from {@link #TABLE_RECORDS} with a specific BSSID.
  520. *
  521. * @param bssid
  522. * The BSSID to match against.
  523. */
  524. public void deleteByBSSID(String bssid) {
  525. SQLiteDatabase db = this.getReadableDatabase();
  526. db.delete(TABLE_RECORDS, KEY_BSSID + " = ?", new String[] { bssid });
  527. db.delete(TABLE_ATTACK_INFO, KEY_BSSID + " = ?", new String[] { bssid });
  528. db.close();
  529. }
  530. // TODO Delete statement �berarbeiten
  531. /**
  532. * Deletes all records from {@link #TABLE_RECORDS} with a time stamp smaller
  533. * then the given
  534. *
  535. * @param date
  536. * A Date represented in milliseconds.
  537. */
  538. public void deleteByDate(long date) {
  539. SQLiteDatabase db = this.getReadableDatabase();
  540. String deleteQuery = "DELETE FROM " + TABLE_RECORDS + " WHERE "
  541. + KEY_TIME + " < " + date;
  542. // TODO Delete statement �berarbeiten
  543. // String deleteQuery2 = "DELETE "
  544. db.execSQL(deleteQuery);
  545. db.close();
  546. }
  547. /**
  548. * Deletes all records from {@link #TABLE_RECORDS}.
  549. */
  550. public void clearData() {
  551. SQLiteDatabase db = this.getReadableDatabase();
  552. db.delete(TABLE_RECORDS, null, null);
  553. db.delete(TABLE_ATTACK_INFO, null, null);
  554. db.close();
  555. }
  556. public ArrayList<HashMap<String, Object>> getNetworkInformation() {
  557. String selectQuery = "SELECT * FROM " + TABLE_BSSIDS;
  558. SQLiteDatabase db = this.getReadableDatabase();
  559. Cursor cursor = db.rawQuery(selectQuery, null);
  560. ArrayList<HashMap<String, Object>> networkInformation = new ArrayList<HashMap<String, Object>>();
  561. // looping through all rows and adding to list
  562. if (cursor.moveToFirst()) {
  563. do {
  564. HashMap<String, Object> values = new HashMap<String, Object>();
  565. values.put(KEY_BSSID, cursor.getString(0));
  566. values.put(KEY_SSID, cursor.getString(1));
  567. values.put(KEY_LATITUDE,
  568. Double.parseDouble(cursor.getString(2)));
  569. values.put(KEY_LONGITUDE,
  570. Double.parseDouble(cursor.getString(3)));
  571. values.put(KEY_ACCURACY, Float.parseFloat(cursor.getString(4)));
  572. values.put(KEY_TIME, cursor.getLong(5));
  573. networkInformation.add(values);
  574. } while (cursor.moveToNext());
  575. }
  576. cursor.close();
  577. db.close();
  578. return networkInformation;
  579. }
  580. public void updateNetworkInformation(
  581. HashMap<String, Object> networkInformation) {
  582. SQLiteDatabase db = this.getReadableDatabase();
  583. String bssid = (String) networkInformation.get(KEY_BSSID);
  584. String bssidQuery = "SELECT * FROM " + TABLE_BSSIDS + " WHERE "
  585. + KEY_BSSID + " = " + "'" + bssid + "'";
  586. Cursor cursor = db.rawQuery(bssidQuery, null);
  587. int result = cursor.getCount();
  588. if (cursor != null
  589. && cursor.moveToFirst()
  590. && (result <= 0 || cursor.getLong(5) < (Long) networkInformation
  591. .get(KEY_TIME)))
  592. ;
  593. {
  594. ContentValues bssidValues = new ContentValues();
  595. bssidValues.put(KEY_BSSID, bssid);
  596. bssidValues
  597. .put(KEY_SSID, (String) networkInformation.get(KEY_SSID));
  598. bssidValues.put(KEY_LATITUDE,
  599. (double) (Double) networkInformation.get(KEY_LATITUDE));
  600. bssidValues.put(KEY_LONGITUDE,
  601. (double) (Double) networkInformation.get(KEY_LONGITUDE));
  602. bssidValues.put(KEY_ACCURACY,
  603. (float) (Float) networkInformation.get(KEY_ACCURACY));
  604. bssidValues.put(KEY_TIME, (Long) networkInformation.get(KEY_TIME));
  605. db.insertWithOnConflict(TABLE_BSSIDS, null, bssidValues,
  606. SQLiteDatabase.CONFLICT_REPLACE);
  607. }
  608. cursor.close();
  609. db.close();
  610. }
  611. public void updateNetworkInformation(
  612. ArrayList<HashMap<String, Object>> networkInformation) {
  613. Log.i("DatabaseHandler", "Starte updating");
  614. for (HashMap<String, Object> values : networkInformation) {
  615. updateNetworkInformation(values);
  616. }
  617. }
  618. }