RecordOverviewFragment.java 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298
  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().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.getSsid();
  662. case 3:
  663. return rec.getBssid();
  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.essids();
  679. case 3:
  680. return this.bssids();
  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. * @return ArrayList<String> tiles*/
  764. public ArrayList<String> groupingTitles(){
  765. ArrayList<String> titles = new ArrayList<String>();
  766. titles.add(MainActivity.getContext().getString(R.string.date));
  767. titles.add(MainActivity.getContext().getString(R.string.rec_protocol));
  768. titles.add(MainActivity.getContext().getString(R.string.ESSID));
  769. titles.add(MainActivity.getContext().getString(R.string.BSSID));
  770. return titles;
  771. }
  772. /**
  773. * Returns a bool array. This array is true at the index of the groupingKey in groupingTitles(), otherwise false.
  774. * @return boolean[] selection
  775. * */
  776. public boolean[] selectedGroup(){
  777. ArrayList<String> groups = this.groupingTitles();
  778. boolean[] selected = new boolean[groups.size()];
  779. int i = 0;
  780. for(String group : groups){
  781. selected[i] =(group.equals(this.groupingKey));
  782. i++;
  783. }
  784. return selected;
  785. }
  786. /**Returns all protocol titles / names.
  787. * @return ArrayList<String> protocolTitles
  788. * */
  789. public ArrayList<String> protocolTitles(){
  790. ArrayList<String> titles = new ArrayList<String>();
  791. for (String protocol : this.getResources().getStringArray(
  792. R.array.protocols)) {
  793. titles.add(protocol);
  794. }
  795. return titles;
  796. }
  797. /**Return a boolean array of the selected / filtered protocols. If the filter object has
  798. * an protocol from the protocolTitles() array, the index of it will be true, otherwise false.
  799. * @return boolean[] protocol selection
  800. * */
  801. public boolean[] selectedProtocols(){
  802. ArrayList<String> protocols = this.protocolTitles();
  803. boolean[] selected = new boolean[protocols.size()];
  804. int i = 0;
  805. for(String protocol : protocols){
  806. selected[i] =(this.filter.protocols.contains(protocol));
  807. i++;
  808. }
  809. return selected;
  810. }
  811. /**
  812. * Returns the Sorttype Titles
  813. * @return ArayList<String> Sort type titles
  814. * */
  815. public ArrayList<String> sortTypeTiles(){
  816. ArrayList<String> titles = new ArrayList<String>();
  817. titles.add(MainActivity.getContext().getString(R.string.rec_time));
  818. titles.add(MainActivity.getContext().getString(R.string.rec_protocol));
  819. titles.add(MainActivity.getContext().getString(R.string.ESSID));
  820. titles.add(MainActivity.getContext().getString(R.string.BSSID));
  821. return titles;
  822. }
  823. /**
  824. * Returns an boolean array. The array is true at the index of the selected sort type..
  825. * The index of the selected sort type is the same index in the sortTypeTiles array.
  826. * @return boolean array, length == sortTypeTiles().length
  827. * */
  828. public boolean[] selectedSorttype(){
  829. ArrayList<String> types = this.sortTypeTiles();
  830. boolean[] selected = new boolean[types.size()];
  831. int i = 0;
  832. for(String sorttype : types){
  833. selected[i] =(this.filter.sorttype.toString().equals(sorttype));
  834. i++;
  835. }
  836. return selected;
  837. }
  838. /**
  839. * Returns all unique bssids.
  840. * @return ArrayList<String>
  841. * */
  842. public ArrayList<String> bssids(){
  843. ArrayList<String> records = dbh.getUniqueBSSIDRecords();
  844. return records;
  845. }
  846. /**
  847. * Returns an boolean array. The array is true at the indices of the selected bssids.
  848. * The index of the selected bssid is the same index in the bssids() array.
  849. * @return boolean array, length == bssids().length
  850. * */
  851. public boolean[] selectedBSSIDs(){
  852. ArrayList<String> bssids = this.bssids();
  853. boolean[] selected = new boolean[bssids.size()];
  854. int i = 0;
  855. for(String bssid : bssids){
  856. selected[i] =(this.filter.BSSIDs.contains(bssid));
  857. i++;
  858. }
  859. return selected;
  860. }
  861. /**
  862. * Returns all unique essids.
  863. * @return ArrayList<String>
  864. * */
  865. public ArrayList<String> essids(){
  866. ArrayList<String> records = dbh.getUniqueESSIDRecords();
  867. return records;
  868. }
  869. /**
  870. * Returns an boolean array. The array is true at the indices of the selected essids.
  871. * The index of the selected essid is the same index in the essids() array.
  872. * @return boolean array, length == essids().length
  873. * */
  874. public boolean[] selectedESSIDs(){
  875. ArrayList<String> essids = this.essids();
  876. boolean[] selected = new boolean[essids.size()];
  877. int i = 0;
  878. for(String essid : essids){
  879. selected[i] =(this.filter.ESSIDs.contains(essid));
  880. i++;
  881. }
  882. return selected;
  883. }
  884. /**
  885. * Returns all filter menu titles.
  886. * @return ArrayList<String>
  887. * */
  888. private ArrayList<String> filterMenuTitles(){
  889. ArrayList<String> titles = new ArrayList<String>();
  890. titles.add(FILTER_MENU_TITLE_BSSID);
  891. titles.add(FILTER_MENU_TITLE_ESSID);
  892. titles.add(FILTER_MENU_TITLE_PROTOCOLS);
  893. titles.add(FILTER_MENU_TITLE_TIMESTAMP_ABOVE);
  894. titles.add(FILTER_MENU_TITLE_TIMESTAMP_BELOW);
  895. if (this.filter.isSet())titles.add(FILTER_MENU_TITLE_REMOVE);
  896. return titles;
  897. }
  898. /*****************************
  899. *
  900. * Listener Actions
  901. *
  902. * ***************************/
  903. /**
  904. * Will be called if the users selects a timestamp.
  905. * @param dialog {@link DateTimeDialogFragment DateTimeDialogFragment }
  906. * */
  907. public void onDateTimePickerPositiveClick(DateTimeDialogFragment dialog) {
  908. if(this.wasBelowTimePicker){
  909. this.filter.setBelowTimestamp(dialog.getDate());
  910. } else {
  911. this.filter.setAboveTimestamp(dialog.getDate());
  912. }
  913. this.actualiseListViewInBackground();
  914. this.actualiseFilterButton();
  915. }
  916. /**
  917. * Will be called if the users cancels a timestamp selection.
  918. * @param dialog {@link DateTimeDialogFragment DateTimeDialogFragment }
  919. * */
  920. public void onDateTimePickerNegativeClick(DateTimeDialogFragment dialog) {
  921. if(this.wasBelowTimePicker){
  922. this.filter.setBelowTimestamp(Long.MAX_VALUE);
  923. } else {
  924. this.filter.setAboveTimestamp(Long.MIN_VALUE);
  925. }
  926. this.actualiseListViewInBackground();
  927. this.actualiseFilterButton();
  928. }
  929. /**
  930. * Will be called if the users clicks the positiv button on a ChechlistDialog.
  931. * @param dialog {@link ChecklistDialog ChecklistDialog }
  932. */
  933. public void onDialogPositiveClick(ChecklistDialog dialog) {
  934. String title = dialog.getTitle();
  935. if(title.equals(FILTER_MENU_TITLE_BSSID)){
  936. ArrayList<String> titles =dialog.getSelectedItemTitles();
  937. if (titles.size() == this.bssids().size()){
  938. this.filter.setBSSIDs(new ArrayList<String>());
  939. } else {
  940. this.filter.setBSSIDs(titles);
  941. }
  942. }
  943. if(title.equals(FILTER_MENU_TITLE_ESSID)){
  944. ArrayList<String> titles =dialog.getSelectedItemTitles();
  945. if (titles.size() == this.essids().size()){
  946. this.filter.setESSIDs(new ArrayList<String>());
  947. } else {
  948. this.filter.setESSIDs(titles);
  949. }
  950. }
  951. if(title.equals(FILTER_MENU_TITLE_PROTOCOLS)){
  952. ArrayList<String> protocols = dialog.getSelectedItemTitles();
  953. if (protocols.size() == this.protocolTitles().size()){
  954. this.filter.setProtocols(new ArrayList<String>());
  955. } else {
  956. this.filter.setProtocols(dialog.getSelectedItemTitles());
  957. }
  958. }
  959. if(title.equals(FILTER_MENU_TITLE_SORTING)){
  960. ArrayList<String> titles = dialog.getSelectedItemTitles();
  961. if (titles.size() == 0) return;
  962. // ALWAYS GET THE FIRST ELEMENT (SHOULD BE ALWAYS ONE)
  963. String t = titles.get(0);
  964. int sortType = this.sortTypeTiles().indexOf(t);
  965. this.filter.setSorttype(SortType.values()[sortType]);
  966. }
  967. if (title.equals(FILTER_MENU_TITLE_GROUP)){
  968. ArrayList<String> titles = dialog.getSelectedItemTitles();
  969. if (titles.size() == 0) return;
  970. // ALWAYS GET THE FIRST ELEMENT (SHOULD BE ALWAYS ONE)
  971. this.groupingKey = titles.get(0);
  972. }
  973. this.actualiseListViewInBackground();
  974. this.actualiseFilterButton();
  975. }
  976. /**Paints the filter button if the current filter object is set.*/
  977. private void actualiseFilterButton(){
  978. if (this.filter.isSet() ){
  979. ImageButton filterButton = this.getFilterButton();
  980. if (filterButton != null){
  981. filterButton.setImageResource(R.drawable.ic_filter_pressed);
  982. filterButton.invalidate();
  983. }
  984. } else {
  985. ImageButton filterButton = this.getFilterButton();
  986. if (filterButton != null){
  987. filterButton.setImageResource(R.drawable.ic_filter);
  988. filterButton.invalidate();
  989. }
  990. }
  991. }
  992. /**
  993. * Will be called if the users clicks the negativ button on a ChechlistDialog.
  994. * @param dialog {@link ChecklistDialog ChecklistDialog }
  995. */
  996. public void onDialogNegativeClick(ChecklistDialog dialog) {}
  997. /*****************************
  998. *
  999. * TEST
  1000. *
  1001. * ***************************/
  1002. /**
  1003. * This will clear the database at first and than add new attacks.
  1004. * @param createNetworks number of networks to create
  1005. * @param attacksPerNetwork maximal number of attack per network
  1006. * @param maxMessagePerAttack maximal number of messages per attack
  1007. * */
  1008. private void addRecordToDB( int createNetworks, int attacksPerNetwork, int maxMessagePerAttack) {
  1009. if ((dbh.getRecordCount() > 0)) dbh.clearData();
  1010. Calendar cal = Calendar.getInstance();
  1011. int maxProtocolsIndex = this.getResources().getStringArray(
  1012. R.array.protocols).length;
  1013. Random random = new Random();
  1014. LatLng tudarmstadtLoc = new LatLng(49.86923, 8.6632768);
  1015. final double ssidRadius = 0.1;
  1016. final double bssidRadius = 0.004;
  1017. int attackId = 0;
  1018. for (int numOfNetworks = 0; numOfNetworks < createNetworks; numOfNetworks++){
  1019. String ssidName = "WiFi" + ((numOfNetworks) + 1);
  1020. String bssidName = "127.0.0." + ((numOfNetworks) + 1);
  1021. int protocolIndex = numOfNetworks % maxProtocolsIndex;
  1022. String protocolName = this.getResources().getStringArray(
  1023. R.array.protocols)[protocolIndex];
  1024. int numOfAttackPerNetwork = (Math.abs(random.nextInt()) % Math.max(1, attacksPerNetwork + 1));
  1025. NetworkRecord network = new NetworkRecord();
  1026. network.setBssid(bssidName);
  1027. network.setSsid(ssidName);
  1028. LatLng ssidLocation = new LatLng(tudarmstadtLoc.latitude - ssidRadius + 2.0 * ssidRadius * Math.random(), tudarmstadtLoc.longitude - ssidRadius + 2.0 * ssidRadius * Math.random());
  1029. double latitude = ssidLocation.latitude - bssidRadius + 2.0 * bssidRadius * Math.random();
  1030. double longitude = ssidLocation.longitude - bssidRadius + 2.0 * bssidRadius * Math.random();
  1031. long timestamp = cal.getTimeInMillis();
  1032. network.setTimestampLocation(timestamp);
  1033. network.setLongitude(longitude);
  1034. network.setLatitude(latitude);
  1035. network.setAccuracy(0.f);
  1036. dbh.updateNetworkInformation(network);
  1037. // ATTACKS PER NETWORK
  1038. for (int attackNumber = 0; attackNumber < numOfAttackPerNetwork; attackNumber++) {
  1039. int numRecordsPerAttack = (Math.abs(random.nextInt()) % (Math.max( maxMessagePerAttack, 1))) + 1;
  1040. if (maxMessagePerAttack <= 0) numRecordsPerAttack = 0;
  1041. /* ADD A ATTACK*/
  1042. AttackRecord attack = new AttackRecord();
  1043. attack.setAttack_id(attackId);
  1044. attack.setBssid(bssidName);
  1045. attack.setProtocol(protocolName);
  1046. attack.setLocalIP(bssidName);
  1047. dbh.addAttackRecord(attack);
  1048. // MESSAGE PER ATTACK
  1049. for (int messageID = attackId; messageID < attackId + numRecordsPerAttack; messageID++) {
  1050. MessageRecord message = new MessageRecord();
  1051. message.setId(messageID);
  1052. message.setAttack_id(attackId);
  1053. // GO BACK IN TIME
  1054. message.setTimestamp(cal.getTimeInMillis()
  1055. - ((messageID * 60 * 60 * 24) * 1000) + (1000 * ((messageID - attackId) + 1)));
  1056. if ((messageID - attackId) % 2 == 0){
  1057. message.setType(MessageRecord.TYPE.RECEIVE);
  1058. } else {
  1059. message.setType(MessageRecord.TYPE.SEND);
  1060. }
  1061. message.setPacket("");
  1062. dbh.addMessageRecord(message);
  1063. }
  1064. attackId+=numRecordsPerAttack;
  1065. }
  1066. }
  1067. // int countAllLogs = dbh.getAllRecords().size();
  1068. // int countRecords = dbh.getRecordCount();
  1069. // int countAttacks = dbh.getAttackCount();
  1070. //
  1071. // if ((countRecords == 0)) {
  1072. // Record rec = dbh.getRecordOfAttackId(0);
  1073. // Record rec2 = dbh.getRecord(0);
  1074. //
  1075. // System.out.println("" + "Could not create logs!");
  1076. // }
  1077. }
  1078. /**Navigation. Shows the record detail view for the given record
  1079. * @param record {@link Record Record } to show
  1080. * */
  1081. private void pushRecordDetailViewForRecord(Record record){
  1082. FragmentManager fm = this.getActivity().getFragmentManager();
  1083. if (fm != null){
  1084. RecordDetailFragment newFragment = new RecordDetailFragment();
  1085. newFragment.setRecord(record);
  1086. newFragment.setUpNavigatible(true);
  1087. MainActivity.getInstance().injectFragment(newFragment);
  1088. }
  1089. }
  1090. }