RecordOverviewFragment.java 55 KB

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