Logger.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. package de.tudarmstadt.informatik.hostage.logging;
  2. import java.util.ArrayList;
  3. import android.app.IntentService;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.os.Bundle;
  7. import android.os.ResultReceiver;
  8. /**
  9. * An {@link IntentService} subclass for handling asynchronous task requests in
  10. * a service on a separate handler thread.
  11. *
  12. * @author Mihai Plasoianu
  13. */
  14. public class Logger extends IntentService {
  15. private static final String ACTION_LOG = "de.tudarmstadt.informatik.hostage.action.LOG";
  16. private static final String ACTION_GET_RECORD_ALL = "de.tudarmstadt.informatik.hostage.action.GET_RECORD_ALL";
  17. private static final String ACTION_GET_RECORD_EACH = "de.tudarmstadt.informatik.hostage.action.GET_RECORD_EACH";
  18. private static final String ACTION_GET_RECORD_ID = "de.tudarmstadt.informatik.hostage.action.GET_RECORD_ID";
  19. private static final String ACTION_GET_COUNT_ALL = "de.tudarmstadt.informatik.hostage.action.GET_COUNT_ALL";
  20. private static final String ACTION_GET_COUNT_PROTOCOL = "de.tudarmstadt.informatik.hostage.action.GET_COUNT_PROTOCOL";
  21. private static final String ACTION_GET_ATTACK_MIN = "de.tudarmstadt.informatik.hostage.action.GET_ATTACK_MIN";
  22. private static final String ACTION_GET_ATTACK_MAX = "de.tudarmstadt.informatik.hostage.action.GET_ATTACK_MAX";
  23. private static final String ACTION_IS_BSSID_SEEN = "de.tudarmstadt.informatik.hostage.action.IS_BSSID_SEEN";
  24. private static final String ACTION_GET_BSSID_ALL = "de.tudarmstadt.informatik.hostage.action.GET_BSSID_ALL";
  25. private static final String ACTION_GET_SSID_BSSID = "de.tudarmstadt.informatik.hostage.action.GET_SSID_BSSID";
  26. private static final String ACTION_CLEAR_DATE = "de.tudarmstadt.informatik.hostage.action.CLEAR_DATE";
  27. private static final String ACTION_CLEAR_BSSID = "de.tudarmstadt.informatik.hostage.action.CLEAR_BSSID";
  28. private static final String ACTION_CLEAR_ALL = "de.tudarmstadt.informatik.hostage.action.CLEAR_ALL";
  29. private static final String EXTRA_RECORD = "de.tudarmstadt.informatik.hostage.extra.RECORD";
  30. private static final String EXTRA_PROTOCOL = "de.tudarmstadt.informatik.hostage.extra.PROTOCOL";
  31. private static final String EXTRA_BSSID = "de.tudarmstadt.informatik.hostage.extra.BSSID";
  32. private static final String EXTRA_PRIMITIVE = "de.tudarmstadt.informatik.hostage.extra.PRIMITIVE";
  33. private static final String RESULT_RECEIVER = "de.tudarmstadt.informatik.hostage.RESULT_RECEIVER";
  34. public static void deleteAll(Context context) {
  35. Intent intent = new Intent(context, Logger.class);
  36. intent.setAction(ACTION_CLEAR_ALL);
  37. context.startService(intent);
  38. }
  39. public static void deleteByBssid(Context context, String bssid) {
  40. Intent intent = new Intent(context, Logger.class);
  41. intent.setAction(ACTION_CLEAR_BSSID);
  42. intent.putExtra(EXTRA_BSSID, bssid);
  43. context.startService(intent);
  44. }
  45. public static void deleteByDate(Context context, long time) {
  46. Intent intent = new Intent(context, Logger.class);
  47. intent.setAction(ACTION_CLEAR_DATE);
  48. intent.putExtra(EXTRA_PRIMITIVE, time);
  49. context.startService(intent);
  50. }
  51. public static void getAllBssids(Context context, ResultReceiver receiver) {
  52. Intent intent = new Intent(context, Logger.class);
  53. intent.setAction(ACTION_GET_BSSID_ALL);
  54. intent.putExtra(RESULT_RECEIVER, receiver);
  55. context.startService(intent);
  56. }
  57. public static void getAllRecords(Context context, ResultReceiver receiver) {
  58. Intent intent = new Intent(context, Logger.class);
  59. intent.setAction(ACTION_GET_RECORD_ALL);
  60. intent.putExtra(RESULT_RECEIVER, receiver);
  61. context.startService(intent);
  62. }
  63. public static void getAttackCount(Context context, ResultReceiver receiver) {
  64. Intent intent = new Intent(context, Logger.class);
  65. intent.setAction(ACTION_GET_COUNT_ALL);
  66. intent.putExtra(RESULT_RECEIVER, receiver);
  67. context.startService(intent);
  68. }
  69. public static void getAttackPerProtocolCount(Context context,
  70. String protocol, ResultReceiver receiver) {
  71. Intent intent = new Intent(context, Logger.class);
  72. intent.setAction(ACTION_GET_COUNT_PROTOCOL);
  73. intent.putExtra(EXTRA_PROTOCOL, protocol);
  74. intent.putExtra(RESULT_RECEIVER, receiver);
  75. context.startService(intent);
  76. }
  77. public static void getMaxAttackId(Context context, ResultReceiver receiver) {
  78. Intent intent = new Intent(context, Logger.class);
  79. intent.setAction(ACTION_GET_ATTACK_MAX);
  80. intent.putExtra(RESULT_RECEIVER, receiver);
  81. context.startService(intent);
  82. }
  83. public static void getMinAttackId(Context context, ResultReceiver receiver) {
  84. Intent intent = new Intent(context, Logger.class);
  85. intent.setAction(ACTION_GET_ATTACK_MIN);
  86. intent.putExtra(RESULT_RECEIVER, receiver);
  87. context.startService(intent);
  88. }
  89. public static void getRecordOfAttackId(Context context, long attack_id,
  90. ResultReceiver receiver) {
  91. Intent intent = new Intent(context, Logger.class);
  92. intent.setAction(ACTION_GET_RECORD_ID);
  93. intent.putExtra(EXTRA_PRIMITIVE, attack_id);
  94. intent.putExtra(RESULT_RECEIVER, receiver);
  95. context.startService(intent);
  96. }
  97. public static void getRecordOfEachAttack(Context context,
  98. int lastUploadedAttackId, ResultReceiver receiver) {
  99. Intent intent = new Intent(context, Logger.class);
  100. intent.setAction(ACTION_GET_RECORD_EACH);
  101. intent.putExtra(EXTRA_PRIMITIVE, lastUploadedAttackId);
  102. intent.putExtra(RESULT_RECEIVER, receiver);
  103. context.startService(intent);
  104. }
  105. public static void getSsid(Context context, String bssid,
  106. ResultReceiver receiver) {
  107. Intent intent = new Intent(context, Logger.class);
  108. intent.setAction(ACTION_GET_SSID_BSSID);
  109. intent.putExtra(EXTRA_BSSID, bssid);
  110. intent.putExtra(RESULT_RECEIVER, receiver);
  111. context.startService(intent);
  112. }
  113. public static void isBssidSeen(Context context, String protocol,
  114. String bssid, ResultReceiver receiver) {
  115. Intent intent = new Intent(context, Logger.class);
  116. intent.setAction(ACTION_IS_BSSID_SEEN);
  117. intent.putExtra(EXTRA_PROTOCOL, protocol);
  118. intent.putExtra(EXTRA_BSSID, bssid);
  119. intent.putExtra(RESULT_RECEIVER, receiver);
  120. context.startService(intent);
  121. }
  122. public static void log(Context context, Record record) {
  123. Intent intent = new Intent(context, Logger.class);
  124. intent.setAction(ACTION_LOG);
  125. intent.putExtra(EXTRA_RECORD, record);
  126. context.startService(intent);
  127. }
  128. private UglyDbHelper mDbHelper;
  129. public Logger() {
  130. super("Logger");
  131. }
  132. @Override
  133. public void onCreate() {
  134. super.onCreate();
  135. mDbHelper = new UglyDbHelper(getApplicationContext());
  136. }
  137. private boolean handleActionBssidSeen(String protocol, String bssid) {
  138. return mDbHelper.bssidSeen(protocol, bssid);
  139. }
  140. /**
  141. * Delete all records.
  142. */
  143. private void handleActionDeleteAll() {
  144. mDbHelper.clearData();
  145. }
  146. private void handleActionDeleteByBssid(String bssid) {
  147. mDbHelper.deleteByBSSID(bssid);
  148. }
  149. private void handleActionDeleteByDate(long time) {
  150. mDbHelper.deleteByDate(time);
  151. }
  152. private String[] handleActionGetAllBssids() {
  153. return mDbHelper.getAllBSSIDS();
  154. }
  155. private ArrayList<Record> handleActionGetAllRecords() {
  156. return mDbHelper.getAllRecords();
  157. }
  158. private int handleActionGetAttackCount() {
  159. return mDbHelper.getAttackCount();
  160. }
  161. private int handleActionGetAttackPerProtocolCount(String protocol) {
  162. return mDbHelper.getAttackPerProtocolCount(protocol);
  163. }
  164. private long handleActionGetMaxAttackId() {
  165. return mDbHelper.getHighestAttackId();
  166. }
  167. private long handleActionGetMinAttackId() {
  168. return mDbHelper.getSmallestAttackId();
  169. }
  170. private Record handleActionGetRecordOfAttackId(long attack_id) {
  171. return mDbHelper.getRecordOfAttackId(attack_id);
  172. }
  173. private ArrayList<Record> handleActionGetRecordOfEachAttack(
  174. int lastUploadedAttackId) {
  175. return mDbHelper.getRecordOfEachAttack(lastUploadedAttackId);
  176. }
  177. private String handleActionGetSsid(String bssid) {
  178. return mDbHelper.getSSID(bssid);
  179. }
  180. /**
  181. * Log a record.
  182. */
  183. private void handleActionLog(Record record) {
  184. mDbHelper.addRecord(record);
  185. }
  186. @Override
  187. protected void onHandleIntent(Intent intent) {
  188. if (intent != null) {
  189. final String action = intent.getAction();
  190. if (ACTION_LOG.equals(action)) {
  191. final Record record = intent.getParcelableExtra(EXTRA_RECORD);
  192. handleActionLog(record);
  193. } else if (ACTION_GET_RECORD_ALL.equals(action)) {
  194. ResultReceiver receiver = intent
  195. .getParcelableExtra(RESULT_RECEIVER);
  196. ArrayList<Record> r = handleActionGetAllRecords();
  197. Bundle result = new Bundle();
  198. result.putParcelableArrayList("result", r);
  199. receiver.send(0, result);
  200. } else if (ACTION_GET_RECORD_EACH.equals(action)) {
  201. final int lastUploadedAttackId = intent.getIntExtra(
  202. EXTRA_PRIMITIVE, -1);
  203. ResultReceiver receiver = intent
  204. .getParcelableExtra(RESULT_RECEIVER);
  205. ArrayList<Record> r = handleActionGetRecordOfEachAttack(lastUploadedAttackId);
  206. Bundle result = new Bundle();
  207. result.putParcelableArrayList("result", r);
  208. receiver.send(0, result);
  209. } else if (ACTION_GET_RECORD_ID.equals(action)) {
  210. final int attack_id = intent.getIntExtra(EXTRA_PRIMITIVE, -1);
  211. ResultReceiver receiver = intent
  212. .getParcelableExtra(RESULT_RECEIVER);
  213. Record r = handleActionGetRecordOfAttackId(attack_id);
  214. Bundle result = new Bundle();
  215. result.putParcelable("result", r);
  216. receiver.send(0, result);
  217. } else if (ACTION_GET_COUNT_ALL.equals(action)) {
  218. ResultReceiver receiver = intent
  219. .getParcelableExtra(RESULT_RECEIVER);
  220. int r = handleActionGetAttackCount();
  221. Bundle result = new Bundle();
  222. result.putInt("result", r);
  223. receiver.send(0, result);
  224. } else if (ACTION_GET_COUNT_PROTOCOL.equals(action)) {
  225. final String protocol = intent.getStringExtra(EXTRA_PROTOCOL);
  226. ResultReceiver receiver = intent
  227. .getParcelableExtra(RESULT_RECEIVER);
  228. int r = handleActionGetAttackPerProtocolCount(protocol);
  229. Bundle result = new Bundle();
  230. result.putInt("result", r);
  231. receiver.send(0, result);
  232. } else if (ACTION_GET_ATTACK_MIN.equals(action)) {
  233. ResultReceiver receiver = intent
  234. .getParcelableExtra(RESULT_RECEIVER);
  235. long r = handleActionGetMinAttackId();
  236. Bundle result = new Bundle();
  237. result.putLong("result", r);
  238. receiver.send(0, result);
  239. handleActionGetMinAttackId();
  240. } else if (ACTION_GET_ATTACK_MAX.equals(action)) {
  241. ResultReceiver receiver = intent
  242. .getParcelableExtra(RESULT_RECEIVER);
  243. long r = handleActionGetMaxAttackId();
  244. Bundle result = new Bundle();
  245. result.putLong("result", r);
  246. receiver.send(0, result);
  247. } else if (ACTION_IS_BSSID_SEEN.equals(action)) {
  248. final String protocol = intent.getStringExtra(EXTRA_PROTOCOL);
  249. final String bssid = intent.getStringExtra(EXTRA_BSSID);
  250. ResultReceiver receiver = intent
  251. .getParcelableExtra(RESULT_RECEIVER);
  252. boolean r = handleActionBssidSeen(protocol, bssid);
  253. Bundle result = new Bundle();
  254. result.putBoolean("result", r);
  255. receiver.send(0, result);
  256. } else if (ACTION_GET_BSSID_ALL.equals(action)) {
  257. ResultReceiver receiver = intent
  258. .getParcelableExtra(RESULT_RECEIVER);
  259. String[] r = handleActionGetAllBssids();
  260. Bundle result = new Bundle();
  261. result.putStringArray("result", r);
  262. receiver.send(0, result);
  263. } else if (ACTION_GET_SSID_BSSID.equals(action)) {
  264. final String bssid = intent.getStringExtra(EXTRA_BSSID);
  265. ResultReceiver receiver = intent
  266. .getParcelableExtra(RESULT_RECEIVER);
  267. String r = handleActionGetSsid(bssid);
  268. Bundle result = new Bundle();
  269. result.putString("result", r);
  270. receiver.send(0, result);
  271. } else if (ACTION_CLEAR_DATE.equals(action)) {
  272. final long time = intent.getLongExtra(EXTRA_PRIMITIVE, -1L);
  273. handleActionDeleteByDate(time);
  274. } else if (ACTION_CLEAR_BSSID.equals(action)) {
  275. final String bssid = intent.getStringExtra(EXTRA_BSSID);
  276. handleActionDeleteByBssid(bssid);
  277. } else if (ACTION_CLEAR_ALL.equals(action)) {
  278. handleActionDeleteAll();
  279. }
  280. }
  281. }
  282. }