RecordOverviewFragment.java 52 KB

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