RecordOverviewFragment.java 53 KB

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