RecordOverviewFragment.java 37 KB

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