RecordOverviewFragment.java 48 KB

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