RecordOverviewFragment.java 40 KB

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