LogFilter.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. package de.tudarmstadt.informatik.hostage.ui2.model;
  2. import android.os.Parcel;
  3. import android.os.Parcelable;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. public class LogFilter implements Parcelable {
  7. public final static String LOG_FILTER_INTENT_KEY = "de.tudarmstadt.informatik.hostage.logfilter";
  8. //private static final String TIMESTAMP_BELOW_KEY = "de.tudarmstadt.informatik.hostage.logfilter.timestampbelow";
  9. //private static final String TIMESTAMP_ABOVE_KEY = "de.tudarmstadt.informatik.hostage.logfilter.timestampabove";
  10. private static final String PROTOCOLS_KEY = "de.tudarmstadt.informatik.hostage.logfilter.protocols";
  11. private static final String ESSID_KEY = "de.tudarmstadt.informatik.hostage.logfilter.essid";
  12. private static final String BSSID_KEY = "de.tudarmstadt.informatik.hostage.logfilter.bssid";
  13. //private static final String SORTTYPE_KEY = "de.tudarmstadt.informatik.hostage.logfilter.sorttype";
  14. /**
  15. * The SortType
  16. */
  17. public enum SortType {
  18. packet_timestamp(0), protocol(1), ssid(2), _bssid(3), _attack_id(7), _id(8);
  19. private final int id;
  20. SortType(int id) {
  21. this.id = id;
  22. }
  23. public int getValue() {
  24. return id;
  25. }
  26. }
  27. public ArrayList<String> BSSIDs;
  28. public ArrayList<String> ESSIDs;
  29. public ArrayList<String> protocols;
  30. public boolean isNotEditable;
  31. public SortType sorttype;
  32. public long belowTimestamp;
  33. public long aboveTimestamp;
  34. /**
  35. * Constructur
  36. */
  37. public LogFilter() {
  38. this.clear();
  39. }
  40. /**
  41. * Clears / resets all attributes of the filter objects
  42. * The below timestamp will be maximal and the above timestamp will be minimal (long).
  43. * The sort type is set to the default: timestamp
  44. */
  45. public void clear() {
  46. this.belowTimestamp = Long.MAX_VALUE;
  47. this.aboveTimestamp = Long.MIN_VALUE;
  48. this.sorttype = SortType.packet_timestamp;
  49. this.BSSIDs = new ArrayList<String>();
  50. this.ESSIDs = new ArrayList<String>();
  51. this.protocols = new ArrayList<String>();
  52. }
  53. @Override
  54. public int describeContents() {
  55. return 0;
  56. }
  57. // write filter's data to the passed-in Parcel
  58. @Override
  59. public void writeToParcel(Parcel out, int flags) {
  60. HashMap<String, ArrayList<String>> values = new HashMap<String, ArrayList<String>>();
  61. if (this.BSSIDs != null && this.BSSIDs.size() > 0) {
  62. values.put(BSSID_KEY, this.getBSSIDs());
  63. }
  64. if (this.ESSIDs != null && this.ESSIDs.size() > 0) {
  65. values.put(ESSID_KEY, this.getESSIDs());
  66. }
  67. if (this.protocols != null && this.protocols.size() > 0) {
  68. values.put(PROTOCOLS_KEY, this.getProtocols());
  69. }
  70. long timeArray[] = new long[] { this.aboveTimestamp, this.belowTimestamp };
  71. out.writeMap(values);
  72. out.writeInt(this.sorttype.getValue());
  73. out.writeDouble(timeArray.length);
  74. out.writeLongArray(timeArray);
  75. out.writeString(this.isNotEditable ? "true" : "false");
  76. }
  77. // needed to create a parcel object
  78. public static final Parcelable.Creator<LogFilter> CREATOR = new Parcelable.Creator<LogFilter>() {
  79. public LogFilter createFromParcel(Parcel in) {
  80. return new LogFilter(in);
  81. }
  82. public LogFilter[] newArray(int size) {
  83. return new LogFilter[size];
  84. }
  85. };
  86. /** constructor
  87. * that takes a (filter) Parcel and gives you an LogFilter populated
  88. * with it's values.
  89. * @param in {@link Parcel parcel}
  90. * */
  91. private LogFilter(Parcel in) {
  92. HashMap<String, ArrayList<String>> values = new HashMap<String, ArrayList<String>>();
  93. in.readMap(values, ArrayList.class.getClassLoader());
  94. this.BSSIDs = values.get(BSSID_KEY);
  95. this.ESSIDs = values.get(ESSID_KEY);
  96. this.protocols = values.get(protocols);
  97. if (this.BSSIDs == null)
  98. this.BSSIDs = new ArrayList<String>();
  99. if (this.ESSIDs == null)
  100. this.ESSIDs = new ArrayList<String>();
  101. if (this.protocols == null)
  102. this.protocols = new ArrayList<String>();
  103. this.sorttype = SortType.values()[Math.min(in.readInt(), SortType.values().length)];
  104. int size = (int) in.readDouble();
  105. long timeArray[] = new long[size];
  106. in.readLongArray(timeArray);
  107. this.belowTimestamp = timeArray[1];
  108. this.aboveTimestamp = timeArray[0];
  109. String bool = in.readString();
  110. if (bool.equals("true"))
  111. this.isNotEditable = true;
  112. }
  113. /**
  114. * If the filter can be edited this method returns false.
  115. * @return boolean
  116. */
  117. public boolean isNotEditable() {
  118. return this.isNotEditable;
  119. }
  120. /**
  121. * Returns the filter's sorttype
  122. * @return {@link de.tudarmstadt.informatik.hostage.ui2.model.LogFilter.SortType, sorttype}
  123. */
  124. public SortType getSorttype() {
  125. return this.sorttype;
  126. }
  127. /**
  128. * Returns the filtered essid names.
  129. * @return ArrayList<String>
  130. */
  131. public ArrayList<String> getBSSIDs() {
  132. return this.BSSIDs;
  133. }
  134. /**
  135. * Returns the filtered bssid names.
  136. * @return ArrayList<String>
  137. */
  138. public ArrayList<String> getESSIDs() {
  139. return this.ESSIDs;
  140. }
  141. /**
  142. * Returns the filtered protocol names.
  143. * @return ArrayList<String>
  144. */
  145. public ArrayList<String> getProtocols() {
  146. return this.protocols;
  147. }
  148. /**
  149. * If you don't want a filter to be editable, call this method and insert true
  150. * @param b boolean
  151. */
  152. public void setIsNotEditable(boolean b) {
  153. this.isNotEditable = b;
  154. }
  155. /**
  156. * Returns the filtered maximal timestamp a entry could have.
  157. * The default is max long.
  158. * @return long timestamp
  159. */
  160. public long getBelowTimestamp() {
  161. return this.belowTimestamp;
  162. }
  163. /**
  164. * Returns the filtered minimal timestamp a entry could have.
  165. * The default is min long.
  166. * @return long timestamp
  167. */
  168. public long getAboveTimestamp() {
  169. return this.aboveTimestamp;
  170. }
  171. /**
  172. * Set the protocols which a {@link de.tudarmstadt.informatik.hostage.logging.Record Record} can have.
  173. * @param protocols ArrayList<String>
  174. */
  175. public void setProtocols(ArrayList<String> protocols) {
  176. this.protocols = protocols;
  177. }
  178. /**
  179. * Set the bssids which a {@link de.tudarmstadt.informatik.hostage.logging.Record Record} can have.
  180. * @param bssids ArrayList<String>
  181. */
  182. public void setBSSIDs(ArrayList<String> bssids) {
  183. this.BSSIDs = bssids;
  184. }
  185. /**
  186. * Set the Essids which a {@link de.tudarmstadt.informatik.hostage.logging.Record Record} can have.
  187. * @param essids ArrayList<String>
  188. */
  189. public void setESSIDs(ArrayList<String> essids) {
  190. this.ESSIDs = essids;
  191. }
  192. /**
  193. * Set the minimal Timestamp a filter {@link de.tudarmstadt.informatik.hostage.logging.Record Record} can have.
  194. * @param timestamp long
  195. */
  196. public void setAboveTimestamp(long timestamp) {
  197. this.aboveTimestamp = timestamp;
  198. }
  199. /**
  200. * Set the maximal Timestamp a filtered {@link de.tudarmstadt.informatik.hostage.logging.Record Record} can have.
  201. * @param timestamp long
  202. */
  203. public void setBelowTimestamp(long timestamp) {
  204. this.belowTimestamp = timestamp;
  205. }
  206. /**
  207. * Set the {@link SortType SortType}.
  208. * @param type SortType
  209. */
  210. public void setSorttype(SortType type) {
  211. this.sorttype = type;
  212. }
  213. /**
  214. * Returns the query statement string for all filtered BSSIDs.
  215. * This method is used to perform a sql query.
  216. * @param tablename String, the table name.
  217. * @param key String, the table column name.
  218. * @return queryString String
  219. */
  220. public String getBSSIDQueryStatement(String tablename, String key) {
  221. return this.convertArrayListToQueryString(this.BSSIDs, tablename, key);
  222. }
  223. /**
  224. * Returns the query statement string for all filtered ESSIDs.
  225. * This method is used to perform a sql query.
  226. * @param tablename String, the table name.
  227. * @param key String, the table column name.
  228. * @return queryString String
  229. */
  230. public String getESSIDQueryStatement(String tablename, String key) {
  231. return this.convertArrayListToQueryString(this.ESSIDs, tablename, key);
  232. }
  233. /**
  234. * Returns the query statement string for all filtered protocols.
  235. * This method is used to perform a sql query.
  236. * @param tablename String, the table name.
  237. * @param key String, the table column name.
  238. * @return queryString String
  239. */
  240. public String getProtocolsQueryStatement(String tablename, String key) {
  241. return this.convertArrayListToQueryString(this.protocols, tablename, key);
  242. }
  243. /**
  244. * Returns true if the filter has any attributes set.
  245. * @return boolean
  246. */
  247. public boolean isSet() {
  248. boolean hasTime = this.hasATimestamp();
  249. boolean hasBSSIDs = this.hasBSSIDs();
  250. boolean hasESSIDs = this.hasESSIDs();
  251. boolean hasProtocols = this.hasProtocols();
  252. return hasBSSIDs || hasESSIDs || hasProtocols | hasTime;
  253. }
  254. /**
  255. * Returns true if the filter has more than one bssid.
  256. * @return boolean
  257. */
  258. public boolean hasBSSIDs() {
  259. return this.getBSSIDs().size() > 0;
  260. }
  261. /**
  262. * Returns true if the filter has more than one essid.
  263. * @return boolean
  264. */
  265. public boolean hasESSIDs() {
  266. return this.getESSIDs().size() > 0;
  267. }
  268. /**
  269. * Returns true if the filter has more than one protocl.
  270. * @return boolean
  271. */
  272. public boolean hasProtocols() {
  273. return this.getProtocols().size() > 0;
  274. }
  275. /**
  276. * Returns true if the filter has a minimal timestamp.
  277. * @return boolean
  278. */
  279. public boolean hasAboveTimestamp() {
  280. return this.aboveTimestamp != Long.MIN_VALUE;
  281. }
  282. /**
  283. * Returns true if the filter has a maximal timestamp.
  284. * @return boolean
  285. */
  286. public boolean hasBelowTimestamp() {
  287. return this.belowTimestamp != Long.MAX_VALUE;
  288. }
  289. /**
  290. * Returns true if the filter has a any timestamp.
  291. * @return boolean
  292. */
  293. public boolean hasATimestamp() {
  294. return this.hasBelowTimestamp() || this.hasAboveTimestamp();
  295. }
  296. /**
  297. * Returns a query statement to perform a sql query. The given list will be concatenate by an OR statement.
  298. * @param list ArrayList<String> The entries which should be concatenate.
  299. * @param table The table name.
  300. * @param key The table column name.
  301. * @return queryString string
  302. */
  303. public String convertArrayListToQueryString(ArrayList<String> list, String table, String key) {
  304. String statement = "";
  305. if (list == null)
  306. return statement;
  307. statement = " ( ";
  308. int i = 0, max = list.size();
  309. for (String element : list) {
  310. i++;
  311. statement = statement + table + "." + key + " = " + "'" + element + "'";
  312. if (i == max)
  313. continue;
  314. statement = statement + " OR ";
  315. }
  316. statement = statement + " ) ";
  317. return statement;
  318. }
  319. }