RecordOverviewFragment.java 53 KB

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