OldLogger.java 12 KB

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