RecordOverviewFragment.java 48 KB

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