RecordOverviewFragment.java 55 KB

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