RecordOverviewFragment.java 54 KB

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