RecordOverviewFragment.java 39 KB

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