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 log(Context context, Record record) {
  35. Intent intent = new Intent(context, Logger.class);
  36. intent.setAction(ACTION_LOG);
  37. intent.putExtra(EXTRA_RECORD, record);
  38. context.startService(intent);
  39. }
  40. public static void getAllRecords(Context context, ResultReceiver receiver) {
  41. Intent intent = new Intent(context, Logger.class);
  42. intent.setAction(ACTION_GET_RECORD_ALL);
  43. intent.putExtra(RESULT_RECEIVER, receiver);
  44. context.startService(intent);
  45. }
  46. public static void getRecordOfEachAttack(Context context,
  47. int lastUploadedAttackId, ResultReceiver receiver) {
  48. Intent intent = new Intent(context, Logger.class);
  49. intent.setAction(ACTION_GET_RECORD_EACH);
  50. intent.putExtra(EXTRA_PRIMITIVE, lastUploadedAttackId);
  51. intent.putExtra(RESULT_RECEIVER, receiver);
  52. context.startService(intent);
  53. }
  54. public static void getRecordOfAttackId(Context context, long attack_id,
  55. ResultReceiver receiver) {
  56. Intent intent = new Intent(context, Logger.class);
  57. intent.setAction(ACTION_GET_RECORD_ID);
  58. intent.putExtra(EXTRA_PRIMITIVE, attack_id);
  59. intent.putExtra(RESULT_RECEIVER, receiver);
  60. context.startService(intent);
  61. }
  62. public static void getAttackCount(Context context, ResultReceiver receiver) {
  63. Intent intent = new Intent(context, Logger.class);
  64. intent.setAction(ACTION_GET_COUNT_ALL);
  65. intent.putExtra(RESULT_RECEIVER, receiver);
  66. context.startService(intent);
  67. }
  68. public static void getAttackPerProtocolCount(Context context,
  69. String protocol, ResultReceiver receiver) {
  70. Intent intent = new Intent(context, Logger.class);
  71. intent.setAction(ACTION_GET_COUNT_PROTOCOL);
  72. intent.putExtra(EXTRA_PROTOCOL, protocol);
  73. intent.putExtra(RESULT_RECEIVER, receiver);
  74. context.startService(intent);
  75. }
  76. public static void getMinAttackId(Context context, ResultReceiver receiver) {
  77. Intent intent = new Intent(context, Logger.class);
  78. intent.setAction(ACTION_GET_ATTACK_MIN);
  79. intent.putExtra(RESULT_RECEIVER, receiver);
  80. context.startService(intent);
  81. }
  82. public static void getMaxAttackId(Context context, ResultReceiver receiver) {
  83. Intent intent = new Intent(context, Logger.class);
  84. intent.setAction(ACTION_GET_ATTACK_MAX);
  85. intent.putExtra(RESULT_RECEIVER, receiver);
  86. context.startService(intent);
  87. }
  88. public static void isBssidSeen(Context context, String protocol,
  89. String bssid, ResultReceiver receiver) {
  90. Intent intent = new Intent(context, Logger.class);
  91. intent.setAction(ACTION_IS_BSSID_SEEN);
  92. intent.putExtra(EXTRA_PROTOCOL, protocol);
  93. intent.putExtra(EXTRA_BSSID, bssid);
  94. intent.putExtra(RESULT_RECEIVER, receiver);
  95. context.startService(intent);
  96. }
  97. public static void getAllBssids(Context context, ResultReceiver receiver) {
  98. Intent intent = new Intent(context, Logger.class);
  99. intent.setAction(ACTION_GET_BSSID_ALL);
  100. intent.putExtra(RESULT_RECEIVER, receiver);
  101. context.startService(intent);
  102. }
  103. public static void getSsid(Context context, String bssid,
  104. ResultReceiver receiver) {
  105. Intent intent = new Intent(context, Logger.class);
  106. intent.setAction(ACTION_GET_SSID_BSSID);
  107. intent.putExtra(EXTRA_BSSID, bssid);
  108. intent.putExtra(RESULT_RECEIVER, receiver);
  109. context.startService(intent);
  110. }
  111. public static void deleteByDate(Context context, long time) {
  112. Intent intent = new Intent(context, Logger.class);
  113. intent.setAction(ACTION_CLEAR_DATE);
  114. intent.putExtra(EXTRA_PRIMITIVE, time);
  115. context.startService(intent);
  116. }
  117. public static void deleteByBssid(Context context, String bssid) {
  118. Intent intent = new Intent(context, Logger.class);
  119. intent.setAction(ACTION_CLEAR_BSSID);
  120. intent.putExtra(EXTRA_BSSID, bssid);
  121. context.startService(intent);
  122. }
  123. public static void deleteAll(Context context) {
  124. Intent intent = new Intent(context, Logger.class);
  125. intent.setAction(ACTION_CLEAR_ALL);
  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. @Override
  138. protected void onHandleIntent(Intent intent) {
  139. if (intent != null) {
  140. final String action = intent.getAction();
  141. if (ACTION_LOG.equals(action)) {
  142. final Record record = intent.getParcelableExtra(EXTRA_RECORD);
  143. handleActionLog(record);
  144. } else if (ACTION_GET_RECORD_ALL.equals(action)) {
  145. ResultReceiver receiver = intent
  146. .getParcelableExtra(RESULT_RECEIVER);
  147. ArrayList<Record> r = handleActionGetAllRecords();
  148. Bundle result = new Bundle();
  149. result.putParcelableArrayList("result", r);
  150. receiver.send(0, result);
  151. } else if (ACTION_GET_RECORD_EACH.equals(action)) {
  152. final int lastUploadedAttackId = intent.getIntExtra(
  153. EXTRA_PRIMITIVE, -1);
  154. ResultReceiver receiver = intent
  155. .getParcelableExtra(RESULT_RECEIVER);
  156. ArrayList<Record> r = handleActionGetRecordOfEachAttack(lastUploadedAttackId);
  157. Bundle result = new Bundle();
  158. result.putParcelableArrayList("result", r);
  159. receiver.send(0, result);
  160. } else if (ACTION_GET_RECORD_ID.equals(action)) {
  161. final int attack_id = intent.getIntExtra(EXTRA_PRIMITIVE, -1);
  162. ResultReceiver receiver = intent
  163. .getParcelableExtra(RESULT_RECEIVER);
  164. Record r = handleActionGetRecordOfAttackId(attack_id);
  165. Bundle result = new Bundle();
  166. result.putParcelable("result", r);
  167. receiver.send(0, result);
  168. } else if (ACTION_GET_COUNT_ALL.equals(action)) {
  169. ResultReceiver receiver = intent
  170. .getParcelableExtra(RESULT_RECEIVER);
  171. int r = handleActionGetAttackCount();
  172. Bundle result = new Bundle();
  173. result.putInt("result", r);
  174. receiver.send(0, result);
  175. } else if (ACTION_GET_COUNT_PROTOCOL.equals(action)) {
  176. final String protocol = intent.getStringExtra(EXTRA_PROTOCOL);
  177. ResultReceiver receiver = intent
  178. .getParcelableExtra(RESULT_RECEIVER);
  179. int r = handleActionGetAttackPerProtocolCount(protocol);
  180. Bundle result = new Bundle();
  181. result.putInt("result", r);
  182. receiver.send(0, result);
  183. } else if (ACTION_GET_ATTACK_MIN.equals(action)) {
  184. ResultReceiver receiver = intent
  185. .getParcelableExtra(RESULT_RECEIVER);
  186. long r = handleActionGetMinAttackId();
  187. Bundle result = new Bundle();
  188. result.putLong("result", r);
  189. receiver.send(0, result);
  190. handleActionGetMinAttackId();
  191. } else if (ACTION_GET_ATTACK_MAX.equals(action)) {
  192. ResultReceiver receiver = intent
  193. .getParcelableExtra(RESULT_RECEIVER);
  194. long r = handleActionGetMaxAttackId();
  195. Bundle result = new Bundle();
  196. result.putLong("result", r);
  197. receiver.send(0, result);
  198. } else if (ACTION_IS_BSSID_SEEN.equals(action)) {
  199. final String protocol = intent.getStringExtra(EXTRA_PROTOCOL);
  200. final String bssid = intent.getStringExtra(EXTRA_BSSID);
  201. ResultReceiver receiver = intent
  202. .getParcelableExtra(RESULT_RECEIVER);
  203. boolean r = handleActionBssidSeen(protocol, bssid);
  204. Bundle result = new Bundle();
  205. result.putBoolean("result", r);
  206. receiver.send(0, result);
  207. } else if (ACTION_GET_BSSID_ALL.equals(action)) {
  208. ResultReceiver receiver = intent
  209. .getParcelableExtra(RESULT_RECEIVER);
  210. String[] r = handleActionGetAllBssids();
  211. Bundle result = new Bundle();
  212. result.putStringArray("result", r);
  213. receiver.send(0, result);
  214. } else if (ACTION_GET_SSID_BSSID.equals(action)) {
  215. final String bssid = intent.getStringExtra(EXTRA_BSSID);
  216. ResultReceiver receiver = intent
  217. .getParcelableExtra(RESULT_RECEIVER);
  218. String r = handleActionGetSsid(bssid);
  219. Bundle result = new Bundle();
  220. result.putString("result", r);
  221. receiver.send(0, result);
  222. } else if (ACTION_CLEAR_DATE.equals(action)) {
  223. final long time = intent.getLongExtra(EXTRA_PRIMITIVE, -1L);
  224. handleActionDeleteByDate(time);
  225. } else if (ACTION_CLEAR_BSSID.equals(action)) {
  226. final String bssid = intent.getStringExtra(EXTRA_BSSID);
  227. handleActionDeleteByBssid(bssid);
  228. } else if (ACTION_CLEAR_ALL.equals(action)) {
  229. handleActionDeleteAll();
  230. }
  231. }
  232. }
  233. /**
  234. * Log a record.
  235. */
  236. private void handleActionLog(Record record) {
  237. mDbHelper.addRecord(record);
  238. }
  239. private ArrayList<Record> handleActionGetAllRecords() {
  240. return mDbHelper.getAllRecords();
  241. }
  242. private ArrayList<Record> handleActionGetRecordOfEachAttack(
  243. int lastUploadedAttackId) {
  244. return mDbHelper.getRecordOfEachAttack(lastUploadedAttackId);
  245. }
  246. private Record handleActionGetRecordOfAttackId(long attack_id) {
  247. return mDbHelper.getRecordOfAttackId(attack_id);
  248. }
  249. private int handleActionGetAttackCount() {
  250. return mDbHelper.getAttackCount();
  251. }
  252. private int handleActionGetAttackPerProtocolCount(String protocol) {
  253. return mDbHelper.getAttackPerProtocolCount(protocol);
  254. }
  255. private long handleActionGetMinAttackId() {
  256. return mDbHelper.getSmallestAttackId();
  257. }
  258. private long handleActionGetMaxAttackId() {
  259. return mDbHelper.getHighestAttackId();
  260. }
  261. private boolean handleActionBssidSeen(String protocol, String bssid) {
  262. return mDbHelper.bssidSeen(protocol, bssid);
  263. }
  264. private String[] handleActionGetAllBssids() {
  265. return mDbHelper.getAllBSSIDS();
  266. }
  267. private String handleActionGetSsid(String bssid) {
  268. return mDbHelper.getSSID(bssid);
  269. }
  270. private void handleActionDeleteByDate(long time) {
  271. mDbHelper.deleteByDate(time);
  272. }
  273. private void handleActionDeleteByBssid(String bssid) {
  274. mDbHelper.deleteByBSSID(bssid);
  275. }
  276. /**
  277. * Delete all records.
  278. */
  279. private void handleActionDeleteAll() {
  280. mDbHelper.clearData();
  281. }
  282. }