RecordOverviewFragment.java 48 KB

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