RecordOverviewFragment.java 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465
  1. package de.tudarmstadt.informatik.hostage.ui.fragment;
  2. import android.annotation.SuppressLint;
  3. import android.app.Activity;
  4. import android.app.AlertDialog;
  5. import android.app.FragmentManager;
  6. import android.content.Context;
  7. import android.content.DialogInterface;
  8. import android.content.Intent;
  9. import android.content.SharedPreferences;
  10. import android.os.Bundle;
  11. import android.preference.PreferenceManager;
  12. import android.util.Log;
  13. import android.view.LayoutInflater;
  14. import android.view.Menu;
  15. import android.view.MenuInflater;
  16. import android.view.MenuItem;
  17. import android.view.View;
  18. import android.view.ViewGroup;
  19. import android.widget.ExpandableListView;
  20. import android.widget.ImageButton;
  21. import android.widget.ProgressBar;
  22. import android.widget.Toast;
  23. import com.google.android.gms.maps.model.LatLng;
  24. import java.text.DateFormat;
  25. import java.text.SimpleDateFormat;
  26. import java.util.ArrayList;
  27. import java.util.Calendar;
  28. import java.util.Collections;
  29. import java.util.Comparator;
  30. import java.util.Date;
  31. import java.util.HashMap;
  32. import java.util.List;
  33. import java.util.Locale;
  34. import java.util.Random;
  35. import de.tudarmstadt.informatik.hostage.Hostage;
  36. import de.tudarmstadt.informatik.hostage.R;
  37. import de.tudarmstadt.informatik.hostage.logging.AttackRecord;
  38. import de.tudarmstadt.informatik.hostage.logging.LogExport;
  39. import de.tudarmstadt.informatik.hostage.logging.MessageRecord;
  40. import de.tudarmstadt.informatik.hostage.logging.NetworkRecord;
  41. import de.tudarmstadt.informatik.hostage.logging.Record;
  42. import de.tudarmstadt.informatik.hostage.logging.SyncData;
  43. import de.tudarmstadt.informatik.hostage.logging.SyncInfo;
  44. import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper;
  45. import de.tudarmstadt.informatik.hostage.sync.Synchronizer;
  46. import de.tudarmstadt.informatik.hostage.sync.bluetooth.BluetoothSyncActivity;
  47. import de.tudarmstadt.informatik.hostage.sync.nfc.NFCSyncActivity;
  48. import de.tudarmstadt.informatik.hostage.sync.p2p.P2PSyncActivity;
  49. import de.tudarmstadt.informatik.hostage.sync.tracing.TracingSyncActivity;
  50. import de.tudarmstadt.informatik.hostage.ui.activity.MainActivity;
  51. import de.tudarmstadt.informatik.hostage.ui.adapter.RecordListAdapter;
  52. import de.tudarmstadt.informatik.hostage.ui.dialog.ChecklistDialog;
  53. import de.tudarmstadt.informatik.hostage.ui.dialog.DateTimeDialogFragment;
  54. import de.tudarmstadt.informatik.hostage.ui.model.ExpandableListItem;
  55. import de.tudarmstadt.informatik.hostage.ui.model.LogFilter;
  56. import de.tudarmstadt.informatik.hostage.ui.model.LogFilter.SortType;
  57. import de.tudarmstadt.informatik.hostage.ui.popup.AbstractPopup;
  58. import de.tudarmstadt.informatik.hostage.ui.popup.AbstractPopupItem;
  59. import de.tudarmstadt.informatik.hostage.ui.popup.SimplePopupItem;
  60. import de.tudarmstadt.informatik.hostage.ui.popup.SimplePopupTable;
  61. import de.tudarmstadt.informatik.hostage.ui.popup.SplitPopupItem;
  62. public class RecordOverviewFragment extends UpNavigatibleFragment implements ChecklistDialog.ChecklistDialogListener, DateTimeDialogFragment.DateTimeDialogFragmentListener {
  63. static final String FILTER_MENU_TITLE_BSSID = MainActivity.getContext().getString(R.string.BSSID);
  64. static final String FILTER_MENU_TITLE_ESSID = MainActivity.getContext().getString(R.string.ESSID);
  65. static final String FILTER_MENU_TITLE_PROTOCOLS = MainActivity.getContext().getString(R.string.rec_protocol);
  66. static final String FILTER_MENU_TITLE_TIMESTAMP_BELOW = MainActivity.getContext().getString(
  67. R.string.rec_latest);
  68. static final String FILTER_MENU_TITLE_TIMESTAMP_ABOVE = MainActivity.getContext().getString(
  69. R.string.rec_earliest);
  70. static final String FILTER_MENU_TITLE_SORTING = MainActivity.getContext().getString(R.string.rec_sortby);
  71. static final String FILTER_MENU_TITLE_REMOVE = MainActivity.getContext().getString(R.string.rec_reset_filter);
  72. static final String FILTER_MENU_TITLE_GROUP = MainActivity.getContext().getString(
  73. R.string.rec_group_by);
  74. static final String FILTER_MENU_POPUP_TITLE = MainActivity.getContext().getString(
  75. R.string.rec_filter_by);
  76. static final int DEFAULT_GROUPING_KEY_INDEX = 0;
  77. private Hostage service;
  78. private boolean wasBelowTimePicker;
  79. private LogFilter filter;
  80. private boolean showFilterButton;
  81. private View rootView;
  82. private int mListPosition = -1;
  83. private int mItemPosition = -1;
  84. public String groupingKey;
  85. private ExpandableListView expListView;
  86. private ProgressBar spinner;
  87. private Toast noDataNotificationToast;
  88. HostageDBOpenHelper dbh;
  89. private String sectionToOpen = "";
  90. private ArrayList<Integer> openSections;
  91. private SharedPreferences pref;
  92. Thread loader;
  93. /* DATE CONVERSION STUFF*/
  94. static final DateFormat localisedDateFormatter = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault());
  95. // DATE WHICH PATTERN
  96. static final String localDatePattern = ((SimpleDateFormat)localisedDateFormatter).toLocalizedPattern();
  97. static final String groupingDatePattern = "MMMM yyyy";
  98. // INSERT HERE YOUR DATE PATERN
  99. static final SimpleDateFormat groupingDateFormatter = new SimpleDateFormat(groupingDatePattern);
  100. static final Calendar calendar = Calendar.getInstance();
  101. // DATE STRINGS
  102. static final String TODAY = MainActivity.getInstance().getResources().getString( R.string.TODAY);
  103. static final String YESTERDAY = MainActivity.getInstance().getResources().getString( R.string.YESTERDAY);
  104. private SyncInfo si ;// = s.getSyncInfo();
  105. private SyncData sd ;//= s.getSyncData(si);
  106. /**
  107. * Constructor
  108. */
  109. public RecordOverviewFragment(){}
  110. @Override
  111. public void onCreate(Bundle savedInstanceState) {
  112. super.onCreate(savedInstanceState);
  113. setHasOptionsMenu(true);
  114. }
  115. @Override
  116. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  117. Bundle savedInstanceState) {
  118. setHasOptionsMenu(true);
  119. getActivity().setTitle(getResources().getString(R.string.drawer_records));
  120. dbh = new HostageDBOpenHelper(this.getActivity().getBaseContext());
  121. pref = PreferenceManager.getDefaultSharedPreferences(getActivity());
  122. // Get the message from the intent
  123. // this.addRecordToDB(4,4,4);
  124. // Synchronizer s = new Synchronizer(this.dbh);
  125. // si = s.getSyncInfo();
  126. // sd = s.getSyncData(si);
  127. // s.updateFromSyncData(sd);
  128. if (this.filter == null){
  129. Intent intent = this.getActivity().getIntent();
  130. LogFilter filter = intent.getParcelableExtra(LogFilter.LOG_FILTER_INTENT_KEY);
  131. if(filter == null){
  132. this.clearFilter();
  133. } else {
  134. this.filter = filter;
  135. }
  136. }
  137. if (this.groupingKey == null) this.groupingKey = this.groupingTitles().get(DEFAULT_GROUPING_KEY_INDEX);
  138. this.setShowFilterButton(!this.filter.isNotEditable());
  139. View rootView = inflater.inflate(this.getLayoutId(), container, false);
  140. this.rootView = rootView;
  141. ExpandableListView mylist = (ExpandableListView) rootView.findViewById(R.id.loglistview);
  142. this.spinner =(ProgressBar) rootView.findViewById(R.id.progressBar1);
  143. this.spinner.setVisibility(View.GONE);
  144. this.expListView = mylist;
  145. this.initialiseListView();
  146. ImageButton filterButton = (ImageButton) rootView.findViewById(R.id.FilterButton);
  147. filterButton.setOnClickListener(new View.OnClickListener() {
  148. public void onClick(View v) {
  149. RecordOverviewFragment.this.openFilterPopupMenuOnView(v);
  150. }
  151. });
  152. filterButton.setVisibility(this.showFilterButton? View.VISIBLE : View.INVISIBLE);
  153. ImageButton sortButton = (ImageButton) rootView.findViewById(R.id.SortButton);
  154. sortButton.setOnClickListener(new View.OnClickListener() {
  155. public void onClick(View v) {
  156. // Open SortMenu
  157. RecordOverviewFragment.this.openSortingDialog();
  158. }
  159. });
  160. ImageButton groupButton = (ImageButton) rootView.findViewById(R.id.GroupButton);
  161. groupButton.setOnClickListener(new View.OnClickListener() {
  162. public void onClick(View v) {
  163. // Open SortMenu
  164. RecordOverviewFragment.this.openGroupingDialog();
  165. }
  166. });
  167. return rootView;
  168. }
  169. /**Initialises the expandable list view in a backgorund thread*/
  170. private void initialiseListView(){
  171. if (loader != null) loader.interrupt();
  172. this.spinner.setVisibility(View.VISIBLE);
  173. loader = new Thread(new Runnable(){
  174. private void updateUI(final RecordListAdapter currentAdapter)
  175. {
  176. if(loader.isInterrupted()){
  177. return;
  178. }
  179. Activity activity = RecordOverviewFragment.this.getActivity();
  180. if (activity != null){
  181. activity.runOnUiThread(new Runnable() {
  182. @Override
  183. public void run() {
  184. RecordOverviewFragment.this.expListView.setAdapter(currentAdapter);
  185. // Update view and remove loading spinner etc...
  186. RecordListAdapter adapter = (RecordListAdapter) RecordOverviewFragment.this.expListView.getExpandableListAdapter();
  187. if (adapter != null){
  188. adapter.notifyDataSetChanged();
  189. if (adapter.getGroupCount() >= 1){
  190. RecordOverviewFragment.this.expListView.expandGroup(DEFAULT_GROUPING_KEY_INDEX);
  191. } else {
  192. RecordOverviewFragment.this.setSectionToOpen(RecordOverviewFragment.this.sectionToOpen);
  193. }
  194. }
  195. if (RecordOverviewFragment.this.openSections != null && RecordOverviewFragment.this.openSections.size() != 0){
  196. for (int i = 0; i < RecordOverviewFragment.this.openSections.size(); i++){
  197. int index = RecordOverviewFragment.this.openSections.get(i);
  198. RecordOverviewFragment.this.expListView.expandGroup(index);
  199. }
  200. } else {
  201. RecordOverviewFragment.this.openSections = new ArrayList<Integer>();
  202. }
  203. if (mListPosition != -1 && mItemPosition != -1)
  204. RecordOverviewFragment.this.expListView.setSelectedChild(mListPosition, mItemPosition, true);
  205. mListPosition = -1;
  206. mItemPosition = -1;
  207. registerListClickCallback(RecordOverviewFragment.this.expListView);
  208. RecordOverviewFragment.this.spinner.setVisibility(View.GONE);
  209. RecordOverviewFragment.this.actualiseFilterButton();
  210. RecordOverviewFragment.this.showEmptyDataNotification();
  211. }
  212. });
  213. }
  214. }
  215. private RecordListAdapter doInBackground()
  216. {
  217. return populateListViewFromDB(RecordOverviewFragment.this.expListView);
  218. }
  219. @Override
  220. public void run()
  221. {
  222. //RecordOverviewFragment.this.addRecordToDB(40, 10, 4);
  223. updateUI(doInBackground());
  224. }
  225. });
  226. loader.start();
  227. this.actualiseFilterButton();
  228. }
  229. /**
  230. * Returns the Fragment layout ID
  231. * @return int The fragment layout ID
  232. * */
  233. public int getLayoutId(){
  234. return R.layout.fragment_record_list;
  235. }
  236. /**
  237. * Gets called if the user clicks on item in the filter menu.
  238. *
  239. * @param item {@link AbstractPopupItem AbstractPopupItem }
  240. * */
  241. public void onFilterMenuItemSelected(AbstractPopupItem item) {
  242. String title = item.getTitle();
  243. if (item instanceof SplitPopupItem){
  244. SplitPopupItem splitItem = (SplitPopupItem)item;
  245. if (splitItem.wasRightTouch){
  246. this.openTimestampToFilterDialog();
  247. } else {
  248. this.openTimestampFromFilterDialog();
  249. }
  250. return;
  251. }
  252. if (title != null){
  253. if(title.equals(FILTER_MENU_TITLE_BSSID)){
  254. this.openBSSIDFilterDialog();
  255. }
  256. if(title.equals(FILTER_MENU_TITLE_ESSID)){
  257. this.openESSIDFilterDialog();
  258. }
  259. if(title.equals(FILTER_MENU_TITLE_PROTOCOLS)){
  260. this.openProtocolsFilterDialog();
  261. }
  262. if(title.equals(FILTER_MENU_TITLE_SORTING)){
  263. this.openSortingDialog();
  264. }
  265. if(title.equals(FILTER_MENU_TITLE_REMOVE)){
  266. this.clearFilter();
  267. this.actualiseListViewInBackground();
  268. }
  269. if(title.equals(FILTER_MENU_TITLE_TIMESTAMP_BELOW)){
  270. this.openTimestampToFilterDialog();
  271. }
  272. if(title.equals(FILTER_MENU_TITLE_TIMESTAMP_ABOVE)){
  273. this.openTimestampFromFilterDialog();
  274. }
  275. }
  276. //return super.onOptionsItemSelected(item);
  277. }
  278. @Override
  279. public void onStart() {
  280. super.onStart();
  281. if (this.expListView.getExpandableListAdapter() != null){
  282. if (this.expListView.getExpandableListAdapter().getGroupCount() == 1){
  283. this.expListView.expandGroup(0);
  284. } else {
  285. this.setSectionToOpen(this.sectionToOpen);
  286. }
  287. }
  288. }
  289. @Override
  290. public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
  291. // Inflate the menu items for use in the action bar
  292. inflater.inflate(R.menu.records_overview_actions, menu);
  293. }
  294. @Override
  295. public boolean onOptionsItemSelected(MenuItem item) {
  296. switch (item.getItemId()) {
  297. case R.id.records_action_synchronize:
  298. AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
  299. builder.setTitle(MainActivity.getInstance().getString(R.string.rec_sync_rec));
  300. builder.setItems(new String[]{
  301. MainActivity.getInstance().getString(R.string.rec_via_bt),
  302. MainActivity.getInstance().getString(R.string.rec_via_nfc),
  303. MainActivity.getInstance().getString(R.string.rec_via_online),
  304. "Via WifiDirect"
  305. }, new DialogInterface.OnClickListener() {
  306. @Override
  307. public void onClick(DialogInterface dialog, int position) {
  308. switch(position){
  309. case 0:
  310. getActivity().startActivity(new Intent(getActivity(), BluetoothSyncActivity.class));
  311. break;
  312. case 1:
  313. getActivity().startActivity(new Intent(getActivity(), NFCSyncActivity.class));
  314. break;
  315. case 2:
  316. getActivity().startActivity(new Intent(getActivity(), TracingSyncActivity.class));
  317. break;
  318. case 3:
  319. getActivity().startActivity(new Intent(getActivity(), P2PSyncActivity.class));
  320. break;
  321. }
  322. }
  323. });
  324. builder.create();
  325. builder.show();
  326. return true;
  327. case R.id.records_action_export:
  328. AlertDialog.Builder builderExport = new AlertDialog.Builder(getActivity());
  329. builderExport.setTitle(MainActivity.getInstance().getString(R.string.rec_choose_export_format));
  330. builderExport.setItems(R.array.format, new DialogInterface.OnClickListener() {
  331. @Override
  332. public void onClick(DialogInterface dialog, int position) {
  333. //RecordOverviewFragment.this.exportDatabase(position);
  334. Intent intent = new Intent(getActivity(), LogExport.class);
  335. intent.setAction(LogExport.ACTION_EXPORT_DATABASE);
  336. intent.putExtra(LogExport.FORMAT_EXPORT_DATABASE, position);
  337. RecordOverviewFragment.this.getActivity().startService(intent);
  338. }
  339. });
  340. builderExport.create();
  341. builderExport.show();
  342. return true;
  343. }
  344. return false;
  345. }
  346. /*****************************
  347. *
  348. * Public API
  349. *
  350. * ***************************/
  351. /**
  352. * Group records by SSID and expand given SSID
  353. *
  354. * @param SSID the SSID
  355. */
  356. public void showDetailsForSSID(Context context, String SSID) {
  357. Log.e("RecordOverviewFragment", "Implement showDetailsForSSID!!");
  358. this.clearFilter();
  359. int ESSID_INDEX = 2;
  360. ArrayList<String> ssids = new ArrayList<String>();
  361. this.sectionToOpen = SSID;
  362. this.groupingKey = this.groupingTitles().get(ESSID_INDEX);
  363. }
  364. /*****************************
  365. *
  366. * ListView Stuff
  367. *
  368. * ***************************/
  369. /**
  370. * Reloads the data in the ExpandableListView for the given filter object.
  371. * @param mylist {@link ExpandableListView ExpandableListView}
  372. * */
  373. private RecordListAdapter populateListViewFromDB(ExpandableListView mylist) {
  374. ArrayList<String> groupTitle = new ArrayList<String>();
  375. HashMap<String, ArrayList<ExpandableListItem>> sectionData = this.fetchDataForFilter(this.filter, groupTitle);
  376. RecordListAdapter adapter = null;
  377. if (mylist.getAdapter() != null && mylist.getAdapter() instanceof RecordListAdapter){
  378. adapter = (RecordListAdapter) mylist.getAdapter();
  379. adapter.setData(sectionData);
  380. adapter.setSectionHeader(groupTitle);
  381. } else {
  382. adapter = new RecordListAdapter( RecordOverviewFragment.this.getApplicationContext(), groupTitle, sectionData);
  383. }
  384. return adapter;
  385. }
  386. private HashMap<String, ArrayList<ExpandableListItem>> fetchDataForFilter(LogFilter filter, ArrayList<String> groupTitle){
  387. HashMap<String, ArrayList<ExpandableListItem>> sectionData = new HashMap<String, ArrayList<ExpandableListItem>>();
  388. ArrayList<Record> data = dbh.getRecordsForFilter(filter == null ? this.filter : filter);
  389. // Adding Items to ListView
  390. 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)};
  391. int ids[] = new int[] {R.id.RecordTextFieldBSSID, R.id.RecordTextFieldSSID, R.id.RecordTextFieldProtocol, R.id.RecordTextFieldTimestamp };
  392. HashMap<String, Integer> mapping = new HashMap<String, Integer>();
  393. int i = 0;
  394. for(String key : keys){
  395. mapping.put(key, ids[i]);
  396. i++;
  397. }
  398. if (groupTitle == null){
  399. groupTitle = new ArrayList<String>();
  400. } else {
  401. groupTitle.clear();
  402. }
  403. for (Record val : data) {
  404. // DO GROUPING IN HERE
  405. HashMap<String, String> map = new HashMap<String, String>();
  406. map.put(RecordOverviewFragment.this.getString(R.string.RecordBSSID), val.getBssid());
  407. map.put(RecordOverviewFragment.this.getString(R.string.RecordSSID), val.getSsid());
  408. map.put(RecordOverviewFragment.this.getString(R.string.RecordProtocol), val.getProtocol());
  409. map.put(RecordOverviewFragment.this.getString(R.string.RecordTimestamp),
  410. RecordOverviewFragment.this.getDateAsString(val.getTimestamp()));
  411. ExpandableListItem item = new ExpandableListItem();
  412. item.setData(map);
  413. item.setId_Mapping(mapping);
  414. item.setTag(val.getAttack_id());
  415. String groupID = RecordOverviewFragment.this.getGroupValue(val);
  416. ArrayList<ExpandableListItem> items = sectionData.get(groupID);
  417. if (items == null) {
  418. items = new ArrayList<ExpandableListItem>();
  419. sectionData.put(groupID, items);
  420. groupTitle.add(groupID);
  421. }
  422. items.add(item);
  423. }
  424. if (this.groupingKey.equals(this.groupingTitles().get(DEFAULT_GROUPING_KEY_INDEX))){
  425. Collections.sort(groupTitle,new DateStringComparator());
  426. } else {
  427. Collections.sort(groupTitle, new Comparator<String>() {
  428. @Override
  429. public int compare(String s1, String s2) {
  430. return s1.compareToIgnoreCase(s2);
  431. }
  432. });
  433. }
  434. return sectionData;
  435. }
  436. /**
  437. * The DateStringComparator compares formatted date strings by converting the into date.
  438. * This class is mainly used for grouping the records by their timestamp.
  439. */
  440. class DateStringComparator implements Comparator<String>
  441. {
  442. public int compare(String lhs, String rhs)
  443. {
  444. Date date1 = RecordOverviewFragment.this.convertStringToDate(lhs);
  445. Date date2 = RecordOverviewFragment.this.convertStringToDate(rhs);
  446. return date2.compareTo(date1);
  447. }
  448. }
  449. /**
  450. * Actualises the list in a background thread
  451. */
  452. private void actualiseListViewInBackground(){
  453. if (loader != null && loader.isAlive()) loader.interrupt();
  454. loader = null;
  455. this.spinner.setVisibility(View.VISIBLE);
  456. this.actualiseFilterButton();
  457. loader = new Thread(new Runnable() {
  458. @Override
  459. public void run() {
  460. this.runOnUiThread(this.doInBackground());
  461. }
  462. private RecordListAdapter doInBackground(){
  463. return RecordOverviewFragment.this.populateListViewFromDB(RecordOverviewFragment.this.expListView);
  464. }
  465. private void runOnUiThread(final RecordListAdapter adapter){
  466. Activity actv = RecordOverviewFragment.this.getActivity();
  467. if (actv != null){
  468. actv.runOnUiThread(new Runnable() {
  469. @Override
  470. public void run() {
  471. this.actualiseUI();
  472. }
  473. private void actualiseUI(){
  474. if (adapter != null){
  475. RecordOverviewFragment.this.expListView.setAdapter(adapter);
  476. adapter.notifyDataSetChanged();
  477. RecordOverviewFragment.this.spinner.setVisibility(View.GONE);
  478. }
  479. RecordOverviewFragment.this.showEmptyDataNotification();
  480. }
  481. });
  482. }
  483. }
  484. });
  485. loader.start();
  486. }
  487. /**
  488. * Shows a small toast if the data to show is empty (no records).
  489. */
  490. private void showEmptyDataNotification(){
  491. if (RecordOverviewFragment.this.noDataNotificationToast == null){
  492. RecordOverviewFragment.this.noDataNotificationToast = Toast.makeText(getApplicationContext(), R.string.no_data_notification, Toast.LENGTH_SHORT);
  493. }
  494. RecordListAdapter adapter = (RecordListAdapter) RecordOverviewFragment.this.expListView.getExpandableListAdapter();
  495. if (this.getFilterButton().getVisibility() == View.VISIBLE && this.filter.isSet()){
  496. this.noDataNotificationToast.setText(R.string.no_data_notification);
  497. } else {
  498. this.noDataNotificationToast.setText(R.string.no_data_notification_no_filter);
  499. }
  500. if (adapter == null || adapter.getData().isEmpty())
  501. RecordOverviewFragment.this.noDataNotificationToast.show();
  502. }
  503. /**This will open a section in the ExpandableListView with the same title as the parameter s.
  504. *
  505. * @param s String (the section title to open)
  506. *
  507. * */
  508. private void setSectionToOpen(String s){
  509. this.sectionToOpen = s;
  510. if (this.sectionToOpen != null && this.sectionToOpen.length() != 0){
  511. if (this.getGroupTitles().contains(this.sectionToOpen)){
  512. int section = this.getGroupTitles().indexOf(this.sectionToOpen);
  513. this.expListView.expandGroup(section);
  514. this.sectionToOpen = "";
  515. }
  516. }
  517. }
  518. /**
  519. * Returns the base context.
  520. * @return Context baseContext
  521. * */
  522. private Context getBaseContext(){
  523. return this.getActivity().getBaseContext();
  524. }
  525. /**Returns the application context.
  526. * @return Context application context
  527. * */
  528. private Context getApplicationContext(){
  529. return this.getActivity().getApplicationContext();
  530. }
  531. /**Sets the list view listener on the given ExpandableListView.
  532. *
  533. * @param mylist {@link ExpandableListView ExpandableListView }
  534. * */
  535. private void registerListClickCallback(ExpandableListView mylist) {
  536. mylist.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
  537. @Override
  538. public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i2, long l) {
  539. RecordListAdapter adapter = (RecordListAdapter)expandableListView.getExpandableListAdapter();
  540. ExpandableListItem item = (ExpandableListItem)adapter.getChild(i,i2);
  541. mListPosition = i;
  542. mItemPosition = i2;
  543. HostageDBOpenHelper dbh = new HostageDBOpenHelper(getBaseContext());
  544. Record rec = dbh.getRecordOfAttackId((int) item.getTag());
  545. RecordOverviewFragment.this.pushRecordDetailViewForRecord(rec);
  546. return true;
  547. }
  548. });
  549. mylist.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
  550. @Override
  551. public void onGroupExpand(int i) {
  552. RecordOverviewFragment.this.openSections.add(new Integer(i));
  553. }
  554. });
  555. mylist.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
  556. @Override
  557. public void onGroupCollapse(int i) {
  558. RecordOverviewFragment.this.openSections.remove(new Integer(i));
  559. }
  560. });
  561. }
  562. /*****************************
  563. *
  564. * Date Transformation / Conversion
  565. *
  566. * ***************************/
  567. /**Returns the localised date format for the given timestamp
  568. * @param timeStamp long */
  569. @SuppressLint("SimpleDateFormat")
  570. private String getDateAsString(long timeStamp) {
  571. Date date = (new Date(timeStamp));
  572. try {
  573. DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.getDefault());
  574. return formatter.format(date);
  575. } catch (Exception ex) {
  576. return "---";
  577. }
  578. }
  579. /**
  580. * Returns the timestamp in a own format.
  581. * Depending on the returned format the grouping by timestamp will change.
  582. *
  583. * e.g.
  584. * If you return a DateAsMonth formatted Date the records will be mapped by their month and year.
  585. * If you return the a DateAsDay formatted Date the records will be mapped by their day, month and year.
  586. * and so on...
  587. *
  588. * @param timestamp long
  589. * @return formatted date String
  590. */
  591. public String getFormattedDateForGrouping(long timestamp) {
  592. // DECIDE WHICH KIND OF FORMAT SHOULD BE USED
  593. // MONTH FORMAT
  594. String date = this.getDateAsMonthString(timestamp);
  595. // DAY FORMAT
  596. //String date = this.getDateAsDayString(timestamp);
  597. return date;
  598. }
  599. /**Returns a date as a formated string
  600. * @param timestamp date
  601. * @return String date format is localised*/
  602. @SuppressLint("SimpleDateFormat")
  603. private String getDateAsDayString(long timestamp) {
  604. try {
  605. Date netDate = (new Date(timestamp));
  606. String dateString;
  607. long date = this.dayMilliseconds(timestamp);
  608. if(this.todayMilliseconds() == date ){
  609. dateString = TODAY;
  610. }else if(this.yesterdayMilliseconds() == date ){
  611. dateString = YESTERDAY;
  612. } else {
  613. dateString = localisedDateFormatter.format(netDate);
  614. }
  615. return dateString;
  616. } catch (Exception ex) {
  617. return "---";
  618. }
  619. }
  620. /**
  621. * Converts a formatted DateString into a date.
  622. * @param dateString String
  623. * @return Date
  624. */
  625. private Date convertStringToDate(String dateString){
  626. if (dateString != null && dateString.length() != 0){
  627. SimpleDateFormat dateFormat = groupingDateFormatter; //new SimpleDateFormat(localDatePattern);
  628. Date date;
  629. try {
  630. if (dateString.equals(TODAY)){
  631. long millisec = RecordOverviewFragment.this.todayMilliseconds();
  632. date = new Date(millisec);
  633. } else if (dateString.equals(YESTERDAY)){
  634. long millisec = RecordOverviewFragment.this.yesterdayMilliseconds();
  635. date = new Date(millisec);
  636. } else {
  637. date = dateFormat.parse(dateString);
  638. }
  639. return date;
  640. } catch (java.text.ParseException e ) {
  641. date = new Date(0);
  642. return date;
  643. }
  644. } else {
  645. return new Date(0);
  646. }
  647. }
  648. /**
  649. * Returns the milliseconds for the day today (not the time).
  650. * @return long
  651. */
  652. private long todayMilliseconds(){
  653. Date current = new Date();
  654. calendar.setTimeInMillis(current.getTime());
  655. int day = calendar.get(Calendar.DATE);
  656. int month = calendar.get(Calendar.MONTH);
  657. int year = calendar.get(Calendar.YEAR);
  658. calendar.set(year, month, day, 0,0,0);
  659. long milli = calendar.getTimeInMillis();
  660. Date today = new Date(milli);
  661. return (milli / (long) 1000) * (long) 1000;
  662. }
  663. /**
  664. * Returns the milliseconds for the day yesterday (not the time).
  665. * @return long
  666. */
  667. private long yesterdayMilliseconds(){
  668. Date current = new Date();
  669. calendar.setTimeInMillis(current.getTime());
  670. int day = calendar.get(Calendar.DATE);
  671. int month = calendar.get(Calendar.MONTH);
  672. int year = calendar.get(Calendar.YEAR);
  673. calendar.set(year, month, day, 0,0,0);
  674. calendar.add(Calendar.DATE, -1);
  675. long milli = calendar.getTimeInMillis();
  676. Date today = new Date(milli);
  677. return (milli / (long) 1000) * (long) 1000;
  678. }
  679. /**
  680. * returns just the date not the time of a date.
  681. * @param date Date
  682. * @return long
  683. */
  684. private long dayMilliseconds(long date){
  685. //Date current = new Date();
  686. calendar.setTimeInMillis(date);
  687. int day = calendar.get(Calendar.DATE);
  688. int month = calendar.get(Calendar.MONTH);
  689. int year = calendar.get(Calendar.YEAR);
  690. calendar.set(year, month, day, 0,0,0);
  691. long milli = calendar.getTimeInMillis();
  692. Date test = new Date(milli);
  693. return (milli / (long) 1000) * (long) 1000;
  694. }
  695. /**Returns a date as a formated string
  696. * @param timeStamp date
  697. * @return String date format is localised*/
  698. @SuppressLint("SimpleDateFormat")
  699. private String getDateAsMonthString(long timeStamp) {
  700. try {
  701. Date netDate = (new Date(timeStamp));
  702. return groupingDateFormatter.format(netDate);
  703. } catch (Exception ex) {
  704. return "xx";
  705. }
  706. }
  707. /*****************************
  708. *
  709. * Getter / Setter
  710. *
  711. * ***************************/
  712. public boolean isShowFilterButton() {
  713. return showFilterButton;
  714. }
  715. public void setShowFilterButton(boolean showFilterButton) {
  716. this.showFilterButton = showFilterButton;
  717. }
  718. /**
  719. * Set the group key for grouping the records.
  720. * All possible grouping keys are:
  721. * R.string.date,
  722. * R.string.rec_protocol,
  723. * R.string.ESSID,
  724. * R.string.BSSID
  725. * @param key String
  726. */
  727. public void setGroupKey(String key){
  728. this.groupingKey = key;
  729. }
  730. public void setFilter(LogFilter filter){
  731. this.filter = filter;
  732. }
  733. /*****************************
  734. *
  735. * Open Dialog Methods
  736. *
  737. * ***************************/
  738. /**Opens the grouping dialog*/
  739. private void openGroupingDialog(){
  740. ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_TITLE_GROUP, this.groupingTitles(), this.selectedGroup(), false , this);
  741. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_GROUP);
  742. }
  743. /**opens the bssid filter dialog*/
  744. private void openBSSIDFilterDialog(){
  745. ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_TITLE_BSSID,this.bssids(), this.selectedBSSIDs(), true , this);
  746. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_BSSID);
  747. }
  748. /**opens the essid filter dialog*/
  749. private void openESSIDFilterDialog(){
  750. ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_TITLE_ESSID,this.essids(), this.selectedESSIDs(), true , this);
  751. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_ESSID);
  752. }
  753. /**opens the protocol filter dialog*/
  754. private void openProtocolsFilterDialog(){
  755. ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_TITLE_PROTOCOLS,this.protocolTitles(), this.selectedProtocols(), true , this);
  756. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_PROTOCOLS);
  757. }
  758. /**opens the timestamp filter dialog (minimal timestamp required)*/
  759. private void openTimestampFromFilterDialog(){
  760. this.wasBelowTimePicker = false;
  761. DateTimeDialogFragment newFragment = new DateTimeDialogFragment(this.getActivity());
  762. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_SORTING);
  763. if (this.filter.aboveTimestamp != Long.MIN_VALUE)newFragment.setDate(this.filter.aboveTimestamp);
  764. }
  765. /**opens time timestamp filter dialog (maximal timestamp required)*/
  766. private void openTimestampToFilterDialog(){
  767. this.wasBelowTimePicker = true;
  768. DateTimeDialogFragment newFragment = new DateTimeDialogFragment(this.getActivity());
  769. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_SORTING);
  770. if (this.filter.belowTimestamp != Long.MAX_VALUE) newFragment.setDate(this.filter.belowTimestamp);
  771. }
  772. /**opens the sorting dialog*/
  773. private void openSortingDialog(){
  774. ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_TITLE_SORTING,this.sortTypeTiles(), this.selectedSorttype(), false , this);
  775. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_SORTING);
  776. }
  777. /*****************************
  778. *
  779. * Grouping Stuff
  780. *
  781. * ***************************/
  782. /**returns the group title for the given record. Uses the groupingKey to decied which value of the record should be used.
  783. * @param rec {@link Record Record }
  784. * @return String grouptitle*/
  785. public String getGroupValue(Record rec){
  786. int index = this.groupingTitles().indexOf(this.groupingKey);
  787. switch (index){
  788. case 1:
  789. return rec.getProtocol();
  790. case 2:
  791. return rec.getSsid();
  792. case 3:
  793. return rec.getBssid();
  794. case 0:
  795. return this.getFormattedDateForGrouping(rec.getTimestamp());
  796. default:
  797. return this.getFormattedDateForGrouping(rec.getTimestamp());
  798. }
  799. }
  800. /**Returns the Group titles for the specified grouping key. e.g. groupingKey is "ESSID" it returns all available essids.
  801. * @return ArrayList<String> grouptitles*/
  802. public List<String> getGroupTitles(){
  803. int index = this.groupingTitles().indexOf(this.groupingKey);
  804. switch (index){
  805. case 1:
  806. return this.protocolTitles();
  807. case 2:
  808. return this.essids();
  809. case 3:
  810. return this.bssids();
  811. case 0:
  812. default:
  813. RecordListAdapter adapter = (RecordListAdapter) this.expListView.getExpandableListAdapter();
  814. if (adapter != null){
  815. return adapter.getSectionHeaders();
  816. }
  817. return new ArrayList<String>();
  818. }
  819. }
  820. /*****************************
  821. *
  822. * Filter Stuff
  823. *
  824. * ***************************/
  825. /**Returns the FilterButton.
  826. * @return ImageButton filterButton*/
  827. private ImageButton getFilterButton(){
  828. return (ImageButton) this.rootView.findViewById(R.id.FilterButton);
  829. }
  830. /**Opens the filter menu on a anchor view. The filter menu will always be on top of the anchor.
  831. * @param v View the anchorView*/
  832. private void openFilterPopupMenuOnView(View v){
  833. SimplePopupTable filterMenu = new SimplePopupTable(this.getActivity(), new AbstractPopup.OnPopupItemClickListener() {
  834. public void onItemClick(Object ob) {
  835. if (ob instanceof AbstractPopupItem){
  836. AbstractPopupItem item = (AbstractPopupItem) ob;
  837. RecordOverviewFragment.this.onFilterMenuItemSelected(item);
  838. }
  839. }
  840. });
  841. filterMenu.setTitle(FILTER_MENU_POPUP_TITLE);
  842. for(String title : RecordOverviewFragment.this.filterMenuTitles()){
  843. AbstractPopupItem item = null;
  844. if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_BELOW)) continue;
  845. if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_ABOVE)){
  846. item = new SplitPopupItem(this.getActivity());
  847. item.setValue(SplitPopupItem.RIGHT_TITLE, FILTER_MENU_TITLE_TIMESTAMP_BELOW);
  848. item.setValue(SplitPopupItem.LEFT_TITLE, FILTER_MENU_TITLE_TIMESTAMP_ABOVE);
  849. if (this.filter.hasBelowTimestamp()){
  850. item.setValue(SplitPopupItem.RIGHT_SUBTITLE, this.getDateAsString(this.filter.belowTimestamp));
  851. }
  852. if (this.filter.hasAboveTimestamp()){
  853. item.setValue(SplitPopupItem.LEFT_SUBTITLE, this.getDateAsString(this.filter.aboveTimestamp));
  854. }
  855. } else {
  856. item = new SimplePopupItem(this.getActivity());
  857. item.setTitle(title);
  858. ((SimplePopupItem)item).setSelected(this.isFilterSetForTitle(title));
  859. }
  860. filterMenu.addItem(item);
  861. }
  862. filterMenu.showOnView(v);
  863. }
  864. /**Returns true if the filter object is set for the given title otherwise false. e.g. the filter object has protocols,
  865. * so the method will return for the title FILTER_MENU_TITLE_PROTOCOLS TRUE.
  866. * @param title String
  867. * @return boolean value
  868. * */
  869. private boolean isFilterSetForTitle(String title){
  870. if (title.equals(FILTER_MENU_TITLE_BSSID)){
  871. return this.filter.hasBSSIDs();
  872. }
  873. if (title.equals(FILTER_MENU_TITLE_ESSID)){
  874. return this.filter.hasESSIDs();
  875. }
  876. if (title.equals(FILTER_MENU_TITLE_PROTOCOLS)){
  877. return this.filter.hasProtocols();
  878. }
  879. if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_BELOW)){
  880. return this.filter.hasBelowTimestamp();
  881. }
  882. if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_ABOVE)){
  883. return this.filter.hasAboveTimestamp();
  884. }
  885. return false;
  886. }
  887. /**clears the filter. Does not invoke populatelistview!*/
  888. private void clearFilter(){
  889. if(filter == null) this.filter = new LogFilter();
  890. this.filter.clear();
  891. }
  892. /**Returns all grouping titles.
  893. * @return ArrayList<String> tiles*/
  894. public ArrayList<String> groupingTitles(){
  895. ArrayList<String> titles = new ArrayList<String>();
  896. titles.add(MainActivity.getContext().getString(R.string.date));
  897. titles.add(MainActivity.getContext().getString(R.string.rec_protocol));
  898. titles.add(MainActivity.getContext().getString(R.string.ESSID));
  899. titles.add(MainActivity.getContext().getString(R.string.BSSID));
  900. return titles;
  901. }
  902. /**
  903. * Returns a bool array. This array is true at the index of the groupingKey in groupingTitles(), otherwise false.
  904. * @return boolean[] selection
  905. * */
  906. public boolean[] selectedGroup(){
  907. ArrayList<String> groups = this.groupingTitles();
  908. boolean[] selected = new boolean[groups.size()];
  909. int i = 0;
  910. for(String group : groups){
  911. selected[i] =(group.equals(this.groupingKey));
  912. i++;
  913. }
  914. return selected;
  915. }
  916. /**Returns all protocol titles / names.
  917. * @return ArrayList<String> protocolTitles
  918. * */
  919. public ArrayList<String> protocolTitles(){
  920. ArrayList<String> titles = new ArrayList<String>();
  921. for (String protocol : this.getResources().getStringArray(
  922. R.array.protocols)) {
  923. titles.add(protocol);
  924. }
  925. titles.add("PORTSCAN");
  926. return titles;
  927. }
  928. /**Return a boolean array of the selected / filtered protocols. If the filter object has
  929. * an protocol from the protocolTitles() array, the index of it will be true, otherwise false.
  930. * @return boolean[] protocol selection
  931. * */
  932. public boolean[] selectedProtocols(){
  933. ArrayList<String> protocols = this.protocolTitles();
  934. boolean[] selected = new boolean[protocols.size()];
  935. int i = 0;
  936. for(String protocol : protocols){
  937. selected[i] =(this.filter.protocols.contains(protocol));
  938. i++;
  939. }
  940. return selected;
  941. }
  942. /**
  943. * Returns the Sorttype Titles
  944. * @return ArayList<String> Sort type titles
  945. * */
  946. public ArrayList<String> sortTypeTiles(){
  947. ArrayList<String> titles = new ArrayList<String>();
  948. titles.add(MainActivity.getContext().getString(R.string.rec_time));
  949. titles.add(MainActivity.getContext().getString(R.string.rec_protocol));
  950. titles.add(MainActivity.getContext().getString(R.string.ESSID));
  951. titles.add(MainActivity.getContext().getString(R.string.BSSID));
  952. return titles;
  953. }
  954. /**
  955. * Returns an boolean array. The array is true at the index of the selected sort type..
  956. * The index of the selected sort type is the same index in the sortTypeTiles array.
  957. * @return boolean array, length == sortTypeTiles().length
  958. * */
  959. public boolean[] selectedSorttype(){
  960. ArrayList<String> types = this.sortTypeTiles();
  961. boolean[] selected = new boolean[types.size()];
  962. int i = 0;
  963. for(String sorttype : types){
  964. selected[i] =(this.filter.sorttype.toString().equals(sorttype));
  965. i++;
  966. }
  967. return selected;
  968. }
  969. /**
  970. * Returns all unique bssids.
  971. * @return ArrayList<String>
  972. * */
  973. public ArrayList<String> bssids(){
  974. ArrayList<String> records = dbh.getUniqueBSSIDRecords();
  975. return records;
  976. }
  977. /**
  978. * Returns an boolean array. The array is true at the indices of the selected bssids.
  979. * The index of the selected bssid is the same index in the bssids() array.
  980. * @return boolean array, length == bssids().length
  981. * */
  982. public boolean[] selectedBSSIDs(){
  983. ArrayList<String> bssids = this.bssids();
  984. boolean[] selected = new boolean[bssids.size()];
  985. int i = 0;
  986. for(String bssid : bssids){
  987. selected[i] =(this.filter.BSSIDs.contains(bssid));
  988. i++;
  989. }
  990. return selected;
  991. }
  992. /**
  993. * Returns all unique essids.
  994. * @return ArrayList<String>
  995. * */
  996. public ArrayList<String> essids(){
  997. ArrayList<String> records = dbh.getUniqueESSIDRecords();
  998. return records;
  999. }
  1000. /**
  1001. * Returns an boolean array. The array is true at the indices of the selected essids.
  1002. * The index of the selected essid is the same index in the essids() array.
  1003. * @return boolean array, length == essids().length
  1004. * */
  1005. public boolean[] selectedESSIDs(){
  1006. ArrayList<String> essids = this.essids();
  1007. boolean[] selected = new boolean[essids.size()];
  1008. int i = 0;
  1009. for(String essid : essids){
  1010. selected[i] =(this.filter.ESSIDs.contains(essid));
  1011. i++;
  1012. }
  1013. return selected;
  1014. }
  1015. /**
  1016. * Returns all filter menu titles.
  1017. * @return ArrayList<String>
  1018. * */
  1019. private ArrayList<String> filterMenuTitles(){
  1020. ArrayList<String> titles = new ArrayList<String>();
  1021. titles.add(FILTER_MENU_TITLE_BSSID);
  1022. titles.add(FILTER_MENU_TITLE_ESSID);
  1023. titles.add(FILTER_MENU_TITLE_PROTOCOLS);
  1024. titles.add(FILTER_MENU_TITLE_TIMESTAMP_ABOVE);
  1025. titles.add(FILTER_MENU_TITLE_TIMESTAMP_BELOW);
  1026. if (this.filter.isSet())titles.add(FILTER_MENU_TITLE_REMOVE);
  1027. return titles;
  1028. }
  1029. /*****************************
  1030. *
  1031. * Listener Actions
  1032. *
  1033. * ***************************/
  1034. /**
  1035. * Will be called if the users selects a timestamp.
  1036. * @param dialog {@link DateTimeDialogFragment DateTimeDialogFragment }
  1037. * */
  1038. public void onDateTimePickerPositiveClick(DateTimeDialogFragment dialog) {
  1039. if(this.wasBelowTimePicker){
  1040. this.filter.setBelowTimestamp(dialog.getDate());
  1041. } else {
  1042. this.filter.setAboveTimestamp(dialog.getDate());
  1043. }
  1044. this.actualiseListViewInBackground();
  1045. this.actualiseFilterButton();
  1046. }
  1047. /**
  1048. * Will be called if the users cancels a timestamp selection.
  1049. * @param dialog {@link DateTimeDialogFragment DateTimeDialogFragment }
  1050. * */
  1051. public void onDateTimePickerNegativeClick(DateTimeDialogFragment dialog) {
  1052. if(this.wasBelowTimePicker){
  1053. this.filter.setBelowTimestamp(Long.MAX_VALUE);
  1054. } else {
  1055. this.filter.setAboveTimestamp(Long.MIN_VALUE);
  1056. }
  1057. this.actualiseListViewInBackground();
  1058. this.actualiseFilterButton();
  1059. }
  1060. /**
  1061. * Will be called if the users clicks the positiv button on a ChechlistDialog.
  1062. * @param dialog {@link ChecklistDialog ChecklistDialog }
  1063. */
  1064. public void onDialogPositiveClick(ChecklistDialog dialog) {
  1065. String title = dialog.getTitle();
  1066. if(title.equals(FILTER_MENU_TITLE_BSSID)){
  1067. ArrayList<String> titles =dialog.getSelectedItemTitles();
  1068. if (titles.size() == this.bssids().size()){
  1069. this.filter.setBSSIDs(new ArrayList<String>());
  1070. } else {
  1071. this.filter.setBSSIDs(titles);
  1072. }
  1073. }
  1074. if(title.equals(FILTER_MENU_TITLE_ESSID)){
  1075. ArrayList<String> titles =dialog.getSelectedItemTitles();
  1076. if (titles.size() == this.essids().size()){
  1077. this.filter.setESSIDs(new ArrayList<String>());
  1078. } else {
  1079. this.filter.setESSIDs(titles);
  1080. }
  1081. }
  1082. if(title.equals(FILTER_MENU_TITLE_PROTOCOLS)){
  1083. ArrayList<String> protocols = dialog.getSelectedItemTitles();
  1084. if (protocols.size() == this.protocolTitles().size()){
  1085. this.filter.setProtocols(new ArrayList<String>());
  1086. } else {
  1087. this.filter.setProtocols(dialog.getSelectedItemTitles());
  1088. }
  1089. }
  1090. if(title.equals(FILTER_MENU_TITLE_SORTING)){
  1091. ArrayList<String> titles = dialog.getSelectedItemTitles();
  1092. if (titles.size() == 0) return;
  1093. // ALWAYS GET THE FIRST ELEMENT (SHOULD BE ALWAYS ONE)
  1094. String t = titles.get(0);
  1095. int sortType = this.sortTypeTiles().indexOf(t);
  1096. this.filter.setSorttype(SortType.values()[sortType]);
  1097. }
  1098. if (title.equals(FILTER_MENU_TITLE_GROUP)){
  1099. ArrayList<String> titles = dialog.getSelectedItemTitles();
  1100. if (titles.size() == 0) return;
  1101. // ALWAYS GET THE FIRST ELEMENT (SHOULD BE ALWAYS ONE)
  1102. this.groupingKey = titles.get(0);
  1103. }
  1104. this.actualiseListViewInBackground();
  1105. this.actualiseFilterButton();
  1106. }
  1107. /**Paints the filter button if the current filter object is set.*/
  1108. private void actualiseFilterButton(){
  1109. if (this.filter.isSet() ){
  1110. ImageButton filterButton = this.getFilterButton();
  1111. if (filterButton != null){
  1112. filterButton.setImageResource(R.drawable.ic_filter_pressed);
  1113. filterButton.invalidate();
  1114. }
  1115. } else {
  1116. ImageButton filterButton = this.getFilterButton();
  1117. if (filterButton != null){
  1118. filterButton.setImageResource(R.drawable.ic_filter);
  1119. filterButton.invalidate();
  1120. }
  1121. }
  1122. }
  1123. /**
  1124. * Will be called if the users clicks the negativ button on a ChechlistDialog.
  1125. * @param dialog {@link ChecklistDialog ChecklistDialog }
  1126. */
  1127. public void onDialogNegativeClick(ChecklistDialog dialog) {}
  1128. /*****************************
  1129. *
  1130. * TEST
  1131. *
  1132. * ***************************/
  1133. /**
  1134. * This will clear the database at first and than add new attacks.
  1135. * @param createNetworks number of networks to create
  1136. * @param attacksPerNetwork maximal number of attack per network
  1137. * @param maxMessagePerAttack maximal number of messages per attack
  1138. * */
  1139. private void addRecordToDB( int createNetworks, int attacksPerNetwork, int maxMessagePerAttack) {
  1140. if ((dbh.getRecordCount() > 0)) dbh.clearData();
  1141. Calendar cal = Calendar.getInstance();
  1142. int maxProtocolsIndex = this.getResources().getStringArray(
  1143. R.array.protocols).length;
  1144. Random random = new Random();
  1145. LatLng tudarmstadtLoc = new LatLng(49.86923, 8.6632768);
  1146. final double ssidRadius = 0.1;
  1147. final double bssidRadius = 0.004;
  1148. int attackId = 0;
  1149. ArrayList<NetworkRecord> networkRecords = new ArrayList<NetworkRecord>();
  1150. ArrayList<AttackRecord> attackRecords = new ArrayList<AttackRecord>();
  1151. ArrayList<MessageRecord> messageRecords = new ArrayList<MessageRecord>();
  1152. for (int numOfNetworks = 0; numOfNetworks < createNetworks; numOfNetworks++){
  1153. String ssidName = "WiFi" + ((numOfNetworks) + 1);
  1154. String bssidName = "127.0.0." + ((numOfNetworks) + 1);
  1155. int protocolIndex = numOfNetworks % maxProtocolsIndex;
  1156. String protocolName = this.getResources().getStringArray(
  1157. R.array.protocols)[protocolIndex];
  1158. int numOfAttackPerNetwork = (Math.abs(random.nextInt()) % Math.max(1, attacksPerNetwork + 1));
  1159. NetworkRecord network = new NetworkRecord();
  1160. network.setBssid(bssidName);
  1161. network.setSsid(ssidName);
  1162. LatLng ssidLocation = new LatLng(tudarmstadtLoc.latitude - ssidRadius + 2.0 * ssidRadius * Math.random(), tudarmstadtLoc.longitude - ssidRadius + 2.0 * ssidRadius * Math.random());
  1163. double latitude = ssidLocation.latitude - bssidRadius + 2.0 * bssidRadius * Math.random();
  1164. double longitude = ssidLocation.longitude - bssidRadius + 2.0 * bssidRadius * Math.random();
  1165. long timestamp = cal.getTimeInMillis();
  1166. network.setTimestampLocation(timestamp);
  1167. network.setLongitude(longitude);
  1168. network.setLatitude(latitude);
  1169. network.setAccuracy(0.f);
  1170. //dbh.updateNetworkInformation(network);
  1171. networkRecords.add(network);
  1172. // ATTACKS PER NETWORK
  1173. for (int attackNumber = 0; attackNumber < numOfAttackPerNetwork; attackNumber++) {
  1174. int numRecordsPerAttack = (Math.abs(random.nextInt()) % (Math.max( maxMessagePerAttack, 1))) + 1;
  1175. if (maxMessagePerAttack <= 0) numRecordsPerAttack = 0;
  1176. /* ADD A ATTACK*/
  1177. AttackRecord attack = new AttackRecord(true);
  1178. attack.setBssid(bssidName);
  1179. attack.setProtocol(protocolName);
  1180. attack.setLocalIP(bssidName);
  1181. //dbh.addAttackRecord(attack);
  1182. attackRecords.add(attack);
  1183. // MESSAGE PER ATTACK
  1184. for (int messageID = attackId; messageID < attackId + numRecordsPerAttack; messageID++) {
  1185. MessageRecord message = new MessageRecord(true);
  1186. //message.setId(messageID);
  1187. message.setAttack_id(attackId);
  1188. // GO BACK IN TIME
  1189. message.setTimestamp(cal.getTimeInMillis()
  1190. - ((messageID * 60 * 60 * 24) * 1000) + (1000 * ((messageID - attackId) + 1)));
  1191. if ((messageID - attackId) % 2 == 0){
  1192. message.setType(MessageRecord.TYPE.RECEIVE);
  1193. } else {
  1194. message.setType(MessageRecord.TYPE.SEND);
  1195. }
  1196. message.setPacket("");
  1197. //dbh.addMessageRecord(message);
  1198. messageRecords.add(message);
  1199. }
  1200. attackId+=numRecordsPerAttack;
  1201. }
  1202. }
  1203. dbh.updateNetworkInformation(networkRecords);
  1204. dbh.insertAttackRecords(attackRecords);
  1205. dbh.insertMessageRecords(messageRecords);
  1206. // int countAllLogs = dbh.getAllRecords().size();
  1207. // int countRecords = dbh.getRecordCount();
  1208. // int countAttacks = dbh.getAttackCount();
  1209. //
  1210. // if ((countRecords == 0)) {
  1211. // Record rec = dbh.getRecordOfAttackId(0);
  1212. // Record rec2 = dbh.getRecord(0);
  1213. //
  1214. // System.out.println("" + "Could not create logs!");
  1215. // }
  1216. }
  1217. /**Navigation. Shows the record detail view for the given record
  1218. * @param record {@link Record Record } to show
  1219. * */
  1220. private void pushRecordDetailViewForRecord(Record record){
  1221. FragmentManager fm = this.getActivity().getFragmentManager();
  1222. if (fm != null){
  1223. RecordDetailFragment newFragment = new RecordDetailFragment();
  1224. newFragment.setRecord(record);
  1225. newFragment.setUpNavigatible(true);
  1226. MainActivity.getInstance().injectFragment(newFragment);
  1227. }
  1228. }
  1229. }