RecordOverviewFragment.java 53 KB

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