RecordOverviewFragment.java 53 KB

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