RecordOverviewFragment.java 52 KB

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