RecordOverviewFragment.java 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. package de.tudarmstadt.informatik.hostage.ui2.fragment;
  2. import android.annotation.SuppressLint;
  3. import android.app.FragmentManager;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.os.Bundle;
  7. import android.util.Log;
  8. import android.view.LayoutInflater;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import android.widget.ExpandableListView;
  12. import android.widget.ImageButton;
  13. import com.google.android.gms.maps.model.LatLng;
  14. import java.text.DateFormat;
  15. import java.text.SimpleDateFormat;
  16. import java.util.ArrayList;
  17. import java.util.Calendar;
  18. import java.util.Collections;
  19. import java.util.Comparator;
  20. import java.util.Date;
  21. import java.util.HashMap;
  22. import java.util.Random;
  23. import de.tudarmstadt.informatik.hostage.R;
  24. import de.tudarmstadt.informatik.hostage.logging.Record;
  25. import de.tudarmstadt.informatik.hostage.logging.UglyDbHelper;
  26. import de.tudarmstadt.informatik.hostage.ui.LogFilter;
  27. import de.tudarmstadt.informatik.hostage.ui.LogFilter.SortType;
  28. import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
  29. import de.tudarmstadt.informatik.hostage.ui2.adapter.RecordListAdapter;
  30. import de.tudarmstadt.informatik.hostage.ui2.dialog.ChecklistDialog;
  31. import de.tudarmstadt.informatik.hostage.ui2.dialog.DateTimeDialogFragment;
  32. import de.tudarmstadt.informatik.hostage.ui2.model.ExpandableListItem;
  33. import de.tudarmstadt.informatik.hostage.ui2.popup.AbstractPopup;
  34. import de.tudarmstadt.informatik.hostage.ui2.popup.AbstractPopupItem;
  35. import de.tudarmstadt.informatik.hostage.ui2.popup.SimplePopupItem;
  36. import de.tudarmstadt.informatik.hostage.ui2.popup.SimplePopupTable;
  37. import de.tudarmstadt.informatik.hostage.ui2.popup.SplitPopupItem;
  38. public class RecordOverviewFragment extends UpNavigatibleFragment implements ChecklistDialog.ChecklistDialogListener, DateTimeDialogFragment.DateTimeDialogFragmentListener {
  39. static final String FILTER_MENU_TITLE_BSSID = "BSSID";
  40. static final String FILTER_MENU_TITLE_ESSID = "ESSID";
  41. static final String FILTER_MENU_TITLE_PROTOCOLS = MainActivity.getContext().getString(R.string.rec_protocol);
  42. static final String FILTER_MENU_TITLE_TIMESTAMP_BELOW = MainActivity.getContext().getString(
  43. R.string.rec_latest);
  44. static final String FILTER_MENU_TITLE_TIMESTAMP_ABOVE = MainActivity.getContext().getString(
  45. R.string.rec_earliest);
  46. static final String FILTER_MENU_TITLE_SORTING = MainActivity.getContext().getString(R.string.rec_sortby);
  47. static final String FILTER_MENU_TITLE_REMOVE = MainActivity.getContext().getString(R.string.rec_reset_filter);
  48. static final String FILTER_MENU_TITLE_GROUP = MainActivity.getContext().getString(
  49. R.string.rec_group_by);
  50. static final String FILTER_MENU_POPUP_TITLE = MainActivity.getContext().getString(
  51. R.string.rec_filter_by);
  52. private boolean wasBelowTimePicker;
  53. private LogFilter filter;
  54. private boolean showFilterButton;
  55. private int mListPosition = -1;
  56. private int mItemPosition = -1;
  57. public String groupingKey;
  58. private ExpandableListView expListView;
  59. UglyDbHelper dbh;
  60. private String sectionToOpen = "";
  61. private ArrayList<Integer> openSections;
  62. public void setFilter(LogFilter filter){
  63. this.filter = filter;
  64. }
  65. public RecordOverviewFragment(){}
  66. public void setGroupKey(String key){
  67. this.groupingKey = key;
  68. }
  69. @Override
  70. public void onCreate(Bundle savedInstanceState) {
  71. super.onCreate(savedInstanceState);
  72. setHasOptionsMenu(true);
  73. }
  74. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  75. Bundle savedInstanceState) {
  76. getActivity().setTitle(getResources().getString(R.string.drawer_records));
  77. dbh = new UglyDbHelper(this.getActivity().getBaseContext());
  78. //this.addRecordToDB(5, 2);
  79. // Get the message from the intent
  80. if (this.filter == null){
  81. Intent intent = this.getActivity().getIntent();
  82. LogFilter filter = intent.getParcelableExtra(LogFilter.LOG_FILTER_INTENT_KEY);
  83. if(filter == null){
  84. this.clearFilter();
  85. } else {
  86. this.filter = filter;
  87. }
  88. }
  89. if (this.groupingKey == null) this.groupingKey = this.groupingTitles().get(0);
  90. this.setShowFilterButton(!this.filter.isNotEditable());
  91. //this.addRecordToDB();
  92. View rootView = inflater.inflate(this.getLayoutId(), container, false);
  93. ExpandableListView mylist = (ExpandableListView) rootView.findViewById(R.id.loglistview);
  94. this.expListView = mylist;
  95. populateListViewFromDB(mylist);
  96. if (this.openSections != null && this.openSections.size() != 0){
  97. for (int i = 0; i < this.openSections.size(); i++){
  98. int index = this.openSections.get(i);
  99. this.expListView.expandGroup(index);
  100. }
  101. } else {
  102. this.openSections = new ArrayList<Integer>();
  103. }
  104. if (mListPosition != -1 && mItemPosition != -1)
  105. this.expListView.setSelectedChild(mListPosition, mItemPosition, true);
  106. mListPosition = -1;
  107. mItemPosition = -1;
  108. registerListClickCallback(mylist);
  109. ImageButton filterButton = (ImageButton) rootView.findViewById(R.id.FilterButton);
  110. filterButton.setOnClickListener(new View.OnClickListener() {
  111. public void onClick(View v) {
  112. RecordOverviewFragment.this.openFilterPopupMenuOnView(v);
  113. }
  114. });
  115. filterButton.setVisibility(this.showFilterButton? View.VISIBLE : View.INVISIBLE);
  116. ImageButton sortButton = (ImageButton) rootView.findViewById(R.id.SortButton);
  117. sortButton.setOnClickListener(new View.OnClickListener() {
  118. public void onClick(View v) {
  119. // Open SortMenu
  120. RecordOverviewFragment.this.openSortingDialog();
  121. }
  122. });
  123. ImageButton groupButton = (ImageButton) rootView.findViewById(R.id.GroupButton);
  124. groupButton.setOnClickListener(new View.OnClickListener() {
  125. public void onClick(View v) {
  126. // Open SortMenu
  127. RecordOverviewFragment.this.openGroupingDialog();
  128. }
  129. });
  130. return rootView;
  131. }
  132. /*
  133. * Returns the Fragment layout ID
  134. * @return int The fragment layout ID
  135. * */
  136. public int getLayoutId(){
  137. return R.layout.fragment_record_list;
  138. }
  139. /*
  140. * Gets called if the user clicks on item in the filter menu.
  141. *
  142. * @param AbstractPopupItem item
  143. * */
  144. public void onFilterMenuItemSelected(AbstractPopupItem item) {
  145. String title = item.getTitle();
  146. if (item instanceof SplitPopupItem){
  147. SplitPopupItem splitItem = (SplitPopupItem)item;
  148. if (splitItem.wasRightTouch){
  149. this.openTimestampToFilterDialog();
  150. } else {
  151. this.openTimestampFromFilterDialog();
  152. }
  153. return;
  154. }
  155. if (title != null){
  156. if(title.equals(FILTER_MENU_TITLE_BSSID)){
  157. this.openBSSIDFilterDialog();
  158. }
  159. if(title.equals(FILTER_MENU_TITLE_ESSID)){
  160. this.openESSIDFilterDialog();
  161. }
  162. if(title.equals(FILTER_MENU_TITLE_PROTOCOLS)){
  163. this.openProtocolsFilterDialog();
  164. }
  165. if(title.equals(FILTER_MENU_TITLE_SORTING)){
  166. this.openSortingDialog();
  167. }
  168. if(title.equals(FILTER_MENU_TITLE_REMOVE)){
  169. this.clearFilter();
  170. this.populateListViewFromDB(this.expListView);
  171. }
  172. if(title.equals(FILTER_MENU_TITLE_TIMESTAMP_BELOW)){
  173. this.openTimestampToFilterDialog();
  174. }
  175. if(title.equals(FILTER_MENU_TITLE_TIMESTAMP_ABOVE)){
  176. this.openTimestampFromFilterDialog();
  177. }
  178. }
  179. //return super.onOptionsItemSelected(item);
  180. }
  181. public void onStart() {
  182. super.onStart();
  183. //this.populateListViewFromDB(this.expListView);
  184. if (this.expListView.getExpandableListAdapter().getGroupCount() == 1){
  185. this.expListView.expandGroup(0);
  186. } else {
  187. this.setSectionToOpen(this.sectionToOpen);
  188. }
  189. }
  190. /*****************************
  191. *
  192. * Public API
  193. *
  194. * ***************************/
  195. /**
  196. * Group records by SSID and expand given SSID
  197. *
  198. * @param SSID the SSID
  199. */
  200. public void showDetailsForSSID(Context context, String SSID) {
  201. Log.e("RecordOverviewFragment", "Implement showDetailsForSSID!!");
  202. this.clearFilter();
  203. int ESSID_INDEX = 2;
  204. ArrayList<String> ssids = new ArrayList<String>();
  205. this.sectionToOpen = SSID;
  206. this.groupingKey = this.groupingTitles(context).get(ESSID_INDEX);
  207. }
  208. /*****************************
  209. *
  210. * ListView Stuff
  211. *
  212. * ***************************/
  213. /*
  214. * Reloads the data in the ExpandableListView for the given filter object.
  215. * @param ExpandableListView listview
  216. * */
  217. private void populateListViewFromDB(ExpandableListView mylist) {
  218. HashMap<String, ArrayList<ExpandableListItem>> sectionData = new HashMap<String, ArrayList<ExpandableListItem>>();
  219. ArrayList<Record> data = dbh.getRecordsForFilter(this.filter);
  220. // Adding Items to ListView
  221. String keys[] = new String[] { this.getString(R.string.RecordBSSID), this.getString(R.string.RecordSSID), this.getString(R.string.RecordProtocol), this.getString(R.string.RecordTimestamp)};
  222. int ids[] = new int[] {R.id.RecordTextFieldBSSID, R.id.RecordTextFieldSSID, R.id.RecordTextFieldProtocol, R.id.RecordTextFieldTimestamp };
  223. HashMap<String, Integer> mapping = new HashMap<String, Integer>();
  224. int i = 0;
  225. for(String key : keys){
  226. mapping.put(key, ids[i]);
  227. i++;
  228. }
  229. ArrayList<String>groupTitle = new ArrayList<String>();
  230. for (Record val : data) {
  231. // DO GROUPING IN HERE
  232. HashMap<String, String> map = new HashMap<String, String>();
  233. map.put(this.getString(R.string.RecordBSSID), val.getBssid());
  234. map.put(this.getString(R.string.RecordSSID), val.getSsid());
  235. map.put(this.getString(R.string.RecordProtocol), val.getProtocol());
  236. map.put(this.getString(R.string.RecordTimestamp),
  237. this.getDateAsString(val.getTimestamp()));
  238. ExpandableListItem item = new ExpandableListItem();
  239. item.setData(map);
  240. item.setId_Mapping(mapping);
  241. item.setTag(val.getAttack_id());
  242. String groupID = this.getGroupValue(val);
  243. ArrayList<ExpandableListItem> items = sectionData.get(groupID);
  244. if (items == null) {
  245. items = new ArrayList<ExpandableListItem>();
  246. sectionData.put(groupID, items);
  247. groupTitle.add(groupID);
  248. }
  249. items.add(item);
  250. }
  251. Collections.sort(groupTitle, new Comparator<String>() {
  252. @Override
  253. public int compare(String s1, String s2) {
  254. return s1.compareToIgnoreCase(s2);
  255. }
  256. });
  257. RecordListAdapter adapter = new RecordListAdapter(this.getApplicationContext(), groupTitle, sectionData);
  258. mylist.setAdapter(adapter);
  259. }
  260. /*Thsi will open a section in the ExpandableListView with the same title as the parameter s.
  261. *
  262. * @param String s (the section title to open)
  263. *
  264. * */
  265. private void setSectionToOpen(String s){
  266. this.sectionToOpen = s;
  267. if (this.sectionToOpen != null && this.sectionToOpen.length() != 0){
  268. if (this.getGroupTitles().contains(this.sectionToOpen)){
  269. int section = this.getGroupTitles().indexOf(this.sectionToOpen);
  270. this.expListView.expandGroup(section);
  271. this.sectionToOpen = "";
  272. }
  273. }
  274. }
  275. /*
  276. * Returns the base context.
  277. * @return Context baseContext
  278. * */
  279. private Context getBaseContext(){
  280. return this.getActivity().getBaseContext();
  281. }
  282. /*Returns the application context.
  283. * @return Context application context
  284. * */
  285. private Context getApplicationContext(){
  286. return this.getActivity().getApplicationContext();
  287. }
  288. /*Sets the list view listener on the given ExpandableListView.
  289. *
  290. * @param ExpandableListView listview
  291. * */
  292. private void registerListClickCallback(ExpandableListView mylist) {
  293. mylist.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
  294. @Override
  295. public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i2, long l) {
  296. RecordListAdapter adapter = (RecordListAdapter)expandableListView.getExpandableListAdapter();
  297. ExpandableListItem item = (ExpandableListItem)adapter.getChild(i,i2);
  298. mListPosition = i;
  299. mItemPosition = i2;
  300. UglyDbHelper dbh = new UglyDbHelper(getBaseContext());
  301. Record rec = dbh.getRecordOfAttackId((int) item.getTag());
  302. RecordOverviewFragment.this.pushRecordDetailViewForRecord(rec);
  303. return true;
  304. }
  305. });
  306. mylist.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
  307. @Override
  308. public void onGroupExpand(int i) {
  309. RecordOverviewFragment.this.openSections.add(new Integer(i));
  310. }
  311. });
  312. mylist.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
  313. @Override
  314. public void onGroupCollapse(int i) {
  315. RecordOverviewFragment.this.openSections.remove(new Integer(i));
  316. }
  317. });
  318. }
  319. /*****************************
  320. *
  321. * Date Transform
  322. *
  323. * ***************************/
  324. /*Returns the date format "H:mm d.M.yy" for the given timestamp (long)
  325. * @param Long timestamp*/
  326. @SuppressLint("SimpleDateFormat")
  327. private String getDateAsString(long timeStamp) {
  328. try {
  329. DateFormat sdf = new SimpleDateFormat("H:mm d.M.yy");
  330. Date netDate = (new Date(timeStamp));
  331. return sdf.format(netDate);
  332. } catch (Exception ex) {
  333. return "xx";
  334. }
  335. }
  336. /*****************************
  337. *
  338. * Getter / Setter
  339. *
  340. * ***************************/
  341. public boolean isShowFilterButton() {
  342. return showFilterButton;
  343. }
  344. public void setShowFilterButton(boolean showFilterButton) {
  345. this.showFilterButton = showFilterButton;
  346. }
  347. /*****************************
  348. *
  349. * Open Dialog Methods
  350. *
  351. * ***************************/
  352. /*Opens the grouping dialog*/
  353. private void openGroupingDialog(){
  354. ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_TITLE_GROUP, this.groupingTitles(), this.selectedGroup(), false , this);
  355. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_GROUP);
  356. }
  357. /*opens the bssid filter dialog*/
  358. private void openBSSIDFilterDialog(){
  359. ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_TITLE_BSSID,this.bssids(), this.selectedBSSIDs(), true , this);
  360. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_BSSID);
  361. }
  362. /*opens the essid filter dialog*/
  363. private void openESSIDFilterDialog(){
  364. ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_TITLE_ESSID,this.essids(), this.selectedESSIDs(), true , this);
  365. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_ESSID);
  366. }
  367. /*opens the protocol filter dialog*/
  368. private void openProtocolsFilterDialog(){
  369. ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_TITLE_PROTOCOLS,this.protocolTitles(), this.selectedProtocols(), true , this);
  370. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_PROTOCOLS);
  371. }
  372. /*opens the timestamp filter dialog (minimal timestamp required)*/
  373. private void openTimestampFromFilterDialog(){
  374. this.wasBelowTimePicker = false;
  375. DateTimeDialogFragment newFragment = new DateTimeDialogFragment(this.getActivity());
  376. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_SORTING);
  377. if (this.filter.aboveTimestamp != Long.MIN_VALUE)newFragment.setDate(this.filter.aboveTimestamp);
  378. }
  379. /*opens time timestamp filter dialog (maximal timestamp required)*/
  380. private void openTimestampToFilterDialog(){
  381. this.wasBelowTimePicker = true;
  382. DateTimeDialogFragment newFragment = new DateTimeDialogFragment(this.getActivity());
  383. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_SORTING);
  384. if (this.filter.belowTimestamp != Long.MAX_VALUE) newFragment.setDate(this.filter.belowTimestamp);
  385. }
  386. /*opens the sorting dialog*/
  387. private void openSortingDialog(){
  388. ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_TITLE_SORTING,this.sortTypeTiles(), this.selectedSorttype(), false , this);
  389. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_SORTING);
  390. }
  391. /*****************************
  392. *
  393. * Grouping Stuff
  394. *
  395. * ***************************/
  396. /*returns the group title for the given record. Uses the groupingKey to decied which value of the record should be used.
  397. * @param Record rec
  398. * @return String grouptitle*/
  399. public String getGroupValue(Record rec){
  400. int index = this.groupingTitles().indexOf(this.groupingKey);
  401. switch (index){
  402. case 0:
  403. return rec.getProtocol();
  404. case 1:
  405. return rec.getBssid();
  406. case 2:
  407. return rec.getSsid();
  408. default:
  409. return rec.getProtocol();
  410. }
  411. }
  412. /*Returns the Group titles for the specified grouping key. e.g. groupingKey is "ESSID" it returns all available essids.
  413. * @return ArrayList<String> grouptitles*/
  414. public ArrayList<String> getGroupTitles(){
  415. int index = this.groupingTitles().indexOf(this.groupingKey);
  416. switch (index){
  417. case 0:
  418. return this.protocolTitles();
  419. case 1:
  420. return this.bssids();
  421. case 2:
  422. return this.essids();
  423. default:
  424. return this.protocolTitles();
  425. }
  426. }
  427. /*****************************
  428. *
  429. * Filter Stuff
  430. *
  431. * ***************************/
  432. /*Opens the filter menu on a anchor view. The filter menu will always be on top of the anchor.
  433. * @param View anchorView*/
  434. private void openFilterPopupMenuOnView(View v){
  435. SimplePopupTable filterMenu = new SimplePopupTable(this.getActivity(), new AbstractPopup.OnPopupItemClickListener() {
  436. public void onItemClick(Object ob) {
  437. if (ob instanceof AbstractPopupItem){
  438. AbstractPopupItem item = (AbstractPopupItem) ob;
  439. RecordOverviewFragment.this.onFilterMenuItemSelected(item);
  440. }
  441. }
  442. });
  443. filterMenu.setTitle(FILTER_MENU_POPUP_TITLE);
  444. for(String title : RecordOverviewFragment.this.filterMenuTitles()){
  445. AbstractPopupItem item = null;
  446. if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_BELOW)) continue;
  447. if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_ABOVE)){
  448. item = new SplitPopupItem(this.getActivity());
  449. item.setValue(SplitPopupItem.RIGHT_TITLE, FILTER_MENU_TITLE_TIMESTAMP_BELOW);
  450. item.setValue(SplitPopupItem.LEFT_TITLE, FILTER_MENU_TITLE_TIMESTAMP_ABOVE);
  451. if (this.filter.hasBelowTimestamp()){
  452. item.setValue(SplitPopupItem.RIGHT_SUBTITLE, this.getDateAsString(this.filter.belowTimestamp));
  453. }
  454. if (this.filter.hasAboveTimestamp()){
  455. item.setValue(SplitPopupItem.LEFT_SUBTITLE, this.getDateAsString(this.filter.aboveTimestamp));
  456. }
  457. } else {
  458. item = new SimplePopupItem(this.getActivity());
  459. item.setTitle(title);
  460. ((SimplePopupItem)item).setSelected(this.isFilterSetForTitle(title));
  461. }
  462. filterMenu.addItem(item);
  463. }
  464. filterMenu.showOnView(v);
  465. }
  466. /*Returns true if the filter object is set for the given title otherwise false. e.g. the filter object has protocols,
  467. * so the method will return for the title FILTER_MENU_TITLE_PROTOCOLS TRUE.
  468. * @param String title
  469. * @return boolean value
  470. * */
  471. private boolean isFilterSetForTitle(String title){
  472. if (title.equals(FILTER_MENU_TITLE_BSSID)){
  473. return this.filter.hasBSSIDs();
  474. }
  475. if (title.equals(FILTER_MENU_TITLE_ESSID)){
  476. return this.filter.hasESSIDs();
  477. }
  478. if (title.equals(FILTER_MENU_TITLE_PROTOCOLS)){
  479. return this.filter.hasProtocols();
  480. }
  481. if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_BELOW)){
  482. return this.filter.hasBelowTimestamp();
  483. }
  484. if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_ABOVE)){
  485. return this.filter.hasAboveTimestamp();
  486. }
  487. return false;
  488. }
  489. /*clears the filter. Does not invoke populatelistview!*/
  490. private void clearFilter(){
  491. if(filter == null) this.filter = new LogFilter();
  492. this.filter.clear();
  493. }
  494. /*Returns all grouping titles
  495. * @param Context context
  496. * @return ArrayList<String> titles*/
  497. public ArrayList<String> groupingTitles(Context context){
  498. ArrayList<String> titles = new ArrayList<String>();
  499. for (String groupTitle : context.getResources().getStringArray(
  500. R.array.Grouping)) {
  501. titles.add(groupTitle);
  502. }
  503. return titles;
  504. }
  505. /*Returns all grouping titles.
  506. * @return ArrayList<String> tiles*/
  507. public ArrayList<String> groupingTitles(){
  508. ArrayList<String> titles = new ArrayList<String>();
  509. for (String groupTitle : this.getResources().getStringArray(
  510. R.array.Grouping)) {
  511. titles.add(groupTitle);
  512. }
  513. return titles;
  514. }
  515. /*Returns a bool array. This array is true at the index of the groupingKey in groupingTitles(), otherwise false.
  516. * @return boolean[] selection
  517. * */
  518. public boolean[] selectedGroup(){
  519. ArrayList<String> groups = this.groupingTitles();
  520. boolean[] selected = new boolean[groups.size()];
  521. int i = 0;
  522. for(String group : groups){
  523. selected[i] =(group.equals(this.groupingKey));
  524. i++;
  525. }
  526. return selected;
  527. }
  528. /*Returns all protocol titles / names.
  529. * @return ArrayList<String> protocolTitles
  530. * */
  531. public ArrayList<String> protocolTitles(){
  532. ArrayList<String> titles = new ArrayList<String>();
  533. for (String protocol : this.getResources().getStringArray(
  534. R.array.protocols)) {
  535. titles.add(protocol);
  536. }
  537. return titles;
  538. }
  539. /*Return a boolean array of the selected / filtered protocols. If the filter object has
  540. * an protocol from the protocolTitles() array, the index of it will be true, otherwise false.
  541. * @return boolean[] protocol selection
  542. * */
  543. public boolean[] selectedProtocols(){
  544. ArrayList<String> protocols = this.protocolTitles();
  545. boolean[] selected = new boolean[protocols.size()];
  546. int i = 0;
  547. for(String protocol : protocols){
  548. selected[i] =(this.filter.protocols.contains(protocol));
  549. i++;
  550. }
  551. return selected;
  552. }
  553. /*
  554. * Returns the Sorttype Titles
  555. * @return ArayList<String> Sort type titles
  556. * */
  557. public ArrayList<String> sortTypeTiles(){
  558. ArrayList<String> titles = new ArrayList<String>();
  559. titles.add(MainActivity.getContext().getString(R.string.rec_time));
  560. titles.add(MainActivity.getContext().getString(R.string.rec_protocol));
  561. titles.add(MainActivity.getContext().getString(R.string.BSSID));
  562. titles.add(MainActivity.getContext().getString(R.string.ESSID));
  563. return titles;
  564. }
  565. /*
  566. * Returns an boolean array. The array is true at the index of the selected sort type..
  567. * The index of the selected sort type is the same index in the sortTypeTiles array.
  568. * @return boolean array, length == sortTypeTiles().length
  569. * */
  570. public boolean[] selectedSorttype(){
  571. ArrayList<String> types = this.sortTypeTiles();
  572. boolean[] selected = new boolean[types.size()];
  573. int i = 0;
  574. for(String sorttype : types){
  575. selected[i] =(this.filter.sorttype.toString().equals(sorttype));
  576. i++;
  577. }
  578. return selected;
  579. }
  580. /*
  581. * Returns all unique bssids.
  582. * @return ArrayList<String>
  583. * */
  584. public ArrayList<String> bssids(){
  585. ArrayList<String> records = dbh.getUniqueBSSIDRecords();
  586. return records;
  587. }
  588. /*
  589. * Returns an boolean array. The array is true at the indices of the selected bssids.
  590. * The index of the selected bssid is the same index in the bssids() array.
  591. * @return boolean array, length == bssids().length
  592. * */
  593. public boolean[] selectedBSSIDs(){
  594. ArrayList<String> bssids = this.bssids();
  595. boolean[] selected = new boolean[bssids.size()];
  596. int i = 0;
  597. for(String bssid : bssids){
  598. selected[i] =(this.filter.BSSIDs.contains(bssid));
  599. i++;
  600. }
  601. return selected;
  602. }
  603. /*
  604. * Returns all unique essids.
  605. * @return ArrayList<String>
  606. * */
  607. public ArrayList<String> essids(){
  608. ArrayList<String> records = dbh.getUniqueESSIDRecords();
  609. return records;
  610. }
  611. /*
  612. * Returns an boolean array. The array is true at the indices of the selected essids.
  613. * The index of the selected essid is the same index in the essids() array.
  614. * @return boolean array, length == essids().length
  615. * */
  616. public boolean[] selectedESSIDs(){
  617. ArrayList<String> essids = this.essids();
  618. boolean[] selected = new boolean[essids.size()];
  619. int i = 0;
  620. for(String essid : essids){
  621. selected[i] =(this.filter.ESSIDs.contains(essid));
  622. i++;
  623. }
  624. return selected;
  625. }
  626. /*
  627. * Returns all filter menu titles.
  628. * @return ArrayList<String>
  629. * */
  630. private ArrayList<String> filterMenuTitles(){
  631. ArrayList<String> titles = new ArrayList<String>();
  632. titles.add(FILTER_MENU_TITLE_BSSID);
  633. titles.add(FILTER_MENU_TITLE_ESSID);
  634. titles.add(FILTER_MENU_TITLE_PROTOCOLS);
  635. titles.add(FILTER_MENU_TITLE_TIMESTAMP_ABOVE);
  636. titles.add(FILTER_MENU_TITLE_TIMESTAMP_BELOW);
  637. if (this.filter.isSet())titles.add(FILTER_MENU_TITLE_REMOVE);
  638. return titles;
  639. }
  640. /*****************************
  641. *
  642. * Listener Actions
  643. *
  644. * ***************************/
  645. public void onDateTimePickerPositiveClick(DateTimeDialogFragment dialog) {
  646. if(this.wasBelowTimePicker){
  647. this.filter.setBelowTimestamp(dialog.getDate());
  648. } else {
  649. this.filter.setAboveTimestamp(dialog.getDate());
  650. }
  651. this.populateListViewFromDB(this.expListView);
  652. }
  653. public void onDateTimePickerNegativeClick(DateTimeDialogFragment dialog) {
  654. if(this.wasBelowTimePicker){
  655. this.filter.setBelowTimestamp(Long.MAX_VALUE);
  656. } else {
  657. this.filter.setAboveTimestamp(Long.MIN_VALUE);
  658. }
  659. }
  660. public void onDialogPositiveClick(ChecklistDialog dialog) {
  661. String title = dialog.getTitle();
  662. if(title.equals(FILTER_MENU_TITLE_BSSID)){
  663. ArrayList<String> titles =dialog.getSelectedItemTitles();
  664. if (titles.size() == this.bssids().size()){
  665. this.filter.setBSSIDs(new ArrayList<String>());
  666. } else {
  667. this.filter.setBSSIDs(titles);
  668. }
  669. }
  670. if(title.equals(FILTER_MENU_TITLE_ESSID)){
  671. ArrayList<String> titles =dialog.getSelectedItemTitles();
  672. if (titles.size() == this.essids().size()){
  673. this.filter.setESSIDs(new ArrayList<String>());
  674. } else {
  675. this.filter.setESSIDs(titles);
  676. }
  677. }
  678. if(title.equals(FILTER_MENU_TITLE_PROTOCOLS)){
  679. ArrayList<String> protocols = dialog.getSelectedItemTitles();
  680. if (protocols.size() == this.protocolTitles().size()){
  681. this.filter.setProtocols(new ArrayList<String>());
  682. } else {
  683. this.filter.setProtocols(dialog.getSelectedItemTitles());
  684. }
  685. }
  686. if(title.equals(FILTER_MENU_TITLE_SORTING)){
  687. ArrayList<String> titles = dialog.getSelectedItemTitles();
  688. if (titles.size() == 0) return;
  689. String t = titles.get(0);
  690. int sortType = this.sortTypeTiles().indexOf(t);
  691. this.filter.setSorttype(SortType.values()[sortType]);
  692. }
  693. if (title.equals(FILTER_MENU_TITLE_GROUP)){
  694. ArrayList<String> titles = dialog.getSelectedItemTitles();
  695. if (titles.size() == 0) return;
  696. this.groupingKey = titles.get(0);
  697. }
  698. this.populateListViewFromDB(this.expListView);
  699. }
  700. public void onDialogNegativeClick(ChecklistDialog dialog) {}
  701. /*****************************
  702. *
  703. * TEST
  704. *
  705. * ***************************/
  706. /*
  707. * This will clear the database at first and than add new attacks.
  708. * @param int number of networks to create
  709. * @param int maximal number of attack per network
  710. * */
  711. private void addRecordToDB( int createNetworks, int attacksPerNetwork) {
  712. if ((dbh.getRecordCount() > 0)) dbh.clearData();
  713. Calendar cal = Calendar.getInstance();
  714. int maxProtocolsIndex = this.getResources().getStringArray(
  715. R.array.protocols).length;
  716. Random random = new Random();
  717. LatLng tudarmstadtLoc = new LatLng(49.86923, 8.6632768);
  718. final double ssidRadius = 0.1;
  719. final double bssidRadius = 0.004;
  720. int id = 0;
  721. for (int numOfNetworks = 0; numOfNetworks < createNetworks; numOfNetworks++){
  722. String ssidName = "WiFi" + ((numOfNetworks) + 1);
  723. String bssidName = "127.0.0." + ((numOfNetworks) + 1);
  724. int protocolIndex = numOfNetworks % maxProtocolsIndex;
  725. String protocolName = this.getResources().getStringArray(
  726. R.array.protocols)[protocolIndex];
  727. int numOfAttackPerNetwork = (Math.abs(random.nextInt()) % attacksPerNetwork) + 1;
  728. // ATTACKS PER NETWORK
  729. for (int attack = 0; attack < numOfAttackPerNetwork; attack++) {
  730. LatLng ssidLocation = new LatLng(tudarmstadtLoc.latitude - ssidRadius + 2.0 * ssidRadius * Math.random(), tudarmstadtLoc.longitude - ssidRadius + 2.0 * ssidRadius * Math.random());
  731. int numRecordsPerAttack = (Math.abs(random.nextInt()) % 5) + 1;
  732. double latitude = ssidLocation.latitude - bssidRadius + 2.0 * bssidRadius * Math.random();
  733. double longitude = ssidLocation.longitude - bssidRadius + 2.0 * bssidRadius * Math.random();
  734. // RECORDS PER ATTACK
  735. for (int recC = id; recC < id + numRecordsPerAttack; recC++) {
  736. Record record = new Record();
  737. record.setId(recC);
  738. record.setAttack_id(id);
  739. record.setSsid(ssidName);
  740. record.setBssid(bssidName);
  741. // GO BACK IN TIME
  742. record.setTimestamp(cal.getTimeInMillis()
  743. - ((recC * 60 * 60 * 24) * 1000) + (1000 * ((recC - id) + 1)));
  744. record.setProtocol(protocolName);
  745. record.setLocalIP(bssidName);
  746. record.setType(Record.TYPE.SEND);
  747. record.setLatitude(latitude);
  748. record.setLongitude(longitude);
  749. dbh.addRecord(record);
  750. }
  751. id+=numRecordsPerAttack;
  752. }
  753. }
  754. int countAllLogs = dbh.getAllRecords().size();
  755. int countRecords = dbh.getRecordCount();
  756. int countAttacks = dbh.getAttackCount();
  757. if ((countRecords == 0)) {
  758. Record rec = dbh.getRecordOfAttackId(0);
  759. Record rec2 = dbh.getRecord(0);
  760. System.out.println("" + "Could not create logs!");
  761. }
  762. }
  763. /*Navigation. Shows the record detail view for the given record
  764. * @param Record record to show
  765. * */
  766. private void pushRecordDetailViewForRecord(Record record){
  767. FragmentManager fm = this.getActivity().getFragmentManager();
  768. if (fm != null){
  769. RecordDetailFragment newFragment = new RecordDetailFragment();
  770. newFragment.setRecord(record);
  771. newFragment.setUpNavigatible(true);
  772. MainActivity.getInstance().injectFragment(newFragment);
  773. }
  774. }
  775. }