RecordOverviewFragment.java 51 KB

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