RecordOverviewFragment.java 38 KB

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