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