RecordOverviewFragment.java 53 KB

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